PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_branchrestriction.py

https://gitlab.com/ztane/python-bitbucket
Python | 172 lines | 135 code | 29 blank | 8 comment | 2 complexity | 7cc1dde39786ac25b40590feb5d620ac MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. from test_bitbucketbase import BitbucketFixture
  3. import json
  4. from uritemplate import expand
  5. from pybitbucket.bitbucket import Bitbucket
  6. from pybitbucket.branchrestriction import (
  7. BranchRestriction, BranchRestrictionKind)
  8. import httpretty
  9. class BranchRestrictionFixture(BitbucketFixture):
  10. # GIVEN: An example repository owner and name
  11. owner = 'ianbuchanan'
  12. repository_name = 'example'
  13. # GIVEN: An ID for an example branch-restriction resource
  14. restriction_id = 913351
  15. # GIVEN: Example data for a branch-restriction resource
  16. @classmethod
  17. def resource_data(cls):
  18. return cls.data_from_file('example_single_branchrestriction.json')
  19. # GIVEN: Example data for a set of branch-restriction resources
  20. @classmethod
  21. def resources_data(cls):
  22. return cls.data_from_file('example_branchrestrictions.json')
  23. # GIVEN: An example BranchRestriction object created from example data
  24. @classmethod
  25. def example_object(cls):
  26. return BranchRestriction(
  27. json.loads(cls.resource_data()),
  28. client=cls.test_client)
  29. # GIVEN: The URL for the example branch-restriction resource
  30. @classmethod
  31. def resource_url(cls):
  32. o = cls.example_object()
  33. return o.links['self']['href']
  34. # GIVEN: The URL for posting branch-restriction resources
  35. @classmethod
  36. def resources_url(cls):
  37. bitbucket = Bitbucket(cls.test_client)
  38. t = bitbucket.data['_links']['repositoryBranchRestrictions']['href']
  39. url = expand(
  40. t, {
  41. 'owner': cls.owner,
  42. 'repository_name': cls.repository_name,
  43. })
  44. return url
  45. class TestGettingTheStringRepresentation(BranchRestrictionFixture):
  46. @classmethod
  47. def setup_class(cls):
  48. cls.branchrestriction_str = str(cls.example_object())
  49. def test_string_is_not_the_default_format(self):
  50. assert not self.branchrestriction_str.startswith('<')
  51. assert not self.branchrestriction_str.endswith('>')
  52. def test_string_has_the_class_name_and_id_attribute(self):
  53. assert self.branchrestriction_str.startswith('BranchRestriction id:')
  54. class TestCreatingPayloadWithInvalidRestrictionKind(BranchRestrictionFixture):
  55. def test_raising_exception_for_invalid_restriction_kind(self):
  56. try:
  57. BranchRestriction.payload(kind='invalid')
  58. except Exception as e:
  59. assert isinstance(e, NameError)
  60. class TestCreatingPushPayloadWithPatternAndUsers(BranchRestrictionFixture):
  61. @classmethod
  62. def setup_class(cls):
  63. cls.payload = BranchRestriction.payload(
  64. kind=BranchRestrictionKind.PUSH,
  65. pattern='master',
  66. users=['ibuchanan'])
  67. def test_payload_structure(self):
  68. assert self.payload == {
  69. "kind": "push",
  70. "pattern": "master",
  71. "users": [{
  72. "username": "ibuchanan"
  73. }]
  74. }
  75. class TestCreatingNewBranchRestriction(BranchRestrictionFixture):
  76. @httpretty.activate
  77. def test_response_is_a_branchrestriction(self):
  78. httpretty.register_uri(
  79. httpretty.POST,
  80. self.resources_url(),
  81. content_type='application/json',
  82. body=self.resource_data(),
  83. status=200)
  84. response = BranchRestriction.create(
  85. owner=self.owner,
  86. repository_name=self.repository_name,
  87. kind=BranchRestrictionKind.PUSH,
  88. pattern='master',
  89. users=['ibuchanan'])
  90. assert 'application/json' == \
  91. httpretty.last_request().headers.get('Content-Type')
  92. assert isinstance(response, BranchRestriction)
  93. class TestUpdatingBranchRestriction(BranchRestrictionFixture):
  94. @httpretty.activate
  95. def test_response_is_a_branchrestriction(self):
  96. httpretty.register_uri(
  97. httpretty.PUT,
  98. self.resource_url(),
  99. content_type='application/json',
  100. body=self.resource_data(),
  101. status=200)
  102. response = self.example_object().update(
  103. pattern='developing')
  104. assert 'application/json' == \
  105. httpretty.last_request().headers.get('Content-Type')
  106. assert isinstance(response, BranchRestriction)
  107. class TestFindingBranchRestrictions(BranchRestrictionFixture):
  108. @httpretty.activate
  109. def test_response_is_a_branchrestriction_generator(self):
  110. httpretty.register_uri(
  111. httpretty.GET,
  112. self.resources_url(),
  113. content_type='application/json',
  114. body=self.resources_data(),
  115. status=200)
  116. response = BranchRestriction.find_branchrestrictions_for_repository(
  117. owner=self.owner,
  118. repository_name=self.repository_name)
  119. assert isinstance(next(response), BranchRestriction)
  120. class TestFindingBranchRestrictionById(BranchRestrictionFixture):
  121. @httpretty.activate
  122. def test_response_is_a_branchrestriction(self):
  123. httpretty.register_uri(
  124. httpretty.GET,
  125. self.resource_url(),
  126. content_type='application/json',
  127. body=self.resource_data(),
  128. status=200)
  129. response = \
  130. BranchRestriction.find_branchrestriction_for_repository_by_id(
  131. owner=self.owner,
  132. repository_name=self.repository_name,
  133. restriction_id=self.restriction_id)
  134. assert isinstance(response, BranchRestriction)
  135. class TestDeletingBranchRestriction(BranchRestrictionFixture):
  136. @httpretty.activate
  137. def test_response_is_not_an_exception(self):
  138. httpretty.register_uri(
  139. httpretty.DELETE,
  140. self.resource_url(),
  141. status=204)
  142. result = self.example_object().delete()
  143. assert result is None