platform/test/local.dart

115 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';
Map headers = {HttpHeaders.ACCEPT: ContentType.JSON.mimeType};
2016-05-09 20:47:28 +00:00
AngelAuthOptions localOpts = new AngelAuthOptions(
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;
} else return false;
}
wireAuth(Angel app) async {
Auth.serializer = (user) async => 1337;
Auth.deserializer = (id) async => sampleUser;
Auth.strategies.add(new LocalAuthStrategy(verifier));
await app.configure(AngelAuth);
}
main() async {
group
('local', () {
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");
HttpServer server = await app.startServer(
InternetAddress.LOOPBACK_IP_V4, 0);
url = "http://${server.address.host}:${server.port}";
2016-05-03 23:43:41 +00:00
basicAuthUrl =
"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
});
test('can use login as middleware', () async {
2016-05-09 20:47:28 +00:00
var response = await client.get(
"$url/success", headers: {'Accept': 'application/json'});
2016-05-03 23:43:41 +00:00
print(response.body);
2016-05-03 04:13:19 +00:00
expect(response.statusCode, equals(401));
});
test('successRedirect', () async {
Map postData = {
'username': 'username',
'password': 'password'
};
var response = await client.post(
"$url/login", body: JSON.encode(postData),
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
expect(response.statusCode, equals(200));
expect(response.headers[HttpHeaders.LOCATION], equals('/success'));
});
test('failureRedirect', () async {
Map postData = {
'username': 'password',
'password': 'username'
};
var response = await client.post(
"$url/login", body: JSON.encode(postData),
headers: {HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType});
expect(response.statusCode, equals(401));
expect(response.headers[HttpHeaders.LOCATION], equals('/failure'));
});
test('allow basic', () async {
String authString = BASE64.encode("username:password".runes.toList());
Map auth = {HttpHeaders.AUTHORIZATION: 'Basic $authString'};
var response = await client.get(
"$url/hello", headers: mergeMap([auth, headers]));
expect(response.body, equals('"Woo auth"'));
});
2016-05-03 23:43:41 +00:00
test('allow basic via URL encoding', () async {
2016-05-09 20:47:28 +00:00
var response = await client.get("$basicAuthUrl/hello", headers: headers);
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();
Auth.strategies.add(new LocalAuthStrategy(
2016-05-09 20:47:28 +00:00
verifier, forceBasic: true, realm: 'test'));
2016-05-03 04:13:19 +00:00
var response = await client.get("$url/hello", headers: headers);
expect(response.headers[HttpHeaders.WWW_AUTHENTICATE],
equals('Basic realm="test"'));
});
});
}