platform/packages/auth/test/protect_cookie_test.dart

46 lines
1.1 KiB
Dart
Raw Normal View History

2018-06-27 16:36:31 +00:00
import 'dart:io';
import 'package:protevus_auth/protevus_auth.dart';
2018-06-27 16:36:31 +00:00
import 'package:test/test.dart';
2021-05-09 11:16:15 +00:00
const Duration threeDays = Duration(days: 3);
2018-06-27 16:36:31 +00:00
void main() {
2021-03-20 23:51:20 +00:00
late Cookie defaultCookie;
var auth = ProtevusAuth(
2021-06-07 00:50:39 +00:00
secureCookies: true,
cookieDomain: 'SECURE',
jwtLifeSpan: threeDays.inMilliseconds,
2023-05-21 11:02:54 +00:00
serializer: (u) => u,
2021-06-07 00:50:39 +00:00
deserializer: (u) => u);
2018-06-27 16:36:31 +00:00
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');
});
}