summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/tests/unit/handlers/test_pod.py
blob: 3a16799a956f9a53b5f51b03122c24a152c183eb (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
##############################################################################
# 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

from opnfv_testapi.common import message
from opnfv_testapi.models import pod_models as pm
from opnfv_testapi.models import result_models as rm
from opnfv_testapi.tests.unit import executor
from opnfv_testapi.tests.unit import fake_pymongo
from opnfv_testapi.tests.unit.handlers import test_base as base


class TestPodBase(base.TestBase):
    def setUp(self):
        super(TestPodBase, self).setUp()
        self.get_res = pm.Pod
        self.list_res = pm.Pods
        self.basePath = '/api/v1/pods'
        self.req_d = pm.PodCreateRequest.from_dict(self.pod_d.format())
        self.req_e = pm.PodCreateRequest.from_dict(self.pod_e.format())
        self.results_d = rm.ResultCreateRequest.from_dict(
            self.load_json('test_result'))

    def assert_get_body(self, pod, req=None):
        if not req:
            req = self.req_d
        self.assertEqual(pod, pm.Pod(creator='ValidUser', **req.format()))
        self.assertIsNotNone(pod.creation_date)
        self.assertIsNotNone(pod._id)


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

    @executor.mock_invalid_lfid()
    @executor.create(httplib.BAD_REQUEST, message.not_lfid())
    def test_invalidLfid(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 pm.PodCreateRequest('')

    @executor.mock_valid_lfid()
    @executor.create(httplib.BAD_REQUEST, message.missing('name'))
    def test_noneName(self):
        return pm.PodCreateRequest(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):
        fake_pymongo.pods.insert(self.pod_d.format())
        return self.req_d

    @executor.mock_valid_lfid()
    @executor.create(httplib.FORBIDDEN, message.exist_base)
    def test_alreadyExistCaseInsensitive(self):
        fake_pymongo.pods.insert(self.pod_d.format())
        self.req_d.name = self.req_d.name.upper()
        return self.req_d


class TestPodGet(TestPodBase):
    def setUp(self):
        super(TestPodGet, self).setUp()
        fake_pymongo.pods.insert(self.pod_d.format())
        fake_pymongo.pods.insert(self.pod_e.format())

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

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

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

    def _assert_list(self, body):
        self.assertEqual(len(body.pods), 2)
        for pod in body.pods:
            if self.req_d.name == pod.name:
                self.assert_get_body(pod)
            else:
                self.assert_get_body(pod, self.req_e)


class TestPodDelete(TestPodBase):
    @executor.mock_valid_lfid()
    def setUp(self):
        super(TestPodDelete, self).setUp()
        fake_pymongo.pods.insert(self.pod_d.format())
        fake_pymongo.projects.insert({'name': self.results_d.project_name})
        fake_pymongo.testcases.insert({
            'name': self.results_d.case_name,
            'project_name': self.results_d.project_name})

    @executor.delete(httplib.BAD_REQUEST, message.not_login())
    def test_notlogin(self):
        return self.pod_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/results', self.results_d)
        return self.pod_d.name

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

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