platform/test/callback_test.dart

144 lines
3.8 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';
2018-12-09 16:29:15 +00:00
import 'package:angel_framework/http.dart';
2018-09-11 22:14:33 +00:00
import 'dart:convert';
2016-12-07 23:09:21 +00:00
import 'package:http/http.dart' as http;
2018-07-10 23:31:50 +00:00
import 'package:io/ansi.dart';
import 'package:logging/logging.dart';
2016-12-07 23:09:21 +00:00
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});
2018-09-11 22:03:35 +00:00
static User parse(Map map) {
return new User(
username: map['username'] as String,
password: map['password'] as String,
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'username': username,
'password': password,
'created_at': createdAt?.toIso8601String(),
'updated_at': updatedAt?.toIso8601String()
};
}
2016-12-07 23:09:21 +00:00
}
main() {
Angel app;
2018-06-27 16:36:31 +00:00
AngelHttp angelHttp;
2018-08-26 23:11:37 +00:00
AngelAuth<User> auth;
2016-12-07 23:09:21 +00:00
http.Client client;
HttpServer server;
String url;
setUp(() async {
2018-07-10 23:31:50 +00:00
hierarchicalLoggingEnabled = true;
2016-12-07 23:09:21 +00:00
app = new Angel();
2018-07-10 23:31:50 +00:00
angelHttp = new AngelHttp(app);
2018-08-26 23:11:37 +00:00
app.use('/users', new MapService());
2016-12-07 23:09:21 +00:00
2018-07-10 23:31:50 +00:00
var oldErrorHandler = app.errorHandler;
app.errorHandler = (e, req, res) {
app.logger.severe(e.message, e, e.stackTrace ?? StackTrace.current);
return oldErrorHandler(e, req, res);
};
app.logger = new Logger('angel_auth')
..level = Level.FINEST
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) {
print(yellow.wrap(rec.error.toString()));
}
if (rec.stackTrace != null) {
print(yellow.wrap(rec.stackTrace.toString()));
}
});
2018-06-27 18:10:56 +00:00
await app
2018-12-09 16:29:15 +00:00
.findService('users')
2016-12-07 23:09:21 +00:00
.create({'username': 'jdoe1', 'password': 'password'});
2018-06-27 16:36:31 +00:00
auth = new AngelAuth<User>();
auth.serializer = (u) => u.id;
2018-07-10 23:31:50 +00:00
auth.deserializer =
2018-12-09 16:29:15 +00:00
(id) async => await app.findService('users').read(id) as User;
2016-12-07 23:09:21 +00:00
2017-09-24 04:32:38 +00:00
await app.configure(auth.configureServer);
2018-08-26 23:11:37 +00:00
app.fallback(auth.decodeJwt);
2017-06-03 21:39:55 +00:00
2018-09-11 22:03:35 +00:00
auth.strategies['local'] =
new LocalAuthStrategy((username, password) async {
2018-12-09 16:29:15 +00:00
var users = await app
.findService('users')
2018-09-11 22:03:35 +00:00
.index()
2018-12-09 16:29:15 +00:00
.then((it) => it.map<User>((m) => User.parse(m as Map)).toList());
2018-09-11 22:03:35 +00:00
return users.firstWhere(
2016-12-07 23:09:21 +00:00
(user) => user.username == username && user.password == password,
orElse: () => null);
2018-09-11 22:03:35 +00:00
});
2016-12-07 23:09:21 +00:00
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!')
2018-08-26 23:11:37 +00:00
..close();
2016-12-07 23:09:21 +00:00
})));
2018-08-26 23:11:37 +00:00
app.chain([
(req, res) {
2018-09-11 22:03:35 +00:00
if (!req.container.has<User>()) {
req.container.registerSingleton<User>(
new User(username: req.params['name']?.toString()));
}
2018-08-26 23:11:37 +00:00
return true;
}
]).post(
'/existing/:name',
auth.authenticate('local'),
);
2018-06-27 17:17:44 +00:00
2016-12-07 23:09:21 +00:00
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!'));
2018-07-10 23:31:50 +00:00
},
skip: Platform.version.contains('2.0.0-dev')
? 'Blocked on https://github.com/dart-lang/sdk/issues/33594'
: null);
2018-06-27 17:17:44 +00:00
test('preserve existing user', () async {
final response = await client.post('$url/existing/foo',
body: {'username': 'jdoe1', 'password': 'password'},
headers: {'accept': 'application/json'});
print('Response: ${response.body}');
expect(json.decode(response.body)['data']['username'], equals('foo'));
});
2016-12-07 23:09:21 +00:00
}