@@ -20,8 +20,10 @@ import (
2020 "github.com/gin-gonic/gin"
2121 "github.com/stretchr/testify/assert"
2222
23+ "github.com/NethServer/nethsecurity-api/models"
2324 "github.com/NethServer/nethsecurity-controller/api/configuration"
2425 "github.com/NethServer/nethsecurity-controller/api/logs"
26+ "github.com/NethServer/nethsecurity-controller/api/methods"
2527)
2628
2729func TestMain (m * testing.M ) {
@@ -92,3 +94,87 @@ func TestJWTLogin(t *testing.T) {
9294 // Should return 401 since storage fails
9395 assert .Equal (t , 401 , w .Code )
9496}
97+
98+ func generateTestToken (t * testing.T , username string ) string {
99+ t .Helper ()
100+ configuration .Config .SecretJWT = "test_secret"
101+ mw := InstanceJWT ()
102+
103+ token , _ , err := mw .TokenGenerator (& models.UserAuthorizations {Username : username })
104+ assert .NoError (t , err )
105+ assert .NotEmpty (t , token )
106+
107+ // Register the token as active
108+ methods .SetTokenValidation (username , token )
109+
110+ return token
111+ }
112+
113+ func TestBasicUserAuthCookie (t * testing.T ) {
114+ token := generateTestToken (t , "testuser" )
115+
116+ r := gin .New ()
117+ r .Use (BasicUserAuth ())
118+ r .GET ("/auth" , func (c * gin.Context ) {
119+ c .JSON (200 , gin.H {"message" : "ok" })
120+ })
121+
122+ // Valid cookie returns 200 and sets X-Auth-User
123+ req , _ := http .NewRequest ("GET" , "/auth" , nil )
124+ req .AddCookie (& http.Cookie {Name : cookieName , Value : token })
125+ w := httptest .NewRecorder ()
126+ r .ServeHTTP (w , req )
127+ assert .Equal (t , 200 , w .Code )
128+ assert .Equal (t , "testuser" , w .Header ().Get ("X-Auth-User" ))
129+
130+ // Invalid cookie → 401 and cookie is cleared (Set-Cookie with Max-Age=-1)
131+ req , _ = http .NewRequest ("GET" , "/auth" , nil )
132+ req .AddCookie (& http.Cookie {Name : cookieName , Value : "invalid.jwt.token" })
133+ w = httptest .NewRecorder ()
134+ r .ServeHTTP (w , req )
135+ assert .Equal (t , 401 , w .Code )
136+ assert .Contains (t , w .Header ().Get ("Set-Cookie" ), cookieName + "=;" )
137+
138+ // No cookie and no Basic Auth → 401
139+ req , _ = http .NewRequest ("GET" , "/auth" , nil )
140+ w = httptest .NewRecorder ()
141+ r .ServeHTTP (w , req )
142+ assert .Equal (t , 401 , w .Code )
143+
144+ // Clean up
145+ methods .DelTokenValidation ("testuser" , token )
146+ }
147+
148+ func TestBasicUserAuthCookieUnitAccess (t * testing.T ) {
149+ token := generateTestToken (t , "limiteduser" )
150+
151+ r := gin .New ()
152+ r .Use (BasicUserAuth ())
153+ r .GET ("/auth/:unit_id" , func (c * gin.Context ) {
154+ c .JSON (200 , gin.H {"message" : "ok" })
155+ })
156+
157+ // Non-admin user with cookie accessing a unit → 403
158+ // (limiteduser is not in adminUsers and has no unit assignments)
159+ req , _ := http .NewRequest ("GET" , "/auth/unit-123" , nil )
160+ req .AddCookie (& http.Cookie {Name : cookieName , Value : token })
161+ w := httptest .NewRecorder ()
162+ r .ServeHTTP (w , req )
163+ assert .Equal (t , 403 , w .Code )
164+ assert .Contains (t , w .Body .String (), "user does not have access to this unit" )
165+
166+ // Same user without unit_id param → 200 (no unit check needed)
167+ r2 := gin .New ()
168+ r2 .Use (BasicUserAuth ())
169+ r2 .GET ("/auth" , func (c * gin.Context ) {
170+ c .JSON (200 , gin.H {"message" : "ok" })
171+ })
172+ req , _ = http .NewRequest ("GET" , "/auth" , nil )
173+ req .AddCookie (& http.Cookie {Name : cookieName , Value : token })
174+ w = httptest .NewRecorder ()
175+ r2 .ServeHTTP (w , req )
176+ assert .Equal (t , 200 , w .Code )
177+
178+ // Clean up
179+ methods .DelTokenValidation ("limiteduser" , token )
180+ }
0 commit comments