mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 12:45:47 +00:00
198 lines
5.4 KiB
Go
198 lines
5.4 KiB
Go
package utils_test
|
|
|
|
import (
|
|
"testing"
|
|
"tinyauth/internal/config"
|
|
"tinyauth/internal/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestGetRootDomain(t *testing.T) {
|
|
// Normal case
|
|
domain := "http://sub.example.com"
|
|
expected := "example.com"
|
|
result, err := utils.GetRootDomain(domain)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Domain with multiple subdomains
|
|
domain = "http://b.c.example.com"
|
|
expected = "c.example.com"
|
|
result, err = utils.GetRootDomain(domain)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Domain with no subdomain
|
|
domain = "http://example.com"
|
|
expected = "example.com"
|
|
_, err = utils.GetRootDomain(domain)
|
|
assert.Error(t, err, "invalid domain, must be at least second level domain")
|
|
|
|
// Invalid domain (only TLD)
|
|
domain = "com"
|
|
_, err = utils.GetRootDomain(domain)
|
|
assert.ErrorContains(t, err, "invalid domain")
|
|
|
|
// IP address
|
|
domain = "http://10.10.10.10"
|
|
_, err = utils.GetRootDomain(domain)
|
|
assert.ErrorContains(t, err, "IP addresses are not allowed")
|
|
|
|
// Invalid URL
|
|
domain = "http://[::1]:namedport"
|
|
_, err = utils.GetRootDomain(domain)
|
|
assert.ErrorContains(t, err, "parse \"http://[::1]:namedport\": invalid port \":namedport\" after host")
|
|
|
|
// URL with scheme and path
|
|
domain = "https://sub.example.com/path"
|
|
expected = "example.com"
|
|
result, err = utils.GetRootDomain(domain)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// URL with port
|
|
domain = "http://sub.example.com:8080"
|
|
expected = "example.com"
|
|
result, err = utils.GetRootDomain(domain)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestParseFileToLine(t *testing.T) {
|
|
// Normal case
|
|
content := "user1\nuser2\nuser3"
|
|
expected := "user1,user2,user3"
|
|
result := utils.ParseFileToLine(content)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Case with empty lines and spaces
|
|
content = " user1 \n\n user2 \n user3 \n"
|
|
expected = "user1,user2,user3"
|
|
result = utils.ParseFileToLine(content)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Case with only empty lines
|
|
content = "\n\n\n"
|
|
expected = ""
|
|
result = utils.ParseFileToLine(content)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Case with single user
|
|
content = "singleuser"
|
|
expected = "singleuser"
|
|
result = utils.ParseFileToLine(content)
|
|
assert.Equal(t, expected, result)
|
|
|
|
// Case with trailing newline
|
|
content = "user1\nuser2\n"
|
|
expected = "user1,user2"
|
|
result = utils.ParseFileToLine(content)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
|
|
func TestFilter(t *testing.T) {
|
|
// Normal case
|
|
slice := []int{1, 2, 3, 4, 5}
|
|
testFunc := func(n int) bool { return n%2 == 0 }
|
|
expected := []int{2, 4}
|
|
result := utils.Filter(slice, testFunc)
|
|
assert.DeepEqual(t, expected, result)
|
|
|
|
// Case with no matches
|
|
slice = []int{1, 3, 5}
|
|
testFunc = func(n int) bool { return n%2 == 0 }
|
|
expected = []int{}
|
|
result = utils.Filter(slice, testFunc)
|
|
assert.DeepEqual(t, expected, result)
|
|
|
|
// Case with all matches
|
|
slice = []int{2, 4, 6}
|
|
testFunc = func(n int) bool { return n%2 == 0 }
|
|
expected = []int{2, 4, 6}
|
|
result = utils.Filter(slice, testFunc)
|
|
assert.DeepEqual(t, expected, result)
|
|
|
|
// Case with empty slice
|
|
slice = []int{}
|
|
testFunc = func(n int) bool { return n%2 == 0 }
|
|
expected = []int{}
|
|
result = utils.Filter(slice, testFunc)
|
|
assert.DeepEqual(t, expected, result)
|
|
|
|
// Case with different type (string)
|
|
sliceStr := []string{"apple", "banana", "cherry"}
|
|
testFuncStr := func(s string) bool { return len(s) > 5 }
|
|
expectedStr := []string{"banana", "cherry"}
|
|
resultStr := utils.Filter(sliceStr, testFuncStr)
|
|
assert.DeepEqual(t, expectedStr, resultStr)
|
|
}
|
|
|
|
func TestGetContext(t *testing.T) {
|
|
// Setup
|
|
gin.SetMode(gin.TestMode)
|
|
c, _ := gin.CreateTestContext(nil)
|
|
|
|
// Normal case
|
|
c.Set("context", &config.UserContext{Username: "testuser"})
|
|
result, err := utils.GetContext(c)
|
|
assert.NilError(t, err)
|
|
assert.Equal(t, "testuser", result.Username)
|
|
|
|
// Case with no context
|
|
c.Set("context", nil)
|
|
_, err = utils.GetContext(c)
|
|
assert.Error(t, err, "invalid user context in request")
|
|
|
|
// Case with invalid context type
|
|
c.Set("context", "invalid type")
|
|
_, err = utils.GetContext(c)
|
|
assert.Error(t, err, "invalid user context in request")
|
|
}
|
|
|
|
func TestIsRedirectSafe(t *testing.T) {
|
|
// Setup
|
|
domain := "example.com"
|
|
|
|
// Case with no subdomain
|
|
redirectURL := "http://example.com/welcome"
|
|
result := utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, false, result)
|
|
|
|
// Case with different domain
|
|
redirectURL = "http://malicious.com/phishing"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, false, result)
|
|
|
|
// Case with subdomain
|
|
redirectURL = "http://sub.example.com/page"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, true, result)
|
|
|
|
// Case with empty redirect URL
|
|
redirectURL = ""
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, false, result)
|
|
|
|
// Case with invalid URL
|
|
redirectURL = "http://[::1]:namedport"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, false, result)
|
|
|
|
// Case with URL having port
|
|
redirectURL = "http://sub.example.com:8080/page"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, true, result)
|
|
|
|
// Case with URL having different subdomain
|
|
redirectURL = "http://another.example.com/page"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, true, result)
|
|
|
|
// Case with URL having different TLD
|
|
redirectURL = "http://example.org/page"
|
|
result = utils.IsRedirectSafe(redirectURL, domain)
|
|
assert.Equal(t, false, result)
|
|
}
|