aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/func_tests/features/steps/perimeter.py
blob: a4a53120befa450389f40080aae5929cc9a30a25 (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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.


from behave import *
from Static_Variables import GeneralVariables
from astropy.table import Table
from common_functions import *
import requests
import json
import logging

apis_urls = GeneralVariables()
commonfunctions = commonfunctions()

logger = logging.getLogger(__name__)

# Step Definition Implementation:
# 1) Get all the existing subject preimeters in the system
# 2) Loop by id to unlink the policies attached
# 3) Then delete the perimeter itself
@Given('the system has no subject perimeter')
def step_impl(context):
    logger.info("Given the system has no subject perimeter")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}
    response = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,headers=apis_urls.auth_headers)
    if len(response.json()[apis_urls.perimetersubjectAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimetersubjectAPI]).keys():
            policies_list = response.json()[apis_urls.perimetersubjectAPI][ids]['policy_list']
            for policy in policies_list:
                response_delete_policies = requests.delete(
                    apis_urls.serverURL + "policies/" + policy + "/" + apis_urls.perimetersubjectAPI + "/" + ids,
                    headers=apis_urls.auth_headers)
            response_delete = requests.delete(apis_urls.serverURL + apis_urls.perimetersubjectAPI + "/" + ids,
                                              headers=apis_urls.auth_headers)

    # exit(0)

# Step Definition Implementation:
# 1) Post subject perimeter using the policy id
@Given('the following subject perimeter exists')
def step_impl(context):
    logger.info("Given the following subject perimeter exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject perimeter description: '" + row[
                "subjectperimeterdescription"]  # "' and subject perimeter email:'" + row[
            # "subjectperimeteremail"] + "' and subject perimeter password '" + row['subjectperimeterpassword']
            + "' and policies '" + row['policies'] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        policyid=""
        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        data = {
            'name': row["subjectperimetername"],
            'description': row["subjectperimeterdescription"],
            # 'email': row['subjectperimeteremail'],
            # 'password': row['subjectperimeterpassword'],

        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimetersubjectAPI, headers=headers,
            data=json.dumps(data))

# Step Definition Implementation:
# 1) Get all the existing object preimeters in the system
# 2) Loop by id to unlink the policies attached
# 3) Then delete the perimeter itself
@Given('the system has no object perimeter')
def step_impl(context):
    logger.info("Given the system has no object perimeter")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}
    response = requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,headers=apis_urls.auth_headers)
    if len(response.json()[apis_urls.perimeterobjectAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimeterobjectAPI]).keys():
            policies_list = response.json()[apis_urls.perimeterobjectAPI][ids]['policy_list']
            for policy in policies_list:
                response_delete_policies = requests.delete(
                    apis_urls.serverURL + "policies/" + policy + "/" + apis_urls.perimeterobjectAPI + "/" + ids,
                    headers=headers)
            response_delete = requests.delete(apis_urls.serverURL + apis_urls.perimeterobjectAPI + "/" + ids,
                                              headers=apis_urls.auth_headers)

# Step Definition Implementation:
# 1) Post object perimeter using the policy id
@Given('the following object perimeter exists')
def step_impl(context):
    logger.info("Given the following object perimeter exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row["objectperimetername"] + "' object perimeter description: '" + row[
                "objectperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}
        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])

        data = {
            'name': row["objectperimetername"],
            'description': row["objectperimeterdescription"],

        }
        response = requests.post(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeterobjectAPI,
                                 headers=headers,
                                 data=json.dumps(data))

# Step Definition Implementation:
# 1) Get all the existing action preimeters in the system
# 2) Loop by id to unlink the policies attached
# 3) Then delete the perimeter itself
@Given('the system has no action perimeter')
def step_impl(context):
    logger.info("Given the system has no action perimeter")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response = requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,headers=apis_urls.auth_headers)
    if len(response.json()[apis_urls.perimeteractionAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimeteractionAPI]).keys():
            policies_list = response.json()[apis_urls.perimeteractionAPI][ids]['policy_list']
            for policy in policies_list:
                response_delete_policies = requests.delete(
                    apis_urls.serverURL + "policies/" + policy + "/" + apis_urls.perimeteractionAPI + "/" + ids,
                    headers=headers)
            response_delete = requests.delete(apis_urls.serverURL + apis_urls.perimeteractionAPI + "/" + ids,
                                              headers=apis_urls.auth_headers)


