platform/packages/websocket/test/auth_test.dart

66 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';
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;
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);
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);
2021-03-05 07:51:48 +00:00
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-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 {
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);
});
}