2016-12-07 23:09:21 +00:00
|
|
|
import 'dart:io';
|
2024-10-13 01:45:27 +00:00
|
|
|
import 'package:protevus_auth/protevus_auth.dart';
|
|
|
|
import 'package:protevus_container/mirrors.dart';
|
|
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
|
|
import 'package:protevus_framework/http.dart';
|
2018-09-11 22:14:33 +00:00
|
|
|
import 'dart:convert';
|
2021-03-20 23:51:20 +00:00
|
|
|
import 'package:collection/collection.dart' show IterableExtension;
|
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';
|
2021-03-20 23:51:20 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2016-12-07 23:09:21 +00:00
|
|
|
|
2017-03-02 21:30:27 +00:00
|
|
|
class User extends Model {
|
2021-03-20 23:51:20 +00:00
|
|
|
String? username, password;
|
2016-12-07 23:09:21 +00:00
|
|
|
|
|
|
|
User({this.username, this.password});
|
2018-09-11 22:03:35 +00:00
|
|
|
|
2023-05-30 11:49:02 +00:00
|
|
|
static User parse(Map<String, dynamic> map) {
|
2019-04-19 07:50:04 +00:00
|
|
|
return User(
|
2021-03-20 23:51:20 +00:00
|
|
|
username: map['username'] as String?,
|
|
|
|
password: map['password'] as String?,
|
2018-09-11 22:03:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-20 23:51:20 +00:00
|
|
|
void main() {
|
2024-10-12 10:35:14 +00:00
|
|
|
late Protevus app;
|
|
|
|
late ProtevusHttp angelHttp;
|
2024-10-13 02:17:24 +00:00
|
|
|
ProtevusAuth<User> auth;
|
2021-03-20 23:51:20 +00:00
|
|
|
http.Client? client;
|
2016-12-07 23:09:21 +00:00
|
|
|
HttpServer server;
|
2021-03-20 23:51:20 +00:00
|
|
|
String? url;
|
2023-05-30 11:49:02 +00:00
|
|
|
String? encodedAuth;
|
2016-12-07 23:09:21 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2018-07-10 23:31:50 +00:00
|
|
|
hierarchicalLoggingEnabled = true;
|
2024-10-12 10:35:14 +00:00
|
|
|
app = Protevus(reflector: MirrorsReflector());
|
|
|
|
angelHttp = ProtevusHttp(app);
|
2021-04-10 11:23:57 +00:00
|
|
|
app.use('/users', MapService());
|
2016-12-07 23:09:21 +00:00
|
|
|
|
2021-04-10 11:23:57 +00:00
|
|
|
var oldErrorHandler = app.errorHandler;
|
|
|
|
app.errorHandler = (e, req, res) {
|
2022-02-21 01:17:27 +00:00
|
|
|
app.logger.severe(e.message, e, e.stackTrace ?? StackTrace.current);
|
2018-07-10 23:31:50 +00:00
|
|
|
return oldErrorHandler(e, req, res);
|
|
|
|
};
|
|
|
|
|
2024-10-13 01:45:27 +00:00
|
|
|
app.logger = Logger('protevus_auth')
|
2018-07-10 23:31:50 +00:00
|
|
|
..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()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-10 11:23:57 +00:00
|
|
|
await app
|
2021-07-08 01:20:21 +00:00
|
|
|
.findService('users')
|
|
|
|
?.create({'username': 'jdoe1', 'password': 'password'});
|
2016-12-07 23:09:21 +00:00
|
|
|
|
2024-10-13 02:17:24 +00:00
|
|
|
auth = ProtevusAuth<User>(
|
2021-09-29 07:40:27 +00:00
|
|
|
serializer: (u) => u.id ?? '',
|
2021-06-07 00:50:39 +00:00
|
|
|
deserializer: (id) async =>
|
|
|
|
await app.findService('users')?.read(id) as User);
|
|
|
|
//auth.serializer = (u) => u.id;
|
|
|
|
//auth.deserializer =
|
|
|
|
// (id) async => await app.findService('users')!.read(id) as User;
|
2016-12-07 23:09:21 +00:00
|
|
|
|
2021-04-10 11:23:57 +00:00
|
|
|
await app.configure(auth.configureServer);
|
2017-06-03 21:39:55 +00:00
|
|
|
|
2019-04-19 07:50:04 +00:00
|
|
|
auth.strategies['local'] = LocalAuthStrategy((username, password) async {
|
2021-04-10 11:23:57 +00:00
|
|
|
var users = await app
|
2021-07-08 01:20:21 +00:00
|
|
|
.findService('users')
|
|
|
|
?.index()
|
2023-05-30 11:49:02 +00:00
|
|
|
.then((it) => it.map<User>((m) => User.parse(m)).toList());
|
2021-03-20 23:51:20 +00:00
|
|
|
|
2021-07-08 01:20:21 +00:00
|
|
|
var result = users?.firstWhereOrNull(
|
2021-03-20 23:51:20 +00:00
|
|
|
(user) => user.username == username && user.password == password);
|
|
|
|
|
|
|
|
return Future.value(result);
|
2023-06-07 16:42:34 +00:00
|
|
|
}, allowBasic: true);
|
2016-12-07 23:09:21 +00:00
|
|
|
|
2021-04-10 11:23:57 +00:00
|
|
|
app.post(
|
2016-12-07 23:09:21 +00:00
|
|
|
'/login',
|
|
|
|
auth.authenticate('local',
|
2024-10-13 02:17:24 +00:00
|
|
|
ProtevusAuthOptions(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
|
|
|
})));
|
|
|
|
|
2021-04-10 11:23:57 +00:00
|
|
|
app.chain([
|
2018-08-26 23:11:37 +00:00
|
|
|
(req, res) {
|
2021-03-20 23:51:20 +00:00
|
|
|
if (!req.container!.has<User>()) {
|
|
|
|
req.container!.registerSingleton<User>(
|
2019-04-19 07:50:04 +00:00
|
|
|
User(username: req.params['name']?.toString()));
|
2018-09-11 22:03:35 +00:00
|
|
|
}
|
2018-08-26 23:11:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
]).post(
|
|
|
|
'/existing/:name',
|
|
|
|
auth.authenticate('local'),
|
|
|
|
);
|
2018-06-27 17:17:44 +00:00
|
|
|
|
2023-05-30 11:49:02 +00:00
|
|
|
encodedAuth = base64.encode(utf8.encode('jdoe1:password'));
|
|
|
|
|
2019-04-19 07:50:04 +00:00
|
|
|
client = 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 {
|
2021-03-20 23:51:20 +00:00
|
|
|
client!.close();
|
2018-06-27 16:36:31 +00:00
|
|
|
await angelHttp.close();
|
2021-04-10 11:23:57 +00:00
|
|
|
//app = null;
|
2016-12-07 23:09:21 +00:00
|
|
|
client = null;
|
|
|
|
url = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('login', () async {
|
2021-03-20 23:51:20 +00:00
|
|
|
final response = await client!.post(Uri.parse('$url/login'),
|
2023-05-30 11:49:02 +00:00
|
|
|
headers: {'Authorization': 'Basic $encodedAuth'});
|
2016-12-07 23:09:21 +00:00
|
|
|
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 {
|
2021-03-20 23:51:20 +00:00
|
|
|
final response = await client!.post(Uri.parse('$url/existing/foo'),
|
2018-06-27 17:17:44 +00:00
|
|
|
body: {'username': 'jdoe1', 'password': 'password'},
|
|
|
|
headers: {'accept': 'application/json'});
|
|
|
|
print('Response: ${response.body}');
|
2021-07-08 01:20:21 +00:00
|
|
|
print(response.headers);
|
2018-06-27 17:17:44 +00:00
|
|
|
expect(json.decode(response.body)['data']['username'], equals('foo'));
|
|
|
|
});
|
2016-12-07 23:09:21 +00:00
|
|
|
}
|