Repository navigation
Expand file tree
/
Copy pathQuick.Core.Security.Authentication.Tests.pas
More file actions
216 lines (197 loc) · 5.54 KB
/
Copy pathQuick.Core.Security.Authentication.Tests.pas
File metadata and controls
216 lines (197 loc) · 5.54 KB
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
unit Quick.Core.Security.Authentication.Tests;
{ Tests: T-AUTH-01..10
T-AUTH-01 TAuthenticationProperties.SetString / GetString round-trip
T-AUTH-02 TAuthenticationProperties.SetBool / GetBool round-trip
T-AUTH-03 TAuthenticationProperties.IsPersistent default = False
T-AUTH-04 TAuthenticationProperties.IsPersistent := True persists
T-AUTH-05 TAuthenticationProperties.AllowRefresh default = False
T-AUTH-06 TAuthenticationProperties.AllowRefresh := True persists
T-AUTH-07 TAuthenticationProperties.Items dictionary is non-nil
T-AUTH-08 TAuthenticationTicket carries AuthenticationScheme
T-AUTH-09 TAuthenticationTicket Principal matches supplied principal
T-AUTH-10 TAuthenticationTicket (2-arg) Properties is non-nil
}
interface
uses
DUnitX.TestFramework,
System.SysUtils,
Quick.Core.Security.Claims,
Quick.Core.Security.Authentication;
type
[TestFixture]
TAuthenticationTests = class
public
[Test]
procedure T_AUTH_01_SetString_GetString_RoundTrip;
[Test]
procedure T_AUTH_02_SetBool_GetBool_RoundTrip;
[Test]
procedure T_AUTH_03_IsPersistent_DefaultFalse;
[Test]
procedure T_AUTH_04_IsPersistent_SetTrue;
[Test]
procedure T_AUTH_05_AllowRefresh_DefaultFalse;
[Test]
procedure T_AUTH_06_AllowRefresh_SetTrue;
[Test]
procedure T_AUTH_07_Properties_Items_NotNil;
[Test]
procedure T_AUTH_08_Ticket_CarriesScheme;
[Test]
procedure T_AUTH_09_Ticket_PrincipalMatchesInput;
[Test]
procedure T_AUTH_10_Ticket_TwoArg_PropertiesNotNil;
end;
implementation
{ TAuthenticationTests }
procedure TAuthenticationTests.T_AUTH_01_SetString_GetString_RoundTrip;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
props.SetString('key1', 'hello');
Assert.AreEqual('hello', props.GetString('key1'));
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_02_SetBool_GetBool_RoundTrip;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
props.SetBool('flagA', True);
Assert.IsTrue(props.GetBool('flagA'));
props.SetBool('flagA', False);
Assert.IsFalse(props.GetBool('flagA'));
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_03_IsPersistent_DefaultFalse;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
Assert.IsFalse(props.IsPersistent, 'IsPersistent should default to False');
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_04_IsPersistent_SetTrue;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
props.IsPersistent := True;
Assert.IsTrue(props.IsPersistent);
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_05_AllowRefresh_DefaultFalse;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
Assert.IsFalse(props.AllowRefresh, 'AllowRefresh should default to False');
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_06_AllowRefresh_SetTrue;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
props.AllowRefresh := True;
Assert.IsTrue(props.AllowRefresh);
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_07_Properties_Items_NotNil;
var
props : TAuthenticationProperties;
begin
props := TAuthenticationProperties.Create;
try
Assert.IsNotNull(props.Items, 'Items dictionary should not be nil');
finally
props.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_08_Ticket_CarriesScheme;
var
principal : TClaimsPrincipal;
identity : TClaimsIdentity;
ticket : TAuthenticationTicket;
begin
identity := TClaimsIdentity.Create;
identity.AuthenticationType := 'Bearer';
principal := TClaimsPrincipal.Create(identity);
try
ticket := TAuthenticationTicket.Create(principal, 'Bearer');
try
Assert.AreEqual('Bearer', ticket.AuthenticationScheme);
finally
ticket.Free;
end;
finally
principal.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_09_Ticket_PrincipalMatchesInput;
var
principal : TClaimsPrincipal;
identity : TClaimsIdentity;
ticket : TAuthenticationTicket;
begin
identity := TClaimsIdentity.Create;
identity.AuthenticationType := 'Cookie';
principal := TClaimsPrincipal.Create(identity);
try
ticket := TAuthenticationTicket.Create(principal, 'Cookie');
try
Assert.AreSame(principal, ticket.Principal,
'Ticket.Principal should be the same object passed to the constructor');
finally
ticket.Free;
end;
finally
principal.Free;
end;
end;
procedure TAuthenticationTests.T_AUTH_10_Ticket_TwoArg_PropertiesNotNil;
var
principal : TClaimsPrincipal;
identity : TClaimsIdentity;
properties : TAuthenticationProperties;
ticket : TAuthenticationTicket;
begin
identity := TClaimsIdentity.Create;
identity.AuthenticationType := 'Jwt';
principal := TClaimsPrincipal.Create(identity);
properties := TAuthenticationProperties.Create;
try
ticket := TAuthenticationTicket.Create(principal, properties, 'Jwt');
try
Assert.IsNotNull(ticket.Properties,
'Ticket.Properties should not be nil when supplied in constructor');
finally
ticket.Free;
end;
finally
principal.Free;
// properties is now owned by ticket (via destructor) — do not free here
end;
end;
initialization
TDUnitX.RegisterTestFixture(TAuthenticationTests);
end.