|
| 1 | +package utils_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "tinyauth/internal/config" |
| 6 | + "tinyauth/internal/utils" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "gotest.tools/v3/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetRootDomain(t *testing.T) { |
| 13 | + // Normal case |
| 14 | + domain := "http://sub.example.com" |
| 15 | + expected := "example.com" |
| 16 | + result, err := utils.GetRootDomain(domain) |
| 17 | + assert.NilError(t, err) |
| 18 | + assert.Equal(t, expected, result) |
| 19 | + |
| 20 | + // Domain with multiple subdomains |
| 21 | + domain = "http://b.c.example.com" |
| 22 | + expected = "c.example.com" |
| 23 | + result, err = utils.GetRootDomain(domain) |
| 24 | + assert.NilError(t, err) |
| 25 | + assert.Equal(t, expected, result) |
| 26 | + |
| 27 | + // Domain with no subdomain |
| 28 | + domain = "http://example.com" |
| 29 | + expected = "example.com" |
| 30 | + _, err = utils.GetRootDomain(domain) |
| 31 | + assert.Error(t, err, "invalid domain, must be at least second level domain") |
| 32 | + |
| 33 | + // Invalid domain (only TLD) |
| 34 | + domain = "com" |
| 35 | + _, err = utils.GetRootDomain(domain) |
| 36 | + assert.ErrorContains(t, err, "invalid domain") |
| 37 | + |
| 38 | + // IP address |
| 39 | + domain = "http://10.10.10.10" |
| 40 | + _, err = utils.GetRootDomain(domain) |
| 41 | + assert.ErrorContains(t, err, "IP addresses are not allowed") |
| 42 | + |
| 43 | + // Invalid URL |
| 44 | + domain = "http://[::1]:namedport" |
| 45 | + _, err = utils.GetRootDomain(domain) |
| 46 | + assert.ErrorContains(t, err, "parse \"http://[::1]:namedport\": invalid port \":namedport\" after host") |
| 47 | + |
| 48 | + // URL with scheme and path |
| 49 | + domain = "https://sub.example.com/path" |
| 50 | + expected = "example.com" |
| 51 | + result, err = utils.GetRootDomain(domain) |
| 52 | + assert.NilError(t, err) |
| 53 | + assert.Equal(t, expected, result) |
| 54 | + |
| 55 | + // URL with port |
| 56 | + domain = "http://sub.example.com:8080" |
| 57 | + expected = "example.com" |
| 58 | + result, err = utils.GetRootDomain(domain) |
| 59 | + assert.NilError(t, err) |
| 60 | + assert.Equal(t, expected, result) |
| 61 | +} |
| 62 | + |
| 63 | +func TestParseFileToLine(t *testing.T) { |
| 64 | + // Normal case |
| 65 | + content := "user1\nuser2\nuser3" |
| 66 | + expected := "user1,user2,user3" |
| 67 | + result := utils.ParseFileToLine(content) |
| 68 | + assert.Equal(t, expected, result) |
| 69 | + |
| 70 | + // Case with empty lines and spaces |
| 71 | + content = " user1 \n\n user2 \n user3 \n" |
| 72 | + expected = "user1,user2,user3" |
| 73 | + result = utils.ParseFileToLine(content) |
| 74 | + assert.Equal(t, expected, result) |
| 75 | + |
| 76 | + // Case with only empty lines |
| 77 | + content = "\n\n\n" |
| 78 | + expected = "" |
| 79 | + result = utils.ParseFileToLine(content) |
| 80 | + assert.Equal(t, expected, result) |
| 81 | + |
| 82 | + // Case with single user |
| 83 | + content = "singleuser" |
| 84 | + expected = "singleuser" |
| 85 | + result = utils.ParseFileToLine(content) |
| 86 | + assert.Equal(t, expected, result) |
| 87 | + |
| 88 | + // Case with trailing newline |
| 89 | + content = "user1\nuser2\n" |
| 90 | + expected = "user1,user2" |
| 91 | + result = utils.ParseFileToLine(content) |
| 92 | + assert.Equal(t, expected, result) |
| 93 | +} |
| 94 | + |
| 95 | +func TestFilter(t *testing.T) { |
| 96 | + // Normal case |
| 97 | + slice := []int{1, 2, 3, 4, 5} |
| 98 | + testFunc := func(n int) bool { return n%2 == 0 } |
| 99 | + expected := []int{2, 4} |
| 100 | + result := utils.Filter(slice, testFunc) |
| 101 | + assert.DeepEqual(t, expected, result) |
| 102 | + |
| 103 | + // Case with no matches |
| 104 | + slice = []int{1, 3, 5} |
| 105 | + testFunc = func(n int) bool { return n%2 == 0 } |
| 106 | + expected = []int{} |
| 107 | + result = utils.Filter(slice, testFunc) |
| 108 | + assert.DeepEqual(t, expected, result) |
| 109 | + |
| 110 | + // Case with all matches |
| 111 | + slice = []int{2, 4, 6} |
| 112 | + testFunc = func(n int) bool { return n%2 == 0 } |
| 113 | + expected = []int{2, 4, 6} |
| 114 | + result = utils.Filter(slice, testFunc) |
| 115 | + assert.DeepEqual(t, expected, result) |
| 116 | + |
| 117 | + // Case with empty slice |
| 118 | + slice = []int{} |
| 119 | + testFunc = func(n int) bool { return n%2 == 0 } |
| 120 | + expected = []int{} |
| 121 | + result = utils.Filter(slice, testFunc) |
| 122 | + assert.DeepEqual(t, expected, result) |
| 123 | + |
| 124 | + // Case with different type (string) |
| 125 | + sliceStr := []string{"apple", "banana", "cherry"} |
| 126 | + testFuncStr := func(s string) bool { return len(s) > 5 } |
| 127 | + expectedStr := []string{"banana", "cherry"} |
| 128 | + resultStr := utils.Filter(sliceStr, testFuncStr) |
| 129 | + assert.DeepEqual(t, expectedStr, resultStr) |
| 130 | +} |
| 131 | + |
| 132 | +func TestGetContext(t *testing.T) { |
| 133 | + // Setup |
| 134 | + gin.SetMode(gin.TestMode) |
| 135 | + c, _ := gin.CreateTestContext(nil) |
| 136 | + |
| 137 | + // Normal case |
| 138 | + c.Set("context", &config.UserContext{Username: "testuser"}) |
| 139 | + result, err := utils.GetContext(c) |
| 140 | + assert.NilError(t, err) |
| 141 | + assert.Equal(t, "testuser", result.Username) |
| 142 | + |
| 143 | + // Case with no context |
| 144 | + c.Set("context", nil) |
| 145 | + _, err = utils.GetContext(c) |
| 146 | + assert.Error(t, err, "invalid user context in request") |
| 147 | + |
| 148 | + // Case with invalid context type |
| 149 | + c.Set("context", "invalid type") |
| 150 | + _, err = utils.GetContext(c) |
| 151 | + assert.Error(t, err, "invalid user context in request") |
| 152 | +} |
| 153 | + |
| 154 | +func TestIsRedirectSafe(t *testing.T) { |
| 155 | + // Setup |
| 156 | + domain := "example.com" |
| 157 | + |
| 158 | + // Case with no subdomain |
| 159 | + redirectURL := "http://example.com/welcome" |
| 160 | + result := utils.IsRedirectSafe(redirectURL, domain) |
| 161 | + assert.Equal(t, false, result) |
| 162 | + |
| 163 | + // Case with different domain |
| 164 | + redirectURL = "http://malicious.com/phishing" |
| 165 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 166 | + assert.Equal(t, false, result) |
| 167 | + |
| 168 | + // Case with subdomain |
| 169 | + redirectURL = "http://sub.example.com/page" |
| 170 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 171 | + assert.Equal(t, true, result) |
| 172 | + |
| 173 | + // Case with empty redirect URL |
| 174 | + redirectURL = "" |
| 175 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 176 | + assert.Equal(t, false, result) |
| 177 | + |
| 178 | + // Case with invalid URL |
| 179 | + redirectURL = "http://[::1]:namedport" |
| 180 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 181 | + assert.Equal(t, false, result) |
| 182 | + |
| 183 | + // Case with URL having port |
| 184 | + redirectURL = "http://sub.example.com:8080/page" |
| 185 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 186 | + assert.Equal(t, true, result) |
| 187 | + |
| 188 | + // Case with URL having different subdomain |
| 189 | + redirectURL = "http://another.example.com/page" |
| 190 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 191 | + assert.Equal(t, true, result) |
| 192 | + |
| 193 | + // Case with URL having different TLD |
| 194 | + redirectURL = "http://example.org/page" |
| 195 | + result = utils.IsRedirectSafe(redirectURL, domain) |
| 196 | + assert.Equal(t, false, result) |
| 197 | +} |
0 commit comments