/tests/test_build.py

https://gitlab.com/ztane/python-bitbucket · Python · 163 lines · 149 code · 11 blank · 3 comment · 2 complexity · a646769ecb0be47920210b5beb82d63c MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. import httpretty
  3. import json
  4. from os import path
  5. from test_auth import TestAuth
  6. from util import data_from_file
  7. from pybitbucket.build import BuildStatus, BuildStatusStates
  8. from pybitbucket.bitbucket import Client
  9. from pybitbucket.commit import Commit
  10. class TestBuildStatus(object):
  11. @classmethod
  12. def setup_class(cls):
  13. cls.test_dir, current_file = path.split(path.abspath(__file__))
  14. cls.client = Client(TestAuth())
  15. def load_example_buildstatus(self):
  16. example_path = path.join(
  17. self.test_dir,
  18. 'example_single_buildstatus.json')
  19. with open(example_path) as f:
  20. example = json.load(f)
  21. return BuildStatus(example, client=self.client)
  22. def test_pullrequest_string_representation(self):
  23. # Just tests that the __str__ method works and
  24. # that it does not use the default representation
  25. build_str = "%s" % self.load_example_buildstatus()
  26. assert not build_str.startswith('<')
  27. assert not build_str.endswith('>')
  28. assert build_str.startswith('BuildStatus key:')
  29. def test_create_buildstatus_payload(self):
  30. payload = BuildStatus.make_payload(
  31. state=BuildStatusStates.SUCCESSFUL,
  32. key='BAMBOO-PROJECT-X',
  33. name='Build #34',
  34. url='https://example.com/path/to/build/info',
  35. description='Changes by John Doe')
  36. example_path = path.join(
  37. self.test_dir,
  38. 'example_buildstatus_create_payload.json')
  39. with open(example_path) as f:
  40. example = json.load(f)
  41. assert payload == example
  42. @httpretty.activate
  43. def test_create_buildstatus(self):
  44. owner = 'emmap1'
  45. repository_name = 'MyRepo'
  46. sha = '61d9e64348f9da407e62f64726337fd3bb24b466'
  47. url = (
  48. self.client.get_bitbucket_url() +
  49. '/2.0/repositories/' +
  50. owner + '/' + repository_name +
  51. '/commit/' + sha +
  52. '/statuses/build')
  53. example = data_from_file(
  54. self.test_dir,
  55. 'example_single_buildstatus.json')
  56. httpretty.register_uri(
  57. httpretty.POST,
  58. url,
  59. content_type='application/json',
  60. body=example,
  61. status=200)
  62. build_status = BuildStatus.create_buildstatus(
  63. owner=owner,
  64. repository_name=repository_name,
  65. revision=sha,
  66. key='BAMBOO-PROJECT-X',
  67. state=BuildStatusStates.SUCCESSFUL,
  68. name='Build #34',
  69. url='https://example.com/path/to/build/info',
  70. description='Changes by John Doe',
  71. client=self.client)
  72. assert 'application/json' == \
  73. httpretty.last_request().headers.get('Content-Type')
  74. assert isinstance(build_status, BuildStatus)
  75. @httpretty.activate
  76. def test_modify_buildstatus(self):
  77. owner = 'emmap1'
  78. repository_name = 'MyRepo'
  79. sha = '61d9e64348f9da407e62f64726337fd3bb24b466'
  80. key = 'BAMBOO-PROJECT-X'
  81. url = (
  82. 'https://api.bitbucket.org' +
  83. '/2.0/repositories/' +
  84. owner + '/' + repository_name +
  85. '/commit/' + sha +
  86. '/statuses/build/' + key)
  87. example = data_from_file(
  88. self.test_dir,
  89. 'example_single_buildstatus.json')
  90. httpretty.register_uri(
  91. httpretty.PUT,
  92. url,
  93. content_type='application/json',
  94. body=example,
  95. status=200)
  96. build_status = self.load_example_buildstatus()
  97. assert url == build_status.links['self']['href']
  98. new_build_status = build_status.modify(
  99. state=BuildStatusStates.INPROGRESS)
  100. assert 'application/json' == \
  101. httpretty.last_request().headers.get('Content-Type')
  102. assert isinstance(new_build_status, BuildStatus)
  103. @httpretty.activate
  104. def test_find_buildstatus_for_repository_commit_by_key(self):
  105. owner = 'emmap1'
  106. repository_name = 'MyRepo'
  107. sha = '61d9e64348f9da407e62f64726337fd3bb24b466'
  108. key = 'BAMBOO-PROJECT-X'
  109. url = (
  110. 'https://api.bitbucket.org' +
  111. '/2.0/repositories/' +
  112. owner + '/' + repository_name +
  113. '/commit/' + sha +
  114. '/statuses/build/' + key)
  115. example = data_from_file(
  116. self.test_dir,
  117. 'example_single_buildstatus.json')
  118. httpretty.register_uri(
  119. httpretty.GET,
  120. url,
  121. content_type='application/json',
  122. body=example,
  123. status=200)
  124. build_status = \
  125. BuildStatus.find_buildstatus_for_repository_commit_by_key(
  126. owner=owner,
  127. repository_name=repository_name,
  128. revision=sha,
  129. key=key,
  130. client=self.client)
  131. assert isinstance(build_status, BuildStatus)
  132. @httpretty.activate
  133. def test_buildstatus_commit(self):
  134. build_status = self.load_example_buildstatus()
  135. owner = 'emmap1'
  136. repository_name = 'MyRepo'
  137. sha = '61d9e64348f9da407e62f64726337fd3bb24b466'
  138. url = (
  139. 'https://api.bitbucket.org' +
  140. '/2.0/repositories/' +
  141. owner + '/' + repository_name +
  142. '/commit/' + sha)
  143. example = data_from_file(
  144. self.test_dir,
  145. 'Commit.json')
  146. httpretty.register_uri(
  147. httpretty.GET,
  148. url,
  149. content_type='application/json',
  150. body=example,
  151. status=200)
  152. assert isinstance(next(build_status.commit()), Commit)