platform/packages/auth/test/protect_cookie_test.dart

45 lines
1.1 KiB
Dart
Raw Normal View History

2018-06-27 16:36:31 +00:00
import 'dart:io';
import 'package:angel_auth/angel_auth.dart';
import 'package:test/test.dart';
const Duration threeDays = const Duration(days: 3);
void main() {
2021-03-20 23:51:20 +00:00
late Cookie defaultCookie;
2019-04-19 07:50:04 +00:00
var auth = AngelAuth(
2018-06-27 16:36:31 +00:00
secureCookies: true,
cookieDomain: 'SECURE',
jwtLifeSpan: threeDays.inMilliseconds,
);
2019-04-19 07:50:04 +00:00
setUp(() => defaultCookie = Cookie('a', 'b'));
2018-06-27 16:36:31 +00:00
test('sets maxAge', () {
expect(auth.protectCookie(defaultCookie).maxAge, threeDays.inSeconds);
});
test('sets expires', () {
2019-04-19 07:50:04 +00:00
var now = DateTime.now();
2021-03-20 23:51:20 +00:00
var expiry = auth.protectCookie(defaultCookie).expires!;
2018-06-27 16:36:31 +00:00
var diff = expiry.difference(now);
expect(diff.inSeconds, threeDays.inSeconds);
});
test('sets httpOnly', () {
expect(auth.protectCookie(defaultCookie).httpOnly, true);
});
test('sets secure', () {
expect(auth.protectCookie(defaultCookie).secure, true);
});
test('sets domain', () {
expect(auth.protectCookie(defaultCookie).domain, 'SECURE');
});
test('preserves domain if present', () {
expect(auth.protectCookie(defaultCookie..domain = 'foo').domain, 'foo');
});
}