@@ -2,6 +2,7 @@ import { RuntimeError, test, TestContext } from "../_gen/test.ts";
22import {
33 assertEquals ,
44 assertRejects ,
5+ assertGreater ,
56} from "https://deno.land/std@0.217.0/assert/mod.ts" ;
67
78test (
1011 const error = await assertRejects ( async ( ) => {
1112 await ctx . modules . tokens . validate ( { token : "invalid token" } ) ;
1213 } , RuntimeError ) ;
13- assertEquals ( error . code , "TOKEN_NOT_FOUND " ) ;
14+ assertEquals ( error . code , "token_not_found " ) ;
1415 } ,
1516) ;
1617
2728 const error = await assertRejects ( async ( ) => {
2829 await ctx . modules . tokens . validate ( { token : token . token } ) ;
2930 } , RuntimeError ) ;
30- assertEquals ( error . code , "TOKEN_REVOKED " ) ;
31+ assertEquals ( error . code , "token_revoked " ) ;
3132 } ,
3233) ;
3334
5253 const error = await assertRejects ( async ( ) => {
5354 await ctx . modules . tokens . validate ( { token : token . token } ) ;
5455 } , RuntimeError ) ;
55- assertEquals ( error . code , "TOKEN_EXPIRED" ) ;
56+ assertEquals ( error . code , "token_expired" ) ;
57+ } ,
58+ ) ;
59+
60+ test (
61+ "validate token extended not expired" ,
62+ async ( ctx : TestContext ) => {
63+ const { token } = await ctx . modules . tokens . create ( {
64+ type : "test" ,
65+ meta : { foo : "bar" } ,
66+ // Set initial expiration to 200ms in the future
67+ expireAt : new Date ( Date . now ( ) + 200 ) . toISOString ( ) ,
68+ } ) ;
69+
70+ // Token should be valid
71+ const validateRes = await ctx . modules . tokens . validate ( {
72+ token : token . token ,
73+ } ) ;
74+ assertEquals ( token . id , validateRes . token . id ) ;
75+
76+ // Extend token expiration by 10 seconds
77+ await ctx . modules . tokens . extend ( {
78+ token : token . token ,
79+ newExpiration : new Date ( Date . now ( ) + 10000 ) . toISOString ( ) ,
80+ } ) ;
81+
82+ // Wait for 0.5 seconds to ensure token WOULD HAVE expired without
83+ // extension.
84+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
85+
86+ // Token should STILL be valid, and have a different `expireAt` time
87+ const validateResAfterWait = await ctx . modules . tokens . validate ( {
88+ token : token . token ,
89+ } ) ;
90+
91+ // Assert that everything except `expireAt` is the same and `expireAt`
92+ // is greater.
93+ assertGreater ( validateResAfterWait . token . expireAt , token . expireAt ) ;
94+ assertEquals ( {
95+ ...validateResAfterWait . token ,
96+ expireAt : null ,
97+ } , {
98+ ...token ,
99+ expireAt : null ,
100+ } )
56101 } ,
57102) ;
0 commit comments