more tests

This commit is contained in:
Tobe O 2017-10-16 12:46:01 -04:00
parent 975aca1df5
commit 0fb8c68c0c

View file

@ -1,75 +1,85 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:angel_framework/angel_framework.dart'; import 'package:angel_framework/angel_framework.dart';
import 'package:angel_test/angel_test.dart';
import 'package:angel_oauth2/angel_oauth2.dart'; import 'package:angel_oauth2/angel_oauth2.dart';
import 'package:angel_validate/angel_validate.dart'; import 'package:logging/logging.dart';
import 'package:oauth2/oauth2.dart' as oauth2;
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'common.dart'; import 'common.dart';
main() { main() {
TestClient client; Angel app;
Uri tokenEndpoint;
setUp(() async { setUp(() async {
var app = new Angel()..lazyParseBodies = true; app = new Angel()..lazyParseBodies = true;
var oauth2 = new _AuthorizationServer(); var auth = new _AuthorizationServer();
app.group('/oauth2', (router) { app.group('/oauth2', (router) {
router router
..get('/authorize', oauth2.authorizationEndpoint) ..get('/authorize', auth.authorizationEndpoint)
..post('/token', oauth2.tokenEndpoint); ..post('/token', auth.tokenEndpoint);
}); });
app.errorHandler = (e, req, res) async { app.errorHandler = (e, req, res) async {
res.json(e.toJson()); res.json(e.toJson());
}; };
client = await connectTo(app); app.logger = new Logger('password_test')..onRecord.listen(print);
var server = await app.startServer();
var url = 'http://${server.address.address}:${server.port}';
tokenEndpoint = Uri.parse('$url/oauth2/token');
}); });
tearDown(() => client.close()); tearDown(() => app.close());
test('authenticate via username+password', () async { test('authenticate via username+password', () async {
var response = await client.post( var client = await oauth2.resourceOwnerPasswordGrant(
'/oauth2/token', tokenEndpoint,
headers: { 'michael',
'Authorization': 'Basic ' + BASE64URL.encode('foo:bar'.codeUnits), 'jackson',
}, identifier: 'foo',
body: { secret: 'bar',
'grant_type': 'password',
'username': 'michael',
'password': 'jackson',
},
); );
print(client.credentials.toJson());
print('Response: ${response.body}'); client.close();
expect(client.credentials.accessToken, 'foo');
expect(response, allOf( expect(client.credentials.refreshToken, 'bar');
hasStatus(200),
hasContentType(ContentType.JSON),
hasValidBody(new Validator({
'token_type': equals('bearer'),
'access_token': equals('foo'),
})),
));
}); });
test('force correct username+password', () async { test('force correct username+password', () async {
var response = await client.post( oauth2.Client client;
'/oauth2/token',
headers: { try {
'Authorization': 'Basic ' + BASE64URL.encode('foo:bar'.codeUnits), client = await oauth2.resourceOwnerPasswordGrant(
}, tokenEndpoint,
body: { 'michael',
'grant_type': 'password', 'jordan',
'username': 'michael', identifier: 'foo',
'password': 'jordan', secret: 'bar',
},
); );
print('Response: ${response.body}'); throw new StateError('should fail');
expect(response, hasStatus(401)); } on oauth2.AuthorizationException catch (e) {
expect(e.error, ErrorResponse.accessDenied);
} finally {
client?.close();
}
});
test('can refresh token', () async {
var client = await oauth2.resourceOwnerPasswordGrant(
tokenEndpoint,
'michael',
'jackson',
identifier: 'foo',
secret: 'bar',
);
client = await client.refreshCredentials();
print(client.credentials.toJson());
client.close();
expect(client.credentials.accessToken, 'baz');
expect(client.credentials.refreshToken, 'bar');
}); });
} }
@ -86,6 +96,16 @@ class _AuthorizationServer
return client.secret == clientSecret; return client.secret == clientSecret;
} }
@override
Future<AuthorizationTokenResponse> refreshAuthorizationToken(
PseudoApplication client,
String refreshToken,
Iterable<String> scopes,
RequestContext req,
ResponseContext res) async {
return new AuthorizationTokenResponse('baz', refreshToken: 'bar');
}
@override @override
Future<AuthorizationTokenResponse> resourceOwnerPasswordCredentialsGrant( Future<AuthorizationTokenResponse> resourceOwnerPasswordCredentialsGrant(
PseudoApplication client, PseudoApplication client,
@ -109,6 +129,6 @@ class _AuthorizationServer
); );
} }
return new AuthorizationTokenResponse('foo'); return new AuthorizationTokenResponse('foo', refreshToken: 'bar');
} }
} }