-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from COS301-SE-2024/fix/backend/logout-session…
…-management chore: Add some more edge cases to session management for users login and logout sessions
- Loading branch information
Showing
9 changed files
with
291 additions
and
17 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,13 +18,13 @@ func TestGenerateToken(t *testing.T) { | |
t.Fatal("Error loading .env file: ", err) | ||
} | ||
|
||
email := "test@example.com" | ||
email := "test1@example.com" | ||
role := constants.Admin | ||
tokenString, expirationTime, err := authenticator.GenerateToken(email, role) | ||
|
||
require.NoError(t, err) | ||
require.NotEmpty(t, tokenString) | ||
require.WithinDuration(t, time.Now().Add(5*time.Minute), expirationTime, time.Second) | ||
require.WithinDuration(t, time.Now().Add(24*7*time.Hour), expirationTime, time.Second) | ||
|
||
// Validate the token | ||
claims, err := authenticator.ValidateToken(tokenString) | ||
|
@@ -40,7 +40,7 @@ func TestValidateToken(t *testing.T) { | |
t.Fatal("Error loading .env file: ", err) | ||
} | ||
|
||
email := "test@example.com" | ||
email := "test2@example.com" | ||
role := constants.Admin | ||
tokenString, _, err := authenticator.GenerateToken(email, role) | ||
|
||
|
@@ -60,3 +60,26 @@ func TestValidateToken(t *testing.T) { | |
require.Error(t, err) | ||
assert.Nil(t, claims) | ||
} | ||
|
||
func TestValidateTokenExpired(t *testing.T) { | ||
// Load environment variables from .env file | ||
if err := godotenv.Load("../.env"); err != nil { | ||
t.Fatal("Error loading .env file: ", err) | ||
} | ||
|
||
email := "[email protected]" | ||
role := constants.Admin | ||
|
||
// Generate a token that expires in 1 second | ||
tokenString, _, err := authenticator.GenerateToken(email, role, 1*time.Second) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, tokenString) | ||
|
||
// Wait for the token to expire | ||
time.Sleep(2 * time.Second) | ||
|
||
// Validate the token | ||
claims, err := authenticator.ValidateToken(tokenString) | ||
require.Error(t, err) | ||
assert.Nil(t, claims) | ||
} |
Oops, something went wrong.