# Step Definition Implementation:
# 1) Post action perimeter using the policy id
@Given('the following action perimeter exists')
def step_impl(context):
    logger.info("Given the following action perimeter exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action perimeter name: '" + row["actionperimetername"] + "' action perimeter description: '" + row[
                "actionperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        policyid=""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        data = {
            'name': row["actionperimetername"],
            'description': row["actionperimeterdescription"],

        }
        response = requests.post(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeteractionAPI,
                                 headers=headers,
                                 data=json.dumps(data))

# Step Definition Implementation:
# 1) Insert subject perimeter using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following subject perimeter')
def step_impl(context):
    logger.info("When the user sets to add the following subject perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject perimeter description: '" + row[
                "subjectperimeterdescription"] +
            # "' and subject perimeter email:'" + row["subjectperimeteremail"] + "' and subject perimeter password '" + row['subjectperimeterpassword'] +
            "' and policies '" + row['policies'] + "'")

        policyid = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        data = {
                'name': row["subjectperimetername"],
                'description': row["subjectperimeterdescription"],
                # 'email': row['subjectperimeteremail'],
                # 'password': row['subjectperimeterpassword'],
        }
        response = requests.post(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimetersubjectAPI, headers=headers,
                                     data=json.dumps(data))

        if response.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing subject perimeter & get its id
# 2) create the new perimeter jason and patch it
# 3) If the request code was 200 set the api response flag to true else false
@When('the user sets to update the following subject perimeter')
def step_impl(context):
    logger.info("When the user sets to update the following subject perimeter")
    model = getattr(context, "model", None)
    policies_list = []
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row[
                'subjectperimetername'] + "' which will be updated to subject perimeter name:'" + row[
                "updatedsubjectperimetername"] + "' subject perimeter description: '" + row[
                "updatedsubjectperimeterdescription"] +
            # "' and subject perimeter email:'" + row["updatedsubjectperimeteremail"] + "' and subject perimeter password '" + row['updatedsubjectperimeterpassword']
            "' and policies '" + row['policies'] + "'")

        policyid = ""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid=commonfunctions.get_policyid(row['policies'])
        else:
            policyid=""
        data = {
                'name': row["updatedsubjectperimetername"],
                'description': row["updatedsubjectperimeterdescription"],
                # 'email': row['subjectperimeteremail'],
                # 'password': row['subjectperimeterpassword'],
        }
        response = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimetersubjectAPI]).keys():
            if (response.json()[apis_urls.perimetersubjectAPI][ids]['name'] == row["subjectperimetername"]):
                #print(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimetersubjectAPI + '/' + ids)
                response = requests.patch(apis_urls.serverURL + apis_urls.perimetersubjectAPI + '/' + ids,
                    headers=headers,data=json.dumps(data))
                print(response)

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing subject perimeter & get its id
# 2) Delete it without having the policy id in the request
@When('the user sets to delete the following subject perimeter')
def step_impl(context):
    logging.info("When the user sets to delete the following subject perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {
            'Content-Type': 'application/json',
        }
        logger.info("subject perimeter name:'" + row["subjectperimetername"] + "'")
        response = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimetersubjectAPI]).keys():
            if (response.json()[apis_urls.perimetersubjectAPI][ids]['name'] == row["subjectperimetername"]):
                response = requests.delete(apis_urls.serverURL + apis_urls.perimetersubjectAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing subject perimeter & get its id
# 2) Delete it while having the policy id in the request
@When('the user sets to delete the following subject perimeter for a given policy')
def step_impl(context):
    logging.info("the user sets to delete the following subject perimeter for a given policy")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {
            'Content-Type': 'application/json',
        }
        logger.info("subject perimeter name:'" + row["subjectperimetername"] + "' and policy:"+ row["policies"]+"'")
        policyid = commonfunctions.get_policyid(row['policies'])
        response = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimetersubjectAPI]).keys():
            if (response.json()[apis_urls.perimetersubjectAPI][ids]['name'] == row["subjectperimetername"]):
                response = requests.delete(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimetersubjectAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)
            logger.info(response.json())
    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Insert object perimeter using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following object perimeter')
