aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/api/responders_test/resource/test_environment_configs.py
blob: 6356f065e6ca0df3a443c4b6046bf82e8cd974df (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
###############################################################################
# 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 test.api.responders_test.test_data import base
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):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "name": environment_configs.NAME
                                  },
                                  mocks={
                                      read: environment_configs.
                                            ENV_CONFIGS_WITH_SPECIFIC_NAME
                                  },
                                  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):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "distribution":
                                          environment_configs.
                                          CORRECT_DISTRIBUTION
                                  },
                                  mocks={
                                      read: environment_configs.
                                               ENV_CONFIGS_WITH_SPECIFIC_DISTRIBUTION
                                  },
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                    ENV_CONFIGS_WITH_SPECIFIC_DISTRIBUTION_RESPONSE)

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

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

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

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

    @patch(base.RESPONDER_BASE_READ)
    def test_get_environment_configs_list_with_user(self, read):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "user": environment_configs.USER
                                  },
                                  mocks={
                                      read: environment_configs.
                                            ENV_CONFIGS_WITH_SPECIFIC_USER
                                  },
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                    ENV_CONFIGS_WITH_SPECIFIC_USER_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):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "listen": environment_configs.BOOL_LISTEN
                                  },
                                  mocks={
                                      read: environment_configs.
                                            ENV_CONFIGS_WITH_SPECIFIC_LISTEN
                                  },
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                     ENV_CONFIGS_WITH_SPECIFIC_LISTEN_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):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "scanned": environment_configs.BOOL_SCANNED
                                  },
                                  mocks={
                                      read: environment_configs.
                                            ENV_CONFIGS_WITH_SPECIFIC_SCANNED
                                  },
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                      ENV_CONFIGS_WITH_SPECIFIC_SCANNED_RESPONSE
                                  )

    def test_get_environment_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):
        self.validate_get_request(environment_configs.URL,
                                  params={
                                      "scanned": environment_configs.
                                                 BOOL_MONITORING_SETUP_DONE
                                  },
                                  mocks={
                                      read: environment_configs.
                                            ENV_CONFIGS_WITH_SPECIFIC_MONITORING_SETUP_DONE
                                  },
                                  expected_code=base.SUCCESSFUL_CODE,
                                  expected_response=environment_configs.
                                      ENV_CONFIGS_WITH_SPECIFIC_MONITORING_SETUP_DONE_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):
        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=environment_configs.
                                    ENV_CONFIGS_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):
        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=environment_configs.
                                     ENV_CONFIGS_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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"distribution": environment_configs.WRONG_DISTRIBUTION})
        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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={"listen": environment_configs.NON_BOOL_LISTEN})
        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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                             "mechanism_drivers":
                                                [environment_configs.WRONG_MECHANISM_DRIVER]
                                          })
        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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                             "scanned": environment_configs.NON_BOOL_SCANNED
                                          })
        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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                             "last_scanned": base.WRONG_FORMAT_TIME
                                          })
        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):
        test_data = self.get_updated_data(environment_configs.ENV_CONFIG,
                                          updates={
                                             "type_drivers": [environment_configs.WRONG_TYPE_DRIVER]
                                          })
        self.validate_post_request(environment_configs.URL,
                                   body=json.dumps(test_data),
                                   expected_code=base.BAD_REQUEST_CODE)

    def mock_validate_env_config_with_supported_envs(self, 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)
        self.validate_post_request(environment_configs.URL,
                                   mocks={
                                       write: None
                                   },
                                   body=json.dumps(environment_configs.ENV_CONFIG),
                                   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
            }
        ]
        for test_case in test_cases:
            self.mock_validate_env_config_with_supported_envs(test_case["scanning"],
                                                              test_case["monitoring"],
                                                              test_case["listening"])
            self.validate_post_request(environment_configs.URL,
                                       body=json.dumps(environment_configs.ENV_CONFIG),
                                       expected_code=base.BAD_REQUEST_CODE)