mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-06 04:09:40 -06:00
* extract and test role claim parsing Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * add failing test Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * read segmented roles claim as array and string Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * reuse more code by extracting WalkSegments Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * add TestSplitWithEscaping Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * docs and error for unhandled case Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * add claims test Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * add missing ReadStringClaim docs Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> --------- Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
121 lines
2.2 KiB
Go
121 lines
2.2 KiB
Go
package userroles
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestExtractRolesArray(t *testing.T) {
|
|
byt := []byte(`{"roles":["a","b"]}`)
|
|
|
|
claims := map[string]interface{}{}
|
|
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]interface{}{}
|
|
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]interface{}{}
|
|
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]interface{}{}
|
|
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]interface{}{}
|
|
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]interface{}{}
|
|
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")
|
|
}
|
|
}
|