def step_impl(context):
    logger.info("When the user sets to add the following object perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row["objectperimetername"] + "' object perimeter description: '" + row[
                "objectperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        policies_list = []
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        else:
            policyid=""
        data = {
                'name': row["objectperimetername"],
                'description': row["objectperimeterdescription"],
        }
        response = requests.post(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeterobjectAPI, headers=headers,
                                     data=json.dumps(data))

        if response.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing object perimeter & get its id
# 2) create the new perimeter jason and patch it
# 3) If the request code was 200 set the api response flag to true else false
@When('the user sets to update the following object perimeter')
def step_impl(context):
    logger.info("When the user sets to update the following object perimeter")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row[
                'objectperimetername'] + "' which will be updated to object perimeter name:" + row[
                "updatedobjectperimetername"] + "' object perimeter description: '" + row[
                "updatedobjectperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        else:
            policyid=""
        data = {
                'name': row["updatedobjectperimetername"],
                'description': row["updatedobjectperimeterdescription"],
            }
        response = requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeterobjectAPI]).keys():
            if (response.json()[apis_urls.perimeterobjectAPI][ids]['name'] == row["objectperimetername"]):
                response = requests.patch(apis_urls.serverURL + apis_urls.perimeterobjectAPI + '/' + ids,
                                          headers=headers,data=json.dumps(data))

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing object perimeter & get its id
# 2) Delete it without having the policy id in the request
@When('the user sets to delete the following object perimeter')
def step_impl(context):
    logging.info("When the user sets to delete the following object perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        logger.info("object perimeter name:'" + row["objectperimetername"] + "'")

        response = requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeterobjectAPI]).keys():
            if (response.json()[apis_urls.perimeterobjectAPI][ids]['name'] == row["objectperimetername"]):
                response = requests.delete(apis_urls.serverURL + apis_urls.perimeterobjectAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing object perimeter & get its id
# 2) Delete it while having the policy id in the request
@When('the user sets to delete the following object perimeter for a given policy')
def step_impl(context):
    logging.info("the user sets to delete the following object perimeter for a given policy")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        logger.info("object perimeter name:'" + row["objectperimetername"] + "' and policy:"+ row["policies"]+"'")
        policyid = commonfunctions.get_policyid(row['policies'])
        response = requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeterobjectAPI]).keys():
            if (response.json()[apis_urls.perimeterobjectAPI][ids]['name'] == row["objectperimetername"]):
                response = requests.delete(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeterobjectAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Insert action perimeter using the post request
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to add the following action perimeter')
def step_impl(context):
    logger.info("When the user sets to add the following action perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action perimeter name: '" + row["actionperimetername"] + "' action perimeter description: '" + row[
                "actionperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        policyid=""
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        else:
            policyid=""
        data = {
                'name': row["actionperimetername"],
                'description': row["actionperimeterdescription"],

        }
        response = requests.post(
                apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeteractionAPI, headers=headers,
                data=json.dumps(data))

        if response.status_code == 200:
            GeneralVariables.api_responseflag['value'] = 'True'
        else:
            GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing action perimeter & get its id
# 2) create the new perimeter jason and patch it
# 3) If the request code was 200 set the api response flag to true else false
@When('the user sets to update the following action perimeter')
def step_impl(context):
    logger.info("When the user sets to update the following action perimeter")

    model = getattr(context, "model", None)

    for row in context.table:

        logger.info(
            "action perimeter name: '" + row[
                'actionperimetername'] + "' which will be updated to action perimeter name:" + row[
                "updatedactionperimetername"] + "' action perimeter description: '" + row[
                "updatedactionperimeterdescription"] + "' and policies '" + row['policies'] + "'")

        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        if (row['policies'] != ""):
            policyid = commonfunctions.get_policyid(row['policies'])
        else:
            policyid=""
        data = {
                'name': row["updatedactionperimetername"],
                'description': row["updatedactionperimeterdescription"],
            }
        response = requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeteractionAPI]).keys():
            if (response.json()[apis_urls.perimeteractionAPI][ids]['name'] == row["actionperimetername"]):
                response = requests.patch(
                    apis_urls.serverURL +  apis_urls.perimeteractionAPI + '/' + ids,
                    headers=headers,data=json.dumps(data))

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing action perimeter & get its id
# 2) Delete it without having the policy id in the request
@When('the user sets to delete the following action perimeter')
def step_impl(context):
    logging.info("When the user sets to delete the following action perimeter")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        logger.info("action perimeter name:'" + row["actionperimetername"] + "'")
        response = requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeteractionAPI]).keys():
            if (response.json()[apis_urls.perimeteractionAPI][ids]['name'] == row["actionperimetername"]):
                response = requests.delete(apis_urls.serverURL + apis_urls.perimeteractionAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)
    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Search for the existing action perimeter & get its id
