From 95ff5d69c63f0958d60dbdfd834abe15e545fae5 Mon Sep 17 00:00:00 2001 From: thuva4 Date: Tue, 8 May 2018 22:35:50 +0530 Subject: Add project client Tests added Doc added Change-Id: I4c6be343606fd3b171ae76e8fb97aaf084bc376d Signed-off-by: thuva4 --- .../developer/devguide/testapi-client-import.rst | 123 ++++++++++++++++++++- .../testapiclient/client/projects.py | 35 ++++++ .../tests/unit/test_project_client.py | 111 +++++++++++++++++++ 3 files changed, 264 insertions(+), 5 deletions(-) create mode 100644 testapi/testapi-client/testapiclient/client/projects.py create mode 100644 testapi/testapi-client/testapiclient/tests/unit/test_project_client.py diff --git a/testapi/docs/developer/devguide/testapi-client-import.rst b/testapi/docs/developer/devguide/testapi-client-import.rst index 69cb6ad..ad41029 100644 --- a/testapi/docs/developer/devguide/testapi-client-import.rst +++ b/testapi/docs/developer/devguide/testapi-client-import.rst @@ -21,7 +21,7 @@ Pod GET """ -User will get the json Pod objects with the get request. +Get a list of all the declared pods from the TestAPI server. .. code-block:: shell @@ -30,7 +30,9 @@ User will get the json Pod objects with the get request. pod_client = pods.PodsClient() pod_client.get() -User can use search parameters to get pods +The user can filter the results by the name attribute. Backend will +use a regular expression to find the list of pods which are +related to given name. .. code-block:: shell @@ -39,10 +41,15 @@ User can use search parameters to get pods pod_client = pods.PodsClient() pod_client.get(name='pod1') +.. NOTE:: + Response format: [{"_id": "", "creator": "", "role": "", "name": "", + "details": "", "mode": "", "creation_date": ""}] + + GET ONE """"""" -User will get the json Pod objects with the get request. +Get a specific pod by its name. .. code-block:: shell @@ -51,9 +58,25 @@ User will get the json Pod objects with the get request. pod_client = pods.PodsClient() pod_client.get_one('name') +.. NOTE:: + Response format: {"_id": "", "creator": "", "role": "", "name": "", + "details": "", "mode": "", "creation_date": ""} + CREATE """""" -User has to authenticate before running the function. +This method used to create a project on the server. +The user should provide the user parameter and the password +parameter while initiating the PodsClient. + +Input for the function : + + * pod-json : Json object of the project + +.. NOTE:: + pod-json-schema - '{"role": "", "name": "", "details": "", "mode": ""}' + + * role should be either "community-ci" or "production-ci" + * mode should be either "metal" or "virtual" .. code-block:: shell @@ -61,5 +84,95 @@ User has to authenticate before running the function. pod_client = pods.PodsClient(user='test', password='pass') pod_client.create({'name': 'test-api', 'mode':'metal', - 'role':'community_ci', 'details':''} + 'role':'community_ci', 'details':''}) + + +Project +^^^^^^^ + +GET +""" + +Get a list of all the declared projects from the TestAPI server. + +.. code-block:: shell + + from testapiclient.client import projects + + project_client = projects.ProjectsClient() + project_client.get() + +User can filter the results by the name attribute. Backend will +use a regular expression to find the list of projects which are +related to given name. + +.. code-block:: shell + + from testapiclient.client import projects + + project_client = projects.ProjectsClient() + project_client.get(name='project1') + +.. NOTE:: + Response format: [{"_id": "", "creator": "", "description": "", + "name": "", "creation_date": ""}] + +GET ONE +""""""" + +Get a specific project by its name. + +.. code-block:: shell + + from testapiclient.client import projects + + project_client = projects.ProjectsClient() + project_client.get_one('name') + +.. NOTE:: + Response format: {"_id": "", "creator": "", "description": "", + "name": "", "creation_date": ""} + +CREATE +"""""" + +This method used to create a project on the server. +The user should provide the user parameter and the password +parameter while initiating the ProjectsClient. + +Input for the function : + + * project-json : Json object of the project + +.. NOTE:: + project-json schema - '{"description": "", "name": ""}' + +.. code-block:: shell + + from testapiclient.client import projects + + project_client = projects.ProjectsClient(user='test', password='pass') + project_client.create({'name': 'functest', 'description':'sample text'} + +UPDATE +"""""" + +This method used to update an existing project on the server. +The user should provide the user parameter and the password +parameter while initiating the ProjectsClient. + +Input for the function : + + * project-name: name of the project which user want to update. + * project-json: Json object of the project + +.. NOTE:: + project-json schema - '{"description": "", "name": ""}' + +.. code-block:: shell + + from testapiclient.client import projects + project_client = projects.ProjectsClient(user='test', password='pass') + project_client.update('functest', {'name': 'functest', + 'description':'updated text'}) \ No newline at end of file diff --git a/testapi/testapi-client/testapiclient/client/projects.py b/testapi/testapi-client/testapiclient/client/projects.py new file mode 100644 index 0000000..63d00fe --- /dev/null +++ b/testapi/testapi-client/testapiclient/client/projects.py @@ -0,0 +1,35 @@ +import json + +from testapiclient.client import base +from testapiclient.utils import urlparse + + +class ProjectsClient(base.Client): + resource = 'projects' + + def __init__(self, **kwargs): + super(ProjectsClient, self).__init__(**kwargs) + + def create(self, project_req): + return self.clientmanager.post(self.url, project_req) + + def get(self, **queries): + if queries: + return json.dumps( + self.clientmanager.get( + urlparse.query_join(self.url, **queries))['projects']) + else: + return json.dumps( + self.clientmanager.get(self.url)['projects']) + + def get_one(self, name): + return json.dumps(self.clientmanager.get( + urlparse.path_join(self.url, name))) + + def delete(self, name): + return self.clientmanager.delete( + urlparse.path_join(self.url, name)) + + def update(self, name, project_req): + return self.clientmanager.put( + urlparse.path_join(self.url, name), project_req) diff --git a/testapi/testapi-client/testapiclient/tests/unit/test_project_client.py b/testapi/testapi-client/testapiclient/tests/unit/test_project_client.py new file mode 100644 index 0000000..7aa11e6 --- /dev/null +++ b/testapi/testapi-client/testapiclient/tests/unit/test_project_client.py @@ -0,0 +1,111 @@ +import json + +from six.moves.urllib import parse +import testtools + +from testapiclient.client import projects +from testapiclient.tests.unit import fakes +from testapiclient.tests.unit import utils +from testapiclient.utils import clientmanager + + +class ProjectClientTest(utils.TestCommand): + def setUp(self): + super(ProjectClientTest, self).setUp() + self.base_url = parse.urljoin(self.api_url, 'projects') + self.project_json = { + 'description': 'test_description', + 'name': 'test_project', + } + self.project_client = projects.ProjectsClient() + self.project_string = json.dumps(self.project_json) + + +class ProjectClientGetTest(ProjectClientTest): + + def setUp(self): + super(ProjectClientGetTest, self).setUp() + self.projects_rsp = {'projects': [self.project_json]} + + def test_get(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.projects_rsp) + self.project_client.get() + self.get_mock.assert_called_once_with( + self.base_url, + headers=clientmanager.ClientManager.headers) + + def test_get_search(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.projects_rsp) + self.project_client.get(name='project1') + self.get_mock.assert_called_once_with( + self.base_url + '?name=project1', + headers=clientmanager.ClientManager.headers) + + def test_get_one(self): + self.get_mock.return_value = fakes.FakeResponse( + data=self.project_json) + self.project_client.get_one('def') + self.get_mock.assert_called_once_with( + self.base_url + '/def', + headers=clientmanager.ClientManager.headers) + + +class ProjectClientCreateTest(ProjectClientTest): + + def setUp(self): + super(ProjectClientCreateTest, self).setUp() + self.succ_rsp = { + 'href': '{}/{}'.format( + self.base_url, self.project_json.get('name')) + } + + def test_create_success(self): + self.post_mock.return_value = fakes.FakeResponse( + data=self.succ_rsp) + self.project_client.create(self.project_json) + self.post_mock.assert_called_once() + + def test_create_failure(self): + with testtools.ExpectedException(Exception, 'Create failed: Error'): + self.post_mock.return_value = utils.FAKE_FAILURE + self.project_client.create(self.project_json) + + +class ProjectClientDeleteTest(ProjectClientTest): + + def setUp(self): + super(ProjectClientDeleteTest, self).setUp() + + def test_delete_success(self): + self.delete_mock.return_value = fakes.FakeResponse() + self.project_client.delete('def') + self.delete_mock.assert_called_once_with( + self.base_url + '/def', + data=None, + headers=clientmanager.ClientManager.headers) + + def test_delete_failure(self): + with testtools.ExpectedException(Exception, 'Delete failed: Error'): + self.delete_mock.return_value = utils.FAKE_FAILURE + self.project_client.delete('def') + + +class ProjectClientUpdateTest(ProjectClientTest): + + def setUp(self): + super(ProjectClientUpdateTest, self).setUp() + + def test_update_success(self): + self.put_mock.return_value = fakes.FakeResponse() + self.project_client.update('def', self.project_json) + self.put_mock.assert_called_once_with( + self.base_url + '/def', + data=self.project_string, + headers=clientmanager.ClientManager.headers) + + def test_update_failure(self): + with testtools.ExpectedException(Exception, 'Update failed: Error'): + self.put_mock.return_value = utils.FAKE_FAILURE + self.project_client.update('def', self.project_json) -- cgit 1.2.3-korg