Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testable date times #50

Merged
merged 3 commits into from
Dec 28, 2023

Conversation

robsonsilv4
Copy link
Contributor

@robsonsilv4 robsonsilv4 commented Dec 24, 2023

Description

This PR replaces untestable DateTime.now() with clock.now() from pkg:clock.
With this change, the package and the clients of the package could be testing token date times using pkg:fake_async.

And the best part, this change doesn't add breaking changes or change the current behaviors!

Here's a sample of testing an expired token:

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';

void main() {
  test('should expires when `expiresIn` duration is elapsed', () {
    final secretKey = SecretKey('secret_key');
    final jwt = JWT({'foo': 'bar'});
    final duration = Duration(hours: 1);
    fakeAsync((async) {
      final token = jwt.sign(secretKey, expiresIn: duration);
      async.elapse(duration);
      expect(
        () => JWT.verify(token, secretKey),
        throwsA(isA<JWTExpiredException>()),
      );
    });
  });
}

Note: clock and fake_async are published by Dart Team:

@robsonsilv4
Copy link
Contributor Author

@jonasroussel I tried to follow your pattern of tests. Let me know if you need to change anything.

@robsonsilv4
Copy link
Contributor Author

CC: @alestiago

@jonasroussel
Copy link
Owner

Hey @robsonsilv4,

Thanks for your changes! I just refactor your tests to put them in another test file so I can evolve the header tests in the future, I made those changes in another branch here: https://github.com/jonasroussel/dart_jsonwebtoken/tree/testable-datetime. I'll let you merge it into your PR repo branch and then I'll approve it.

@robsonsilv4
Copy link
Contributor Author

Already merged, thanks @jonasroussel
It's really better to split the tests before they grow more

@jonasroussel jonasroussel merged commit aab2ace into jonasroussel:main Dec 28, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants