platform/packages/websocket/test/auth_test.dart

64 lines
1.8 KiB
Dart
Raw Normal View History

2017-06-03 18:13:38 +00:00
import 'dart:async';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_client/io.dart' as c;
import 'package:angel_framework/angel_framework.dart';
2018-10-22 23:01:24 +00:00
import "package:angel_framework/http.dart";
2017-06-03 18:13:38 +00:00
import 'package:angel_websocket/io.dart' as c;
import 'package:angel_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';
const Map<String, String> USER = const {'username': 'foo', 'password': 'bar'};
main() {
Angel app;
2018-07-10 16:54:55 +00:00
AngelHttp http;
2017-06-03 18:13:38 +00:00
c.Angel client;
c.WebSockets ws;
setUp(() async {
app = new Angel();
2018-07-10 16:54:55 +00:00
http = new AngelHttp(app, useZone: false);
2017-06-03 18:13:38 +00:00
var auth = new AngelAuth();
auth.serializer = (_) async => 'baz';
auth.deserializer = (_) async => USER;
2018-10-02 15:32:06 +00:00
auth.strategies['local'] = new LocalAuthStrategy(
(username, password) async {
if (username == 'foo' && password == 'bar') return USER;
},
);
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);
var sock = new AngelWebSocket(app);
await app.configure(sock.configureServer);
app.all('/ws', sock.handleRequest);
app.logger = new 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();
2017-06-03 18:13:38 +00:00
client = new c.Rest('http://${server.address.address}:${server.port}');
ws = new c.WebSockets('ws://${server.address.address}:${server.port}/ws');
await ws.connect();
});
2017-09-24 04:37:58 +00:00
tearDown(() {
return Future.wait([
2018-07-10 16:54:55 +00:00
http.close(),
2017-09-24 04:37:58 +00:00
client.close(),
ws.close(),
]);
});
2017-06-03 18:13:38 +00:00
test('auth event fires', () async {
var localAuth = await client.authenticate(type: 'local', credentials: USER);
print('JWT: ${localAuth.token}');
ws.authenticateViaJwt(localAuth.token);
var auth = await ws.onAuthenticated.first;
expect(auth.token, localAuth.token);
});
}