Skip to content

Commit 1f9382c

Browse files
committed
test: login local auth disabled
1 parent c61b0d7 commit 1f9382c

2 files changed

Lines changed: 60 additions & 9 deletions

File tree

api/session_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ func (s *SessionSuite) Test_Login_Success() {
9393
assert.Equal(s.T(), uint(auth.CookieMaxAge), clients[0].ExpiresAfterInactivitySeconds)
9494
}
9595

96+
func (s *SessionSuite) Test_Login_LocalAuthDisabled() {
97+
s.a.LocalAuthEnabled = false
98+
s.ctx.Request = httptest.NewRequest("POST", "/auth/local/login", strings.NewReader("name=test-browser"))
99+
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
100+
s.ctx.Request.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("testuser:testpass")))
101+
102+
s.a.Login(s.ctx)
103+
104+
assert.Equal(s.T(), 403, s.recorder.Code)
105+
106+
for _, c := range s.recorder.Result().Cookies() {
107+
assert.NotEqual(s.T(), auth.CookieName, c.Name)
108+
}
109+
}
110+
96111
func (s *SessionSuite) Test_Login_WrongPassword() {
97112
s.ctx.Request = httptest.NewRequest("POST", "/auth/local/login", strings.NewReader("name=test-browser"))
98113
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

config/config_test.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/gotify/server/v2/mode"
9+
"github.com/rs/zerolog"
910
"github.com/stretchr/testify/assert"
1011
)
1112

@@ -21,7 +22,6 @@ func TestConfigEnv(t *testing.T) {
2122
t.Setenv("GOTIFY_SERVER_CORS_ALLOWMETHODS", "GET,POST")
2223
t.Setenv("GOTIFY_SERVER_CORS_ALLOWHEADERS", "Authorization,content-type")
2324
t.Setenv("GOTIFY_SERVER_STREAM_ALLOWEDORIGINS", ".+.example.com,otherdomain.com")
24-
t.Setenv(EnvLocalAuthEnabled, "false")
2525

2626
conf, _ := Get()
2727
assert.Equal(t, 80, conf.Server.Port, "should use defaults")
@@ -33,17 +33,53 @@ func TestConfigEnv(t *testing.T) {
3333
assert.Equal(t, []string{"GET", "POST"}, conf.Server.Cors.AllowMethods)
3434
assert.Equal(t, []string{"Authorization", "content-type"}, conf.Server.Cors.AllowHeaders)
3535
assert.Equal(t, []string{".+.example.com", "otherdomain.com"}, conf.Server.Stream.AllowedOrigins)
36-
assert.False(t, conf.LocalAuthEnabled)
3736
}
3837

39-
func TestRegistrationRequiresLocalAuth(t *testing.T) {
40-
mode.Set(mode.TestDev)
41-
t.Setenv(EnvLocalAuthEnabled, "false")
42-
t.Setenv(EnvOIDCEnabled, "true")
43-
t.Setenv(EnvRegistration, "true")
38+
func TestLocalAuthDisabled(t *testing.T) {
39+
tests := []struct {
40+
name string
41+
env map[string]string
42+
fatals []FutureLog
43+
}{
44+
{
45+
name: "with oidc",
46+
env: map[string]string{EnvLocalAuthEnabled: "false", EnvOIDCEnabled: "true"},
47+
},
48+
{
49+
name: "without oidc",
50+
env: map[string]string{EnvLocalAuthEnabled: "false"},
51+
fatals: []FutureLog{futureFatal("either local authentication or OIDC must be enabled")},
52+
},
53+
{
54+
name: "with registration",
55+
env: map[string]string{
56+
EnvLocalAuthEnabled: "false",
57+
EnvOIDCEnabled: "true",
58+
EnvRegistration: "true",
59+
},
60+
fatals: []FutureLog{futureFatal("registration requires local authentication to be enabled")},
61+
},
62+
}
4463

45-
_, logs := Get()
46-
assert.Contains(t, logs, futureFatal("registration requires local authentication to be enabled"))
64+
for _, tc := range tests {
65+
t.Run(tc.name, func(t *testing.T) {
66+
mode.Set(mode.TestDev)
67+
for key, value := range tc.env {
68+
t.Setenv(key, value)
69+
}
70+
71+
conf, logs := Get()
72+
assert.False(t, conf.LocalAuthEnabled)
73+
74+
var fatals []FutureLog
75+
for _, entry := range logs {
76+
if entry.Level == zerolog.FatalLevel {
77+
fatals = append(fatals, entry)
78+
}
79+
}
80+
assert.Equal(t, tc.fatals, fatals)
81+
})
82+
}
4783
}
4884

4985
func TestFile(t *testing.T) {

0 commit comments

Comments
 (0)