Initial QSfera import
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package userroles
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestExtractRolesArray(t *testing.T) {
|
||||
byt := []byte(`{"roles":["a","b"]}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("roles", claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := roles["a"]; !ok {
|
||||
t.Fatal("must contain 'a'")
|
||||
}
|
||||
if _, ok := roles["b"]; !ok {
|
||||
t.Fatal("must contain 'b'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractRolesString(t *testing.T) {
|
||||
byt := []byte(`{"roles":"a"}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("roles", claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := roles["a"]; !ok {
|
||||
t.Fatal("must contain 'a'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractRolesPathArray(t *testing.T) {
|
||||
byt := []byte(`{"sub":{"roles":["a","b"]}}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("sub.roles", claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := roles["a"]; !ok {
|
||||
t.Fatal("must contain 'a'")
|
||||
}
|
||||
if _, ok := roles["b"]; !ok {
|
||||
t.Fatal("must contain 'b'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractRolesPathString(t *testing.T) {
|
||||
byt := []byte(`{"sub":{"roles":"a"}}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("sub.roles", claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := roles["a"]; !ok {
|
||||
t.Fatal("must contain 'a'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractEscapedRolesPathString(t *testing.T) {
|
||||
byt := []byte(`{"sub.roles":"a"}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("sub\\.roles", claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := roles["a"]; !ok {
|
||||
t.Fatal("must contain 'a'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoRoles(t *testing.T) {
|
||||
byt := []byte(`{"sub":{"foo":"a"}}`)
|
||||
|
||||
claims := map[string]any{}
|
||||
err := json.Unmarshal(byt, &claims)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
roles, err := extractRoles("sub.roles", claims)
|
||||
if err == nil {
|
||||
t.Fatal("must not find a role")
|
||||
}
|
||||
if len(roles) != 0 {
|
||||
t.Fatal("length of roles mut be 0")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user