platform/packages/websocket/test/auth_test.dart

66 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';
2021-02-21 02:47:23 +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';
2021-02-21 02:47:23 +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;
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 {
2021-02-21 02:47:23 +00:00
app = Angel();
http = AngelHttp(app, useZone: false);
var auth = AngelAuth();
2017-06-03 18:13:38 +00:00
auth.serializer = (_) async => 'baz';
auth.deserializer = (_) async => USER;
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') {
return USER;
}
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);
2017-09-24 04:37:58 +00:00
await app.configure(sock.configureServer);
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-02-21 02:47:23 +00:00
client = c.Rest('http://${server.address.address}:${server.port}');
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(() {
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);
});
}