# 2) Delete it while having the policy id in the request
@When('the user sets to delete the following action perimeter for a given policy')
def step_impl(context):
    logging.info("the user sets to delete the following action perimeter for a given policy")

    model = getattr(context, "model", None)
    for row in context.table:
        headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

        logger.info("action perimeter name:'" + row["actionperimetername"] + "' and policy:"+ row["policies"]+"'")
        policyid = commonfunctions.get_policyid(row['policies'])
        response = requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,headers=apis_urls.auth_headers)
        for ids in dict(response.json()[apis_urls.perimeteractionAPI]).keys():
            if (response.json()[apis_urls.perimeteractionAPI][ids]['name'] == row["actionperimetername"]):
                response = requests.delete(apis_urls.serverURL + "policies/" + policyid + "/" + apis_urls.perimeteractionAPI + "/" + ids,
                                           headers=apis_urls.auth_headers)

    if response.status_code == 200:
        GeneralVariables.api_responseflag['value'] = 'True'
    else:
        GeneralVariables.api_responseflag['value'] = 'False'

# Step Definition Implementation:
# 1) Get all the existing subject perimeter by get request and put them into a table
# 2) Sort the table by subject perimeter
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following subject perimeter should be existed in the system')
def step_impl(context):
    logger.info("Then the following subject perimeter should be existed in the system")

    response = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,headers=apis_urls.auth_headers)
    apiresult = Table(
        names=('subjectperimetername', 'subjectperimeterdescription',
               # 'subjectperimeteremail',
               # 'subjectperimeterpassword',
               'policies'),
        dtype=('S100', 'S100', 'S100'))

    if len(response.json()[apis_urls.perimetersubjectAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimetersubjectAPI]).keys():
            apipoliciesid = []
            apipolicies = ""
            GeneralVariables.assignsubjectperimeterid['value']=ids
            apisubjectperimetername = response.json()[apis_urls.perimetersubjectAPI][ids]['name']
            apisubjectperimeterdescription = response.json()[apis_urls.perimetersubjectAPI][ids]['description']
            # apisubjectperimeteremail = response.json()[apis_urls.perimetersubjectAPI][ids]['email']
            # apisubjectperimeterpassword = response.json()[apis_urls.perimetersubjectAPI][ids]['password']
            if (len(response.json()[apis_urls.perimetersubjectAPI][ids]['policy_list']) != 0):
                for policies in response.json()[apis_urls.perimetersubjectAPI][ids]['policy_list']:
                    apipoliciesid.append(commonfunctions.get_policyname(str(policies)))
                apipolicies = ",".join(apipoliciesid)
            else:
                apipolicies = ""
            apiresult.add_row(vals=(
                apisubjectperimetername, apisubjectperimeterdescription,
                # apisubjectperimeteremail,# apisubjectperimeterpassword,
                apipolicies))
    else:
        apiresult.add_row(vals=("", "", ""))

    apiresult.sort('subjectperimetername')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected subject perimeter name: '" + str(
            row1["subjectperimetername"]) + "' is the same as the actual existing '" + str(
            row2["subjectperimetername"]) + "'")
        assert str(row1["subjectperimetername"]) == str(
            row2["subjectperimetername"]), "subject perimeter name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected subject perimeter description: '" + str(
            row1["subjectperimeterdescription"]) + "' is the same as the actual existing '" + str(
            row2["subjectperimeterdescription"]) + "'")
        assert str(row1["subjectperimeterdescription"]) == str(
            row2["subjectperimeterdescription"]), "subject perimeter description is not correct!"
        logger.info("assertion passed!")

        # logger.info("asserting the expected subject perimeter email: '" + str(
        #     row1["subjectperimeteremail"]) + "' is the same as the actual existing '" + str(
        #     row2["subjectperimeteremail"]) + "'")
        # assert str(row1["subjectperimeteremail"]) == str(
        #     row2["subjectperimeteremail"]), "subject perimeter email is not correct!"
        # logger.info("assertion passed!")
        #
        # logger.info("asserting the expected subject perimeter password: '" + str(
        #     row1["subjectperimeterpassword"]) + "' is the same as the actual existing '" + str(
        #     row2["subjectperimeterpassword"]) + "'")
        # assert str(row1["subjectperimeterpassword"]) == str(
        #     row2["subjectperimeterpassword"]), "subject perimeter password is not correct!"
        # logger.info("assertion passed!")

        if (str(row1["policies"]).find(',') == -1):
            logger.info("asserting the expected policies: '" + str(
                row1["policies"]) + "' is the same as the actual existing '" + str(
                row2["policies"]) + "'")
            logger.info("policies is not correct!")
            assert str(row1["policies"]) == str(row2["policies"]), " policies is not correct!"
        else:

            logger.info("asserting the expected policies: '" + ','.join(
                sorted(str(row1["policies"]).split(','), key=str.lower)) + "' is the same as the actual existing '" +
                        ','.join(sorted(str(row2["policies"]).split(','), key=str.lower)) + "'")
            logger.info("policies is not correct!")
            assert ','.join(sorted(str(row1["policies"]).split(','), key=str.lower)) == ','.join(
                sorted(str(row2["policies"]).split(','), key=str.lower)), " policies is not correct!"
        logger.info("assertion passed!")

