Browse Source

Merge branch 'test-20240203' of tmp/arozos into master

I dont oauth, so I will just let it merge :D
TC 1 year ago
parent
commit
81671fd4ec

+ 1 - 0
mod/auth/authlogger/authlogger_test.go

@@ -19,6 +19,7 @@ func setupSuite(t *testing.T) func(t *testing.T) {
 	return func(t *testing.T) {
 	return func(t *testing.T) {
 		t.Log("Cleaning up")
 		t.Log("Cleaning up")
 		err := os.RemoveAll(filePath)
 		err := os.RemoveAll(filePath)
+		os.RemoveAll("./system/")
 		if err != nil {
 		if err != nil {
 			t.Fatalf("Failed to clean up: %v", err)
 			t.Fatalf("Failed to clean up: %v", err)
 		}
 		}

+ 142 - 0
mod/auth/explogin/explogin_test.go

@@ -0,0 +1,142 @@
+package explogin
+
+import (
+	"net/http"
+	"testing"
+)
+
+func TestAllowImmediateAccess_FirstAttempt(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	allowed, _ := handler.AllowImmediateAccess(username, request)
+
+	if !allowed {
+		t.Error("Access should be allowed for the first attempt")
+	}
+}
+
+func TestAllowImmediateAccess_LimitExceeded(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	// Set the retry count to a value exceeding the limit
+	handler.AddUserRetrycount(username, request)
+	handler.AddUserRetrycount(username, request)
+	handler.AddUserRetrycount(username, request)
+
+	allowed, _ := handler.AllowImmediateAccess(username, request)
+
+	if allowed {
+		t.Error("Access should be denied when retry count exceeds the limit")
+	}
+}
+
+func TestAddUserRetrycount(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	handler.AddUserRetrycount(username, request)
+
+	entry, exists := handler.LoginRecord.Load(username + "/0.0.0.0")
+
+	if !exists {
+		t.Error("User entry should exist after failed login attempt")
+	}
+
+	loginEntry := entry.(*UserLoginEntry)
+
+	if loginEntry.RetryCount != 1 {
+		t.Errorf("Retry count should be 1, got %d", loginEntry.RetryCount)
+	}
+}
+
+func TestResetUserRetryCount(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	handler.AddUserRetrycount(username, request)
+	handler.ResetUserRetryCount(username, request)
+
+	_, exists := handler.LoginRecord.Load(username + "/0.0.0.0")
+
+	if exists {
+		t.Error("User entry should be reset after successful login")
+	}
+}
+
+func TestResetAllUserRetryCounter(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username1 := "testuser1"
+	username2 := "testuser2"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	handler.AddUserRetrycount(username1, request)
+	handler.AddUserRetrycount(username2, request)
+
+	handler.ResetAllUserRetryCounter()
+
+	_, exists1 := handler.LoginRecord.Load(username1 + "/0.0.0.0")
+	_, exists2 := handler.LoginRecord.Load(username2 + "/0.0.0.0")
+
+	if exists1 || exists2 {
+		t.Error("All user entries should be reset")
+	}
+}
+
+func TestGetDelayTimeFromRetryCount(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+
+	// Test with different retry counts
+	tests := []struct {
+		retryCount int
+		expected   int64
+	}{
+		{1, 2},
+		{2, 3},
+		{3, 5},
+		{4, 9},
+		{5, 10},
+		{6, 10}, // Exceeds the DelayCeiling
+	}
+
+	for _, test := range tests {
+		result := handler.getDelayTimeFromRetryCount(test.retryCount)
+
+		if result != test.expected {
+			t.Errorf("For RetryCount %d, expected delay %d, got %d", test.retryCount, test.expected, result)
+		}
+	}
+}
+
+func TestAllowImmediateAccess_DeniedUntilNextRetry(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	// Deny access and retrieve the remaining time until the next retry
+	handler.AddUserRetrycount(username, request)
+	handler.AddUserRetrycount(username, request)
+	handler.AddUserRetrycount(username, request)
+	allowed, remainingTime := handler.AllowImmediateAccess(username, request)
+	//t.Log(allowed, remainingTime)
+	if allowed || remainingTime == 0 {
+		t.Error("Access should be denied, and remaining time should be greater than 0")
+	}
+}
+
+func TestAllowImmediateAccess_IPNotFound(t *testing.T) {
+	handler := NewExponentialLoginHandler(2, 10)
+	username := "testuser"
+	request, _ := http.NewRequest("GET", "/", nil)
+
+	allowed, _ := handler.AllowImmediateAccess(username, request)
+
+	if !allowed {
+		t.Error("Access should be allowed even if IP information is not found")
+	}
+}

+ 95 - 0
mod/auth/ldap/syncdb/syncdb_test.go

@@ -0,0 +1,95 @@
+package syncdb
+
+import (
+	"testing"
+)
+
+func TestSyncDB_Store(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Read the value from the SyncDB
+	result := syncDB.Read(uuid)
+
+	// Verify that the value is stored and retrieved correctly
+	if result != "TestValue" {
+		t.Errorf("Expected 'TestValue', got %s", result)
+	}
+}
+
+func TestSyncDB_Read(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Read the value from the SyncDB
+	result := syncDB.Read(uuid)
+
+	// Verify that the value is stored and retrieved correctly
+	if result != "TestValue" {
+		t.Errorf("Expected 'TestValue', got %s", result)
+	}
+
+	// Try to read a non-existent UUID
+	nonExistentResult := syncDB.Read("NonExistentUUID")
+
+	// Verify that the result is empty for non-existent UUID
+	if nonExistentResult != "" {
+		t.Errorf("Expected empty result for non-existent UUID, got %s", nonExistentResult)
+	}
+}
+
+func TestSyncDB_Delete(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Delete the stored value
+	syncDB.Delete(uuid)
+
+	// Try to read the deleted value
+	deletedResult := syncDB.Read(uuid)
+
+	// Verify that the result is empty for the deleted UUID
+	if deletedResult != "" {
+		t.Errorf("Expected empty result for deleted UUID, got %s", deletedResult)
+	}
+}
+
+/*
+func TestSyncDB_AutoCleaning(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Wait for auto-cleaning routine to run
+	time.Sleep(6 * time.Minute)
+
+	// Try to read the cleaned value
+	cleanedResult := syncDB.Read(uuid)
+
+	// Verify that the result is empty for the cleaned UUID
+	if cleanedResult != "" {
+		t.Errorf("Expected empty result for cleaned UUID, got %s", cleanedResult)
+	}
+}
+*/
+
+func TestSyncDB_ToString(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store some values in the SyncDB
+	syncDB.Store("Value1")
+	syncDB.Store("Value2")
+
+	// Display the contents of the SyncDB
+	syncDB.ToString()
+
+	// Verify that the values are displayed correctly
+	// This should be manually inspected as it prints to stdout
+}

+ 95 - 0
mod/auth/oauth2/syncdb/syncdb_test.go

@@ -0,0 +1,95 @@
+package syncdb
+
+import (
+	"testing"
+)
+
+func TestSyncDB_Store(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Read the value from the SyncDB
+	result := syncDB.Read(uuid)
+
+	// Verify that the value is stored and retrieved correctly
+	if result != "TestValue" {
+		t.Errorf("Expected 'TestValue', got %s", result)
+	}
+}
+
+func TestSyncDB_Read(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Read the value from the SyncDB
+	result := syncDB.Read(uuid)
+
+	// Verify that the value is stored and retrieved correctly
+	if result != "TestValue" {
+		t.Errorf("Expected 'TestValue', got %s", result)
+	}
+
+	// Try to read a non-existent UUID
+	nonExistentResult := syncDB.Read("NonExistentUUID")
+
+	// Verify that the result is empty for non-existent UUID
+	if nonExistentResult != "" {
+		t.Errorf("Expected empty result for non-existent UUID, got %s", nonExistentResult)
+	}
+}
+
+func TestSyncDB_Delete(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Delete the stored value
+	syncDB.Delete(uuid)
+
+	// Try to read the deleted value
+	deletedResult := syncDB.Read(uuid)
+
+	// Verify that the result is empty for the deleted UUID
+	if deletedResult != "" {
+		t.Errorf("Expected empty result for deleted UUID, got %s", deletedResult)
+	}
+}
+
+/*
+func TestSyncDB_AutoCleaning(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store a value in the SyncDB
+	uuid := syncDB.Store("TestValue")
+
+	// Wait for auto-cleaning routine to run
+	time.Sleep(6 * time.Minute)
+
+	// Try to read the cleaned value
+	cleanedResult := syncDB.Read(uuid)
+
+	// Verify that the result is empty for the cleaned UUID
+	if cleanedResult != "" {
+		t.Errorf("Expected empty result for cleaned UUID, got %s", cleanedResult)
+	}
+}
+*/
+
+func TestSyncDB_ToString(t *testing.T) {
+	syncDB := NewSyncDB()
+
+	// Store some values in the SyncDB
+	syncDB.Store("Value1")
+	syncDB.Store("Value2")
+
+	// Display the contents of the SyncDB
+	syncDB.ToString()
+
+	// Verify that the values are displayed correctly
+	// This should be manually inspected as it prints to stdout
+}