enterprise-gogs-routes.test.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Observed Gogs 0.13 route compatibility without broad write retries."""
  2. import json
  3. from pathlib import Path
  4. import sys
  5. import unittest
  6. sys.dont_write_bytecode = True
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / 'deploy/enterprise-workshop'))
  9. from bootstrap import Stopped
  10. from gogs_setup import create_organization, inspect, lookup_organization
  11. class OrganizationRouteTests(unittest.TestCase):
  12. cfg = json.loads((ROOT / 'deploy/enterprise-workshop/enterprise.json').read_bytes())
  13. def test_gogs013_uses_verified_admin_route_and_form_fields(self):
  14. calls = []
  15. def call(method, route, body=None, missing=False):
  16. calls.append((method, route, body))
  17. if route == '/api/v1/orgs':
  18. raise Stopped('GOGS_HTTP_404')
  19. if method == 'GET':
  20. return [] if route == '/api/v1/user/orgs' else None
  21. self.assertEqual(route, '/api/v1/admin/users/18307996893/orgs')
  22. self.assertEqual(body['UserName'], 'kangshifu')
  23. self.assertEqual(body['FullName'], '康师傅')
  24. return {'id': 624152, 'username': 'kangshifu'}
  25. api = type('Api', (), {'call': staticmethod(call)})()
  26. self.assertEqual(create_organization(api, self.cfg)['id'], 624152)
  27. self.assertEqual(len(calls), 5)
  28. def test_modern_route_success_does_not_try_admin_route(self):
  29. calls = []
  30. def call(method, route, body=None, missing=False):
  31. calls.append(route)
  32. self.assertEqual(body['username'], 'kangshifu')
  33. return {'username': 'kangshifu'}
  34. api = type('Api', (), {'call': staticmethod(call)})()
  35. create_organization(api, self.cfg)
  36. self.assertEqual(calls, ['/api/v1/orgs'])
  37. def test_authorization_or_uncertain_error_is_not_retried(self):
  38. for code in ('GOGS_HTTP_403', 'GOGS_HTTP_422', 'GOGS_HTTP_500', 'REQUEST_UNCERTAIN'):
  39. calls = []
  40. def call(method, route, body=None, missing=False):
  41. calls.append(route)
  42. raise Stopped(code)
  43. api = type('Api', (), {'call': staticmethod(call)})()
  44. with self.assertRaisesRegex(Stopped, code):
  45. create_organization(api, self.cfg)
  46. self.assertEqual(calls, ['/api/v1/orgs'])
  47. def test_existing_organization_after404_is_not_created_twice(self):
  48. calls = []
  49. def call(method, route, body=None, missing=False):
  50. calls.append(method)
  51. if method == 'POST':
  52. raise Stopped('GOGS_HTTP_404')
  53. return {'username': 'kangshifu'}
  54. api = type('Api', (), {'call': staticmethod(call)})()
  55. self.assertEqual(create_organization(api, self.cfg)['username'], 'kangshifu')
  56. self.assertEqual(calls, ['POST', 'GET'])
  57. def test_unimplemented_org_get_uses_authenticated_membership(self):
  58. def call(method, route, body=None, missing=False):
  59. if route == '/api/v1/user/orgs':
  60. return [{'id': 624152, 'username': 'kangshifu'}]
  61. return None
  62. api = type('Api', (), {'call': staticmethod(call)})()
  63. self.assertEqual(lookup_organization(api, 'kangshifu')['id'], 624152)
  64. def test_same_name_outside_memberships_is_never_treated_as_absent(self):
  65. def call(method, route, body=None, missing=False):
  66. if route == '/api/v1/user/orgs':
  67. return []
  68. if route == '/api/v1/users/kangshifu':
  69. return {'id': 624152, 'username': 'kangshifu'}
  70. return None
  71. api = type('Api', (), {'call': staticmethod(call)})()
  72. with self.assertRaisesRegex(Stopped, 'EXISTING_ORGANIZATION_MEMBERSHIP_UNVERIFIED'):
  73. lookup_organization(api, 'kangshifu')
  74. def test_legacy_read_reports_exists_without_claiming_owners_verified(self):
  75. def call(method, route, body=None, missing=False):
  76. self.assertEqual(method, 'GET')
  77. if route == '/api/v1/user':
  78. return {'login': '18307996893'}
  79. if route == '/api/v1/user/orgs':
  80. return [{'username': 'kangshifu'}]
  81. if route.startswith('/api/v1/repos/'):
  82. return {'private': True, 'owner': {'login': 'kangshifu'}}
  83. return None
  84. api = type('Api', (), {'call': staticmethod(call)})()
  85. result = inspect(api, self.cfg)
  86. self.assertTrue(result['organizationExists'])
  87. self.assertTrue(result['repositoryExists'])
  88. self.assertFalse(result['organizationOwnerVerified'])
  89. self.assertTrue(result['ownerVerificationUnavailable'])
  90. def test_discovered_owner_team_can_use_real_admin_members_route(self):
  91. def call(method, route, body=None, missing=False):
  92. if route == '/api/v1/user':
  93. return {'login': '18307996893'}
  94. if route == '/api/v1/orgs/kangshifu':
  95. return {'username': 'kangshifu'}
  96. if route == '/api/v1/orgs/kangshifu/teams':
  97. return [{'id': 61, 'name': 'Owners'}]
  98. if route == '/api/v1/admin/teams/61/members':
  99. return [{'login': '18307996893'}]
  100. return None
  101. api = type('Api', (), {'call': staticmethod(call)})()
  102. self.assertTrue(inspect(api, self.cfg)['organizationOwnerVerified'])
  103. if __name__ == '__main__':
  104. unittest.main(verbosity=2)