platform/test/local.dart

110 lines
3.7 KiB
Dart
Raw Normal View History

2016-05-03 04:13:19 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_auth/angel_auth.dart';
import 'package:http/http.dart' as http;
import 'package:merge_map/merge_map.dart';
import 'package:test/test.dart';
2016-09-21 06:19:52 +00:00
final AngelAuth Auth = new AngelAuth();
2016-05-03 04:13:19 +00:00
Map headers = {HttpHeaders.ACCEPT: ContentType.JSON.mimeType};
2016-05-09 20:47:28 +00:00
AngelAuthOptions localOpts = new AngelAuthOptions(
2016-09-21 06:19:52 +00:00
failureRedirect: '/failure', successRedirect: '/success');
2016-05-03 04:13:19 +00:00
Map sampleUser = {'hello': 'world'};
verifier(username, password) async {
if (username == 'username' && password == 'password') {
return sampleUser;
2016-09-21 06:19:52 +00:00
} else
return false;
2016-05-03 04:13:19 +00:00
}
wireAuth(Angel app) async {
Auth.serializer = (user) async => 1337;
Auth.deserializer = (id) async => sampleUser;
2016-09-21 23:09:23 +00:00
Auth.strategies.add(new LocalAuthStrategy(verifier));
2016-09-21 06:19:52 +00:00
await app.configure(Auth);
2016-05-03 04:13:19 +00:00
}
main() async {
2016-09-21 06:19:52 +00:00
group('local', () {
2016-05-03 04:13:19 +00:00
Angel app;
http.Client client;
String url;
2016-05-03 23:43:41 +00:00
String basicAuthUrl;
2016-05-03 04:13:19 +00:00
setUp(() async {
client = new http.Client();
app = new Angel();
await app.configure(wireAuth);
app.get('/hello', 'Woo auth', middleware: [Auth.authenticate('local')]);
app.post('/login', 'This should not be shown',
middleware: [Auth.authenticate('local', localOpts)]);
2016-05-03 23:43:41 +00:00
app.get('/success', "yep", middleware: ['auth']);
2016-05-03 04:13:19 +00:00
app.get('/failure', "nope");
2016-09-21 06:19:52 +00:00
HttpServer server =
await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0);
2016-05-03 04:13:19 +00:00
url = "http://${server.address.host}:${server.port}";
2016-05-03 23:43:41 +00:00
basicAuthUrl =
2016-09-21 06:19:52 +00:00
"http://username:password@${server.address.host}:${server.port}";
2016-05-03 04:13:19 +00:00
});
tearDown(() async {
await app.httpServer.close(force: true);
client = null;
url = null;
2016-05-03 23:43:41 +00:00
basicAuthUrl = null;
2016-05-03 04:13:19 +00:00
});
2016-09-21 06:19:52 +00:00
test('can use "auth" as middleware', () async {
var response = await client
.get("$url/success", headers: {'Accept': 'application/json'});
2016-05-03 23:43:41 +00:00
print(response.body);
2016-09-21 06:19:52 +00:00
expect(response.statusCode, equals(403));
2016-05-03 04:13:19 +00:00
});
test('successRedirect', () async {
2016-09-21 06:19:52 +00:00
Map postData = {'username': 'username', 'password': 'password'};
var response = await client.post("$url/login",
body: JSON.encode(postData),
2016-05-03 04:13:19 +00:00
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
expect(response.statusCode, equals(200));
expect(response.headers[HttpHeaders.LOCATION], equals('/success'));
});
test('failureRedirect', () async {
2016-09-21 06:19:52 +00:00
Map postData = {'username': 'password', 'password': 'username'};
var response = await client.post("$url/login",
body: JSON.encode(postData),
2016-05-03 04:13:19 +00:00
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
2016-09-21 23:09:23 +00:00
print("Login response: ${response.body}");
2016-05-03 04:13:19 +00:00
expect(response.headers[HttpHeaders.LOCATION], equals('/failure'));
2016-09-21 23:09:23 +00:00
expect(response.statusCode, equals(401));
2016-05-03 04:13:19 +00:00
});
test('allow basic', () async {
String authString = BASE64.encode("username:password".runes.toList());
2016-09-21 23:09:23 +00:00
var response = await client.get("$url/hello",
headers: {HttpHeaders.AUTHORIZATION: 'Basic $authString'});
2016-05-03 04:13:19 +00:00
expect(response.body, equals('"Woo auth"'));
});
2016-05-03 23:43:41 +00:00
test('allow basic via URL encoding', () async {
2016-09-21 23:09:23 +00:00
var response = await client.get("$basicAuthUrl/hello");
2016-05-03 23:43:41 +00:00
expect(response.body, equals('"Woo auth"'));
});
2016-05-03 04:13:19 +00:00
test('force basic', () async {
Auth.strategies.clear();
2016-09-21 23:09:23 +00:00
Auth.strategies.add(new LocalAuthStrategy(verifier,
2016-09-21 06:19:52 +00:00
forceBasic: true, realm: 'test'));
2016-05-03 04:13:19 +00:00
var response = await client.get("$url/hello", headers: headers);
2016-09-21 23:09:23 +00:00
print(response.headers);
2016-05-03 04:13:19 +00:00
expect(response.headers[HttpHeaders.WWW_AUTHENTICATE],
equals('Basic realm="test"'));
});
});
2016-09-21 06:19:52 +00:00
}