@@ -224,3 +224,41 @@ func TestAPIKey_Deleted(t *testing.T) {
224224 require .ErrorAs (t , err , & apiErr )
225225 require .Equal (t , http .StatusBadRequest , apiErr .StatusCode ())
226226}
227+
228+ func TestAPIKey_Refresh (t * testing.T ) {
229+ t .Parallel ()
230+
231+ db , pubsub := dbtestutil .NewDB (t )
232+ client := coderdtest .New (t , & coderdtest.Options {
233+ Database : db ,
234+ Pubsub : pubsub ,
235+ })
236+ owner := coderdtest .CreateFirstUser (t , client )
237+
238+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
239+ defer cancel ()
240+
241+ token , err := client .CreateAPIKey (ctx , owner .UserID .String ())
242+ require .NoError (t , err )
243+ split := strings .Split (token .Key , "-" )
244+ apiKey1 , err := client .APIKeyByID (ctx , owner .UserID .String (), split [0 ])
245+ require .NoError (t , err )
246+ require .Equal (t , int64 (86400 ), apiKey1 .LifetimeSeconds , "default should be 24 hours" )
247+
248+ err = db .UpdateAPIKeyByID (ctx , database.UpdateAPIKeyByIDParams {
249+ ID : apiKey1 .ID ,
250+ LastUsed : apiKey1 .LastUsed ,
251+ // Cross the no-refresh threshold
252+ ExpiresAt : apiKey1 .ExpiresAt .Add (time .Hour * - 2 ),
253+ })
254+ require .NoError (t , err , "update login key" )
255+
256+ // Refresh the token
257+ client .SetSessionToken (token .Key )
258+ _ , err = client .User (ctx , codersdk .Me )
259+ require .NoError (t , err )
260+
261+ apiKey2 , err := client .APIKeyByID (ctx , owner .UserID .String (), split [0 ])
262+ require .NoError (t , err )
263+ require .True (t , apiKey2 .ExpiresAt .After (apiKey1 .ExpiresAt ), "token should have a later expiry" )
264+ }
0 commit comments