mirror of
https://github.com/ddworken/hishtory.git
synced 2025-08-23 04:32:21 +02:00
Add support for limiting the number of registrations to fix #46
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -521,6 +522,27 @@ func TestHealthcheck(t *testing.T) {
|
||||
assertNoLeakedConnections(t, GLOBAL_DB)
|
||||
}
|
||||
|
||||
func TestLimitRegistrations(t *testing.T) {
|
||||
// Set up
|
||||
InitDB()
|
||||
defer testutils.BackupAndRestoreEnv("HISHTORY_MAX_NUM_USERS")()
|
||||
os.Setenv("HISHTORY_MAX_NUM_USERS", "2")
|
||||
|
||||
// Register three devices across two users
|
||||
deviceReq := httptest.NewRequest(http.MethodGet, "/?device_id="+uuid.Must(uuid.NewRandom()).String()+"&user_id="+data.UserId("user1"), nil)
|
||||
apiRegisterHandler(context.Background(), nil, deviceReq)
|
||||
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+uuid.Must(uuid.NewRandom()).String()+"&user_id="+data.UserId("user1"), nil)
|
||||
apiRegisterHandler(context.Background(), nil, deviceReq)
|
||||
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+uuid.Must(uuid.NewRandom()).String()+"&user_id="+data.UserId("user2"), nil)
|
||||
apiRegisterHandler(context.Background(), nil, deviceReq)
|
||||
|
||||
// And this next one should fail since it is a new user
|
||||
defer func() { _ = recover() }()
|
||||
deviceReq = httptest.NewRequest(http.MethodGet, "/?device_id="+uuid.Must(uuid.NewRandom()).String()+"&user_id="+data.UserId("user3"), nil)
|
||||
apiRegisterHandler(context.Background(), nil, deviceReq)
|
||||
t.Errorf("expected panic")
|
||||
}
|
||||
|
||||
func assertNoLeakedConnections(t *testing.T, db *gorm.DB) {
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user