1 |
(* |
2 |
* IBX Test suite. This program is used to test the IBX non-visual |
3 |
* components and provides a semi-automated pass/fail check for each test. |
4 |
* |
5 |
* The contents of this file are subject to the Initial Developer's |
6 |
* Public License Version 1.0 (the "License"); you may not use this |
7 |
* file except in compliance with the License. You may obtain a copy |
8 |
* of the License here: |
9 |
* |
10 |
* http://www.firebirdsql.org/index.php?op=doc&id=idpl |
11 |
* |
12 |
* Software distributed under the License is distributed on an "AS |
13 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
14 |
* implied. See the License for the specific language governing rights |
15 |
* and limitations under the License. |
16 |
* |
17 |
* The Initial Developer of the Original Code is Tony Whyman. |
18 |
* |
19 |
* The Original Code is (C) 2021 Tony Whyman, MWA Software |
20 |
* (http://www.mwasoftware.co.uk). |
21 |
* |
22 |
* All Rights Reserved. |
23 |
* |
24 |
* Contributor(s): ______________________________________. |
25 |
* |
26 |
*) |
27 |
unit Test22; |
28 |
|
29 |
{$mode objfpc}{$H+} |
30 |
|
31 |
{Test 22: TIBUpdate Tests} |
32 |
|
33 |
{ This test uses TIBUpdate to allow a list of database users to be presented |
34 |
as a table and edited using normal insert/edit/delete/post methods. |
35 |
} |
36 |
|
37 |
interface |
38 |
|
39 |
uses |
40 |
Classes, SysUtils, TestApplication, IBXTestBase, DB, IB, IBSQL, IBUpdate, |
41 |
IBQuery, IBCustomDataset; |
42 |
|
43 |
const |
44 |
aTestID = '22'; |
45 |
aTestTitle = 'TIBUpdate Tests'; |
46 |
|
47 |
type |
48 |
|
49 |
{ TTest22 } |
50 |
|
51 |
TTest22 = class(TIBXTestBase) |
52 |
private |
53 |
FIBUpdate: TIBUpdate; |
54 |
ExecDDL: TIBSQL; |
55 |
procedure UserListAfterInsert(DataSet: TDataSet); |
56 |
procedure UpdateUsersApplyUpdates(Sender: TObject; UpdateKind: TUpdateKind; |
57 |
Params: ISQLParams); |
58 |
protected |
59 |
procedure CreateObjects(Application: TTestApplication); override; |
60 |
function GetTestID: AnsiString; override; |
61 |
function GetTestTitle: AnsiString; override; |
62 |
procedure InitTest; override; |
63 |
function SkipTest: boolean; override; |
64 |
public |
65 |
procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override; |
66 |
end; |
67 |
|
68 |
|
69 |
implementation |
70 |
|
71 |
uses IBUtils; |
72 |
|
73 |
const |
74 |
UsersQuery = |
75 |
'Select A.SEC$DESCRIPTION, Trim(A.SEC$PLUGIN) as SEC$PLUGIN, A.SEC$ADMIN, '+ |
76 |
'A.SEC$ACTIVE, Trim(A.SEC$USER_NAME) as SEC$USER_NAME, '+ |
77 |
'Trim(A.SEC$FIRST_NAME) as SEC$FIRST_NAME, '+ |
78 |
'Trim(A.SEC$MIDDLE_NAME) as SEC$MIDDLE_NAME, '+ |
79 |
'Trim(A.SEC$LAST_NAME) as SEC$LAST_NAME, '+ |
80 |
'cast(NULL as VarChar(32)) as SEC$PASSWORD, '+ |
81 |
'case when Count(B.MON$ATTACHMENT_ID) > 0 then true else false end as LoggedIn, '+ |
82 |
'case When C.SEC$USER is not null then true else false end as DBCreator '+ |
83 |
'From SEC$USERS A '+ |
84 |
'Left Outer Join MON$ATTACHMENTS B '+ |
85 |
'On A.SEC$USER_NAME = B.MON$USER '+ |
86 |
'Left Outer Join SEC$DB_CREATORS C on C.SEC$USER = A.SEC$USER_NAME'; |
87 |
UsersQueryGroupBy = |
88 |
'Group By A.SEC$DESCRIPTION, A.SEC$PLUGIN, A.SEC$ADMIN, '+ |
89 |
'A.SEC$ACTIVE, A.SEC$USER_NAME, A.SEC$MIDDLE_NAME, '+ |
90 |
'A.SEC$FIRST_NAME, A.SEC$LAST_NAME, C.SEC$USER'; |
91 |
|
92 |
{ TTest22 } |
93 |
|
94 |
procedure TTest22.UserListAfterInsert(DataSet: TDataSet); |
95 |
begin |
96 |
DataSet.FieldByName('SEC$ADMIN').AsBoolean := false; |
97 |
DataSet.FieldByName('SEC$ACTIVE').AsBoolean := false; |
98 |
DataSet.FieldByName('DBCreator').AsBoolean := false; |
99 |
DataSet.FieldByName('SEC$PLUGIN').AsString := 'Srp'; |
100 |
DataSet.FieldByName('SEC$PASSWORD').Clear; |
101 |
end; |
102 |
|
103 |
procedure TTest22.UpdateUsersApplyUpdates(Sender: TObject; |
104 |
UpdateKind: TUpdateKind; Params: ISQLParams); |
105 |
|
106 |
var UserName: string; |
107 |
|
108 |
function FormatStmtOptions: string; |
109 |
var Param: ISQLParam; |
110 |
begin |
111 |
Result := UserName; |
112 |
Param := Params.ByName('SEC$PASSWORD'); |
113 |
if (Param <> nil) and not Param.IsNull then |
114 |
Result += ' PASSWORD ''' + SQLSafeString(Param.AsString) + ''''; |
115 |
Param := Params.ByName('SEC$FIRST_NAME'); |
116 |
if Param <> nil then |
117 |
Result += ' FIRSTNAME ''' + SQLSafeString(Param.AsString) + ''''; |
118 |
Param := Params.ByName('SEC$MIDDLE_NAME'); |
119 |
if Param <> nil then |
120 |
Result += ' MIDDLENAME ''' + SQLSafeString(Param.AsString) + ''''; |
121 |
Param := Params.ByName('SEC$LAST_NAME'); |
122 |
if Param <> nil then |
123 |
Result += ' LASTNAME ''' + SQLSafeString(Param.AsString) + ''''; |
124 |
Param := Params.ByName('SEC$ACTIVE'); |
125 |
if Param <> nil then |
126 |
begin |
127 |
if Param.AsBoolean then |
128 |
Result += ' ACTIVE' |
129 |
else |
130 |
Result += ' INACTIVE'; |
131 |
end; |
132 |
Param := Params.ByName('SEC$PLUGIN'); |
133 |
if Param <> nil then |
134 |
Result += ' USING PLUGIN ' + QuoteIdentifierIfNeeded((Sender as TIBUpdate).DataSet.Database.SQLDialect,Param.AsString); |
135 |
end; |
136 |
|
137 |
function GetAlterPasswordStmt: string; |
138 |
var Param: ISQLParam; |
139 |
begin |
140 |
Result := ''; |
141 |
Param := Params.ByName('SEC$PASSWORD'); |
142 |
if (UpdateKind = ukModify) and not Param.IsNull then |
143 |
begin |
144 |
Result := 'ALTER USER ' + UserName + |
145 |
' PASSWORD ''' + SQLSafeString(Param.AsString) + ''''; |
146 |
Param := Params.ByName('SEC$PLUGIN'); |
147 |
if Param <> nil then |
148 |
Result += ' USING PLUGIN ' + QuoteIdentifierIfNeeded((Sender as TIBUpdate).DataSet.Database.SQLDialect,Param.AsString); |
149 |
end; |
150 |
end; |
151 |
|
152 |
begin |
153 |
UserName := Trim(Params.ByName('SEC$USER_NAME').AsString); |
154 |
{non SYSDBA user not an RDB$ADMIN can only change their password} |
155 |
if (Owner.GetUserName <> 'SYSDBA') and (RoleName <> 'RDB$ADMIN') then |
156 |
begin |
157 |
ExecDDL.SQL.Text := GetAlterPasswordStmt; |
158 |
if ExecDDL.SQL.Text <> '' then |
159 |
ExecDDL.ExecQuery; |
160 |
Exit; |
161 |
end; |
162 |
|
163 |
case UpdateKind of |
164 |
ukInsert: |
165 |
ExecDDL.SQL.Text := 'CREATE USER ' + FormatStmtOptions; |
166 |
ukModify: |
167 |
ExecDDL.SQL.Text := 'ALTER USER ' + FormatStmtOptions; |
168 |
ukDelete: |
169 |
ExecDDL.SQL.Text := 'DROP USER ' + UserName; |
170 |
end; |
171 |
ExecDDL.ExecQuery; |
172 |
|
173 |
if UpdateKind = ukInsert then |
174 |
begin |
175 |
{if new user is also given the admin role then we need to add this} |
176 |
if Params.ByName('SEC$ADMIN').AsBoolean then |
177 |
begin |
178 |
ExecDDL.SQL.Text := 'ALTER USER ' + UserName + ' GRANT ADMIN ROLE'; |
179 |
ExecDDL.ExecQuery; |
180 |
end; |
181 |
if Params.ByName('DBCreator').AsBoolean then |
182 |
begin |
183 |
ExecDDL.SQL.Text := 'GRANT CREATE DATABASE TO USER ' + UserName; |
184 |
ExecDDL.ExecQuery; |
185 |
end |
186 |
else |
187 |
begin |
188 |
ExecDDL.SQL.Text := 'REVOKE CREATE DATABASE FROM USER ' + UserName; |
189 |
ExecDDL.ExecQuery; |
190 |
end; |
191 |
end |
192 |
else |
193 |
if UpdateKind = ukModify then |
194 |
{Update Admin Role if allowed} |
195 |
begin |
196 |
if Params.ByName('SEC$ADMIN').AsBoolean and not Params.ByName('OLD_SEC$ADMIN').AsBoolean then |
197 |
begin |
198 |
ExecDDL.SQL.Text := 'ALTER USER ' + UserName + ' GRANT ADMIN ROLE'; |
199 |
ExecDDL.ExecQuery; |
200 |
end |
201 |
else |
202 |
if not Params.ByName('SEC$ADMIN').AsBoolean and Params.ByName('OLD_SEC$ADMIN').AsBoolean then |
203 |
begin |
204 |
ExecDDL.SQL.Text := 'ALTER USER ' + UserName + ' REVOKE ADMIN ROLE'; |
205 |
ExecDDL.ExecQuery; |
206 |
end; |
207 |
|
208 |
{Update DB Creator Role} |
209 |
if Params.ByName('DBCreator').AsBoolean and not Params.ByName('OLD_DBCreator').AsBoolean then |
210 |
begin |
211 |
ExecDDL.SQL.Text := 'GRANT CREATE DATABASE TO USER ' + UserName; |
212 |
ExecDDL.ExecQuery; |
213 |
end |
214 |
else |
215 |
if not Params.ByName('DBCreator').AsBoolean and Params.ByName('OLD_DBCreator').AsBoolean then |
216 |
begin |
217 |
ExecDDL.SQL.Text := 'REVOKE CREATE DATABASE FROM USER ' + UserName; |
218 |
ExecDDL.ExecQuery; |
219 |
end; |
220 |
end; |
221 |
end; |
222 |
|
223 |
procedure TTest22.CreateObjects(Application: TTestApplication); |
224 |
begin |
225 |
inherited CreateObjects(Application); |
226 |
FIBUpdate := TIBUpdate.Create(Application); |
227 |
FIBUpdate.RefreshSQL.Text := UsersQuery + ' Where A.SEC$USER_NAME = :SEC$USER_NAME ' + UsersQueryGroupBy; |
228 |
FIBUpdate.OnApplyUpdates := @UpdateUsersApplyUpdates; |
229 |
IBQuery.SQL.Text := UsersQuery + ' ' + UsersQueryGroupBy; |
230 |
IBQuery.AfterInsert:= @UserListAfterInsert; |
231 |
IBQuery.UpdateObject := FIBUpdate; |
232 |
IBQuery.AutoCommit := acCommitRetaining; |
233 |
ExecDDL := TIBSQL.Create(Application); |
234 |
ExecDDL.Database := IBDatabase; |
235 |
ExecDDL.Transaction := IBTransaction; |
236 |
end; |
237 |
|
238 |
function TTest22.GetTestID: AnsiString; |
239 |
begin |
240 |
Result := aTestID; |
241 |
end; |
242 |
|
243 |
function TTest22.GetTestTitle: AnsiString; |
244 |
begin |
245 |
Result := aTestTitle; |
246 |
end; |
247 |
|
248 |
procedure TTest22.InitTest; |
249 |
begin |
250 |
inherited InitTest; |
251 |
IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName; |
252 |
ReadWriteTransaction; |
253 |
end; |
254 |
|
255 |
function TTest22.SkipTest: boolean; |
256 |
begin |
257 |
Result := FirebirdAPI.GetClientMajor < 3; |
258 |
if Result then |
259 |
writeln(OutFile,'Skipping ',TestTitle); |
260 |
end; |
261 |
|
262 |
procedure TTest22.RunTest(CharSet: AnsiString; SQLDialect: integer); |
263 |
begin |
264 |
IBDatabase.Connected := true; |
265 |
IBTransaction.Active := true; |
266 |
try |
267 |
writeln(Outfile,'RoleName = ',RoleName); |
268 |
IBQuery.Active := true; |
269 |
writeln(Outfile,'User List'); |
270 |
PrintDataSet(IBQuery); |
271 |
writeln(Outfile,'Add a user'); |
272 |
with IBQuery do |
273 |
begin |
274 |
Append; |
275 |
FieldByName('SEC$USER_NAME').AsString := 'TESTER'; |
276 |
FieldByName('SEC$FIRST_NAME').AsString := 'Chief'; |
277 |
FieldByName('SEC$LAST_NAME').AsString := 'Tester'; |
278 |
FieldByName('SEC$PASSWORD').AsString := 'LetMeIn'; |
279 |
Post; |
280 |
IBTransaction.Commit; |
281 |
IBTransaction.Active := true; |
282 |
Active := true; |
283 |
end; |
284 |
writeln(Outfile,'Updated User List'); |
285 |
PrintDataSet(IBQuery); |
286 |
writeln(Outfile,'Modify a User'); |
287 |
with IBQuery do |
288 |
if Locate('SEC$USER_NAME','TESTER',[]) then |
289 |
begin |
290 |
Edit; |
291 |
FieldByName('SEC$MIDDLE_NAME').AsString := 'Database'; |
292 |
FieldByName('DBCreator').AsBoolean := true; |
293 |
Post; |
294 |
IBTransaction.Commit; |
295 |
IBTransaction.Active := true; |
296 |
Active := true; |
297 |
end |
298 |
else |
299 |
writeln(Outfile,'Error: unable to located new user'); |
300 |
writeln(Outfile,'Updated User List'); |
301 |
PrintDataSet(IBQuery); |
302 |
writeln(Outfile,'Delete a user'); |
303 |
with IBQuery do |
304 |
if Locate('SEC$USER_NAME','TESTER',[]) then |
305 |
Delete; |
306 |
IBTransaction.Commit; |
307 |
IBTransaction.Active := true; |
308 |
IBQuery.Active := true; |
309 |
writeln(Outfile,'Updated User List'); |
310 |
PrintDataSet(IBQuery); |
311 |
finally |
312 |
IBDatabase.ReConnect; |
313 |
IBTransaction.Active := true; |
314 |
with IBQuery do |
315 |
begin {make sure user is removed} |
316 |
Active := true; |
317 |
if Locate('SEC$USER_NAME','TESTER',[]) then |
318 |
Delete; |
319 |
IBTransaction.Commit; |
320 |
end; |
321 |
IBDatabase.Connected := false; |
322 |
end; |
323 |
end; |
324 |
|
325 |
initialization |
326 |
RegisterTest(TTest22); |
327 |
|
328 |
end. |
329 |
|