William Petit
3ef495445a
serveur - Création d'un service d'autorisation dynamique basé sur des "voter" (à la Symfony) - Mise en place des autorisations sur les principales queries/mutations de l'API GraphQL
126 lines
2.3 KiB
Go
126 lines
2.3 KiB
Go
package voter
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestStrategyUnanimous(t *testing.T) {
|
|
testCases := []struct {
|
|
Decisions []Decision
|
|
Expect Decision
|
|
}{
|
|
{
|
|
Decisions: []Decision{Allow, Allow, Allow},
|
|
Expect: Allow,
|
|
},
|
|
{
|
|
Decisions: []Decision{Abstain, Abstain, Abstain},
|
|
Expect: Abstain,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Abstain, Abstain},
|
|
Expect: Deny,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Allow, Abstain},
|
|
Expect: Deny,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
ctx := context.Background()
|
|
|
|
result, err := StrategyUnanimous(ctx, tc.Decisions)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if e, g := tc.Expect, result; e != g {
|
|
t.Errorf("result: expected '%v', got '%v'", AsString(e), AsString(g))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrategyAffirmative(t *testing.T) {
|
|
testCases := []struct {
|
|
Decisions []Decision
|
|
Expect Decision
|
|
}{
|
|
{
|
|
Decisions: []Decision{Allow, Allow, Allow},
|
|
Expect: Allow,
|
|
},
|
|
{
|
|
Decisions: []Decision{Abstain, Abstain, Abstain},
|
|
Expect: Abstain,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Abstain, Abstain},
|
|
Expect: Deny,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Allow, Abstain},
|
|
Expect: Allow,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
ctx := context.Background()
|
|
|
|
result, err := StrategyAffirmative(ctx, tc.Decisions)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if e, g := tc.Expect, result; e != g {
|
|
t.Errorf("result: expected '%v', got '%v'", AsString(e), AsString(g))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStrategyConsensus(t *testing.T) {
|
|
testCases := []struct {
|
|
Decisions []Decision
|
|
Expect Decision
|
|
}{
|
|
{
|
|
Decisions: []Decision{Allow, Allow, Allow},
|
|
Expect: Allow,
|
|
},
|
|
{
|
|
Decisions: []Decision{Abstain, Abstain, Abstain},
|
|
Expect: Abstain,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Allow, Abstain},
|
|
Expect: Abstain,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Deny, Allow},
|
|
Expect: Deny,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Deny, Allow, Allow},
|
|
Expect: Abstain,
|
|
},
|
|
{
|
|
Decisions: []Decision{Deny, Deny, Allow, Allow, Allow},
|
|
Expect: Allow,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
ctx := context.Background()
|
|
|
|
result, err := StrategyConsensus(ctx, tc.Decisions)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
|
|
if e, g := tc.Expect, result; e != g {
|
|
t.Errorf("result: expected '%v', got '%v'", AsString(e), AsString(g))
|
|
}
|
|
}
|
|
}
|