From 7ca1b18d09a797ff0f96b7cb84cd437b66ffe804 Mon Sep 17 00:00:00 2001 From: farhaanbukhsh Date: Apr 14 2017 10:32:36 +0000 Subject: Add test for api end point to get projects The changes in `api/project.py` make it a compulsion to pass `username` for requesting information for private repo. The tests are written in a way to check the same. --- diff --git a/pagure/api/project.py b/pagure/api/project.py index e60ba1f..52d993a 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -15,7 +15,7 @@ from sqlalchemy.exc import SQLAlchemyError import pagure import pagure.exceptions import pagure.lib -from pagure import SESSION, APP, is_repo_admin +from pagure import SESSION, APP, is_repo_admin, authenticated from pagure.api import API, api_method, APIERROR, api_login_required @@ -57,9 +57,6 @@ def api_git_tags(repo, username=None, namespace=None): if repo is None: raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) - if repo.private and not is_repo_admin(repo): - raise pagure.exceptions.APIError(404, error_code=APIERROR.ENOPROJECT) - tags = pagure.lib.git.get_git_tags(repo) jsonout = flask.jsonify({ @@ -164,8 +161,12 @@ def api_projects(): elif str(fork).lower() in ['0', 'false']: fork = False + private = False + if authenticated() and username == flask.g.fas_user.username: + private = flask.g.fas_user.username + projects = pagure.lib.search_projects( - SESSION, username=username, fork=fork, tags=tags, pattern=pattern) + SESSION, username=username, fork=fork, tags=tags, pattern=pattern, private=private) if not projects: raise pagure.exceptions.APIError( diff --git a/tests/test_pagure_private_repo.py b/tests/test_pagure_private_repo.py index 5dcb96a..a15f775 100644 --- a/tests/test_pagure_private_repo.py +++ b/tests/test_pagure_private_repo.py @@ -586,8 +586,9 @@ class PagurePrivateRepotest(tests.Modeltests): output = self.app.get('/test4/issue/1') self.assertEqual(output.status_code, 200) - def test_api_private_repo(self): - """ Test api points for private repo""" + # API checks + def test_api_private_repo_projects(self): + """ Test api points for private repo for projects""" # Add private repo item = pagure.lib.model.Project( @@ -681,6 +682,99 @@ class PagurePrivateRepotest(tests.Modeltests): shutil.rmtree(newpath) + # Check before adding + repo = pagure.lib.get_project(self.session, 'test4') + self.assertEqual(repo.tags, []) + + # Adding a tag + output = pagure.lib.update_tags( + self.session, repo, 'infra', 'pingou', + ticketfolder=None) + self.assertEqual(output, ['Tag added: infra']) + + # Check after adding + repo = pagure.lib.get_project(self.session, 'test4') + self.assertEqual(len(repo.tags), 1) + self.assertEqual(repo.tags_text, ['infra']) + + # Check the API + output = self.app.get('/api/0/projects?tags=inf') + self.assertEqual(output.status_code, 404) + data = json.loads(output.data) + self.assertDictEqual( + data, + {'error_code': 'ENOPROJECTS', 'error': 'No projects found'} + ) + + # Request by not a loggged in user + output = self.app.get('/api/0/projects?tags=infra') + self.assertEqual(output.status_code, 404) + + user = tests.FakeUser() + with tests.user_set(pagure.APP, user): + # Request by a non authorized user + output = self.app.get('/api/0/projects?tags=infra') + self.assertEqual(output.status_code, 404) + + user.username = 'pingou' + with tests.user_set(pagure.APP, user): + # Private repo username is compulsion to pass + output = self.app.get('/api/0/projects?tags=infra') + self.assertEqual(output.status_code, 404) + + output = self.app.get('/api/0/projects?username=pingou') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['projects'][0]['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "total_projects": 1, + "projects": [ + { + "date_created": "1436527638", + "description": "test project description", + "id": 1, + "name": "test4", + "parent": None, + "priorities": {}, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + }, + ] + } + ) + + output = self.app.get('/api/0/projects?username=pingou&tags=infra') + self.assertEqual(output.status_code, 200) + data = json.loads(output.data) + data['projects'][0]['date_created'] = "1436527638" + self.assertDictEqual( + data, + { + "total_projects": 1, + "projects": [ + { + "date_created": "1436527638", + "description": "test project description", + "id": 1, + "name": "test4", + "parent": None, + "priorities": {}, + "tags": ["infra"], + "user": { + "fullname": "PY C", + "name": "pingou" + } + } + ] + } + ) + + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagurePrivateRepotest) unittest.TextTestRunner(verbosity=2).run(SUITE)