-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_auth_test.dart
154 lines (125 loc) · 4.88 KB
/
client_auth_test.dart
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
import 'package:test/test.dart';
import '../lib/client.dart';
import 'utils.dart';
import 'dtos/test.dtos.dart';
CreateJwt createJwt([Map? options]) {
var to = CreateJwt.fromJson(
options as Map<String, dynamic>? ?? Map<String, dynamic>());
if (to.userAuthId == null) to.userAuthId = "1";
if (to.displayName == null) to.displayName = "test jwt";
if (to.email == null) to.email = "test@auth.com";
return to;
}
Future<void> clearSession(JsonServiceClient client) async =>
await client.post(Authenticate(provider: "logout"));
void main() {
test('Can auth with JWT', () async {
var client = createTestClient();
var response = await client.post(createJwt());
client.bearerToken = response.token!;
var testAuth = await client.get(TestAuth());
expect(testAuth.userId, equals("1"));
expect(testAuth.displayName, equals("test jwt"));
expect(testAuth.sessionId, isNotNull);
});
test('Does fire onAuthenticationRequired callback on 401', () async {
var client = createTestClient();
var count = 0;
client.onAuthenticationRequired = () async {
count++;
};
try {
await client.get(TestAuth());
fail("should throw");
} on WebServiceException catch (e) {
var status = e.responseStatus!;
expect(status.errorCode, equals("401"));
expect(status.message, equals("Unauthorized"));
expect(count, equals(1));
}
});
test('Can use onAuthenticationRequired to auth client', () async {
var client = createTestClient();
var count = 0;
client.onAuthenticationRequired = () async {
count++;
client.userName = "test";
client.password = "test";
};
await client.get(TestAuth());
expect(count, equals(1));
});
test('Can reauthenticate after an auto refresh access token', () async {
var client = createTestClient();
var auth = Authenticate(
provider: "credentials", userName: "test", password: "test");
var authResponse = await client.post(auth);
var refreshToken = authResponse.refreshToken;
var createExpiredJwt = createJwt();
createExpiredJwt.jwtExpiry = DateTime(2000, 1, 1);
var expiredJwt = await client.post(createExpiredJwt);
var bearerToken = expiredJwt.token!;
await clearSession(client);
client = createTestClient();
client.bearerToken = bearerToken;
client.refreshToken = refreshToken;
auth.password = "notvalid";
try {
await client.post(auth);
fail("should throw");
} on WebServiceException catch (e) {
var status = e.responseStatus!;
expect(status.errorCode, equals("Unauthorized"));
expect(status.message, equals("Invalid Username or Password"));
}
});
test('Does fetch AccessToken using RefreshTokenCookies', () async {
var client = createTestClient();
await client.post(Authenticate(
provider: "credentials", userName: "test", password: "test"));
var initialAccessToken = client.getTokenCookie();
var initialRefreshToken = client.getRefreshTokenCookie();
expect(initialAccessToken, isNotNull);
expect(initialRefreshToken, isNotNull);
var request = Secured()..name = "test";
var response = await client.send(request);
expect(response.result, equals(request.name));
await client.post(InvalidateLastAccessToken());
response = await client.send(request);
expect(response.result, equals(request.name));
var latestAccessToken = client.getTokenCookie();
expect(latestAccessToken, isNot(equals(initialAccessToken)));
});
test('Invalid RefreshToken throws RefreshTokenException ErrorResponse',
() async {
var client = createTestClient();
client.refreshToken = "Invalid.Refresh.Token";
try {
await client.get(TestAuth());
fail("should throw");
} on WebServiceException catch (e) {
expect(e.type, equals(WebServiceExceptionType.RefreshTokenException));
expect(e.responseStatus!.errorCode, equals("ArgumentException"));
expect(e.responseStatus!.message, equals("Illegal base64url string!"));
}
});
test('Expires RefreshToken throws RefreshTokenException', () async {
var client = createTestClient();
var createExpiredJwt = CreateRefreshJwt();
createExpiredJwt.jwtExpiry = DateTime(2000, 1, 1);
var expiredJwt = await client.post(createExpiredJwt);
client.refreshToken = expiredJwt.token!;
try {
await client.get(TestAuth());
fail("should throw");
} on WebServiceException catch (e) {
expect(e.type, equals(WebServiceExceptionType.RefreshTokenException));
expect(e.responseStatus!.errorCode, equals("TokenException"));
expect(e.responseStatus!.message, equals("Token has expired"));
}
});
test("TODO Can authenticate with ServerEvents using JWT", () async {
// TODO after ServerEventsClient is implemented
// https://github.com/ServiceStack/servicestack-client/blob/master/tests/client.auth.spec.ts#L225
});
}