aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/tests/func_tests/features/steps/assignments.py
blob: e3f7b5a71e657d0e432ba3e70fe19111ed3112a8 (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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
# 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, Column
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 meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get subject assignment id using both the policy id, data id & the category id
# 3) Loop by assignment id and delete it
@Given('the system has no subject assignments')
def step_impl(context):
    logger.info("Given the system has no subject assignments")
    api_responseflag = {'value': False}
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI, headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            # subjectcategoryidslist = []
            # subjectdataidslist = []
            # modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            # if (modelid != None and modelid != ""):
            #     metaruleslist = \
            #         requests.get(apis_urls.serverURL + apis_urls.modelAPI, headers=apis_urls.auth_headers).json()[apis_urls.modelAPI][modelid]['meta_rules']
            #     for metarule_ids in metaruleslist:
            #         categorieslist = \
            #             requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,
            #                          headers=apis_urls.auth_headers).json()[apis_urls.metarulesAPI][
            #                 metarule_ids]['subject_categories']
            #         for categoryid in categorieslist:
            #             if (categoryid not in subjectcategoryidslist):
            #                 subjectcategoryidslist.append(categoryid)
            #
            #     response_perimeters = requests.get(apis_urls.serverURL + apis_urls.perimetersubjectAPI,
            #                                        headers=apis_urls.auth_headers).json()[
            #         apis_urls.perimetersubjectAPI]
            #     for perimeterid in dict(response_perimeters).keys():
            #         for categoryid in subjectcategoryidslist:
            #             response_assignment = requests.get(
            #                 apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementssubjectAPI + "/" +
            #                 perimeterid + "/" + categoryid, headers=apis_urls.auth_headers)
            #             if len(response_assignment.json()[apis_urls.assignementssubjectAPI]) != 0:
            #                 for ids in dict(response_assignment.json()[apis_urls.assignementssubjectAPI]).keys():
            #                     assignmentsid = response_assignment.json()[apis_urls.assignementssubjectAPI][str(ids)][
            #                         'assignments']
            #                     for dataid in assignmentsid:
            response = requests.delete(
                apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementssubjectAPI , headers=headers)

# Step Definition Implementation:
# 1) Get all the existing object meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get object assignment id using both the policy id, data id & the category id
# 3) Loop by assignment id and delete it
@Given('the system has no object assignments')
def step_impl(context):
    logger.info("Given the system has no object assignments")
    api_responseflag = {'value': False}
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI, headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            # objectcategoryidslist = []
            # objectdataidslist = []
            # modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            # if (modelid != None and modelid != ""):
            #     metaruleslist = \
            #         requests.get(apis_urls.serverURL + apis_urls.modelAPI, headers=apis_urls.auth_headers).json()[
            #             apis_urls.modelAPI][modelid][
            #             'meta_rules']
            #     for metarule_ids in metaruleslist:
            #         categorieslist = \
            #             requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,
            #                          headers=apis_urls.auth_headers).json()[apis_urls.metarulesAPI][
            #                 metarule_ids]['object_categories']
            #         for categoryid in categorieslist:
            #             if (categoryid not in objectcategoryidslist):
            #                 objectcategoryidslist.append(categoryid)
            #
            #     response_perimeters = \
            #         requests.get(apis_urls.serverURL + apis_urls.perimeterobjectAPI,
            #                      headers=apis_urls.auth_headers).json()[
            #             apis_urls.perimeterobjectAPI]
            #     for perimeterid in dict(response_perimeters).keys():
            #         for categoryid in objectcategoryidslist:
            #             response_assignment = requests.get(
            #                 apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementsobjectAPI + "/" +
            #                 perimeterid + "/" + categoryid, headers=apis_urls.auth_headers)
            #             if len(response_assignment.json()[apis_urls.assignementsobjectAPI]) != 0:
            #                 for ids in dict(response_assignment.json()[apis_urls.assignementsobjectAPI]).keys():
            #                     assignmentsid = response_assignment.json()[apis_urls.assignementsobjectAPI][str(ids)][
            #                         'assignments']
            #                     for dataid in assignmentsid:
            response = requests.delete(
                apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementsobjectAPI , headers=headers)

# Step Definition Implementation:
# 1) Get all the existing action meta data in the system by getting the policies then their models then the model attached meta rules and then the categories
# 2) Get action assignment id using both the policy id, data id & the category id
# 3) Loop by assignment id and delete it
@Given('the system has no action assignments')
def step_impl(context):
    logger.info("Given the system has no action assignments")
    api_responseflag = {'value': False}
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    response_policies = requests.get(apis_urls.serverURL + apis_urls.policyAPI, headers=apis_urls.auth_headers)
    if len(response_policies.json()[apis_urls.policyAPI]) != 0:
        for policies_ids in dict(response_policies.json()[apis_urls.policyAPI]).keys():
            # actioncategoryidslist = []
            # actiondataidslist = []
            # modelid = response_policies.json()[apis_urls.policyAPI][policies_ids]['model_id']
            # if (modelid != None and modelid != ""):
            #     metaruleslist = \
            #         requests.get(apis_urls.serverURL + apis_urls.modelAPI, headers=apis_urls.auth_headers).json()[
            #             apis_urls.modelAPI][modelid][
            #             'meta_rules']
            #     for metarule_ids in metaruleslist:
            #         categorieslist = \
            #             requests.get(apis_urls.serverURL + apis_urls.metarulesAPI,
            #                          headers=apis_urls.auth_headers).json()[apis_urls.metarulesAPI][
            #                 metarule_ids]['action_categories']
            #         for categoryid in categorieslist:
            #             if (categoryid not in actioncategoryidslist):
            #                 actioncategoryidslist.append(categoryid)
            #
            #     response_perimeters = \
            #         requests.get(apis_urls.serverURL + apis_urls.perimeteractionAPI,
            #                      headers=apis_urls.auth_headers).json()[
            #             apis_urls.perimeteractionAPI]
            #     for perimeterid in dict(response_perimeters).keys():
            #         for categoryid in actioncategoryidslist:
            #             response_assignment = requests.get(
            #                 apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementsactionAPI + "/" +
            #                 perimeterid + "/" + categoryid, headers=apis_urls.auth_headers)
            #             if len(response_assignment.json()[apis_urls.assignementsactionAPI]) != 0:
            #                 for ids in dict(response_assignment.json()[apis_urls.assignementsactionAPI]).keys():
            #                     assignmentsid = response_assignment.json()[apis_urls.assignementsactionAPI][str(ids)][
            #                         'assignments']
            #                     for dataid in assignmentsid:
            response = requests.delete(
                apis_urls.serverURL + "policies/" + policies_ids + "/" + apis_urls.assignementsactionAPI , headers=headers)

# Step Definition Implementation:
# 1) Post subject assignment using the policy id, subject perimeter id, subject category, list of subject data ids
@Given('the following subject assignment exists')
def step_impl(context):
    logger.info("Given the following subject assignment exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject data: '" + row[
                "subjectdata"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_subjectperimeterid(row['subjectperimetername'])
        categories_id = commonfunctions.get_subjectcategoryid(row['subjectcategory'])
        dataids = commonfunctions.get_subjectdataid(row['subjectdata'], categories_id, policies_id)
        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementssubjectAPI,
            headers=headers, data=json.dumps(data))

        GeneralVariables.assignpolicyid['value'] = policies_id
        GeneralVariables.assignsubjectperimeterid['value'] = perimeter_id
        GeneralVariables.assignsubjectcategoryid['value'] = categories_id

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

# Step Definition Implementation:
# 1) Post object assignment using the policy id, object perimeter id, object category, list of object data ids
@Given('the following object assignment exists')
def step_impl(context):
    logger.info("Given the following object assignment exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row["objectperimetername"] + "' object data: '" + row[
                "objectdata"] + "' and object category: '" + row[
                "objectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_objectperimeterid(row['objectperimetername'])
        categories_id = commonfunctions.get_objectcategoryid(row['objectcategory'])
        dataids = commonfunctions.get_objectdataid(row['objectdata'], categories_id, policies_id)

        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'policy_id': policies_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementsobjectAPI,
            headers=headers, data=json.dumps(data))

        GeneralVariables.assignpolicyid['value'] = policies_id
        GeneralVariables.assignobjectperimeterid['value'] = perimeter_id
        GeneralVariables.assignobjectcategoryid['value'] = categories_id

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

# Step Definition Implementation:
# 1) Post action assignment using the policy id, action perimeter id, action category, list of action data ids
@Given('the following action assignment exists')
def step_impl(context):
    logger.info("Given the following action assignment exists")
    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action perimeter name: '" + row["actionperimetername"] + "' action data: '" + row[
                "actiondata"] + "' and action category: '" + row[
                "actioncategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_actionperimeterid(row['actionperimetername'])
        categories_id = commonfunctions.get_actioncategoryid(row['actioncategory'])
        dataids = commonfunctions.get_actiondataid(row['actiondata'], categories_id, policies_id)

        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'policy_id': policies_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementsactionAPI,
            headers=headers, data=json.dumps(data))

        GeneralVariables.assignpolicyid['value'] = policies_id
        GeneralVariables.assignactionperimeterid['value'] = perimeter_id
        GeneralVariables.assignactioncategoryid['value'] = categories_id

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

# Step Definition Implementation:
# 1) Post action assignment using the policy id, action perimeter id, action category, list of action data ids
# 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 assignment')
def step_impl(context):
    logger.info("When the user sets to add the following subject assignment")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject data: '" + row[
                "subjectdata"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        if (row['policyname'] == "" or row['policyname'] == "000000000000000000000000000000000000000000000000000"):
            policyname = "Stanford Policy"
        else:
            policyname = row['policyname']
        policies_id = commonfunctions.get_policyid(policyname)

        if (row["subjectperimetername"] == "" or row[
            "subjectperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimetername = "WilliamsJoeseph"
        else:
            perimetername = row["subjectperimetername"]
        perimeter_id = commonfunctions.get_subjectperimeterid(perimetername)

        if (row["subjectcategory"] == "" or row[
            "subjectcategory"] == "000000000000000000000000000000000000000000000000000"):
            categoriesname = "Affiliation:"
        else:
            categoriesname = row['subjectcategory']
        categories_id = commonfunctions.get_subjectcategoryid(categoriesname)

        if (row["subjectdata"] == "" or row["subjectdata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = "Professor"
        else:
            dataids = row['subjectdata']
        dataids = commonfunctions.get_subjectdataid(dataids, categories_id, policies_id)

        if (dataids == None):
            dataids = ""

        if (row["policyname"] == "" or row["policyname"] == "000000000000000000000000000000000000000000000000000"):
            policies_id = row["policyname"]
        if (row["subjectperimetername"] == "" or row[
            "subjectperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimeter_id = row["subjectperimetername"]
        if (row["subjectcategory"] == "" or row[
            "subjectcategory"] == "000000000000000000000000000000000000000000000000000"):
            categories_id = row["subjectcategory"]
        if (row["subjectdata"] == "" or row["subjectdata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = row['subjectdata']
        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementssubjectAPI,
            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) Post action assignment using the policy id, action perimeter id, action category, list of action data ids
# 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 assignment')
def step_impl(context):
    logger.info("When the user sets to add the following object assignment")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row["objectperimetername"] + "' object data: '" + row[
                "objectdata"] + "' and object category: '" + row[
                "objectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        if (row['policyname'] == "" or row['policyname'] == "000000000000000000000000000000000000000000000000000"):
            policyname = "Stanford Policy"
        else:
            policyname = row['policyname']
        policies_id = commonfunctions.get_policyid(policyname)

        if (row["objectperimetername"] == "" or row[
            "objectperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimetername = "StudentsGradesSheet"
        else:
            perimetername = row["objectperimetername"]
        perimeter_id = commonfunctions.get_objectperimeterid(perimetername)

        if (row["objectcategory"] == "" or row[
            "objectcategory"] == "000000000000000000000000000000000000000000000000000"):
            categoriesname = "Clearance:"
        else:
            categoriesname = row['objectcategory']
        categories_id = commonfunctions.get_objectcategoryid(categoriesname)

        if (row["objectdata"] == "" or row["objectdata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = "Confidential"
        else:
            dataids = row['objectdata']
        dataids = commonfunctions.get_objectdataid(dataids, categories_id, policies_id)

        if (dataids == None):
            dataids = ""

        if (row["policyname"] == "" or row["policyname"] == "000000000000000000000000000000000000000000000000000"):
            policies_id = row["policyname"]
        if (row["objectperimetername"] == "" or row[
            "objectperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimeter_id = row["objectperimetername"]
        if (row["objectcategory"] == "" or row[
            "objectcategory"] == "000000000000000000000000000000000000000000000000000"):
            categories_id = row["objectcategory"]
        if (row["objectdata"] == "" or row["objectdata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = row['objectdata']
        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementsobjectAPI,
            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) Post action assignment using the policy id, action perimeter id, action category, list of action data ids
# 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 assignment')
def step_impl(context):
    logger.info("When the user sets to add the following action assignment")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action perimeter name: '" + row["actionperimetername"] + "' action data: '" + row[
                "actiondata"] + "' and action category: '" + row[
                "actioncategory"] + "' and policies: '" + row['policyname'] + "'")

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

        if (row['policyname'] == "" or row['policyname'] == "000000000000000000000000000000000000000000000000000"):
            policyname = "Stanford Policy"
        else:
            policyname = row['policyname']
        policies_id = commonfunctions.get_policyid(policyname)

        if (row["actionperimetername"] == "" or row[
            "actionperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimetername = "Read"
        else:
            perimetername = row["actionperimetername"]
        perimeter_id = commonfunctions.get_actionperimeterid(perimetername)

        if (row["actioncategory"] == "" or row[
            "actioncategory"] == "000000000000000000000000000000000000000000000000000"):
            categoriesname = "Action-Class:"
        else:
            categoriesname = row['actioncategory']
        categories_id = commonfunctions.get_actioncategoryid(categoriesname)

        if (row["actiondata"] == "" or row["actiondata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = "Severe"
        else:
            dataids = row['actiondata']
        dataids = commonfunctions.get_actiondataid(dataids, categories_id, policies_id)

        if (dataids == None):
            dataids = ""

        if (row["policyname"] == "" or row["policyname"] == "000000000000000000000000000000000000000000000000000"):
            policies_id = row["policyname"]
        if (row["actionperimetername"] == "" or row[
            "actionperimetername"] == "000000000000000000000000000000000000000000000000000"):
            perimeter_id = row["actionperimetername"]
        if (row["actioncategory"] == "" or row[
            "actioncategory"] == "000000000000000000000000000000000000000000000000000"):
            categories_id = row["actioncategory"]
        if (row["actiondata"] == "" or row["actiondata"] == "000000000000000000000000000000000000000000000000000"):
            dataids = row['actiondata']
        data = {
            'id': perimeter_id,
            'category_id': categories_id,
            'policy_id': policies_id,
            'data_id': dataids,
        }
        response = requests.post(
            apis_urls.serverURL + "policies/" + str(policies_id) + "/" + apis_urls.assignementsactionAPI,
            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) Delete subject assignment by policy id,subject perimeter id, subject data id, subject category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following subject assignment')
def step_impl(context):
    logging.info("When the user sets to delete the following subject assignment")
    headers = {"Content-Type": "application/json", "X-Api-Key": apis_urls.token}

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject data list: '" + row[
                "subjectdata"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_subjectperimeterid(row['subjectperimetername'])
        categories_id = commonfunctions.get_subjectcategoryid(row['subjectcategory'])
        dataid = commonfunctions.get_subjectdataid(row["subjectdata"], categories_id, policies_id)

        response_assignment = requests.get(
            apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementssubjectAPI + "/" +
            perimeter_id + "/" + categories_id, headers=apis_urls.auth_headers)
        logging.info(response_assignment.json()[apis_urls.assignementssubjectAPI])
        if len(response_assignment.json()[apis_urls.assignementssubjectAPI]) != 0:
            for ids in dict(response_assignment.json()[apis_urls.assignementssubjectAPI]).keys():
                assignmentsidlist = response_assignment.json()[apis_urls.assignementssubjectAPI][str(ids)][
                    'assignments']
                if dataid in assignmentsidlist:
                    response = requests.delete(
                        apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementssubjectAPI + "/" +
                        perimeter_id + "/" + categories_id + "/" + dataid, 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) Delete object assignment by policy id, object perimeter id, object data id, object category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following object assignment')
def step_impl(context):
    logging.info("When the user sets to delete the following object assignment")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "object perimeter name: '" + row["objectperimetername"] + "' object data list: '" + row[
                "objectdata"] + "' and object category: '" + row[
                "objectcategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_objectperimeterid(row['objectperimetername'])
        categories_id = commonfunctions.get_objectcategoryid(row['objectcategory'])
        dataid = commonfunctions.get_objectdataid(row["objectdata"], categories_id, policies_id)

        response_assignment = requests.get(
            apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementsobjectAPI + "/" +
            perimeter_id + "/" + categories_id, headers=apis_urls.auth_headers)
        if len(response_assignment.json()[apis_urls.assignementsobjectAPI]) != 0:
            for ids in dict(response_assignment.json()[apis_urls.assignementsobjectAPI]).keys():
                assignmentsidlist = response_assignment.json()[apis_urls.assignementsobjectAPI][str(ids)][
                    'assignments']
                if dataid in assignmentsidlist:
                    response = requests.delete(
                        apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementsobjectAPI + "/" +
                        perimeter_id + "/" + categories_id + "/" + dataid, headers=headers)

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


# Step Definition Implementation:
# 1) Delete action assignment by policy id, action perimeter id, action data id, action category id
# 2) If the request code was 200 set the api response flag to true else false
@When('the user sets to delete the following action assignment')
def step_impl(context):
    logging.info("When the user sets to delete the following action assignment")

    model = getattr(context, "model", None)
    for row in context.table:
        logger.info(
            "action perimeter name: '" + row["actionperimetername"] + "' action data list: '" + row[
                "actiondata"] + "' and action category: '" + row[
                "actioncategory"] + "' and policies: '" + row['policyname'] + "'")

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

        policies_id = commonfunctions.get_policyid(row['policyname'])
        perimeter_id = commonfunctions.get_actionperimeterid(row['actionperimetername'])
        categories_id = commonfunctions.get_actioncategoryid(row['actioncategory'])
        dataid = commonfunctions.get_actiondataid(row["actiondata"], categories_id, policies_id)

        response_assignment = requests.get(
            apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementsactionAPI + "/" +
            perimeter_id + "/" + categories_id, headers=apis_urls.auth_headers)
        if len(response_assignment.json()[apis_urls.assignementsactionAPI]) != 0:
            for ids in dict(response_assignment.json()[apis_urls.assignementsactionAPI]).keys():
                assignmentsidlist = response_assignment.json()[apis_urls.assignementsactionAPI][str(ids)][
                    'assignments']
                if dataid in assignmentsidlist:
                    response = requests.delete(
                        apis_urls.serverURL + "policies/" + policies_id + "/" + apis_urls.assignementsactionAPI + "/" +
                        perimeter_id + "/" + categories_id + "/" + dataid, 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 assignment per a given policy, subject perimeter and subject category by get request and put them into a table
# 2) Sort the table by subject perimeter name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following subject assignment should be existed in the system')
def step_impl(context):
    logger.info("Then the following subject assignment should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('subjectperimetername', 'subjectcategory', 'subjectdata', 'policyname'),
                      dtype=('S100', 'S100', 'S100', 'S100'))
    for row in context.table:
        logger.info(
            "subject perimeter name: '" + row["subjectperimetername"] + "' subject data list: '" + row[
                "subjectdata"] + "' and subject category: '" + row[
                "subjectcategory"] + "' and policies: '" + row['policyname'] + "'")
        if (row['policyname'] == "" or row['subjectperimetername'] == ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + GeneralVariables.assignpolicyid[
                    'value'] + "/" + apis_urls.assignementssubjectAPI + "/" +
                GeneralVariables.assignsubjectperimeterid['value'] + "/" +
                GeneralVariables.assignsubjectcategoryid['value'], headers=apis_urls.auth_headers)
        else:
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.assignementssubjectAPI + "/" +
                commonfunctions.get_subjectperimeterid(row['subjectperimetername']) + "/" +
                commonfunctions.get_subjectcategoryid(row['subjectcategory']), headers=apis_urls.auth_headers)
        if len(response.json()[apis_urls.assignementssubjectAPI]) != 0:
            for ids in dict(response.json()[apis_urls.assignementssubjectAPI]).keys():
                apipolicies = ""
                apisubjectname = commonfunctions.get_subjectperimetername(
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['subject_id'])
                apisubjectcategory = commonfunctions.get_subjectcategoryname(
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['category_id'])
                apiassignments = commonfunctions.get_subjectdataname(
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['assignments'],
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['category_id'],
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['policy_id'])
                apipolicies = commonfunctions.get_policyname(
                    response.json()[apis_urls.assignementssubjectAPI][str(ids)]['policy_id'])
                if ((row['policyname'] == "" or row['subjectperimetername'] == "") and "".join(apiassignments)==""):
                    apiresult.add_row(vals=("", "", "", ""))
                else:
                    apiresult.add_row(vals=(
                        apisubjectname, apisubjectcategory, apiassignments, 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 data description: '" + str(
            row1["subjectcategory"]) + "' is the same as the actual existing '" + str(
            row2["subjectcategory"]) + "'")
        assert str(row1["subjectcategory"]) == str(
            row2["subjectcategory"]), "subject category is not correct!"
        logger.info("assertion passed!")

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

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

# Step Definition Implementation:
# 1) Get all the existing object assignment per a given policy, object perimeter and object category by get request and put them into a table
# 2) Sort the table by object perimeter name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following object assignment should be existed in the system')
def step_impl(context):
    logger.info("Then the following object assignment should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('objectperimetername', 'objectcategory', 'objectdata', 'policyname'),
                      dtype=('S100', 'S100', 'S400', 'S100'))
    for row in context.table:
        if (row['policyname'] == "" or row['objectperimetername'] == ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + GeneralVariables.assignpolicyid[
                    'value'] + "/" + apis_urls.assignementsobjectAPI + "/" +
                GeneralVariables.assignobjectperimeterid['value'] + "/" +
                GeneralVariables.assignobjectcategoryid['value'], headers=apis_urls.auth_headers)
        else:
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.assignementsobjectAPI + "/" +
                commonfunctions.get_objectperimeterid(row['objectperimetername']) + "/" +
                commonfunctions.get_objectcategoryid(row['objectcategory']), headers=apis_urls.auth_headers)

        if len(response.json()[apis_urls.assignementsobjectAPI]) != 0:
            for ids in dict(response.json()[apis_urls.assignementsobjectAPI]).keys():
                apipolicies = ""
                apiobjectname = commonfunctions.get_objectperimetername(
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['object_id'])
                apiobjectcategory = commonfunctions.get_objectcategoryname(
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['category_id'])
                apiassignments = commonfunctions.get_objectdataname(
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['assignments'],
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['category_id'],
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['policy_id'])
                apipolicies = commonfunctions.get_policyname(
                    response.json()[apis_urls.assignementsobjectAPI][str(ids)]['policy_id'])
                if ((row['policyname'] == "" or row['objectperimetername'] == "") and "".join(apiassignments) == ""):
                    apiresult.add_row(vals=("", "", "", ""))
                else:
                    apiresult.add_row(vals=(
                        apiobjectname, apiobjectcategory, ",".join(apiassignments), 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 data description: '" + str(
            row1["objectcategory"]) + "' is the same as the actual existing '" + str(
            row2["objectcategory"]) + "'")
        assert str(row1["objectcategory"]) == str(
            row2["objectcategory"]), "object category is not correct!"
        logger.info("assertion passed!")

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

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

# Step Definition Implementation:
# 1) Get all the existing action assignment per a given policy, action perimeter and action category by get request and put them into a table
# 2) Sort the table by action perimeter name
# 3) Loop using both the expected and actual tables and assert the data row by row
@Then('the following action assignment should be existed in the system')
def step_impl(context):
    logger.info("Then the following action assignment should be existed in the system")
    model = getattr(context, "model", None)
    apiresult = Table(names=('actionperimetername', 'actioncategory', 'actiondata', 'policyname'),
                      dtype=('S100', 'S100', 'S100', 'S100'))
    for row in context.table:
        if (row['policyname'] == "" or row['actionperimetername'] == ""):
            response = requests.get(
                apis_urls.serverURL + "policies/" + GeneralVariables.assignpolicyid[
                    'value'] + "/" + apis_urls.assignementsactionAPI + "/" +
                GeneralVariables.assignactionperimeterid['value'] + "/" +
                GeneralVariables.assignactioncategoryid['value'], headers=apis_urls.auth_headers)
        else:
            response = requests.get(
                apis_urls.serverURL + "policies/" + commonfunctions.get_policyid(
                    row['policyname']) + "/" + apis_urls.assignementsactionAPI + "/" +
                commonfunctions.get_actionperimeterid(row['actionperimetername']) + "/" +
                commonfunctions.get_actioncategoryid(row['actioncategory']), headers=apis_urls.auth_headers)

        if len(response.json()[apis_urls.assignementsactionAPI]) != 0:
            for ids in dict(response.json()[apis_urls.assignementsactionAPI]).keys():
                apipolicies = ""
                apiactionname = commonfunctions.get_actionperimetername(
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['action_id'])
                apiactioncategory = commonfunctions.get_actioncategoryname(
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['category_id'])
                apiassignments = commonfunctions.get_actiondataname(
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['assignments'],
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['category_id'],
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['policy_id'])
                apipolicies = commonfunctions.get_policyname(
                    response.json()[apis_urls.assignementsactionAPI][str(ids)]['policy_id'])
                logger.info(apiassignments)
                if ((row['policyname'] == "" or row['actionperimetername'] == "") and "".join(apiassignments) == ""):
                    apiresult.add_row(vals=("", "", "", ""))
                else:
                    apiresult.add_row(vals=(
                        apiactionname, apiactioncategory, apiassignments, 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 data description: '" + str(
            row1["actioncategory"]) + "' is the same as the actual existing '" + str(
            row2["actioncategory"]) + "'")
        assert str(row1["actioncategory"]) == str(
            row2["actioncategory"]), "action category is not correct!"
        logger.info("assertion passed!")

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

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