-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate_test.go
96 lines (78 loc) · 2.25 KB
/
validate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"testing"
mapset "github.com/deckarep/golang-set/v2"
)
func TestValidateRequestAccept(t *testing.T) {
validationRequest := RawValidationRequest{
Request: Request{
User: "tonio",
Action: "eats",
Resource: "hay",
},
Settings: Settings{
ValidUsers: mapset.NewSet[string]("tonio", "wanda"),
ValidActions: mapset.NewSet[string]("eats", "likes"),
ValidResources: mapset.NewSet[string]("hay", "carrot", "banana"),
},
}
validationRequestJSON, err := json.Marshal(&validationRequest)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
responseJSON := validate(validationRequestJSON)
var response ValidationResponse
err = json.Unmarshal(responseJSON, &response)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if !response.Accepted {
t.Errorf("response should be accepted: %s", *response.Message)
}
}
func TestValidateRequestReject(t *testing.T) {
validationRequest := RawValidationRequest{
Request: Request{
User: "oscar",
Action: "eats",
Resource: "hay",
},
Settings: Settings{
ValidUsers: mapset.NewSet[string]("tonio", "wanda"),
ValidActions: mapset.NewSet[string]("eats", "likes"),
ValidResources: mapset.NewSet[string]("hay", "carrot", "banana"),
},
}
validationRequestJSON, err := json.Marshal(&validationRequest)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
responseJSON := validate(validationRequestJSON)
var response ValidationResponse
err = json.Unmarshal(responseJSON, &response)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if response.Accepted {
t.Errorf("response should be rejected")
}
if *response.Message != "User 'oscar' is not allowed" {
t.Errorf("wrong message: %s", *response.Message)
}
}
func TestValidateSettingsRejectInvalidPayload(t *testing.T) {
payload := []byte(`{"foo": "bar"}`)
responseJSON := validate(payload)
var response ValidationResponse
err := json.Unmarshal(responseJSON, &response)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if response.Accepted {
t.Errorf("response should be rejected")
}
if *response.Message != "Error deserializing validation request: json: unknown field \"foo\"" {
t.Errorf("wrong message: %s", *response.Message)
}
}