-
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 #338 from COS301-SE-2024/feat/backend/block-login-…
…spamming Feat/backend/block login spamming
- Loading branch information
Showing
6 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
|
@@ -52,6 +52,15 @@ func TestImageKey(t *testing.T) { | |
} | ||
} | ||
|
||
func TestLoginKey(t *testing.T) { | ||
email := "[email protected]" | ||
expected := "Login:[email protected]" | ||
result := cache.LoginKey(email) | ||
if result != expected { | ||
t.Errorf("LoginKey(%s) = %s; want %s", email, result, expected) | ||
} | ||
} | ||
|
||
func TestGetUser(t *testing.T) { | ||
// Test case: cache is nil | ||
t.Run("cache is nil", func(t *testing.T) { | ||
|
@@ -1080,3 +1089,124 @@ func TestDeleteSession(t *testing.T) { | |
} | ||
}) | ||
} | ||
|
||
func TestCanMakeLogin(t *testing.T) { | ||
email := "[email protected]" | ||
key := cache.LoginKey(email) | ||
|
||
t.Run("cache not found", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, _ := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
appsession.Cache = nil | ||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.False(t, canLogin) | ||
assert.EqualError(t, err, "cache not found") | ||
}) | ||
|
||
t.Run("new user - set value", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, mock := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
// Simulate Get returning a nil value (key not found) | ||
mock.ExpectGet(key).RedisNil() | ||
// Simulate successful Set operation | ||
mock.ExpectSet(key, 1, 2*time.Second).SetVal("OK") | ||
|
||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.True(t, canLogin) | ||
assert.NoError(t, err) | ||
|
||
// Ensure all expectations were met | ||
err = mock.ExpectationsWereMet() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("existing user with login count less than 5", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, mock := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
// Simulate Get returning a value of 3 | ||
mock.ExpectGet(key).SetVal("3") | ||
// Simulate successful Set operation to update the value to 4 | ||
mock.ExpectSet(key, 4, 2*time.Second).SetVal("OK") | ||
|
||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.True(t, canLogin) | ||
assert.NoError(t, err) | ||
|
||
// Ensure all expectations were met | ||
err = mock.ExpectationsWereMet() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("existing user with login count 5 or more", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, mock := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
// Simulate Get returning a value of 5 | ||
mock.ExpectGet(key).SetVal("5") | ||
|
||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.False(t, canLogin) | ||
assert.NoError(t, err) | ||
|
||
// Ensure all expectations were met | ||
err = mock.ExpectationsWereMet() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("error on Get", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, mock := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
// Simulate Get operation error | ||
mock.ExpectGet(key).SetErr(errors.New("redis get error")) | ||
// Simulate successful Set operation | ||
mock.ExpectSet(key, 1, 2*time.Second).SetVal("OK") | ||
|
||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.True(t, canLogin) | ||
assert.NoError(t, err) | ||
|
||
// Ensure all expectations were met | ||
err = mock.ExpectationsWereMet() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("error on Set", func(t *testing.T) { | ||
// Set up a mock Redis client | ||
db, mock := redismock.NewClientMock() | ||
|
||
appsession := &models.AppSession{ | ||
Cache: db, | ||
} | ||
// Simulate Get returning a value of 3 | ||
mock.ExpectGet(key).SetVal("3") | ||
// Simulate Set operation error | ||
mock.ExpectSet(key, 4, 2*time.Second).SetErr(errors.New("redis set error")) | ||
|
||
canLogin, err := cache.CanMakeLogin(appsession, email) | ||
assert.False(t, canLogin) | ||
assert.EqualError(t, err, "redis set error") | ||
|
||
// Ensure all expectations were met | ||
err = mock.ExpectationsWereMet() | ||
assert.NoError(t, err) | ||
}) | ||
} |