Fix security issues of eval-s in testapi
[releng.git] / utils / test / result_collection_api / opnfv_testapi / tests / unit / test_dashboard.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
11 from opnfv_testapi.common.constants import HTTP_NOT_FOUND, HTTP_OK
12
13 from test_result import TestResultBase
14
15
16 class TestDashboardBase(TestResultBase):
17     def setUp(self):
18         super(TestDashboardBase, self).setUp()
19         self.basePath = '/dashboard/v1/results'
20         self.create_help('/api/v1/results', self.req_d)
21         self.create_help('/api/v1/results', self.req_d)
22         self.list_res = None
23
24
25 class TestDashboardQuery(TestDashboardBase):
26     def test_projectMissing(self):
27         code, body = self.query(self._set_query(project='missing'))
28         self.assertEqual(code, HTTP_NOT_FOUND)
29         self.assertIn('Project name missing', body)
30
31     def test_projectNotReady(self):
32         code, body = self.query(self._set_query(project='notReadyProject'))
33         self.assertEqual(code, HTTP_NOT_FOUND)
34         self.assertIn('Project [notReadyProject] not dashboard ready', body)
35
36     def test_testcaseMissing(self):
37         code, body = self.query(self._set_query(case='missing'))
38         self.assertEqual(code, HTTP_NOT_FOUND)
39         self.assertIn('Test case missing for project [{}]'
40                       .format(self.project),
41                       body)
42
43     def test_testcaseNotReady(self):
44         code, body = self.query(self._set_query(case='notReadyCase'))
45         self.assertEqual(code, HTTP_NOT_FOUND)
46         self.assertIn(
47             'Test case [notReadyCase] not dashboard ready for project [%s]'
48             % self.project,
49             body)
50
51     def test_success(self):
52         code, body = self.query(self._set_query())
53         self.assertEqual(code, HTTP_OK)
54         self.assertIn('{"description": "vPing results for Dashboard"}', body)
55
56     def test_caseIsStatus(self):
57         code, body = self.query(self._set_query(case='status'))
58         self.assertEqual(code, HTTP_OK)
59         self.assertIn('{"description": "Functest status"}', body)
60
61     def _set_query(self, project=None, case=None):
62         uri = ''
63         for k, v in list(locals().iteritems()):
64             if k == 'self' or k == 'uri':
65                 continue
66             if v is None:
67                 v = self.__getattribute__(k)
68             if v != 'missing':
69                 uri += '{}={}&'.format(k, v)
70         uri += 'pod={}&'.format(self.pod)
71         uri += 'version={}&'.format(self.version)
72         uri += 'installer={}&'.format(self.installer)
73         uri += 'period={}&'.format(5)
74         return uri[0:-1]
75
76
77 if __name__ == '__main__':
78     unittest.main()