1
0
Fork 0

Adding upstream version 3.0.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-16 22:32:21 +02:00
parent 4199417ac3
commit 8274b1bf1b
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
21 changed files with 2147 additions and 0 deletions

108
authorization_test.go Normal file
View file

@ -0,0 +1,108 @@
package g8
import "testing"
func TestAuthorizationService_Authorize(t *testing.T) {
authorizationService := NewAuthorizationService().WithToken("token")
if _, authorized := authorizationService.Authorize("token", nil); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("bad-token", nil); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("token", []string{"admin"}); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("", nil); authorized {
t.Error("should've returned false")
}
}
func TestAuthorizationService_AuthorizeWithPermissions(t *testing.T) {
authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"}))
if _, authorized := authorizationService.Authorize("token", nil); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"a"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"b"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"a", "b"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"c"}); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("token", []string{"a", "c"}); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("bad-token", nil); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("bad-token", []string{"a"}); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("", []string{"a"}); authorized {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithToken(t *testing.T) {
authorizationService := NewAuthorizationService().WithToken("token")
if _, authorized := authorizationService.Authorize("token", nil); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("bad-token", nil); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("token", []string{"admin"}); authorized {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithTokens(t *testing.T) {
authorizationService := NewAuthorizationService().WithTokens([]string{"1", "2"})
if _, authorized := authorizationService.Authorize("1", nil); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("2", nil); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("3", nil); authorized {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithClient(t *testing.T) {
authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"}))
if _, authorized := authorizationService.Authorize("token", []string{"a", "b"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"a"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"b"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("token", []string{"c"}); authorized {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithClients(t *testing.T) {
authorizationService := NewAuthorizationService().WithClients([]*Client{NewClient("1").WithPermission("a"), NewClient("2").WithPermission("b")})
if _, authorized := authorizationService.Authorize("1", []string{"a"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("2", []string{"b"}); !authorized {
t.Error("should've returned true")
}
if _, authorized := authorizationService.Authorize("1", []string{"b"}); authorized {
t.Error("should've returned false")
}
if _, authorized := authorizationService.Authorize("2", []string{"a"}); authorized {
t.Error("should've returned false")
}
}