Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
99 lines (89 loc) · 3.07 KB
/
Copy patherrors_test.go
File metadata and controls
99 lines (89 loc) · 3.07 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package forwardauth
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestErrors(t *testing.T) {
// Test that error variables are defined correctly
assert.NotNil(t, ErrNoPasswordConfigured)
assert.NotNil(t, ErrNoUserCheckFunc)
assert.NotNil(t, ErrInvalidConfig)
assert.NotNil(t, ErrNotAuthenticated)
assert.NotNil(t, ErrInvalidPassword)
assert.NotNil(t, ErrUserNotFound)
assert.NotNil(t, ErrSessionRequired)
assert.NotNil(t, ErrStepUpRequired)
assert.NotNil(t, ErrSessionStoreError)
assert.NotNil(t, ErrUnauthorized)
assert.NotNil(t, ErrForbidden)
// Test error messages
assert.Equal(t, "password authentication enabled but no passwords configured", ErrNoPasswordConfigured.Error())
assert.Equal(t, "header authentication enabled but no user check function provided", ErrNoUserCheckFunc.Error())
assert.Equal(t, "invalid configuration", ErrInvalidConfig.Error())
assert.Equal(t, "not authenticated", ErrNotAuthenticated.Error())
assert.Equal(t, "invalid password", ErrInvalidPassword.Error())
assert.Equal(t, "user not found", ErrUserNotFound.Error())
assert.Equal(t, "session required", ErrSessionRequired.Error())
assert.Equal(t, "step-up authentication required", ErrStepUpRequired.Error())
assert.Equal(t, "session store error", ErrSessionStoreError.Error())
assert.Equal(t, "unauthorized", ErrUnauthorized.Error())
assert.Equal(t, "forbidden", ErrForbidden.Error())
}
func TestErrorCodes(t *testing.T) {
assert.Equal(t, ErrorCode("unauthorized"), ErrorCodeUnauthorized)
assert.Equal(t, ErrorCode("invalid_password"), ErrorCodeInvalidPassword)
assert.Equal(t, ErrorCode("user_not_found"), ErrorCodeUserNotFound)
assert.Equal(t, ErrorCode("session_required"), ErrorCodeSessionRequired)
assert.Equal(t, ErrorCode("step_up_required"), ErrorCodeStepUpRequired)
assert.Equal(t, ErrorCode("session_error"), ErrorCodeSessionError)
assert.Equal(t, ErrorCode("internal_error"), ErrorCodeInternalError)
}
func TestNewErrorResponse(t *testing.T) {
tests := []struct {
name string
code ErrorCode
message string
status int
}{
{
name: "unauthorized error",
code: ErrorCodeUnauthorized,
message: "You are not authorized",
status: 401,
},
{
name: "forbidden error",
code: ErrorCodeStepUpRequired,
message: "Step-up authentication required",
status: 403,
},
{
name: "internal error",
code: ErrorCodeInternalError,
message: "Internal server error",
status: 500,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp := NewErrorResponse(tt.code, tt.message, tt.status)
assert.NotNil(t, resp)
assert.False(t, resp.OK)
assert.Equal(t, tt.code, resp.Code)
assert.Equal(t, tt.message, resp.Message)
assert.Equal(t, tt.status, resp.Status)
})
}
}
func TestErrorResponse(t *testing.T) {
resp := &ErrorResponse{
OK: false,
Code: ErrorCodeUnauthorized,
Message: "Not authenticated",
Status: 401,
}
assert.False(t, resp.OK)
assert.Equal(t, ErrorCodeUnauthorized, resp.Code)
assert.Equal(t, "Not authenticated", resp.Message)
assert.Equal(t, 401, resp.Status)
}