platform/packages/websocket/test/auth_test.dart

67 lines
1.8 KiB
Dart
Raw Normal View History

2021-05-15 07:19:35 +00:00
import 'package:angel3_auth/angel3_auth.dart';
import 'package:angel3_client/io.dart' as c;
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_websocket/io.dart' as c;
import 'package:angel3_websocket/server.dart';
2017-09-24 04:37:58 +00:00
import 'package:logging/logging.dart';
2017-06-03 18:13:38 +00:00
import 'package:test/test.dart';
2022-08-28 17:51:56 +00:00
const Map<String, String> user = {'username': 'foo', 'password': 'bar'};
2017-06-03 18:13:38 +00:00
2021-02-21 02:47:23 +00:00
void main() {
2017-06-03 18:13:38 +00:00
Angel app;
2021-04-26 00:47:32 +00:00
late AngelHttp http;
late c.Angel client;
late c.WebSockets ws;
2017-06-03 18:13:38 +00:00
setUp(() async {
2021-02-21 02:47:23 +00:00
app = Angel();
http = AngelHttp(app, useZone: false);
2021-07-10 04:32:42 +00:00
var auth = AngelAuth(
2022-08-28 17:51:56 +00:00
serializer: (_) async => 'baz', deserializer: (_) async => user);
2017-06-03 18:13:38 +00:00
2021-02-21 02:47:23 +00:00
auth.strategies['local'] = LocalAuthStrategy(
2018-10-02 15:32:06 +00:00
(username, password) async {
2021-02-21 02:47:23 +00:00
if (username == 'foo' && password == 'bar') {
2022-08-28 17:51:56 +00:00
return user;
2021-02-21 02:47:23 +00:00
}
2022-10-06 00:00:03 +00:00
return {};
2018-10-02 15:32:06 +00:00
},
);
2017-06-03 18:13:38 +00:00
app.post('/auth/local', auth.authenticate('local'));
2017-09-24 04:37:58 +00:00
await app.configure(auth.configureServer);
2021-02-21 02:47:23 +00:00
var sock = AngelWebSocket(app);
2021-03-05 07:51:48 +00:00
2017-09-24 04:37:58 +00:00
await app.configure(sock.configureServer);
2021-07-15 08:14:52 +00:00
2017-09-24 04:37:58 +00:00
app.all('/ws', sock.handleRequest);
2021-02-21 02:47:23 +00:00
app.logger = Logger('angel_auth')..onRecord.listen(print);
2017-06-03 18:13:38 +00:00
2018-07-10 16:54:55 +00:00
var server = await http.startServer();
2021-03-05 07:51:48 +00:00
2021-02-21 02:47:23 +00:00
client = c.Rest('http://${server.address.address}:${server.port}');
2021-03-05 07:51:48 +00:00
2021-02-21 02:47:23 +00:00
ws = c.WebSockets('ws://${server.address.address}:${server.port}/ws');
2017-06-03 18:13:38 +00:00
await ws.connect();
});
2017-09-24 04:37:58 +00:00
tearDown(() {
2021-03-05 07:51:48 +00:00
http.close();
client.close();
ws.close();
2017-09-24 04:37:58 +00:00
});
2017-06-03 18:13:38 +00:00
test('auth event fires', () async {
2022-08-28 17:51:56 +00:00
var localAuth = await client.authenticate(type: 'local', credentials: user);
2017-06-03 18:13:38 +00:00
print('JWT: ${localAuth.token}');
ws.authenticateViaJwt(localAuth.token);
var auth = await ws.onAuthenticated.first;
expect(auth.token, localAuth.token);
});
}