platform/packages/client/test/auth_test.dart
2024-10-12 19:17:24 -07:00

60 lines
1.6 KiB
Dart

import 'package:protevus_auth/protevus_auth.dart';
import 'package:protevus_client/io.dart' as c;
import 'package:protevus_framework/protevus_framework.dart';
import 'package:protevus_framework/http.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
const Map<String, String> user = {'username': 'foo', 'password': 'bar'};
var localOpts =
ProtevusAuthOptions<Map<String, String>>(canRespondWithJson: true);
void main() {
late Protevus app;
late ProtevusHttp http;
late c.Protevus client;
setUp(() async {
app = Protevus();
http = ProtevusHttp(app, useZone: false);
var auth = ProtevusAuth(
serializer: (_) async => 'baz', deserializer: (_) async => user);
auth.strategies['local'] = LocalAuthStrategy(
(username, password) async {
if (username == 'foo' && password == 'bar') {
return user;
}
return {};
},
);
app.post('/auth/local', auth.authenticate('local', localOpts));
await app.configure(auth.configureServer);
app.logger = Logger('auth_test')
..onRecord.listen((rec) {
print(
'${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
});
var server = await http.startServer();
client = c.Rest('http://${server.address.address}:${server.port}');
});
tearDown(() {
http.close();
client.close();
});
test('auth event fires', () async {
var localAuth = await client.authenticate(type: 'local', credentials: user);
print('JWT: ${localAuth.token}');
print('Data: ${localAuth.data}');
expect(localAuth.data, user);
});
}