From 7134b6f8757330a574d87940112099c4f9129294 Mon Sep 17 00:00:00 2001 From: Bram Vanbilsen Date: Mon, 5 Feb 2024 11:58:09 -0600 Subject: [PATCH] Moved hashed token test to better location --- data_test.go | 90 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/data_test.go b/data_test.go index ed48d41..2aed85c 100644 --- a/data_test.go +++ b/data_test.go @@ -198,6 +198,50 @@ func TestSessionManager_Load(T *testing.T) { t.Error("returned context is unexpectedly nil") } }) + + T.Run("with token hashing", func(t *testing.T) { + s := New() + s.HashTokenInStore = true + s.IdleTimeout = time.Hour * 24 + + expectedToken := "example" + expectedExpiry := time.Now().Add(time.Hour) + + initialCtx := context.WithValue(context.Background(), s.contextKey, &sessionData{ + deadline: expectedExpiry, + token: expectedToken, + values: map[string]interface{}{ + "blah": "blah", + }, + mu: sync.Mutex{}, + }) + + actualToken, actualExpiry, err := s.Commit(initialCtx) + if expectedToken != actualToken { + t.Errorf("expected token to equal %q, but received %q", expectedToken, actualToken) + } + if expectedExpiry != actualExpiry { + t.Errorf("expected expiry to equal %v, but received %v", expectedExpiry, actualExpiry) + } + if err != nil { + t.Errorf("unexpected error returned: %v", err) + } + + retrievedCtx, err := s.Load(context.Background(), expectedToken) + if err != nil { + t.Errorf("unexpected error returned: %v", err) + } + retrievedSessionData, ok := retrievedCtx.Value(s.contextKey).(*sessionData) + if !ok { + t.Errorf("unexpected data in retrieved context") + } else if retrievedSessionData.token != expectedToken { + t.Errorf("expected token in context's session data data to equal %v, but received %v", expectedToken, retrievedSessionData.token) + } + + if err := s.Destroy(retrievedCtx); err != nil { + t.Errorf("unexpected error returned: %v", err) + } + }) } func TestSessionManager_Commit(T *testing.T) { @@ -351,52 +395,6 @@ func TestSessionManager_Commit(T *testing.T) { }) } -func TestTokenHashing(T *testing.T) { - T.Run("with token hashing", func(t *testing.T) { - s := New() - s.HashTokenInStore = true - s.IdleTimeout = time.Hour * 24 - - expectedToken := "example" - expectedExpiry := time.Now().Add(time.Hour) - - initialCtx := context.WithValue(context.Background(), s.contextKey, &sessionData{ - deadline: expectedExpiry, - token: expectedToken, - values: map[string]interface{}{ - "blah": "blah", - }, - mu: sync.Mutex{}, - }) - - actualToken, actualExpiry, err := s.Commit(initialCtx) - if expectedToken != actualToken { - t.Errorf("expected token to equal %q, but received %q", expectedToken, actualToken) - } - if expectedExpiry != actualExpiry { - t.Errorf("expected expiry to equal %v, but received %v", expectedExpiry, actualExpiry) - } - if err != nil { - t.Errorf("unexpected error returned: %v", err) - } - - retrievedCtx, err := s.Load(context.Background(), expectedToken) - if err != nil { - t.Errorf("unexpected error returned: %v", err) - } - retrievedSessionData, ok := retrievedCtx.Value(s.contextKey).(*sessionData) - if !ok { - t.Errorf("unexpected data in retrieved context") - } else if retrievedSessionData.token != expectedToken { - t.Errorf("expected token in context's session data data to equal %v, but received %v", expectedToken, retrievedSessionData.token) - } - - if err := s.Destroy(retrievedCtx); err != nil { - t.Errorf("unexpected error returned: %v", err) - } - }) -} - func TestPut(t *testing.T) { t.Parallel()