@@ -392,6 +392,49 @@ func TestCheckSessionGroupPredicate(t *testing.T) {
392392 require .True (t , ok )
393393}
394394
395+ // TestStateShift tests that the ShiftState method works as expected.
396+ func TestStateShift (t * testing.T ) {
397+ // Set up a new DB.
398+ clock := clock .NewTestClock (testTime )
399+ db , err := NewDB (t .TempDir (), "test.db" , clock )
400+ require .NoError (t , err )
401+ t .Cleanup (func () {
402+ _ = db .Close ()
403+ })
404+
405+ // Add a new session to the DB.
406+ s1 := newSession (t , db , clock , "label 1" )
407+ require .NoError (t , db .CreateSession (s1 ))
408+
409+ // Check that the session is in the StateCreated state. Also check that
410+ // the "RevokedAt" time has not yet been set.
411+ s1 , err = db .GetSession (s1 .LocalPublicKey )
412+ require .NoError (t , err )
413+ require .Equal (t , StateCreated , s1 .State )
414+ require .Equal (t , time.Time {}, s1 .RevokedAt )
415+
416+ // Shift the state of the session to StateRevoked.
417+ err = db .ShiftState (s1 .ID , StateRevoked )
418+ require .NoError (t , err )
419+
420+ // This should have worked. Since it is now in a terminal state, the
421+ // "RevokedAt" time should be set.
422+ s1 , err = db .GetSession (s1 .LocalPublicKey )
423+ require .NoError (t , err )
424+ require .Equal (t , StateRevoked , s1 .State )
425+ require .True (t , clock .Now ().Equal (s1 .RevokedAt ))
426+
427+ // Trying to do the same state shift again should succeed since the
428+ // session is already in the expected "dest" state.
429+ err = db .ShiftState (s1 .ID , StateRevoked )
430+ require .NoError (t , err )
431+
432+ // Trying to shift the state from a terminal state back to StateCreated
433+ // should also fail since this is not a legal state transition.
434+ err = db .ShiftState (s1 .ID , StateCreated )
435+ require .ErrorContains (t , err , "illegal session state transition" )
436+ }
437+
395438// testSessionModifier is a functional option that can be used to modify the
396439// default test session created by newSession.
397440type testSessionModifier func (* Session )
0 commit comments