swagger-ize result-apis of testAPI
[releng.git] / utils / test / result_collection_api / tests / unit / test_pod.py
1 import unittest
2
3 from test_base import TestBase
4 from resources.pod_models import PodCreateRequest, Pod, Pods
5 from common.constants import HTTP_OK, HTTP_BAD_REQUEST, \
6     HTTP_FORBIDDEN, HTTP_NOT_FOUND
7
8
9 class TestPodBase(TestBase):
10     def setUp(self):
11         super(TestPodBase, self).setUp()
12         self.req_d = PodCreateRequest('zte-1', 'virtual',
13                                       'zte pod 1', 'ci-pod')
14         self.req_e = PodCreateRequest('zte-2', 'metal', 'zte pod 2')
15         self.get_res = Pod
16         self.list_res = Pods
17         self.basePath = '/api/v1/pods'
18
19     def assert_get_body(self, pod, req=None):
20         if not req:
21             req = self.req_d
22         self.assertEqual(pod.name, req.name)
23         self.assertEqual(pod.mode, req.mode)
24         self.assertEqual(pod.details, req.details)
25         self.assertEqual(pod.role, req.role)
26         self.assertIsNotNone(pod.creation_date)
27         self.assertIsNotNone(pod._id)
28
29
30 class TestPodCreate(TestPodBase):
31     def test_withoutBody(self):
32         (code, body) = self.create()
33         self.assertEqual(code, HTTP_BAD_REQUEST)
34
35     def test_emptyName(self):
36         req_empty = PodCreateRequest('')
37         (code, body) = self.create(req_empty)
38         self.assertEqual(code, HTTP_BAD_REQUEST)
39         self.assertIn('name missing', body)
40
41     def test_noneName(self):
42         req_none = PodCreateRequest(None)
43         (code, body) = self.create(req_none)
44         self.assertEqual(code, HTTP_BAD_REQUEST)
45         self.assertIn('name missing', body)
46
47     def test_success(self):
48         code, body = self.create_d()
49         self.assertEqual(code, HTTP_OK)
50         self.assert_create_body(body)
51
52     def test_alreadyExist(self):
53         self.create_d()
54         code, body = self.create_d()
55         self.assertEqual(code, HTTP_FORBIDDEN)
56         self.assertIn('already exists', body)
57
58
59 class TestPodGet(TestPodBase):
60     def test_notExist(self):
61         code, body = self.get('notExist')
62         self.assertEqual(code, HTTP_NOT_FOUND)
63
64     def test_getOne(self):
65         self.create_d()
66         code, body = self.get(self.req_d.name)
67         self.assert_get_body(body)
68
69     def test_list(self):
70         self.create_d()
71         self.create_e()
72         code, body = self.get()
73         self.assertEqual(len(body.pods), 2)
74         for pod in body.pods:
75             if self.req_d.name == pod.name:
76                 self.assert_get_body(pod)
77             else:
78                 self.assert_get_body(pod, self.req_e)
79
80 if __name__ == '__main__':
81     unittest.main()