# Step Definition Implementation:
# 1) Get all the existing object perimeter by get request and put them into a table
# 2) Sort the table by subject perimeter
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following object perimeter should be existed in the system')
def step_impl(context):
    logger.info("Then the following object perimeter should be existed in the system")
    response = requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,headers=apis_urls.auth_headers)
    apiresult = Table(
        names=('objectperimetername', 'objectperimeterdescription', 'policies'),
        dtype=('S100', 'S100', 'S100'))
    if len(response.json()[apis_urls.perimeterobjectAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimeterobjectAPI]).keys():
            apipolicies = ""
            apipoliciesid = []
            apiobjectperimetername = response.json()[apis_urls.perimeterobjectAPI][ids]['name']
            apiobjectperimeterdescription = response.json()[apis_urls.perimeterobjectAPI][ids]['description']
            if (len(response.json()[apis_urls.perimeterobjectAPI][ids]['policy_list']) != 0):
                for policies in response.json()[apis_urls.perimeterobjectAPI][ids]['policy_list']:
                    apipoliciesid.append(commonfunctions.get_policyname(str(policies)))
                apipolicies = ",".join(apipoliciesid)
            else:
                apipolicies = ""
            apiresult.add_row(vals=(
                apiobjectperimetername, apiobjectperimeterdescription, apipolicies))
    else:
        apiresult.add_row(vals=("", "", ""))

    apiresult.sort('objectperimetername')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected object perimeter name: '" + str(
            row1["objectperimetername"]) + "' is the same as the actual existing '" + str(
            row2["objectperimetername"]) + "'")
        assert str(row1["objectperimetername"]) == str(
            row2["objectperimetername"]), "object perimeter name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected object perimeter description: '" + str(
            row1["objectperimeterdescription"]) + "' is the same as the actual existing '" + str(
            row2["objectperimeterdescription"]) + "'")
        assert str(row1["objectperimeterdescription"]) == str(
            row2["objectperimeterdescription"]), "object perimeter description is not correct!"
        logger.info("assertion passed!")

        if (str(row1["policies"]).find(',') == -1):
            logger.info("asserting the expected policies: '" + str(
                row1["policies"]) + "' is the same as the actual existing '" + str(
                row2["policies"]) + "'")
            logger.info("policies is not correct!")
            assert str(row1["policies"]) == str(row2["policies"]), " policies is not correct!"
        else:
            logger.info("asserting the expected policies: '" + ','.join(
                sorted(str(row1["policies"]).split(','), key=str.lower)) + "' is the same as the actual existing '" +
                        ','.join(sorted(str(row2["policies"]).split(','), key=str.lower)) + "'")
            logger.info("policies is not correct!")
            assert ','.join(sorted(str(row1["policies"]).split(','), key=str.lower)) == ','.join(
                sorted(str(row2["policies"]).split(','), key=str.lower)), " policies is not correct!"
        logger.info("assertion passed!")

