1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation
3 # feng.xiaowei@zte.com.cn
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from opnfv_testapi.common import constants
12 from opnfv_testapi.resources import project_models
13 import test_base as base
16 class TestProjectBase(base.TestBase):
18 super(TestProjectBase, self).setUp()
19 self.req_d = project_models.ProjectCreateRequest('vping',
21 self.req_e = project_models.ProjectCreateRequest('doctor',
23 self.get_res = project_models.Project
24 self.list_res = project_models.Projects
25 self.update_res = project_models.Project
26 self.basePath = '/api/v1/projects'
28 def assert_body(self, project, req=None):
31 self.assertEqual(project.name, req.name)
32 self.assertEqual(project.description, req.description)
33 self.assertIsNotNone(project._id)
34 self.assertIsNotNone(project.creation_date)
37 class TestProjectCreate(TestProjectBase):
38 def test_withoutBody(self):
39 (code, body) = self.create()
40 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
42 def test_emptyName(self):
43 req_empty = project_models.ProjectCreateRequest('')
44 (code, body) = self.create(req_empty)
45 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
46 self.assertIn('name missing', body)
48 def test_noneName(self):
49 req_none = project_models.ProjectCreateRequest(None)
50 (code, body) = self.create(req_none)
51 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
52 self.assertIn('name missing', body)
54 def test_success(self):
55 (code, body) = self.create_d()
56 self.assertEqual(code, constants.HTTP_OK)
57 self.assert_create_body(body)
59 def test_alreadyExist(self):
61 (code, body) = self.create_d()
62 self.assertEqual(code, constants.HTTP_FORBIDDEN)
63 self.assertIn('already exists', body)
66 class TestProjectGet(TestProjectBase):
67 def test_notExist(self):
68 code, body = self.get('notExist')
69 self.assertEqual(code, constants.HTTP_NOT_FOUND)
71 def test_getOne(self):
73 code, body = self.get(self.req_d.name)
74 self.assertEqual(code, constants.HTTP_OK)
75 self.assert_body(body)
80 code, body = self.get()
81 for project in body.projects:
82 if self.req_d.name == project.name:
83 self.assert_body(project)
85 self.assert_body(project, self.req_e)
88 class TestProjectUpdate(TestProjectBase):
89 def test_withoutBody(self):
90 code, _ = self.update(None, 'noBody')
91 self.assertEqual(code, constants.HTTP_BAD_REQUEST)
93 def test_notFound(self):
94 code, _ = self.update(self.req_e, 'notFound')
95 self.assertEqual(code, constants.HTTP_NOT_FOUND)
97 def test_newNameExist(self):
100 code, body = self.update(self.req_e, self.req_d.name)
101 self.assertEqual(code, constants.HTTP_FORBIDDEN)
102 self.assertIn("already exists", body)
104 def test_noUpdate(self):
106 code, body = self.update(self.req_d, self.req_d.name)
107 self.assertEqual(code, constants.HTTP_FORBIDDEN)
108 self.assertIn("Nothing to update", body)
110 def test_success(self):
112 code, body = self.get(self.req_d.name)
115 req = project_models.ProjectUpdateRequest('newName', 'new description')
116 code, body = self.update(req, self.req_d.name)
117 self.assertEqual(code, constants.HTTP_OK)
118 self.assertEqual(_id, body._id)
119 self.assert_body(body, req)
121 _, new_body = self.get(req.name)
122 self.assertEqual(_id, new_body._id)
123 self.assert_body(new_body, req)
126 class TestProjectDelete(TestProjectBase):
127 def test_notFound(self):
128 code, body = self.delete('notFound')
129 self.assertEqual(code, constants.HTTP_NOT_FOUND)
131 def test_success(self):
133 code, body = self.delete(self.req_d.name)
134 self.assertEqual(code, constants.HTTP_OK)
135 self.assertEqual(body, '')
137 code, body = self.get(self.req_d.name)
138 self.assertEqual(code, constants.HTTP_NOT_FOUND)
140 if __name__ == '__main__':