summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/tests/unit/handlers/test_project.py
blob: 884dd6816da420a96e9c01e8102ecc3409616378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
##############################################################################
# Copyright (c) 2016 ZTE Corporation
# feng.xiaowei@zte.com.cn
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import httplib
import unittest
import urllib

from opnfv_testapi.common import message
from opnfv_testapi.models import project_models
from opnfv_testapi.models import testcase_models as tcm
from opnfv_testapi.tests.unit import executor
from opnfv_testapi.tests.unit.handlers import test_base as base


class TestProjectBase(base.TestBase):
    def setUp(self):
        super(TestProjectBase, self).setUp()
        self.req_d = project_models.ProjectCreateRequest('qtip',
                                                         'qtip-ssh test')
        self.req_e = project_models.ProjectCreateRequest('functest',
                                                         'functest test')
        self.testcase_d = tcm.TestcaseCreateRequest.from_dict(
            self.load_json('testcase_d'))
        self.project = 'qtip'
        self.get_res = project_models.Project
        self.list_res = project_models.Projects
        self.update_res = project_models.Project
        self.name = 'functest'
        self.basePath = '/api/v1/projects'

    def assert_body(self, project, req=None):
        if not req:
            req = self.req_d
        self.assertEqual(project.name, req.name)
        self.assertEqual(project.description, req.description)
        self.assertIsNotNone(project._id)
        self.assertIsNotNone(project.creation_date)


class TestProjectCreate(TestProjectBase):

    @executor.create(httplib.BAD_REQUEST, message.not_login())
    def test_notlogin(self):
        return self.req_d

    @executor.mock_valid_lfid()
    @executor.create(httplib.BAD_REQUEST, message.no_body())
    def test_withoutBody(self):
        return None

    @executor.mock_valid_lfid()
    @executor.create(httplib.BAD_REQUEST, message.missing('name'))
    def test_emptyName(self):
        return project_models.ProjectCreateRequest('')

    @executor.mock_valid_lfid()
    @executor.create(httplib.BAD_REQUEST, message.missing('name'))
    def test_noneName(self):
        return project_models.ProjectCreateRequest(None)

    @executor.mock_valid_lfid()
    @executor.create(httplib.OK, 'assert_create_body')
    def test_success(self):
        return self.req_d

    @executor.mock_valid_lfid()
    @executor.create(httplib.FORBIDDEN, message.exist_base)
    def test_alreadyExist(self):
        self.create_d()
        return self.req_d


class TestProjectGet(TestProjectBase):

    @executor.mock_valid_lfid()
    def setUp(self):
        super(TestProjectGet, self).setUp()
        self.create_d()
        self.create_e()

    @executor.get(httplib.NOT_FOUND, message.not_found_base)
    def test_notExist(self):
        return 'notExist'

    @executor.get(httplib.OK, 'assert_body')
    def test_getOne(self):
        return self.req_d.name

    @executor.get(httplib.OK, '_assert_list')
    def test_list(self):
        return None

    @executor.query(httplib.OK, '_query_success', 1)
    def test_queryName(self):
        return self._set_query('name')

    def _assert_list(self, body):
        for project in body.projects:
            if self.req_d.name == project.name:
                self.assert_body(project)
            else:
                self.assert_body(project, self.req_e)

    def _set_query(self, *args, **kwargs):
        def get_value(arg):
            return self.__getattribute__(arg)
        query = []
        for arg in args:
            query.append((arg, get_value(arg)))
        for k, v in kwargs.iteritems():
            query.append((k, v))
        return urllib.urlencode(query)

    def _query_success(self, body, number):
        self.assertEqual(number, len(body.projects))


class TestProjectUpdate(TestProjectBase):
    @executor.mock_valid_lfid()
    def setUp(self):
        super(TestProjectUpdate, self).setUp()
        _, d_body = self.create_d()
        _, get_res = self.get(self.req_d.name)
        self.index_d = get_res._id
        self.create_e()

    @executor.update(httplib.BAD_REQUEST, message.not_login())
    def test_notlogin(self):
        req = project_models.ProjectUpdateRequest('apex', 'apex test')
        return req, self.req_d.name

    @executor.update(httplib.BAD_REQUEST, message.no_body())
    def test_withoutBody(self):
        return None, 'noBody'

    @executor.mock_valid_lfid()
    @executor.update(httplib.NOT_FOUND, message.not_found_base)
    def test_notFound(self):
        return self.req_e, 'notFound'

    @executor.mock_valid_lfid()
    @executor.update(httplib.FORBIDDEN, message.exist_base)
    def test_newNameExist(self):
        return self.req_e, self.req_d.name

    @executor.mock_valid_lfid()
    @executor.update(httplib.FORBIDDEN, message.no_update())
    def test_noUpdate(self):
        return self.req_d, self.req_d.name

    @executor.mock_valid_lfid()
    @executor.update(httplib.UNAUTHORIZED, message.tied_with_resource())
    def test_updateNotAllowed(self):
        self.create_help('/api/v1/projects/%s/cases', self.testcase_d, self.req_d.name)
        req = project_models.ProjectUpdateRequest('apex', 'apex test')
        return req, self.req_d.name

    @executor.mock_valid_lfid()
    @executor.update(httplib.OK, '_assert_update')
    def test_success(self):
        req = project_models.ProjectUpdateRequest('apex', 'apex test')
        return req, self.req_d.name

    def _assert_update(self, req, body):
        self.assertEqual(self.index_d, body._id)
        self.assert_body(body, req)
        _, new_body = self.get(req.name)
        self.assertEqual(self.index_d, new_body._id)
        self.assert_body(new_body, req)


class TestProjectDelete(TestProjectBase):
    @executor.mock_valid_lfid()
    def setUp(self):
        super(TestProjectDelete, self).setUp()
        self.create_d()

    @executor.delete(httplib.BAD_REQUEST, message.not_login())
    def test_notlogin(self):
        return self.req_d.name

    @executor.delete(httplib.NOT_FOUND, message.not_found_base)
    def test_notFound(self):
        return 'notFound'

    @executor.mock_valid_lfid()
    @executor.delete(httplib.UNAUTHORIZED, message.tied_with_resource())
    def test_deleteNotAllowed(self):
        self.create_help('/api/v1/projects/%s/cases', self.testcase_d, self.req_d.name)
        return self.req_d.name

    @executor.mock_valid_lfid()
    @executor.delete(httplib.OK, '_assert_delete')
    def test_success(self):
        return self.req_d.name

    def _assert_delete(self, body):
        self.assertEqual(body, '')
        code, body = self.get(self.req_d.name)
        self.assertEqual(code, httplib.NOT_FOUND)


if __name__ == '__main__':
    unittest.main()