test_pagure_flask_api_project.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import pygit2
  17. from mock import patch
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskApiProjecttests(tests.Modeltests):
  24. """ Tests for the flask API of pagure for issue """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskApiProjecttests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.api.SESSION = self.session
  31. pagure.api.project.SESSION = self.session
  32. pagure.lib.SESSION = self.session
  33. pagure.APP.config['REQUESTS_FOLDER'] = None
  34. pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
  35. self.app = pagure.APP.test_client()
  36. def test_api_git_tags(self):
  37. """ Test the api_git_tags method of the flask api. """
  38. tests.create_projects(self.session)
  39. # Create a git repo to play with
  40. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  41. repo = pygit2.init_repository(gitrepo, bare=True)
  42. newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
  43. repopath = os.path.join(newpath, 'test')
  44. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  45. # Create a file in that git repo
  46. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  47. stream.write('foo\n bar')
  48. clone_repo.index.add('sources')
  49. clone_repo.index.write()
  50. # Commits the files added
  51. tree = clone_repo.index.write_tree()
  52. author = pygit2.Signature(
  53. 'Alice Author', 'alice@authors.tld')
  54. committer = pygit2.Signature(
  55. 'Cecil Committer', 'cecil@committers.tld')
  56. clone_repo.create_commit(
  57. 'refs/heads/master', # the name of the reference to update
  58. author,
  59. committer,
  60. 'Add sources file for testing',
  61. # binary string representing the tree object ID
  62. tree,
  63. # list of binary strings representing parents of the new commit
  64. []
  65. )
  66. refname = 'refs/heads/master:refs/heads/master'
  67. ori_remote = clone_repo.remotes[0]
  68. PagureRepo.push(ori_remote, refname)
  69. # Tag our first commit
  70. first_commit = repo.revparse_single('HEAD')
  71. tagger = pygit2.Signature('Alice Doe', 'adoe@example.com', 12347, 0)
  72. repo.create_tag(
  73. "0.0.1", first_commit.oid.hex, pygit2.GIT_OBJ_COMMIT, tagger,
  74. "Release 0.0.1")
  75. # Check tags
  76. output = self.app.get('/api/0/test/git/tags')
  77. self.assertEqual(output.status_code, 200)
  78. data = json.loads(output.data)
  79. self.assertDictEqual(
  80. data,
  81. {'tags': ['0.0.1'], 'total_tags': 1}
  82. )
  83. shutil.rmtree(newpath)
  84. def test_api_projects(self):
  85. """ Test the api_projects method of the flask api. """
  86. tests.create_projects(self.session)
  87. # Check before adding
  88. repo = pagure.lib.get_project(self.session, 'test')
  89. self.assertEqual(repo.tags, [])
  90. # Adding a tag
  91. output = pagure.lib.update_tags(
  92. self.session, repo, 'infra', 'pingou',
  93. ticketfolder=None)
  94. self.assertEqual(output, ['Tag added: infra'])
  95. # Check after adding
  96. repo = pagure.lib.get_project(self.session, 'test')
  97. self.assertEqual(len(repo.tags), 1)
  98. self.assertEqual(repo.tags_text, ['infra'])
  99. # Check the API
  100. output = self.app.get('/api/0/projects?tags=inf')
  101. self.assertEqual(output.status_code, 404)
  102. data = json.loads(output.data)
  103. self.assertDictEqual(
  104. data,
  105. {'error_code': 'ENOPROJECTS', 'error': 'No projects found'}
  106. )
  107. output = self.app.get('/api/0/projects?tags=infra')
  108. self.assertEqual(output.status_code, 200)
  109. data = json.loads(output.data)
  110. data['projects'][0]['date_created'] = "1436527638"
  111. self.assertDictEqual(
  112. data,
  113. {
  114. "total_projects": 1,
  115. "projects": [
  116. {
  117. "date_created": "1436527638",
  118. "description": "test project #1",
  119. "id": 1,
  120. "name": "test",
  121. "parent": None,
  122. "priorities": {},
  123. "tags": ["infra"],
  124. "user": {
  125. "fullname": "PY C",
  126. "name": "pingou"
  127. }
  128. }
  129. ]
  130. }
  131. )
  132. output = self.app.get('/api/0/projects?username=pingou')
  133. self.assertEqual(output.status_code, 200)
  134. data = json.loads(output.data)
  135. data['projects'][0]['date_created'] = "1436527638"
  136. data['projects'][1]['date_created'] = "1436527638"
  137. self.assertDictEqual(
  138. data,
  139. {
  140. "total_projects": 2,
  141. "projects": [
  142. {
  143. "date_created": "1436527638",
  144. "description": "test project #1",
  145. "id": 1,
  146. "name": "test",
  147. "parent": None,
  148. "priorities": {},
  149. "tags": ["infra"],
  150. "user": {
  151. "fullname": "PY C",
  152. "name": "pingou"
  153. }
  154. },
  155. {
  156. "date_created": "1436527638",
  157. "description": "test project #2",
  158. "id": 2,
  159. "name": "test2",
  160. "parent": None,
  161. "priorities": {},
  162. "tags": [],
  163. "user": {
  164. "fullname": "PY C",
  165. "name": "pingou"
  166. }
  167. }
  168. ]
  169. }
  170. )
  171. output = self.app.get('/api/0/projects?username=pingou&tags=infra')
  172. self.assertEqual(output.status_code, 200)
  173. data = json.loads(output.data)
  174. data['projects'][0]['date_created'] = "1436527638"
  175. self.assertDictEqual(
  176. data,
  177. {
  178. "total_projects": 1,
  179. "projects": [
  180. {
  181. "date_created": "1436527638",
  182. "description": "test project #1",
  183. "id": 1,
  184. "name": "test",
  185. "parent": None,
  186. "priorities": {},
  187. "tags": ["infra"],
  188. "user": {
  189. "fullname": "PY C",
  190. "name": "pingou"
  191. }
  192. }
  193. ]
  194. }
  195. )
  196. if __name__ == '__main__':
  197. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  198. PagureFlaskApiProjecttests)
  199. unittest.TextTestRunner(verbosity=2).run(SUITE)