ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/testsuite/Test22.pas
Revision: 323
Committed: Thu Feb 25 12:14:35 2021 UTC (3 years, 6 months ago) by tony
Content type: text/x-pascal
File size: 9838 byte(s)
Log Message:
Fixed Merged

File Contents

# User Rev Content
1 tony 323 (*
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 tony 315 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     end
182     else
183     if UpdateKind = ukModify then
184     {Update Admin Role if allowed}
185     begin
186     if Params.ByName('SEC$ADMIN').AsBoolean and not Params.ByName('OLD_SEC$ADMIN').AsBoolean then
187     begin
188     ExecDDL.SQL.Text := 'ALTER USER ' + UserName + ' GRANT ADMIN ROLE';
189     ExecDDL.ExecQuery;
190     end
191     else
192     if not Params.ByName('SEC$ADMIN').AsBoolean and Params.ByName('OLD_SEC$ADMIN').AsBoolean then
193     begin
194     ExecDDL.SQL.Text := 'ALTER USER ' + UserName + ' REVOKE ADMIN ROLE';
195     ExecDDL.ExecQuery;
196     end
197     end;
198    
199     {Update DB Creator Role}
200     if Params.ByName('DBCreator').AsBoolean and not Params.ByName('OLD_DBCreator').AsBoolean then
201     begin
202     ExecDDL.SQL.Text := 'GRANT CREATE DATABASE TO USER ' + UserName;
203     ExecDDL.ExecQuery;
204     end
205     else
206     if not Params.ByName('DBCreator').AsBoolean and Params.ByName('OLD_DBCreator').AsBoolean then
207     begin
208     ExecDDL.SQL.Text := 'REVOKE CREATE DATABASE FROM USER ' + UserName;
209     ExecDDL.ExecQuery;
210     end
211     end;
212    
213     procedure TTest22.CreateObjects(Application: TTestApplication);
214     begin
215     inherited CreateObjects(Application);
216     FIBUpdate := TIBUpdate.Create(Application);
217     FIBUpdate.RefreshSQL.Text := UsersQuery + ' Where A.SEC$USER_NAME = :SEC$USER_NAME ' + UsersQueryGroupBy;
218     FIBUpdate.OnApplyUpdates := @UpdateUsersApplyUpdates;
219     IBQuery.SQL.Text := UsersQuery + ' ' + UsersQueryGroupBy;
220     IBQuery.AfterInsert:= @UserListAfterInsert;
221     IBQuery.UpdateObject := FIBUpdate;
222     IBQuery.AutoCommit := acCommitRetaining;
223     ExecDDL := TIBSQL.Create(Application);
224     ExecDDL.Database := IBDatabase;
225     ExecDDL.Transaction := IBTransaction;
226     end;
227    
228     function TTest22.GetTestID: AnsiString;
229     begin
230     Result := aTestID;
231     end;
232    
233     function TTest22.GetTestTitle: AnsiString;
234     begin
235     Result := aTestTitle;
236     end;
237    
238     procedure TTest22.InitTest;
239     begin
240     inherited InitTest;
241     IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName;
242     ReadWriteTransaction;
243     end;
244    
245     function TTest22.SkipTest: boolean;
246     begin
247     Result := FirebirdAPI.GetClientMajor < 3;
248     if Result then
249     writeln(OutFile,'Skipping ',TestTitle);
250     end;
251    
252     procedure TTest22.RunTest(CharSet: AnsiString; SQLDialect: integer);
253     begin
254     IBDatabase.Connected := true;
255     IBTransaction.Active := true;
256     try
257     writeln(Outfile,'RoleName = ',RoleName);
258     IBQuery.Active := true;
259     writeln(Outfile,'User List');
260     PrintDataSet(IBQuery);
261     writeln(Outfile,'Add a user');
262     with IBQuery do
263     begin
264     Append;
265     FieldByName('SEC$USER_NAME').AsString := 'TESTER';
266     FieldByName('SEC$FIRST_NAME').AsString := 'Chief';
267     FieldByName('SEC$LAST_NAME').AsString := 'Tester';
268     FieldByName('SEC$PASSWORD').AsString := 'LetMeIn';
269     Post;
270     IBTransaction.Commit;
271     IBTransaction.Active := true;
272     Active := true;
273     end;
274     writeln(Outfile,'Updated User List');
275     PrintDataSet(IBQuery);
276     writeln(Outfile,'Modify a User');
277     with IBQuery do
278     if Locate('SEC$USER_NAME','TESTER',[]) then
279     begin
280     Edit;
281     FieldByName('SEC$MIDDLE_NAME').AsString := 'Database';
282     FieldByName('DBCreator').AsBoolean := true;
283     Post;
284     IBTransaction.Commit;
285     IBTransaction.Active := true;
286     Active := true;
287     end
288     else
289     writeln(Outfile,'Error: unable to located new user');
290     writeln(Outfile,'Updated User List');
291     PrintDataSet(IBQuery);
292     writeln(Outfile,'Delete a user');
293     with IBQuery do
294     if Locate('SEC$USER_NAME','TESTER',[]) then
295     Delete;
296     IBTransaction.Commit;
297     IBTransaction.Active := true;
298     IBQuery.Active := true;
299     writeln(Outfile,'Updated User List');
300     PrintDataSet(IBQuery);
301     finally
302     IBDatabase.ReConnect;
303     IBTransaction.Active := true;
304     with IBQuery do
305     begin {make sure user is removed}
306     Active := true;
307     if Locate('SEC$USER_NAME','TESTER',[]) then
308     Delete;
309     IBTransaction.Commit;
310     end;
311     IBDatabase.Connected := false;
312     end;
313     end;
314    
315     initialization
316     RegisterTest(TTest22);
317    
318     end.
319