2024-10-13 01:45:27 +00:00
|
|
|
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';
|
2021-07-15 08:11:54 +00:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2022-08-27 07:49:21 +00:00
|
|
|
const Map<String, String> user = {'username': 'foo', 'password': 'bar'};
|
2024-10-13 02:17:24 +00:00
|
|
|
var localOpts =
|
|
|
|
ProtevusAuthOptions<Map<String, String>>(canRespondWithJson: true);
|
2021-07-15 08:11:54 +00:00
|
|
|
|
|
|
|
void main() {
|
2024-10-12 10:35:14 +00:00
|
|
|
late Protevus app;
|
|
|
|
late ProtevusHttp http;
|
|
|
|
late c.Protevus client;
|
2021-07-15 08:11:54 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
app = Protevus();
|
|
|
|
http = ProtevusHttp(app, useZone: false);
|
2024-10-13 02:17:24 +00:00
|
|
|
var auth = ProtevusAuth(
|
2022-08-27 07:49:21 +00:00
|
|
|
serializer: (_) async => 'baz', deserializer: (_) async => user);
|
2021-07-15 08:11:54 +00:00
|
|
|
|
|
|
|
auth.strategies['local'] = LocalAuthStrategy(
|
|
|
|
(username, password) async {
|
|
|
|
if (username == 'foo' && password == 'bar') {
|
2022-08-27 07:49:21 +00:00
|
|
|
return user;
|
2021-07-15 08:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-08-27 07:49:21 +00:00
|
|
|
var localAuth = await client.authenticate(type: 'local', credentials: user);
|
2021-07-15 08:11:54 +00:00
|
|
|
print('JWT: ${localAuth.token}');
|
|
|
|
print('Data: ${localAuth.data}');
|
|
|
|
|
2022-08-27 07:49:21 +00:00
|
|
|
expect(localAuth.data, user);
|
2021-07-15 08:11:54 +00:00
|
|
|
});
|
|
|
|
}
|