Loading...
Searching...
No Matches
getdata.test.2.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief testing that selectbox (cell dropdown) values respect Row Level Security
4
5 @details A HARDSELECT / SOFTSELECT rule whose RULE_VALUE is a
6 library.member.column reference has its distinct values read straight from
7 that source table by getdata.sas. If the source table carries Row Level
8 Security rules, the restricted rows must be filtered out of the dropdown too -
9 otherwise values the user is not permitted to see appear in the cell dropdown
10 while the grid itself is correctly filtered.
11
12 This test makes the runner a 'regular' (non admin) user and restricts
13 SOME_BESTNUM in MPE_X_TEST to one value selected from the data (the demo
14 data holds random SOME_BESTNUM values, refreshed by testsetup, so the
15 value cannot be hardcoded). The demo data must hold more than one
16 distinct SOME_BESTNUM - true of the SAS-seeded estate, but NOT of the
17 SASjs server mock, which seeds the seven 44 rows only (see the
18 'distinct_bestnum' precondition assertion below).
19
20 Runners whose SAS identity group is the admin group cannot be demoted by
21 editing MPE_GROUPS alone - Row Level Security is skipped for them - so the
22 test reports a skip row instead of running the assertions.
23
24 The call passes a filter (PRIMARY_KEY_FIELD < 100) so the whole selection is
25 served - the unfiltered MPE_X_TEST is above the row limit (642 rows against
26 DC_MAXOBS_WEBEDIT:250) and would come back truncated. The limits test
27 (getdata.test.1.sas) covers that truncation separately.
28
29 <h4> SAS Macros </h4>
30 @li mf_getuniquefileref.sas
31 @li mf_getuniquename.sas
32 @li mf_getuser.sas
33 @li mpe_getgroups.sas
34 @li mp_assert.sas
35 @li mp_assertcolvals.sas
36 @li mp_assertdsobs.sas
37 @li mp_filterstore.sas
38 @li mx_execute.sas
39
40 @sasjslint {"maxLineLength": 81}
41
42**/
43
44%let _program=&appLoc/services/editors/getdata;
45
46/**
47 * The body is wrapped in a macro so the conditional skip below uses
48 * %if/%then/%do inside a macro definition. Open-code %if needs SAS 9.4M5+,
49 * and the suite also runs on the sas9 target, so the wrapper keeps the test
50 * portable across the supported estates (as testsetup.sas's update_settings
51 * does for its own conditionals).
52 */
53%macro getdata_rls_test();
54
55/**
56 * Row Level Security is skipped for admins, so only a non admin runner can
57 * verify it. The group membership is checked against &dc_admin_group (the
58 * mpeinit-resolved &mpeadmins is not available in a test session, see
59 * testinit.sas) and taken from the SAS identity groups, NOT the MPE_GROUPS
60 * table this test goes on to edit. Admin runners (eg a shared test account
61 * that is a member of the admin group) simply skip the RLS assertions.
62 */
63%mpe_getgroups(user=%mf_getuser(),outds=work.testgroups)
64proc sql noprint;
65select count(*) into: admin_check
66 from work.testgroups
67 where groupname="&dc_admin_group";
68quit;
69%if &admin_check>0 %then %do;
70 %put %str(WARN)ING: %mf_getuser() is in the &dc_admin_group group;
71 %put %str(WARN)ING: Row Level Security is skipped for admins - test skipped;
72 data work.test_results;
73 length test_result $4 test_description $256 test_comments $256;
74 test_result='PASS';
75 test_description="GETDATA_TEST_2 skipped - runner is an admin";
76 test_comments="RLS is skipped for admins so the assertions cannot run";
77 run;
78%end;
79%else %do;
80
81/**
82 * Make the runner a 'regular' user and grant the temp group EDIT access so
83 * the getdata access check still passes.
84 */
85data work.groups_backup;
86 set &dc_libref..MPE_GROUPS;
87 where upcase(user_name)="%upcase(%mf_getuser())";
88run;
89
90%let tempgroup=%mf_getuniquename();
91
92proc sql;
93delete from &dc_libref..MPE_GROUPS
94 where upcase(user_name)="%upcase(%mf_getuser())";
95
96insert into &dc_libref..MPE_GROUPS
97 set user_name="%mf_getuser()"
98 ,group_name="&tempgroup"
99 ,group_desc="temp group"
100 ,tx_from='01Jan1960:00:00:00'dt
101 ,tx_to='31Dec9999:23:59:59'dt;
102
103insert into &dc_libref..MPE_SECURITY
104 set tx_from=0
105 ,libref="*ALL*"
106 ,dsn="*ALL*"
107 ,access_level="EDIT"
108 ,sas_group="&tempgroup"
109 ,tx_to='31DEC9999:23:59:59'dt;
110quit;
111
112/**
113 * Row Level Security on MPE_X_TEST - only one SOME_BESTNUM value (selected
114 * from the data, see header) is visible. The value is taken from the rows
115 * that match the user filter below, so the filtered grid cannot be empty.
116 */
117proc sql noprint;
118delete from &dc_libref..MPE_ROW_LEVEL_SECURITY
119 where RLS_TABLE="MPE_X_TEST" and rls_group="&tempgroup";
120
121select distinct some_bestnum into :rls_value trimmed
122 from &dc_libref..MPE_X_TEST
123 where some_bestnum is not null and primary_key_field < 100;
124select coalesce(max(rls_rk),0) into: max_rk
125 from &dc_libref..MPE_ROW_LEVEL_SECURITY;
126quit;
127
128proc sql;
129insert into &dc_libref..mpe_row_level_security set
130 tx_from=0
131 ,tx_to='31DEC5999:23:59:59'dt
132 ,RLS_RK=&max_rk+1
133 ,RLS_SCOPE='ALL'
134 ,RLS_GROUP="&tempgroup"
135 ,RLS_LIBREF="&dc_libref"
136 ,RLS_TABLE="MPE_X_TEST"
137 ,RLS_GROUP_LOGIC='AND'
138 ,RLS_SUBGROUP_LOGIC='OR'
139 ,RLS_SUBGROUP_ID=0
140 ,RLS_VARIABLE_NM='SOME_BESTNUM'
141 ,RLS_OPERATOR_NM='IN'
142 ,RLS_RAW_VALUE="(&rls_value)"
143 ,RLS_ACTIVE=1;
144quit;
145
146/**
147 * Preconditions - the source column really does hold more than one distinct
148 * value (so the assertions are meaningful) and the user filter matches at
149 * least one of them (so the grid cannot be empty).
150 */
151proc sql noprint;
152select count(distinct some_bestnum) into: distinct_bestnum
153 from &dc_libref..MPE_X_TEST;
154quit;
155%mp_assert(
156 iftrue=(&distinct_bestnum>1),
157 desc=Precondition - MPE_X_TEST.SOME_BESTNUM has more than one distinct value,
158 outds=work.test_results
159)
160%mp_assert(
161 iftrue=(%length(&rls_value)>0),
162 desc=Precondition - the user filter matches at least one SOME_BESTNUM value,
163 outds=work.test_results
164)
165
166/**
167 * Store a filter (PRIMARY_KEY_FIELD < 100) so the selection stays under the
168 * DC_MAXOBS_WEBEDIT row limit.
169 */
170data work.inquery;
171 length GROUP_LOGIC $3 SUBGROUP_LOGIC $3 SUBGROUP_ID 8
172 VARIABLE_NM $32 OPERATOR_NM $10 RAW_VALUE $4000;
173 GROUP_LOGIC='AND';
174 SUBGROUP_LOGIC='AND';
175 SUBGROUP_ID=1;
176 VARIABLE_NM='PRIMARY_KEY_FIELD';
177 OPERATOR_NM='<';
178 RAW_VALUE='100';
179run;
180%mp_filterstore(
181 libds=&dclib..MPE_X_TEST,
182 filter_summary=&dc_libref..mpe_filteranytable,
183 filter_detail=&dc_libref..mpe_filtersource,
184 lock_table=&dc_libref..mpe_lockanytable,
185 maxkeytable=&dc_libref..mpe_maxkeyvalues,
186 queryds=work.inquery,
187 outresult=work.filter_result,
188 outquery=work.filter_query
189)
190data _null_;
191 set work.filter_result;
192 call symputx('f1_rk',filter_rk,'l');
193run;
194
195/**
196 * Call the getdata service - filtered to keep the row count under
197 * DC_MAXOBS_WEBEDIT (see header).
198 */
199%let f1=%mf_getuniquefileref();
200data _null_;
201 file &f1 termstr=crlf;
202 put 'LIBDS:$43. FILTER_RK:8.';
203 put "&dclib..MPE_X_TEST,&f1_rk";
204run;
205%mx_execute(&_program,
206 viyacontext=&defaultcontext,
207 inputfiles=&f1:sascontroltable,
208 outlib=web1,
209 mdebug=&sasjs_mdebug
210)
211
212data work.dqdata;
213 set web1.dqdata;
214run;
215data work.sasdata;
216 set web1.sasdata;
217run;
218
219/* the selectbox source rows only (exclude MPE_SELECTBOX-derived values) */
220data work.dqtest;
221 set work.dqdata;
222 where upcase(base_col)='SOME_BESTNUM';
223run;
224
225data work.checkchar;
226 val="&rls_value"; output;
227run;
228data work.checknum;
229 val=&rls_value; output;
230run;
231
232/**
233 * Test 1 - control: the grid itself is filtered by RLS. If this fails the
234 * runner is an admin and the dropdown assertion below cannot be trusted.
235 */
236%mp_assertcolvals(work.sasdata.SOME_BESTNUM,
237 checkvals=work.checknum.val,
238 desc=Test 1 - grid rows respect Row Level Security (control),
239 test=ALLVALS,
240 outds=work.test_results
241)
242
243/**
244 * Test 2 - the selectbox values sourced from MPE_X_TEST.SOME_BESTNUM must
245 * also be restricted to the RLS-visible value (44).
246 */
247%mp_assertdsobs(work.dqtest,
248 desc=Test 2 precondition - selectbox values were returned for SOME_BESTNUM,
249 test=HASOBS,
250 outds=work.test_results
251)
252%mp_assertcolvals(work.dqtest.RULE_DATA,
253 checkvals=work.checkchar.val,
254 desc=Test 2 - selectbox values respect Row Level Security,
255 test=ALLVALS,
256 outds=work.test_results
257)
258
259/**
260 * Cleanup - restore the group / security / RLS rows
261 */
262proc sql;
263delete from &dc_libref..MPE_ROW_LEVEL_SECURITY where rls_group="&tempgroup";
264delete from &dc_libref..MPE_SECURITY where sas_group="&tempgroup";
265delete from &dc_libref..MPE_GROUPS where group_name="&tempgroup";
266quit;
267proc append base=&dc_libref..MPE_GROUPS data=work.groups_backup;
268run;
269%end;
270
271%mend getdata_rls_test;
272
273%getdata_rls_test()