-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathauth_test.go
More file actions
66 lines (59 loc) · 1.41 KB
/
Copy pathauth_test.go
File metadata and controls
66 lines (59 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2024-2026, s0up and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package server
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestIsWhitelisted(t *testing.T) {
gin.SetMode(gin.TestMode)
tests := []struct {
name string
remoteAddr string
whitelist []string
expected bool
}{
{
name: "IP in whitelist",
remoteAddr: "192.168.1.100",
whitelist: []string{"192.168.1.0/24"},
expected: true,
},
{
name: "IP not in whitelist",
remoteAddr: "10.0.0.5",
whitelist: []string{"192.168.1.0/24"},
expected: false,
},
{
name: "Multiple whitelists",
remoteAddr: "10.0.0.5",
whitelist: []string{"192.168.1.0/24", "10.0.0.0/8"},
expected: true,
},
{
name: "Empty whitelist",
remoteAddr: "192.168.1.100",
whitelist: []string{},
expected: false,
},
{
name: "Invalid CIDR",
remoteAddr: "192.168.1.100",
whitelist: []string{"invalid"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
c.Request = &http.Request{RemoteAddr: tt.remoteAddr + ":12345"}
got := isWhitelisted(c, tt.whitelist)
assert.Equal(t, tt.expected, got, "isWhitelisted() result mismatch")
})
}
}