This commit is contained in:
2025-04-01 11:52:46 +02:00
parent f1586abc86
commit c8c30d3838
7 changed files with 270 additions and 122 deletions
+82
View File
@@ -0,0 +1,82 @@
import "package:arcane_helper_utils/arcane_helper_utils.dart";
import "package:test/test.dart";
void main() {
// Example JWT token for testing
const String validToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiZXhwIjoxNzM1Njg5NjAwLCJ1aWQiOiIxMjM0NTYiLCJnaXZlbl9uYW1lIjoiZ2l2ZW4iLCJmYW1pbHlfbmFtZSI6ImZhbWlseSJ9."
"Ki_D9fOhv7bLe86j6fdZ1guNGI7ldKSeeANUfinwNtc";
group("JWTUtility", () {
test("jwt getter parses a valid JWT", () {
expect(validToken.jwt, isNotNull);
});
test("jwt getter throws an exception for invalid token", () {
expect(() => "invalid.token".jwt, throwsException);
expect(() => "".jwt, throwsException);
});
test("jwt.email extracts email correctly", () {
expect(validToken.jwt.email, "test@example.com");
});
test("jwt.email throws an exception for invalid token", () {
expect(
() => "invalid.token".jwt.email,
throwsA(isA<InvalidTokenException>()),
);
expect(() => "".jwt.email, throwsA(isA<InvalidTokenException>()));
});
test("jwt.givenName extracts given name correctly", () {
expect(validToken.jwt.givenName, "given");
});
test("jwt.givenName throws an exception for invalid token", () {
expect(
() => "invalid.token".jwt.givenName,
throwsA(isA<InvalidTokenException>()),
);
expect(() => "".jwt.givenName, throwsA(isA<InvalidTokenException>()));
});
test("jwt.familyName extracts family name correctly", () {
expect(validToken.jwt.familyName, "family");
});
test("jwt.familyName returns null for invalid token", () {
expect(
() => "invalid.token".jwt.familyName,
throwsA(isA<InvalidTokenException>()),
);
expect(() => "".jwt.familyName, throwsA(isA<InvalidTokenException>()));
});
test("jwt.expiryTime extracts expiry time correctly", () {
final DateTime? expiry = validToken.jwt.expiryTime;
expect(expiry, isNotNull);
expect(expiry?.year, 2025); // Based on the exp value in the test token
});
test("jwt.expiryTime throws an exception for invalid token", () {
expect(
() => "invalid.token".jwt.expiryTime,
throwsA(isA<InvalidTokenException>()),
);
expect(() => "".jwt.expiryTime, throwsA(isA<InvalidTokenException>()));
});
test("jwt.userId extracts user ID correctly", () {
expect(validToken.jwt.userId, "123456");
});
test("jwt.userId throws an exception for invalid token", () {
expect(
() => "invalid.token".jwt.userId,
throwsA(isA<InvalidTokenException>()),
);
expect(() => "".jwt.userId, throwsA(isA<InvalidTokenException>()));
});
});
}
-40
View File
@@ -1,40 +0,0 @@
import "package:arcane_helper_utils/arcane_helper_utils.dart";
import "package:test/test.dart";
void main() {
// Example JWT token for testing
const String validToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJzdWIiOiJ0ZXN0QGV4YW1wbGUuY29tIiwiZXhwIjoxNzM1Njg5NjAwLCJ1aWQiOiIxMjM0NTYifQ."
"signature";
group("JWTUtility", () {
test("jwtEmail extracts email correctly", () {
expect(validToken.jwtEmail(), "test@example.com");
});
test("jwtEmail returns null for invalid token", () {
expect("invalid.token".jwtEmail(), null);
expect("".jwtEmail(), null);
});
test("jwtExpiryTime extracts expiry time correctly", () {
final DateTime? expiry = validToken.jwtExpiryTime();
expect(expiry, isNotNull);
expect(expiry?.year, 2025); // Based on the exp value in the test token
});
test("jwtExpiryTime returns null for invalid token", () {
expect("invalid.token".jwtExpiryTime(), null);
expect("".jwtExpiryTime(), null);
});
test("jwtUserId extracts user ID correctly", () {
expect(validToken.jwtUserId(), "123456");
});
test("jwtUserId returns null for invalid token", () {
expect("invalid.token".jwtUserId(), null);
expect("".jwtUserId(), null);
});
});
}