support keys start with '$' or contain '.' in testAPI
[releng.git] / utils / test / result_collection_api / opnfv_testapi / tests / unit / test_testcase.py
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 ##############################################################################
9 import unittest
10 import copy
11
12 from test_base import TestBase
13 from opnfv_testapi.resources.testcase_models import TestcaseCreateRequest, \
14     Testcase, Testcases, TestcaseUpdateRequest
15 from opnfv_testapi.resources.project_models import ProjectCreateRequest
16 from opnfv_testapi.common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
17     HTTP_FORBIDDEN, HTTP_NOT_FOUND
18
19
20 class TestCaseBase(TestBase):
21     def setUp(self):
22         super(TestCaseBase, self).setUp()
23         self.req_d = TestcaseCreateRequest('vping_1',
24                                            '/cases/vping_1',
25                                            'vping-ssh test')
26         self.req_e = TestcaseCreateRequest('doctor_1',
27                                            '/cases/doctor_1',
28                                            'create doctor')
29         self.update_d = TestcaseUpdateRequest('vping_1',
30                                               'vping-ssh test',
31                                               'functest')
32         self.update_e = TestcaseUpdateRequest('doctor_1',
33                                               'create doctor',
34                                               'functest')
35         self.get_res = Testcase
36         self.list_res = Testcases
37         self.update_res = Testcase
38         self.basePath = '/api/v1/projects/%s/cases'
39         self.create_project()
40
41     def assert_body(self, case, req=None):
42         if not req:
43             req = self.req_d
44         self.assertEqual(case.name, req.name)
45         self.assertEqual(case.description, req.description)
46         self.assertEqual(case.url, req.url)
47         self.assertIsNotNone(case._id)
48         self.assertIsNotNone(case.creation_date)
49
50     def assert_update_body(self, old, new, req=None):
51         if not req:
52             req = self.req_d
53         self.assertEqual(new.name, req.name)
54         self.assertEqual(new.description, req.description)
55         self.assertEqual(new.url, old.url)
56         self.assertIsNotNone(new._id)
57         self.assertIsNotNone(new.creation_date)
58
59     def create_project(self):
60         req_p = ProjectCreateRequest('functest', 'vping-ssh test')
61         self.create_help('/api/v1/projects', req_p)
62         self.project = req_p.name
63
64     def create_d(self):
65         return super(TestCaseBase, self).create_d(self.project)
66
67     def create_e(self):
68         return super(TestCaseBase, self).create_e(self.project)
69
70     def get(self, case=None):
71         return super(TestCaseBase, self).get(self.project, case)
72
73     def update(self, new=None, case=None):
74         return super(TestCaseBase, self).update(new, self.project, case)
75
76     def delete(self, case):
77         return super(TestCaseBase, self).delete(self.project, case)
78
79
80 class TestCaseCreate(TestCaseBase):
81     def test_noBody(self):
82         (code, body) = self.create(None, 'vping')
83         self.assertEqual(code, HTTP_BAD_REQUEST)
84
85     def test_noProject(self):
86         code, body = self.create(self.req_d, 'noProject')
87         self.assertEqual(code, HTTP_FORBIDDEN)
88         self.assertIn('Could not find project', body)
89
90     def test_emptyName(self):
91         req_empty = TestcaseCreateRequest('')
92         (code, body) = self.create(req_empty, self.project)
93         self.assertEqual(code, HTTP_BAD_REQUEST)
94         self.assertIn('name missing', body)
95
96     def test_noneName(self):
97         req_none = TestcaseCreateRequest(None)
98         (code, body) = self.create(req_none, self.project)
99         self.assertEqual(code, HTTP_BAD_REQUEST)
100         self.assertIn('name missing', body)
101
102     def test_success(self):
103         code, body = self.create_d()
104         self.assertEqual(code, HTTP_OK)
105         self.assert_create_body(body, None, self.project)
106
107     def test_alreadyExist(self):
108         self.create_d()
109         code, body = self.create_d()
110         self.assertEqual(code, HTTP_FORBIDDEN)
111         self.assertIn('already exists', body)
112
113
114 class TestCaseGet(TestCaseBase):
115     def test_notExist(self):
116         code, body = self.get('notExist')
117         self.assertEqual(code, HTTP_NOT_FOUND)
118
119     def test_getOne(self):
120         self.create_d()
121         code, body = self.get(self.req_d.name)
122         self.assertEqual(code, HTTP_OK)
123         self.assert_body(body)
124
125     def test_list(self):
126         self.create_d()
127         self.create_e()
128         code, body = self.get()
129         for case in body.testcases:
130             if self.req_d.name == case.name:
131                 self.assert_body(case)
132             else:
133                 self.assert_body(case, self.req_e)
134
135
136 class TestCaseUpdate(TestCaseBase):
137     def test_noBody(self):
138         code, _ = self.update(case='noBody')
139         self.assertEqual(code, HTTP_BAD_REQUEST)
140
141     def test_notFound(self):
142         code, _ = self.update(self.update_e, 'notFound')
143         self.assertEqual(code, HTTP_NOT_FOUND)
144
145     def test_newNameExist(self):
146         self.create_d()
147         self.create_e()
148         code, body = self.update(self.update_e, self.req_d.name)
149         self.assertEqual(code, HTTP_FORBIDDEN)
150         self.assertIn("already exists", body)
151
152     def test_noUpdate(self):
153         self.create_d()
154         code, body = self.update(self.update_d, self.req_d.name)
155         self.assertEqual(code, HTTP_FORBIDDEN)
156         self.assertIn("Nothing to update", body)
157
158     def test_success(self):
159         self.create_d()
160         code, body = self.get(self.req_d.name)
161         _id = body._id
162
163         code, body = self.update(self.update_e, self.req_d.name)
164         self.assertEqual(code, HTTP_OK)
165         self.assertEqual(_id, body._id)
166         self.assert_update_body(self.req_d, body, self.update_e)
167
168         _, new_body = self.get(self.req_e.name)
169         self.assertEqual(_id, new_body._id)
170         self.assert_update_body(self.req_d, new_body, self.update_e)
171
172     def test_with_dollar(self):
173         self.create_d()
174         update = copy.deepcopy(self.update_d)
175         update.description = {'2. change': 'dollar change'}
176         code, body = self.update(update, self.req_d.name)
177         self.assertEqual(code, HTTP_OK)
178
179
180 class TestCaseDelete(TestCaseBase):
181     def test_notFound(self):
182         code, body = self.delete('notFound')
183         self.assertEqual(code, HTTP_NOT_FOUND)
184
185     def test_success(self):
186         self.create_d()
187         code, body = self.delete(self.req_d.name)
188         self.assertEqual(code, HTTP_OK)
189         self.assertEqual(body, '')
190
191         code, body = self.get(self.req_d.name)
192         self.assertEqual(code, HTTP_NOT_FOUND)
193
194
195 if __name__ == '__main__':
196     unittest.main()