diff --git a/tests/test_pagure_flask_api_project.py b/tests/test_pagure_flask_api_project.py index f85adf5..9963cda 100644 --- a/tests/test_pagure_flask_api_project.py +++ b/tests/test_pagure_flask_api_project.py @@ -318,6 +318,114 @@ class PagureFlaskApiProjecttests(tests.Modeltests): {'message': 'Project "test_42" created'} ) + @patch('pagure.lib.git.generate_gitolite_acls') + def test_api_fork_project(self, p_gga): + """ Test the api_fork_project method of the flask api. """ + p_gga.return_value = True + + tests.create_projects(self.session) + for folder in ['docs', 'tickets', 'requests', 'repos']: + tests.create_projects_git( + os.path.join(tests.HERE, folder), bare=True) + tests.create_tokens(self.session) + tests.create_tokens_acl(self.session) + + headers = {'Authorization': 'token foo_token'} + + # Invalid token + output = self.app.post('/api/0/fork', headers=headers) + self.assertEqual(output.status_code, 401) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or expired token. Please visit " \ + "https://pagure.org/ to get or renew your API token.", + "error_code": "EINVALIDTOK" + } + ) + + headers = {'Authorization': 'token aaabbbcccddd'} + + # No input + output = self.app.post('/api/0/fork', headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + data = { + 'name': 'test', + } + + # Incomplete request + output = self.app.post( + '/api/0/fork', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Invalid or incomplete input submited", + "error_code": "EINVALIDREQ", + } + ) + + data = { + 'repo': 'test', + } + + # Valid request + output = self.app.post( + '/api/0/fork/', data=data, headers=headers) + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "message": "Repo \"test\" cloned to \"pingou/test\"" + } + ) + + data = { + 'repo': 'test', + } + + # project already forked + output = self.app.post( + '/api/0/fork/', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Repo \"forks/pingou/test\" already exists", + "error_code": "ENOCODE" + } + ) + + data = { + 'repo': 'test', + 'username': 'pingou', + } + + # Fork already exists + output = self.app.post( + '/api/0/fork/', data=data, headers=headers) + self.assertEqual(output.status_code, 400) + data = json.loads(output.data) + self.assertDictEqual( + data, + { + "error": "Repo \"forks/pingou/test\" already exists", + "error_code": "ENOCODE" + } + ) if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(