aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/api/responders_test/resource/test_environment_configs.py
blob: 4405f2bceb3e8737ca9213a8203f9499cd308984 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# 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 json

from api.responders.resource.environment_configs import EnvironmentConfigs
from test.api.responders_test.test_data import base
from test.api.responders_test.test_data.base import CONSTANTS_BY_NAMES
from test.api.test_base import TestBase
from test.api.responders_test.test_data import environment_configs
from utils.constants import EnvironmentFeatures
from utils.inventory_mgr import InventoryMgr
from unittest.mock import patch


class TestEnvironmentConfigs(TestBase):

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list(self, read):
        self.validate_get_request(environment_configs.URL,
                                  params={},
                                  mocks={read: environment_configs.ENV_CONFIGS},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                  ENV_CONFIGS_RESPONSE)

    def test_get_environment_configs_list_with_invalid_filters(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"unknown": "unknown"},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_name(self, read):
        mocks = {read: environment_configs.ENV_CONFIGS_WITH_SPECIFIC_NAME}
        self.validate_get_request(environment_configs.URL,
                                  params={"name": environment_configs.NAME},
                                  mocks=mocks,
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                  ENV_CONFIGS_WITH_SPECIFIC_NAME[0])

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_unknown_name(self, read):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "name": environment_configs.UNKNOWN_NAME
                                  },
                                  mocks={
                                      read: []
                                  },
                                  expected_code=base.NOT_FOUND_CODE)

    def test_get_environment_configs_list_with_wrong_distribution(self):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "distribution":
                                          environment_configs.WRONG_DISTRIBUTION
                                  },
                                  expected_code=base.BAD_REQUEST_CODE)

    def test_get_environment_configs_list_with_wrong_distribution_version(self):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "distribution_version":
                                          environment_configs.WRONG_DIST_VER
                                  },
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_distribution(self, read):
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_DISTRIBUTION
        config_response = \
            environment_configs.ENV_CONFIGS_WITH_SPECIFIC_DISTRIBUTION_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "distribution":
                                          environment_configs.
                                          CORRECT_DISTRIBUTION
                                  },
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_wrong_mechanism_driver(self):
        config = environment_configs.WRONG_MECHANISM_DRIVER
        self.validate_get_request(environment_configs.URL,
                                  params={"mechanism_drivers": config},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_mechanism_driver(self, read):
        mechanism = environment_configs.CORRECT_MECHANISM_DRIVER
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_MECHANISM_DRIVER
        config_response = environment_configs.\
            ENV_CONFIGS_WITH_SPECIFIC_MECHANISM_DRIVER_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"mechanism_drivers": mechanism},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_wrong_type_driver(self):
        driver = environment_configs.WRONG_TYPE_DRIVER
        self.validate_get_request(environment_configs.URL,
                                  params={"type_drivers": driver},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_type_driver(self, read):
        driver = environment_configs.CORRECT_TYPE_DRIVER
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_TYPE_DRIVER
        config_response = environment_configs.\
            ENV_CONFIGS_WITH_SPECIFIC_TYPE_DRIVER_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"type_drivers": driver},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response
                                  )

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_user(self, read):
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_USER
        config_response = \
            environment_configs.ENV_CONFIGS_WITH_SPECIFIC_USER_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"user": environment_configs.USER},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_non_bool_listen(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"listen": environment_configs.
                                          NON_BOOL_LISTEN},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_bool_listen(self, read):
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_LISTEN
        config_response = \
            environment_configs.ENV_CONFIGS_WITH_SPECIFIC_LISTEN_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"listen": environment_configs.
                                          BOOL_LISTEN},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_non_bool_scanned(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"scanned": environment_configs.
                                          NON_BOOL_SCANNED},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_bool_scanned(self, read):
        config = environment_configs.ENV_CONFIGS_WITH_SPECIFIC_SCANNED
        config_response = \
            environment_configs.ENV_CONFIGS_WITH_SPECIFIC_SCANNED_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"scanned": environment_configs.
                                          BOOL_SCANNED},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response
                                  )

    def test_get_env_configs_list_with_non_bool_monitoring_setup_done(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"listen": environment_configs.
                                          NON_BOOL_MONITORING_SETUP_DONE},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_bool_monitoring_setup_done(self,
                                                                          read):
        config = environment_configs.\
            ENV_CONFIGS_WITH_SPECIFIC_MONITORING_SETUP_DONE
        config_response = environment_configs.\
            ENV_CONFIGS_WITH_SPECIFIC_MONITORING_SETUP_DONE_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"scanned": environment_configs.
                                          BOOL_MONITORING_SETUP_DONE},
                                  mocks={read: config},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_non_int_page(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"page": base.NON_INT_PAGE},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_int_page(self, read):
        config_response = environment_configs.ENV_CONFIGS_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"page": base.INT_PAGE},
                                  mocks={read: environment_configs.ENV_CONFIGS},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_get_environment_configs_list_with_non_int_page_size(self):
        self.validate_get_request(environment_configs.URL,
                                  params={"page_size": base.NON_INT_PAGESIZE},
                                  expected_code=base.BAD_REQUEST_CODE)

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_int_page_size(self, read):
        config_response = environment_configs.ENV_CONFIGS_RESPONSE
        self.validate_get_request(environment_configs.URL,
                                  params={"page_size": base.INT_PAGESIZE},
                                  mocks={read: environment_configs.ENV_CONFIGS},
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=config_response)

    def test_post_environment_config_without_app_path(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["app_path"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_configuration(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["configuration"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_distribution(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["distribution"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_distribution(self):
        dist = environment_configs.WRONG_DISTRIBUTION
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"distribution": dist})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_listen(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["listen"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_listen(self):
        listen_val = environment_configs.NON_BOOL_LISTEN
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"listen": listen_val})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_mechanism_driver(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["mechanism_drivers"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_mechanism_driver(self):
        mechanism = environment_configs.WRONG_MECHANISM_DRIVER
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                             "mechanism_drivers": [mechanism]
                                          })
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_name(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["name"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_operational(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["operational"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_scanned(self):
        scanned_val = environment_configs.NON_BOOL_SCANNED
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"scanned": scanned_val})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_last_scanned(self):
        scanned_val = base.WRONG_FORMAT_TIME
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"last_scanned": scanned_val})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_type(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["type"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_type_drivers(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          deleted_keys=["type_drivers"])
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_wrong_type_drivers(self):
        driver = environment_configs.WRONG_TYPE_DRIVER
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"type_drivers": [driver]})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_duplicate_configurations(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG)
        test_data["configuration"].append({
            "name": "OpenStack"
        })
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_empty_configuration(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG)
        test_data["configuration"].append({})
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_unknown_configuration(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG)
        test_data["configuration"].append({
            "name": "Unknown configuration",
        })
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_without_required_configurations(self):
        for env_type in CONSTANTS_BY_NAMES["environment_types"]:
            required_conf_list = (
                EnvironmentConfigs.REQUIRED_CONFIGURATIONS_NAMES.get(env_type,
                                                                     [])
            )
            if required_conf_list:
                test_data = \
                    self.get_updated_data(environment_configs.ENV_CONFIG)
                test_data['environment_type'] = env_type
                test_data['configuration'] = [
                    c
                    for c in test_data['configuration']
                    if c['name'] != required_conf_list[0]
                ]

                self.validate_post_request(environment_configs.URL,
                                           body=json.dumps(test_data),
                                           expected_code=base.BAD_REQUEST_CODE)

    def test_post_environment_config_with_incomplete_configuration(self):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                              "configuration": [{
                                                  "host": "10.56.20.239",
                                                  "name": "mysql",
                                                  "user": "root"
                                              }, {
                                                  "name": "OpenStack",
                                                  "host": "10.56.20.239",
                                              }, {
                                                  "host": "10.56.20.239",
                                                  "name": "CLI",
                                                  "user": "root"
                                              }]
                                          })
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    @staticmethod
    def mock_validate_env_config_with_supported_envs(scanning, monitoring,
                                                     listening):
        InventoryMgr.is_feature_supported_in_env = \
            lambda self, matches, feature: {
                EnvironmentFeatures.SCANNING: scanning,
                EnvironmentFeatures.MONITORING: monitoring,
                EnvironmentFeatures.LISTENING: listening
            }[feature]

    @patch(base.RESPONDER_BASE_WRITE)
    def test_post_environment_config(self, write):
        self.mock_validate_env_config_with_supported_envs(True, True, True)
        post_body = json.dumps(environment_configs.ENV_CONFIG)
        self.validate_post_request(environment_configs.URL,
                                   mocks={
                                       write: None
                                   },
                                   body=post_body,
                                   expected_code=base.CREATED_CODE)

    def test_post_unsupported_environment_config(self):
        test_cases = [
            {
                "scanning": False,
                "monitoring": True,
                "listening": True
            },
            {
                "scanning": True,
                "monitoring": False,
                "listening": True
            },
            {
                "scanning": True,
                "monitoring": True,
                "listening": False
            }
        ]
        mock_validate = self.mock_validate_env_config_with_supported_envs
        config = environment_configs.ENV_CONFIG
        for test_case in test_cases:
            mock_validate(test_case["scanning"], test_case["monitoring"],
                          test_case["listening"])
            self.validate_post_request(environment_configs.URL,
                                       body=json.dumps(config),
                                       expected_code=base.BAD_REQUEST_CODE)