@@ -6,31 +6,22 @@ import (
66 "testing"
77
88 "github.com/gotify/server/v2/mode"
9+ "github.com/rs/zerolog"
910 "github.com/stretchr/testify/assert"
1011)
1112
1213func TestConfigEnv (t * testing.T ) {
1314 mode .Set (mode .TestDev )
14- os .Setenv ("GOTIFY_DEFAULTUSER_NAME" , "jmattheis" )
15- os .Setenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS" , "push.example.tld,push.other.tld" )
16- os .Setenv (
15+ t .Setenv ("GOTIFY_DEFAULTUSER_NAME" , "jmattheis" )
16+ t .Setenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS" , "push.example.tld,push.other.tld" )
17+ t .Setenv (
1718 "GOTIFY_SERVER_RESPONSEHEADERS" ,
1819 `{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Methods":"GET,POST"}` ,
1920 )
20- os .Setenv ("GOTIFY_SERVER_CORS_ALLOWORIGINS" , ".+.example.com,otherdomain.com" )
21- os .Setenv ("GOTIFY_SERVER_CORS_ALLOWMETHODS" , "GET,POST" )
22- os .Setenv ("GOTIFY_SERVER_CORS_ALLOWHEADERS" , "Authorization,content-type" )
23- os .Setenv ("GOTIFY_SERVER_STREAM_ALLOWEDORIGINS" , ".+.example.com,otherdomain.com" )
24-
25- defer func () {
26- os .Unsetenv ("GOTIFY_DEFAULTUSER_NAME" )
27- os .Unsetenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS" )
28- os .Unsetenv ("GOTIFY_SERVER_RESPONSEHEADERS" )
29- os .Unsetenv ("GOTIFY_SERVER_CORS_ALLOWORIGINS" )
30- os .Unsetenv ("GOTIFY_SERVER_CORS_ALLOWMETHODS" )
31- os .Unsetenv ("GOTIFY_SERVER_CORS_ALLOWHEADERS" )
32- os .Unsetenv ("GOTIFY_SERVER_STREAM_ALLOWEDORIGINS" )
33- }()
21+ t .Setenv ("GOTIFY_SERVER_CORS_ALLOWORIGINS" , ".+.example.com,otherdomain.com" )
22+ t .Setenv ("GOTIFY_SERVER_CORS_ALLOWMETHODS" , "GET,POST" )
23+ t .Setenv ("GOTIFY_SERVER_CORS_ALLOWHEADERS" , "Authorization,content-type" )
24+ t .Setenv ("GOTIFY_SERVER_STREAM_ALLOWEDORIGINS" , ".+.example.com,otherdomain.com" )
3425
3526 conf , _ := Get ()
3627 assert .Equal (t , 80 , conf .Server .Port , "should use defaults" )
@@ -44,6 +35,53 @@ func TestConfigEnv(t *testing.T) {
4435 assert .Equal (t , []string {".+.example.com" , "otherdomain.com" }, conf .Server .Stream .AllowedOrigins )
4536}
4637
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+ }
63+
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+ }
83+ }
84+
4785func TestFile (t * testing.T ) {
4886 mode .Set (mode .TestDev )
4987 dir := t .TempDir ()
@@ -52,10 +90,8 @@ func TestFile(t *testing.T) {
5290 assert .Nil (t , os .WriteFile (passPath , []byte ("filesecret\n " ), 0o600 ))
5391 assert .Nil (t , os .WriteFile (hostsPath , []byte ("a.example.com,b.example.com" ), 0o600 ))
5492
55- os .Setenv ("GOTIFY_DEFAULTUSER_PASS_FILE" , passPath )
56- os .Setenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS_FILE" , hostsPath )
57- defer os .Unsetenv ("GOTIFY_DEFAULTUSER_PASS_FILE" )
58- defer os .Unsetenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS_FILE" )
93+ t .Setenv ("GOTIFY_DEFAULTUSER_PASS_FILE" , passPath )
94+ t .Setenv ("GOTIFY_SERVER_SSL_LETSENCRYPT_HOSTS_FILE" , hostsPath )
5995
6096 conf , _ := Get ()
6197 assert .Equal (t , "filesecret" , conf .DefaultUser .Pass )
@@ -68,27 +104,24 @@ func TestGotifyConfigFile(t *testing.T) {
68104 configPath := filepath .Join (dir , "custom.env" )
69105 assert .Nil (t , os .WriteFile (configPath , []byte ("GOTIFY_DEFAULTUSER_NAME=fromfile\n " ), 0o600 ))
70106
71- os .Setenv ("GOTIFY_CONFIG_FILE" , configPath )
72- defer os .Unsetenv ("GOTIFY_CONFIG_FILE" )
107+ t .Setenv ("GOTIFY_CONFIG_FILE" , configPath )
73108
74109 conf , _ := Get ()
75110 assert .Equal (t , "fromfile" , conf .DefaultUser .Name )
76111}
77112
78113func TestAddSlash (t * testing.T ) {
79114 mode .Set (mode .TestDev )
80- os .Setenv ("GOTIFY_UPLOADEDIMAGESDIR" , "../data/images" )
115+ t .Setenv ("GOTIFY_UPLOADEDIMAGESDIR" , "../data/images" )
81116 conf , _ := Get ()
82117 assert .Equal (t , "../data/images" + string (filepath .Separator ), conf .UploadedImagesDir )
83- os .Unsetenv ("GOTIFY_UPLOADEDIMAGESDIR" )
84118}
85119
86120func TestNotAddSlash (t * testing.T ) {
87121 mode .Set (mode .TestDev )
88- os .Setenv ("GOTIFY_UPLOADEDIMAGESDIR" , "../data/" )
122+ t .Setenv ("GOTIFY_UPLOADEDIMAGESDIR" , "../data/" )
89123 conf , _ := Get ()
90124 assert .Equal (t , "../data/" , conf .UploadedImagesDir )
91- os .Unsetenv ("GOTIFY_UPLOADEDIMAGESDIR" )
92125}
93126
94127func TestParseList (t * testing.T ) {
@@ -106,8 +139,7 @@ func TestParseList(t *testing.T) {
106139
107140 for _ , tc := range tests {
108141 t .Run (tc .name , func (t * testing.T ) {
109- os .Setenv (env , tc .raw )
110- defer os .Unsetenv (env )
142+ t .Setenv (env , tc .raw )
111143
112144 var got []string
113145 assert .Nil (t , parseList (& got , env ))
0 commit comments