# Step Definition Implementation:
# 1) Get all the existing subject perimeter by get request and put them into a table
# 2) Sort the table by subject perimeter
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following action perimeter should be existed in the system')
def step_impl(context):
    logger.info("Then the following action perimeter should be existed in the system")
    response = requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,headers=apis_urls.auth_headers)
    apiresult = Table(
        names=('actionperimetername', 'actionperimeterdescription', 'policies'),
        dtype=('S100', 'S100', 'S100'))
    if len(response.json()[apis_urls.perimeteractionAPI]) != 0:
        for ids in dict(response.json()[apis_urls.perimeteractionAPI]).keys():
            apipolicies = ""
            apipoliciesid = []
            apiactionperimetername = response.json()[apis_urls.perimeteractionAPI][ids]['name']
            apiactionperimeterdescription = response.json()[apis_urls.perimeteractionAPI][ids]['description']
            if (len(response.json()[apis_urls.perimeteractionAPI][ids]['policy_list']) != 0):
                for policies in response.json()[apis_urls.perimeteractionAPI][ids]['policy_list']:
                    apipoliciesid.append(commonfunctions.get_policyname(str(policies)))
                apipolicies = ",".join(apipoliciesid)
            else:
                apipolicies = ""
            apiresult.add_row(vals=(
                apiactionperimetername, apiactionperimeterdescription, apipolicies))
    else:
        apiresult.add_row(vals=("", "", ""))

    apiresult.sort('actionperimetername')
    for row1, row2 in zip(context.table, apiresult):
        logger.info("asserting the expected action perimeter name: '" + str(
            row1["actionperimetername"]) + "' is the same as the actual existing '" + str(
            row2["actionperimetername"]) + "'")
        assert str(row1["actionperimetername"]) == str(
            row2["actionperimetername"]), "action perimeter name is not correct!"
        logger.info("assertion passed!")

        logger.info("asserting the expected action perimeter description: '" + str(
            row1["actionperimeterdescription"]) + "' is the same as the actual existing '" + str(
            row2["actionperimeterdescription"]) + "'")
        assert str(row1["actionperimeterdescription"]) == str(
            row2["actionperimeterdescription"]), "action perimeter description is not correct!"
        logger.info("assertion passed!")

        if(str(row1["policies"]).find(',')==-1):
            logger.info("asserting the expected policies: '" + str(
            row1["policies"]) + "' is the same as the actual existing '" + str(
            row2["policies"]) + "'")
            logger.info("policies is not correct!")
            assert str(row1["policies"]) == str(row2["policies"]), " policies is not correct!"
        else:

            logger.info("asserting the expected policies: '" + ','.join(sorted(str(row1["policies"]).split(','),key=str.lower)) + "' is the same as the actual existing '" +
                        ','.join(sorted(str(row2["policies"]).split(','), key=str.lower)) + "'")
            logger.info("policies is not correct!")
            assert ','.join(sorted(str(row1["policies"]).split(','),key=str.lower)) == ','.join(sorted(str(row2["policies"]).split(','),key=str.lower)), " policies is not correct!"
        logger.info("assertion passed!")