-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.sql
1927 lines (1824 loc) · 91 KB
/
tests.sql
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\set keep_test `echo "$KEEP_TEST_DATA"`
\set del_data `echo "$DELETE_EXISTING_DATA"`
create or replace function test_persons_users_groups()
returns boolean as $$
declare pid uuid;
declare num int;
declare uid int;
declare gid int;
declare pgrp text;
declare exp timestamptz;
begin
insert into persons (full_name, person_expiry_date)
values ('Sarah Conner', '2020-10-01');
select person_id from persons where full_name like '%Conner' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-sconne', '2020-03-28');
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p66-sconne', '2019-12-01');
-- creation
assert (select count(*) from persons) = 1, 'person creation issue';
assert (select count(*) from users) = 2, 'user creation issue';
assert (select count(*) from groups) = 3, 'group creation issue';
-- posix uids
select user_posix_uid from users where user_name = 'p66-sconne' into uid;
assert (select generate_new_posix_uid() = uid + 1), 'uid generation not monotonically increasing';
begin
update users set user_posix_uid = '2000' where user_name = 'p11-sconne';
assert false, 'user_posix_uid is mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- posix gids
select group_posix_gid from groups where group_name = 'p66-sconne-group' into gid;
assert (select generate_new_posix_gid() = gid + 1), 'gid generation not monotonically increasing';
select person_group from persons where person_id = pid into pgrp;
select group_posix_gid from groups where group_name = pgrp into gid;
assert gid is null, 'person groups are being assigned gids';
begin
insert into groups (group_name, group_type, group_posix_gid)
values ('g1', 'generic', 0);
assert false, 'can assign gid between 0 and 999';
exception when check_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_posix_gid = '2000' where group_name = 'p11-sconne-group';
assert false, 'group_posix_gid is mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
insert into users (person_id, user_name, user_expiry_date, user_group_posix_gid)
values (pid, 'p89-sconne', '2019-12-01', 9001);
assert (select group_posix_gid from groups where group_name = 'p89-sconne-group') = 9001,
'cannot explicitly set user gid';
assert (select user_group_posix_gid from users where user_group = 'p89-sconne-group') = 9001,
'user group gids are nor being synced to the users table';
-- person identifiers uniqueness
begin
insert into persons (full_name, identifiers)
values ('Piet Mondrian', '[{"k1": 0}, {"k2": 1}]'::json);
insert into persons (full_name, identifiers)
values ('Piet Mondrian', '[{"k2": 1}]'::json);
assert false, 'persons identifiers are not ensured to be unique';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
insert into persons (full_name, identifiers)
values ('Jackson Pollock', '{"k3": 99}'::json);
assert false, 'persons identifiers are not ensured to be json arrays';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
-- person attribute immutability
begin
update persons set row_id = 'e14c538a-4b8b-4393-9fb2-056e363899e1';
assert false, 'row_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update persons set person_id = 'e14c538a-4b8b-4393-9fb2-056e363899e1';
assert false, 'person_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update persons set person_group = 'e14c538a-4b8b-4393-9fb2-056e363899e1-group';
assert false, 'person_group mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- user attribute immutability
begin
update users set row_id = 'e14c538a-4b8b-4393-9fb2-056e363899e1';
assert false, 'row_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update users set user_id = 'a3981c7f-8e41-4222-9183-1815b6ec9c3b';
assert false, 'user_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update users set user_name = 'p11-scnr';
assert false, 'user_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update users set user_group = 'p11-s-group';
assert false, 'user_group mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- group attribute immutability
begin
update groups set row_id = 'e14c538a-4b8b-4393-9fb2-056e363899e1';
assert false, 'row_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_id = 'e14c538a-4b8b-4393-9fb2-056e363899e1';
assert false, 'group_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_name = 'p22-lcd-group';
assert false, 'group_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_class = 'secondary';
assert false, 'group_class mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_type = 'person';
assert false, 'group_type mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- web groups, and their gid life-cycle
insert into groups (group_name, group_class, group_type)
values ('p11-wonderful-group', 'secondary', 'web');
assert (select group_posix_gid from groups where group_name = 'p11-wonderful-group') is null,
'web groups are not allowed to not have gids - while they should be';
update groups set group_posix_gid = (select max(group_posix_gid) + 1 from groups)
where group_name = 'p11-wonderful-group';
begin
update groups set group_posix_gid = null where group_name = 'p11-wonderful-group';
assert false, 'can remove gids for web groups';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
delete from groups where group_name = 'p11-wonderful-group';
-- states; cascades, constraints
set session "request.identity" = 'milen';
-- activation status
update persons set person_activated = 'f';
select person_group from persons where person_id = pid into pgrp;
assert (select group_activated from groups where group_name = pgrp) = 'f',
'person activation status does not propagate to person groups';
assert (select count(*) from users where user_activated = 't') = 0,
'person state changes not propagating to users';
assert (select count(*) from groups where group_activated = 't') = 0,
'person state changes not propagating to groups';
begin
update groups set group_activated = 't'
where group_name = pid::text || '-group';
assert false, 'person groups can be dectivated directly on group table';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_activated = 't'
where group_name = 'p66-sconne-group';
assert false, 'user groups can be dectivated directly on group table';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
-- expiry dates
update persons set person_expiry_date = '2019-09-09';
-- expiry person group
select group_expiry_date from groups where group_name = pgrp into exp;
assert exp = '2019-09-09', 'person expiry not propagating to person group';
-- expiry from person to users
for exp in select user_expiry_date from users where person_id = pid loop
assert exp = '2019-09-09', 'person expiry not propagating to its users';
end loop;
update users set user_expiry_date = '2000-08-08' where user_name like 'p11-%';
-- user exp restrictions
select group_expiry_date from groups where group_name = (
select user_group from users where user_name = 'p11-sconne'
) into exp;
assert exp = '2000-08-08', 'user expiry not propagating to user group';
begin
update groups set group_expiry_date = '2000-01-01'
where group_primary_member = 'p11-sconne';
assert false, 'user group exp updates bypasses restriction via primary';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
begin
update groups set group_expiry_date = '2000-01-01'
where group_primary_member = pid::text;
assert false, 'person group exp updates bypasses restriction via primary';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
begin
update users set user_expiry_date = '2030-01-01'
where user_name like 'p66-s%';
assert false, 'users can expire _after_ persons - update';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'lol-user', '2080-01-01');
assert false, 'users can expire _after_ persons - insert';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- deletion; cascades, constraints
begin
delete from groups where group_type = 'person';
assert false, 'person group deletion not protected';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
begin
delete from groups where group_type = 'user';
assert false, 'user group deletion not protected';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
begin
delete from groups where group_class = 'primary';
assert false, 'primary group deletion not protected';
exception when restrict_violation then
raise notice '%', sqlerrm;
end;
delete from persons;
assert (select count(*) from users) = 0, 'cascading delete from person to users not working';
assert (select count(*) from groups) = 0, 'cascading delete from person to groups not working';
return true;
end;
$$ language plpgsql;
create or replace function test_posix_with_missing_audit_log()
returns boolean as $$
declare
pid uuid;
uid int;
gid int;
next_uid int;
next_gid int;
begin
-- Insert a new person
insert into persons (full_name)
values ('Rumpus McFowl') returning person_id into pid;
-- Insert a user for this person
insert into users (person_id, user_name)
values (pid, 'p11-jubalon')
returning user_posix_uid into uid;
-- Insert user primary group ID into gid variable
select user_group_posix_gid from users where person_id = pid into gid;
-- Debug output
raise notice 'Create user for % with uid = %, gid = %', pid, uid, gid;
-- Retrieve the next available POSIX UID
select generate_new_posix_uid() into next_uid;
assert next_uid = (uid + 1);
-- Delete the corresponding audit log entry
delete from audit_log_objects_users where new_data = uid::text;
-- Assert that the generated UID is as expected
if (select generate_new_posix_uid() != next_uid) then
raise exception 'UID generation not verifying whether ID in use';
end if;
-- Output the results
raise notice 'Test passed: uid = %, new_posix_uid = %', uid, generate_new_posix_uid();
-- Retrieve the next available POSIX GID
select generate_new_posix_gid() into next_gid;
assert next_gid = (gid + 1);
-- Delete the corresponding audit log entry
delete from audit_log_objects_groups where new_data = gid::text;
-- Assert that the generated UID is as expected
if (select generate_new_posix_gid() != next_gid) then
raise exception 'GID generation not verifying whether ID in use';
end if;
-- Output the results
raise notice 'Test passed: gid = %, new_posix_gid = %', gid, generate_new_posix_gid();
return true;
end;
$$ language plpgsql;
create or replace function test_group_memeberships_moderators()
returns boolean as $$
declare pid uuid;
declare row record;
declare out json;
begin
-- create persons and users
insert into persons (full_name, person_expiry_date)
values ('Sarah Conner', '2050-10-01');
select person_id from persons where full_name like '%Conner' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-sconne', '2050-03-28');
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p66-sconne', '2019-12-01');
insert into persons (full_name, person_expiry_date)
values ('John Conner2', '2050-10-01');
select person_id from persons where full_name like '%Conner2' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-jconn', '2050-03-28');
insert into persons (full_name, person_expiry_date)
values ('Frank Castle', '2050-10-01');
select person_id from persons where full_name like '%Castle' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-fcl', '2050-03-28');
insert into persons (full_name, person_expiry_date)
values ('Virginia Woolf', '2050-10-01');
select person_id from persons where full_name like '%Woolf' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-vwf', '2050-03-28');
insert into persons (full_name, person_expiry_date)
values ('David Gilgamesh', '2050-10-01');
select person_id from persons where full_name like '%Gilgamesh' into pid;
insert into users (person_id, user_name, user_expiry_date)
values (pid, 'p11-dgmsh', '2050-03-28');
-- create groups
insert into groups (group_name, group_class, group_type)
values ('p11-admin-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p11-export-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p11-publication-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p11-clinical-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p11-import-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p11-special-group', 'secondary', 'generic');
-- add members
insert into group_memberships (group_name, group_member_name)
values ('p11-export-group', 'p11-admin-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-export-group', 'p11-sconne-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-export-group', 'p11-jconn-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-export-group', 'p11-clinical-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-admin-group', 'p11-fcl-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-publication-group', 'p11-vwf-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-admin-group', 'p11-publication-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-clinical-group', 'p11-dgmsh-group');
insert into group_memberships (group_name, group_member_name)
values ('p11-special-group', 'p11-import-group');
/*
This gives a valid group membership graph as follows:
p11-export-group
-> p11-sconne-group
-> p11-jconn-group
-> p11-clinical-group
-> p11-dgmsh-group
-> p11-admin-group
-> p11-fcl-group
-> p11-publication-group
-> p11-vwf-group
We should be able to resolve such DAGs, of arbitrary depth
until we can report back the list of all group_primary_member(s).
And optionally, the structure of the graph. In this case the list is:
p11-sconne
p11-jconn
p11-dgmsh
p11-fcl
p11-vwf
*/
raise notice 'group_name, group_member_name, group_class, group_type, group_primary_member';
for row in select * from pgiam.first_order_members loop
raise notice '%', row;
end loop;
/* GROUP MEMBERS */
-- referential constraints
begin
insert into group_memberships (group_name, group_member_name)
values ('p77-clinical-group', 'p11-special-group');
assert false, 'group_memberships: referential constraints do not work';
exception when foreign_key_violation then
raise notice '%', sqlerrm;
end;
-- redundancy
begin
insert into group_memberships (group_name, group_member_name) values ('p11-export-group','p11-publication-group');
assert false, 'group_memberships: redundancy check not working';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- cyclicality
begin
insert into group_memberships (group_name, group_member_name) values ('p11-publication-group','p11-export-group');
assert false, 'group_memberships: cyclicality check not working';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
insert into group_memberships (group_name, group_member_name) values ('p11-admin-group','p11-export-group');
assert false, 'group_memberships: cyclicality check not working, for transitive members';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- immutability
begin
update group_memberships set group_name = 'p11-clinical-group' where group_name = 'p11-special-group';
assert false, 'group_memberships: group_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update group_memberships set group_member_name = 'p11-clinical-group' where group_name = 'p11-special-group';
assert false, 'group_memberships: group_member_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- group classes
begin
insert into group_memberships values ('p11-sconne-group', 'p11-special-group');
assert false, 'group_memberships: primary groups cannot have new members';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- new relations and group activation state
begin
update groups set group_activated = 'f' where group_name = 'p11-import-group';
insert into group_memberships (group_name, group_member_name) values ('p11-publication-group','p11-import-group');
assert false, 'group_memberships: deactivated groups cannot be used in new relations';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- new relations and group expiry
begin
update groups set group_expiry_date = '2017-01-01' where group_name = 'p11-import-group';
insert into group_memberships (group_name, group_member_name) values ('p11-publication-group','p11-import-group');
assert false, 'group_memberships: expired groups cannot be used in new relations';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- shouldnt be able to be a member of itself
begin
insert into group_memberships (group_name, group_member_name)
values ('p11-special-group', 'p11-special-group');
assert false, 'group_memberships: redundancy check - groups can be members of themselves';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- safeguards in group_member_add
begin
perform group_member_add('lol', 'p11-import-group');
assert false, 'group_member_add does not detect non-existent group';
exception when foreign_key_violation then
raise notice '%', sqlerrm;
end;
begin
perform group_member_add('p11-import-group', 'yeah');
assert false, 'group_member_add does not detect non-existent group';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
begin
perform group_member_add('p11-import-group', '59c1987e-fa15-4509-9b4e-12557fdb9ed9');
assert false, 'group_member_add does not detect non-existent person_id';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
/* GROUP MODERATORS */
insert into group_moderators (group_name, group_moderator_name)
values ('p11-import-group', 'p11-admin-group');
insert into group_moderators (group_name, group_moderator_name)
values ('p11-clinical-group', 'p11-special-group');
-- self-moderation
insert into group_moderators (group_name, group_moderator_name)
values ('p11-admin-group', 'p11-admin-group');
-- referential constraints
begin
insert into group_moderators (group_name, group_moderator_name)
values ('p79-clinical-group', 'p11-special-group');
assert false, 'group_moderators: referential constraints do not work';
exception when foreign_key_violation then
raise notice '%', sqlerrm;
end;
-- immutability
begin
update group_moderators set group_name = 'p11-admin-group' where group_name = 'p11-import-group';
assert false, 'group_moderators: group_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update group_moderators set group_moderator_name = 'p11-export-group' where group_name = 'p11-import-group';
assert false, 'group_moderators: group_moderator_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- redundancy
begin
insert into group_moderators (group_name, group_moderator_name)
values ('p11-clinical-group', 'p11-special-group');
assert false, 'group_moderators: redundancy check - can recreate existing relations';
exception when unique_violation then
raise notice '%', sqlerrm;
end;
-- cyclicality
begin
insert into group_moderators (group_name, group_moderator_name)
values ('p11-special-group', 'p11-clinical-group');
assert false, 'group_moderators: cyclicality check not working';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- new relations and group activation state
begin
insert into groups (group_name, group_class, group_type)
values ('p11-lol-group', 'secondary', 'generic');
update groups set group_activated = 'f' where group_name = 'p11-lol-group';
insert into group_moderators (group_name, group_moderator_name)
values ('p11-lol-group', 'p11-admin-group');
assert false, 'group_moderators: deactivated groups can be used';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- new relations and group expiry
begin
insert into groups (group_name, group_class, group_type)
values ('p11-lol-group', 'secondary', 'generic');
update groups set group_expiry_date = '2011-01-01' where group_name = 'p11-lol-group';
insert into group_moderators (group_name, group_moderator_name)
values ('p11-lol-group', 'p11-admin-group');
assert false, 'group_moderators: expired groups cannot be used';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
update groups set group_expiry_date = '2011-01-01' where group_name = 'p11-export-group';
--delete from persons;
--delete from groups;
return true;
end;
$$ language plpgsql;
create or replace function test_group_membership_constraints()
returns boolean as $$
declare mems json;
declare grps json;
declare tomorrow jsonb;
declare hour_from_now jsonb;
declare num int;
begin
-- create persons, users, groups
insert into persons (full_name, person_expiry_date)
values ('Bruce Wayne', '2050-10-01');
insert into persons (full_name, person_expiry_date)
values ('Peter Parker', '2060-10-01');
insert into persons (full_name, person_expiry_date)
values ('Frida Kahlo', '2077-10-01');
insert into persons (full_name, person_expiry_date)
values ('Carol Danvers', '2080-10-01');
insert into persons (full_name, person_expiry_date)
values ('Breyten Breytenbach', '2090-10-01');
insert into users (person_id, user_name, user_expiry_date)
values (
(select person_id from persons where full_name like 'Bruce%'),
'p12-bwn',
'2050-03-28'
);
insert into users (person_id, user_name, user_expiry_date)
values (
(select person_id from persons where full_name like 'Peter%'),
'p12-pp',
'2060-10-01'
);
insert into users (person_id, user_name, user_expiry_date)
values (
(select person_id from persons where full_name like 'Frida%'),
'p12-fkl',
'2060-10-01'
);
insert into users (person_id, user_name, user_expiry_date)
values (
(select person_id from persons where full_name like 'Carol%'),
'p12-cld',
'2060-10-01'
);
insert into users (person_id, user_name, user_expiry_date)
values (
(select person_id from persons where full_name like 'Breyten%'),
'p12-brb',
'2060-10-01'
);
insert into groups (group_name, group_class, group_type)
values ('p12-admin-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p12-export-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p12-temp-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type)
values ('p12-guest-group', 'secondary', 'generic');
insert into groups (group_name, group_class, group_type, group_expiry_date)
values ('p12-lol-group', 'secondary', 'generic', '2090-01-01');
-- check membership data validation
-- start and end date constraints
begin
perform group_member_add('p12-lol-group', 'p12-brb', null, '2091-01-01');
assert false, 'membership: end_date can exceed group expiry check works';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
insert into group_memberships(
group_name, group_member_name, start_date, end_date
) values (
'p12-admin-group', 'p12-brb-group', '2080-01-01', '2000-01-01'
);
assert false, 'membership: start_date < end_date check works';
exception when check_violation then
raise notice '%', sqlerrm;
end;
-- weekdays constraints
begin
insert into group_memberships(
group_name, group_member_name, weekdays
) values (
'p12-admin-group', 'p12-brb-group', '{"Lol": {}}'::jsonb
);
assert false, 'membership: weekdays wrong key name refused';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
begin
insert into group_memberships(
group_name, group_member_name, weekdays
) values (
'p12-admin-group', 'p12-brb-group', '{"mon": {"end": "12:00"}}'::jsonb
);
assert false, 'membership: weekdays missing start time';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
begin
insert into group_memberships(
group_name, group_member_name, weekdays
) values (
'p12-admin-group', 'p12-brb-group', '{"mon": {"start": "12:00"}}'::jsonb
);
assert false, 'membership: weekdays missing end date';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
begin
insert into group_memberships(
group_name, group_member_name, weekdays
) values (
'p12-admin-group', 'p12-brb-group', '{"mon": {"start": "13:00", "end": "12:00"}}'::jsonb
);
assert false, 'membership: weekdays start > end time';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
-- create a valid membership graph
/*
p12-export-group
-> p12-bwn-group -> p12-bwn
-> p12-admin-group
-> p12-pp-group -> p12-pp
-> p12-temp-group
-> p12-fkl-group -> p12-fkl (initial: not active yet)
-> p12-guest-group
-> p12-cld-group -> p12-cld (initial: expired)
*/
perform group_member_add('p12-export-group', 'p12-bwn');
perform group_member_add('p12-export-group', 'p12-admin-group');
perform group_member_add('p12-admin-group', 'p12-pp');
perform group_member_add('p12-admin-group', 'p12-temp-group');
perform group_member_add('p12-temp-group', 'p12-fkl', '2080-01-01', '2081-11-01');
perform group_member_add('p12-admin-group', 'p12-guest-group');
perform group_member_add('p12-guest-group', 'p12-cld', '2020-01-01', '2021-03-01');
-- check membership reporting (without and with constraint enforcement)
-- group_members
select group_members('p12-export-group') into mems;
assert json_array_length(mems->'direct_members') = 2, 'not reporting direct_members correctly';
assert json_array_length(mems->'transitive_members') = 5, 'not reporting transitive_members correctly';
assert json_array_length(mems->'ultimate_members') = 4, 'not reporting ultimate_members correctly';
-- with constraint filtering
select group_members('p12-export-group', 't') into mems;
assert json_array_length(mems->'direct_members') = 2, 'not reporting direct_members correctly';
assert json_array_length(mems->'transitive_members') = 3, 'not reporting transitive_members correctly';
assert json_array_length(mems->'ultimate_members') = 2, 'not filtering ultimate_members correctly';
-- add a weekday filter allowing only tomorrow
select ('{"' || day_from_ts(current_timestamp + interval '1 day') ||
'": {"start": "08:00", "end": "19:00"}}')::jsonb
into tomorrow;
update group_memberships set weekdays = tomorrow
where group_name = 'p12-export-group'
and group_member_name = 'p12-bwn-group';
-- using current_timestamp (default)
select group_members('p12-export-group', 't') into mems;
assert json_array_length(mems->'ultimate_members') = 1, 'not filtering ultimate_members correctly (defaut client_timestamp)';
-- pretend we're at time zone 0
-- add a weekday filter or after an hour from now (relative to time zone 0), today
select ('{"' || day_from_ts(current_timestamp at time zone '0') ||
'": {"start": "' || (current_time at time zone '0' + interval '1 hour')::time ||
'", "end": "' || (current_time at time zone '0' + interval '3 hour')::time || '"}}')::jsonb
into hour_from_now;
update group_memberships set weekdays = hour_from_now
where group_name = 'p12-export-group'
and group_member_name = 'p12-bwn-group';
-- using client_timestamp provided by the caller (within the allowed time period)
select group_members('p12-export-group', 't', (current_timestamp at time zone '0' + interval '2 hour')) into mems;
assert json_array_length(mems->'ultimate_members') = 2, 'not filtering ultimate_members correctly (caller client_timestamp)';
-- using client_timestamp provided by the caller (outside the allowed time period)
select group_members('p12-export-group', 't', (current_timestamp at time zone '0' + interval '4 hour')) into mems;
assert json_array_length(mems->'ultimate_members') = 1, 'not filtering ultimate_members correctly (caller client_timestamp)';
-- ensure working limits on client timestamp
begin
select group_members('p12-export-group', 't', (current_timestamp + interval '4 days')) into mems;
assert false, 'membership: impossible client_timestamp refused';
exception when invalid_parameter_value then
raise notice '%', sqlerrm;
end;
-- user_groups
select user_groups('p12-fkl') into grps;
assert json_array_length(grps->'user_groups') = 4, 'user_groups issue (no filtering)';
select user_groups('p12-fkl', 't') into grps;
assert json_array_length(grps->'user_groups') = 1, 'user_groups issue (with filtering - start date)';
select user_groups('p12-bwn') into grps;
assert json_array_length(grps->'user_groups') = 2, 'user_groups issue (no filtering)';
select user_groups('p12-bwn', 't', (current_timestamp at time zone '0' + interval '4 hour')) into grps;
assert json_array_length(grps->'user_groups') = 1, 'user_groups issue (with filtering - weekdays)';
-- check audit
select count(*) from audit_log_relations
where parent = 'p12-export-group'
and operation = 'UPDATE'
and weekdays is not null
into num;
assert num > 0, 'audit_log_relations not recording changes to weekdays';
-- delete persons, users, and groups
delete from persons where
full_name like 'Bruce%'
or full_name like 'Peter%'
or full_name like 'Frida%'
or full_name like 'Carol%'
or full_name like 'Breyten%';
delete from groups where group_name like 'p12%';
return 'true';
end;
$$ language plpgsql;
create or replace function test_capabilities_http()
returns boolean as $$
declare cid uuid;
declare grid uuid;
declare ans boolean;
declare grid1 uuid;
declare grid2 uuid;
declare grid3 uuid;
declare grid4 uuid;
begin
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date)
values ('p11import', '{api.com}', '{"role": "p11_import_user"}',
'{"p11-export-group", "p11-special-group"}', 'exact',
'123', 'bla', current_date);
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date)
values ('export', '{api.com}', '{"role": "export_user"}',
'{"admin-group", "export-group"}', 'wildcard',
'123', 'bla', current_date);
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date)
values ('admin', '{api.com}', '{"role": "admin_user"}',
'{"admin-group", "special-group"}', 'wildcard',
'123', 'bla', current_date);
-- immutability
begin
update capabilities_http set row_id = '35b77cf9-0a6f-49d7-83df-e388d75c4b0b';
assert false, 'capabilities_http: row_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update capabilities_http set capability_id = '35b77cf9-0a6f-49d7-83df-e388d75c4b0b';
assert false, 'capabilities_http: capability_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update capabilities_http set capability_name = 'lol';
assert false, 'capabilities_http: capability_name mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- uniqueness
begin
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date)
values ('admin', '{api.com}', '{"role": "admin_user"}',
'{"admin-group", "special-group"}', 'wildcard',
'123', 'bla', current_date);
assert false, 'capabilities_http: name uniqueness not guaranteed';
exception when others then
raise notice '%', sqlerrm;
end;
begin
update capabilities_http set capability_required_groups = '{self,self}'
where capability_name = 'admin';
assert false, 'capabilities_http: required groups are guaranteed unique';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- referential constraints
begin
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date)
values ('admin2', '{api.com}', '{"role": "admin_user"}',
'{"admin2-group", "very-special-group"}', 'wildcard',
'123', 'bla', current_date);
assert false, 'capabilities_http: optional group existence check not working';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- ability to override group references
insert into capabilities_http (capability_name, capability_hostnames, capability_default_claims,
capability_required_groups, capability_group_match_method,
capability_lifetime, capability_description, capability_expiry_date,
capability_group_existence_check)
values ('admin2', '{api.com}', '{"role": "admin_user"}',
'{"admin2-group", "very-special-group"}', 'wildcard',
'123', 'bla', current_date, 'f');
delete from capabilities_http where capability_name = 'admin2';
insert into capabilities_http_grants (capability_grant_rank, capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern)
values (null, '{export}',
'{api.com}', 'files',
'PUT', '/p11/files');
insert into capabilities_http_grants (capability_grant_rank, capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern)
values (1, '{export}',
'{api.com}', 'files',
'GET', '/(.*)/export');
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern)
values ('{export}',
'{api.com}', 'files',
'DELETE', '/(.*)/files');
insert into capabilities_http_grants (capability_grant_rank, capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern)
values (2, '{export,admin}',
'{api.com}', 'files',
'GET', '/(.*)/admin');
-- immutability
begin
update capabilities_http_grants set row_id = '35b77cf9-0a6f-49d7-83df-e388d75c4b0b';
assert false, 'capabilities_http_grants: row_id mutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
update capabilities_http_grants set capability_grant_id = '35b77cf9-0a6f-49d7-83df-e388d75c4b0b';
assert false, 'capabilities_http_grants: capability_grant_id immutable';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- referential constraints
begin
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern,
capability_grant_required_groups)
values ('{i-do-not-exist}',
'{api.com}', 'files',
'GET', '/(.*)/admin',
'{"p11-export-group"}');
assert false, 'possible to reference non-existent capability from grants';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
begin
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern,
capability_grant_required_groups)
values ('{export}',
'{api.com}', 'files',
'GET', '/(.*)/admin',
'{"my-own-crazy-group"}');
assert false, 'capabilities_http_grants: required groups need to exist when referenced, by default';
exception when integrity_constraint_violation then
raise notice '%', sqlerrm;
end;
-- ability to override group references
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern,
capability_grant_required_groups, capability_grant_group_existence_check)
values ('{export}',
'{api.com}', 'files',
'GET', '/(.*)/admin',
'{"my-own-crazy-group"}', 'f');
-- add some more test data
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern,
capability_grant_required_groups, capability_grant_group_existence_check)
values ('{export}',
'{api.com}', 'files',
'GET', '/(.*)/export',
'{"my-own-custom-export-group"}', 'f');
insert into capabilities_http_grants (capability_names_allowed,
capability_grant_hostnames, capability_grant_namespace,
capability_grant_http_method, capability_grant_uri_pattern,
capability_grant_required_groups, capability_grant_group_existence_check)
values ('{export}',
'{api.com}', 'files',