platform/test/callback_test.dart

76 lines
1.9 KiB
Dart
Raw Normal View History

2016-12-07 23:09:21 +00:00
import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:angel_framework/angel_framework.dart';
2017-03-02 21:30:27 +00:00
import 'package:angel_framework/common.dart';
2016-12-07 23:09:21 +00:00
import 'package:http/http.dart' as http;
import 'package:test/test.dart';
2017-03-02 21:30:27 +00:00
class User extends Model {
2016-12-07 23:09:21 +00:00
String username, password;
User({this.username, this.password});
}
main() {
Angel app;
2018-06-27 16:36:31 +00:00
AngelHttp angelHttp;
2016-12-07 23:09:21 +00:00
AngelAuth auth;
http.Client client;
HttpServer server;
String url;
setUp(() async {
app = new Angel();
2018-06-27 16:36:31 +00:00
angelHttp = new AngelHttp(app, useZone: false);
2017-03-02 21:30:27 +00:00
app.use('/users', new TypedService<User>(new MapService()));
2016-12-07 23:09:21 +00:00
await app
.service('users')
.create({'username': 'jdoe1', 'password': 'password'});
2018-06-27 16:36:31 +00:00
auth = new AngelAuth<User>();
auth.serializer = (u) => u.id;
2016-12-07 23:09:21 +00:00
auth.deserializer = app.service('users').read;
2017-09-24 04:32:38 +00:00
await app.configure(auth.configureServer);
app.use(auth.decodeJwt);
2017-06-03 21:39:55 +00:00
2016-12-07 23:09:21 +00:00
auth.strategies.add(new LocalAuthStrategy((username, password) async {
final List<User> users = await app.service('users').index();
final found = users.firstWhere(
(user) => user.username == username && user.password == password,
orElse: () => null);
return found != null ? found : false;
}));
app.post(
'/login',
auth.authenticate('local',
new AngelAuthOptions(callback: (req, res, token) {
2017-01-13 15:50:38 +00:00
res
2016-12-07 23:09:21 +00:00
..write('Hello!')
..end();
})));
client = new http.Client();
2018-06-27 16:36:31 +00:00
server = await angelHttp.startServer();
2016-12-07 23:09:21 +00:00
url = 'http://${server.address.address}:${server.port}';
});
tearDown(() async {
client.close();
2018-06-27 16:36:31 +00:00
await angelHttp.close();
2016-12-07 23:09:21 +00:00
app = null;
client = null;
url = null;
});
test('login', () async {
final response = await client.post('$url/login',
body: {'username': 'jdoe1', 'password': 'password'});
print('Response: ${response.body}');
expect(response.body, equals('Hello!'));
});
}