Loading...
Searching...
No Matches
mpe_filtermaster.test.2.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief testing row level security with a truncated comparison
4
5 @details A row level security rule may use the DATA step truncated
6 comparison operators - `=:` ("starts with") and `^=:` ("does not start
7 with"). mpe_filtermaster writes the operator into the clause verbatim and
8 the clause is applied by the DATA step that reads the target table, so the
9 rule must select exactly the rows whose value begins with the supplied
10 prefix.
11
12 Both operators are exercised: the generated clause must carry the operator
13 as it was configured, and applying it in a DATA step must keep only the rows
14 that match.
15
16 <h4> SAS Macros </h4>
17 @li mf_getuniquename.sas
18 @li mf_getuser.sas
19 @li mf_nobs.sas
20 @li mp_assert.sas
21 @li mp_assertdsobs.sas
22 @li mpe_filtermaster.sas
23
24 @author 4GL Apps Ltd
25 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
26 and may not be re-distributed or re-sold without the express permission of
27 4GL Apps Ltd.
28
29 @sasjslint {"maxLineLength": 81}
30
31**/
32
33/**
34 * During tests a DC group is made admin. Replacing this account's MPE_GROUPS
35 * row with a SAS system group this account is a member of makes it a
36 * "regular" member, so the row level security rules apply to it.
37 */
38data work.groups_backup;
39 set &dc_libref..MPE_GROUPS;
40 where upcase(user_name)="%upcase(%mf_getuser())";
41run;
42
43proc sql;
44delete from &dc_libref..MPE_GROUPS
45 where upcase(user_name)="%upcase(%mf_getuser())";
46%let tempgroup=%mf_getuniquename();
47insert into &dc_libref..MPE_GROUPS
48 set user_name="%mf_getuser()"
49 ,group_name="&tempgroup"
50 ,group_desc="temp group"
51 ,tx_from='01Jan1960:00:00:00'dt
52 ,tx_to='31Dec9999:23:59:59'dt;
53delete from &dc_libref..MPE_ROW_LEVEL_SECURITY
54 where RLS_TABLE="MPE_X_TEST";
55select max(rls_rk) into: max_rk from &dc_libref..MPE_ROW_LEVEL_SECURITY;
56quit;
57
58%let prefix=Option 1;
59
60/* =: - starts with */
61proc sql;
62insert into &dc_libref..mpe_row_level_security set
63 tx_from=0
64 ,tx_to='31DEC5999:23:59:59'dt
65 ,RLS_RK=&max_rk+1
66 ,RLS_SCOPE='ALL'
67 ,RLS_GROUP="&tempgroup"
68 ,RLS_LIBREF="&dc_libref"
69 ,RLS_TABLE="MPE_X_TEST"
70 ,RLS_GROUP_LOGIC='AND'
71 ,RLS_SUBGROUP_LOGIC='OR'
72 ,RLS_SUBGROUP_ID=0
73 ,RLS_VARIABLE_NM='SOME_DROPDOWN'
74 ,RLS_OPERATOR_NM='=:'
75 ,RLS_RAW_VALUE="'&prefix'"
76 ,RLS_ACTIVE=1;
77quit;
78
79%mpe_filtermaster(VIEW,&dc_libref..MPE_X_TEST,
80 dclib=&dc_libref,
81 outref=qref
82)
83
84/* the operator reaches the clause unaltered */
85%let clause_ok=0;
86data _null_;
87 infile qref end=eof;
88 input;
89 if index(_infile_,'=:')>0 then call symputx('clause_ok',1);
90run;
91%mp_assert(iftrue=(&clause_ok=1),
92 desc=Test 1 - the generated clause carries the =: operator,
93 outds=work.test_results
94)
95
96data work.test;
97 set &dc_libref..MPE_X_TEST;
98 where %inc qref;;
99run;
100
101data work.mismatch;
102 set work.test;
103 if substr(strip(some_dropdown),1,%length(&prefix)) ne "&prefix";
104run;
105
106%mp_assertdsobs(work.test,
107 desc=Test 2 precondition - the =: rule selects rows,
108 test=HASOBS,
109 outds=work.test_results
110)
111%mp_assertdsobs(work.mismatch,
112 desc=Test 2 - =: keeps only values starting with the prefix,
113 test=EMPTY,
114 outds=work.test_results
115)
116
117/* ^=: - does not start with */
118proc sql;
119delete from &dc_libref..MPE_ROW_LEVEL_SECURITY
120 where RLS_TABLE="MPE_X_TEST";
121select max(rls_rk) into: max_rk from &dc_libref..MPE_ROW_LEVEL_SECURITY;
122insert into &dc_libref..mpe_row_level_security set
123 tx_from=0
124 ,tx_to='31DEC5999:23:59:59'dt
125 ,RLS_RK=&max_rk+1
126 ,RLS_SCOPE='ALL'
127 ,RLS_GROUP="&tempgroup"
128 ,RLS_LIBREF="&dc_libref"
129 ,RLS_TABLE="MPE_X_TEST"
130 ,RLS_GROUP_LOGIC='AND'
131 ,RLS_SUBGROUP_LOGIC='OR'
132 ,RLS_SUBGROUP_ID=0
133 ,RLS_VARIABLE_NM='SOME_DROPDOWN'
134 ,RLS_OPERATOR_NM='^=:'
135 ,RLS_RAW_VALUE="'&prefix'"
136 ,RLS_ACTIVE=1;
137quit;
138
139%mpe_filtermaster(VIEW,&dc_libref..MPE_X_TEST,
140 dclib=&dc_libref,
141 outref=qref
142)
143
144%let clause_ok=0;
145data _null_;
146 infile qref end=eof;
147 input;
148 if index(_infile_,'^=:')>0 then call symputx('clause_ok',1);
149run;
150%mp_assert(iftrue=(&clause_ok=1),
151 desc=Test 3 - the generated clause carries the ^=: operator,
152 outds=work.test_results
153)
154
155data work.test;
156 set &dc_libref..MPE_X_TEST;
157 where %inc qref;;
158run;
159
160data work.mismatch;
161 set work.test;
162 if substr(strip(some_dropdown),1,%length(&prefix)) eq "&prefix";
163run;
164
165%mp_assertdsobs(work.test,
166 desc=Test 4 precondition - the ^=: rule selects rows,
167 test=HASOBS,
168 outds=work.test_results
169)
170%mp_assertdsobs(work.mismatch,
171 desc=Test 4 - ^=: keeps only values not starting with the prefix,
172 test=EMPTY,
173 outds=work.test_results
174)
175
176/**
177 * Cleanup - restore the group and drop the rules
178 */
179proc sql;
180delete from &dc_libref..MPE_ROW_LEVEL_SECURITY
181 where RLS_TABLE="MPE_X_TEST";
182delete from &dc_libref..MPE_GROUPS
183 where group_name="&tempgroup";
184quit;
185
186%macro restore_groups();
187 %if %mf_nobs(work.groups_backup)>0 %then %do;
188 proc append base=&dc_libref..MPE_GROUPS data=work.groups_backup;
189 run;
190 %end;
191%mend restore_groups;
192%restore_groups()