07f55db8d803f9d152e3f5e92e92633c6f5bb0f9
[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', 'fuel', 'zte pod 1')
13         self.req_e = PodCreateRequest('zte-2', 'apex', 'zte pod 2')
14         self.get_res = Pod
15         self.list_res = Pods
16         self.basePath = '/pods'
17
18     def assert_get_body(self, pod, req=None):
19         if not req:
20             req = self.req_d
21         self.assertEqual(pod.name, req.name)
22         self.assertEqual(pod.mode, req.mode)
23         self.assertEqual(pod.details, req.details)
24         self.assertIsNotNone(pod.creation_date)
25         self.assertIsNotNone(pod._id)
26
27
28 class TestPodCreate(TestPodBase):
29     def test_withoutBody(self):
30         (code, body) = self.create()
31         self.assertEqual(code, HTTP_BAD_REQUEST)
32
33     def test_success(self):
34         code, body = self.create_d()
35         self.assertEqual(code, HTTP_OK)
36         self.assert_create_body(body)
37
38     def test_alreadyExist(self):
39         self.create_d()
40         code, body = self.create_d()
41         self.assertEqual(code, HTTP_FORBIDDEN)
42         self.assertIn('already exists', body)
43
44     def _assertMeta(self, meta, success):
45         self.assertEqual(meta.success, success)
46         if success:
47             self.assertEqual(meta.uri, '/pods/{}'.format(self.req_d.name))
48
49
50 class TestPodGet(TestPodBase):
51     def test_notExist(self):
52         code, body = self.get('notExist')
53         self.assertEqual(code, HTTP_NOT_FOUND)
54
55     def test_getOne(self):
56         self.create_d()
57         code, body = self.get(self.req_d.name)
58         self.assert_get_body(body)
59
60     def test_list(self):
61         self.create_d()
62         self.create_e()
63         code, body = self.get()
64         self.assertEqual(len(body.pods), 2)
65         for pod in body.pods:
66             if self.req_d.name == pod.name:
67                 self.assert_get_body(pod)
68             else:
69                 self.assert_get_body(pod, self.req_e)
70
71 if __name__ == '__main__':
72     unittest.main()