Repository navigation
Expand file tree
/
Copy pathrules_test.go
More file actions
194 lines (175 loc) · 6.67 KB
/
Copy pathrules_test.go
File metadata and controls
194 lines (175 loc) · 6.67 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package definitioncheck_test
import (
"strings"
"testing"
"webtyp.com/definitioncheck"
"webtyp.com/model"
)
func patientDef(allow model.Action, rules model.Rules) model.Definition {
return model.Definition{Name: "patient", Allow: allow, Rules: rules,
Fields: model.Fields{{Name: "id", Type: model.Text(), DB: &model.FieldDB{PK: true}}}}
}
func patientDefWire(allow model.Action, rules model.Rules) model.Definition {
def := patientDef(allow, rules)
def.Wire = true
return def
}
func patientDefMFA(allow model.Action, rules model.Rules, mfa model.Action) model.Definition {
def := patientDef(allow, rules)
def.MFA = mfa
return def
}
func TestCheckRules_AllowedActionMustBeDeclared(t *testing.T) {
def := patientDef(model.Create|model.Update, model.Rules{Create: model.AddMyBusinessRule})
err := definitioncheck.CheckRules(def)
if err == nil {
t.Fatal("expected error, got nil")
}
msg := err.Error()
if !strings.Contains(msg, "patient") {
t.Errorf("error %q does not contain 'patient'", msg)
}
if !strings.Contains(msg, "u") {
t.Errorf("error %q does not contain 'u'", msg)
}
}
func TestCheckRules_DeclaredActionMustBeAllowed(t *testing.T) {
def := patientDef(model.Create, model.Rules{Create: model.HaveNoBusinessRule, Delete: model.AddMyBusinessRule})
err := definitioncheck.CheckRules(def)
if err == nil {
t.Fatal("expected error, got nil")
}
msg := err.Error()
if !strings.Contains(msg, "patient") {
t.Errorf("error %q does not contain 'patient'", msg)
}
if !strings.Contains(msg, "d") {
t.Errorf("error %q does not contain 'd'", msg)
}
}
func TestCheckRules_CompleteDeclarationPasses(t *testing.T) {
def := patientDef(model.Create|model.Read, model.Rules{Create: model.AddMyBusinessRule, Read: model.HaveNoBusinessRule})
err := definitioncheck.CheckRules(def)
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
}
func TestCheckRules_NothingAllowedNeedsNoRules(t *testing.T) {
def := patientDef(0, model.Rules{})
err := definitioncheck.CheckRules(def)
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
}
func TestCheckRules_WireModelOnlyReadsThroughItsRule(t *testing.T) {
def1 := patientDefWire(model.Read, model.Rules{Read: model.AddMyBusinessRule})
if err := definitioncheck.CheckRules(def1); err != nil {
t.Errorf("expected nil error for Wire Read/AddMyBusinessRule, got %v", err)
}
def2 := patientDefWire(model.Read, model.Rules{Read: model.HaveNoBusinessRule})
err2 := definitioncheck.CheckRules(def2)
if err2 == nil {
t.Error("expected error for Wire Read/HaveNoBusinessRule")
} else if !strings.Contains(err2.Error(), "patient") {
t.Errorf("error %q does not contain 'patient'", err2.Error())
}
def3 := patientDefWire(model.Create, model.Rules{Create: model.AddMyBusinessRule})
if err := definitioncheck.CheckRules(def3); err == nil {
t.Error("expected error for Wire Create")
}
def4 := patientDefWire(0, model.Rules{})
if err := definitioncheck.CheckRules(def4); err != nil {
t.Errorf("expected nil error for Wire 0, got %v", err)
}
}
func TestCheckRules_MFAMustBeAllowed(t *testing.T) {
def1 := patientDefMFA(model.Create|model.Update, model.Rules{Create: model.HaveNoBusinessRule, Update: model.HaveNoBusinessRule}, model.Update)
if err := definitioncheck.CheckRules(def1); err != nil {
t.Errorf("expected nil error for MFA Update with Allow Create|Update, got %v", err)
}
def2 := patientDefMFA(model.Create, model.Rules{Create: model.HaveNoBusinessRule}, model.Update)
err2 := definitioncheck.CheckRules(def2)
if err2 == nil {
t.Error("expected error for MFA Update with Allow Create")
} else {
msg := err2.Error()
if !strings.Contains(msg, "patient") {
t.Errorf("error %q does not contain 'patient'", msg)
}
if !strings.Contains(msg, "u") {
t.Errorf("error %q does not contain 'u'", msg)
}
}
def3 := patientDefMFA(model.Create, model.Rules{Create: model.HaveNoBusinessRule}, 0)
if err := definitioncheck.CheckRules(def3); err != nil {
t.Errorf("expected nil error for MFA 0, got %v", err)
}
}
func TestRules_TextSearchNameIsReserved(t *testing.T) {
def := model.Definition{Name: "patient", Allow: model.Read, Rules: model.Rules{Read: model.HaveNoBusinessRule},
Fields: model.Fields{{Name: "text_search", Type: model.Text()}}}
err := definitioncheck.CheckRules(def)
want := "text_search is the column the framework keeps for search; rename the field: patient.text_search"
if err == nil || err.Error() != want {
t.Errorf("err = %v, want %q", err, want)
}
}
func TestRules_ListsOnlyAfterSearchNeedsRead(t *testing.T) {
def := model.Definition{Name: "patient", Allow: model.Create, Rules: model.Rules{Create: model.HaveNoBusinessRule}, ListsOnlyAfterSearch: true}
err := definitioncheck.CheckRules(def)
want := "ListsOnlyAfterSearch needs Read in Allow: patient"
if err == nil || err.Error() != want {
t.Errorf("err = %v, want %q", err, want)
}
}
func TestCheckRules_AgentDeclarations(t *testing.T) {
cases := []struct {
name string
allow model.Action
offered, asks, mfa model.Action
wantErr string // "" = must pass
}{
{"nothing offered passes", model.AllActions, 0, 0, 0, ""},
{"all offered, delete asks first", model.AllActions, model.AllActions, model.Delete, 0, ""},
{"offered must be allowed", model.Read, model.Read | model.Update, 0, 0, "Allow does not offer"},
{"offered cannot need MFA", model.AllActions, model.Update, 0, model.Update, "cannot give a second factor"},
{"asks first must be offered", model.AllActions, model.Read, model.Update, 0, "OfferedToAgents does not offer"},
{"offered delete must ask first", model.AllActions, model.Read | model.Delete, 0, 0, "add model.Delete to AgentAsksFirst"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
def := patientDef(c.allow, model.Rules{})
def.OfferedToAgents, def.AgentAsksFirst, def.MFA = c.offered, c.asks, c.mfa
for _, a := range [...]model.Action{model.Create, model.Read, model.Update, model.Delete} {
if c.allow.Has(a) {
setRule(&def.Rules, a)
}
}
err := definitioncheck.CheckRules(def)
if c.wantErr == "" {
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
return
}
if err == nil {
t.Fatalf("expected error containing %q, got nil", c.wantErr)
}
if msg := err.Error(); !strings.Contains(msg, c.wantErr) || !strings.Contains(msg, "patient") {
t.Errorf("error %q must contain %q and the model name", msg, c.wantErr)
}
})
}
}
func setRule(r *model.Rules, a model.Action) {
switch a {
case model.Create:
r.Create = model.HaveNoBusinessRule
case model.Read:
r.Read = model.HaveNoBusinessRule
case model.Update:
r.Update = model.HaveNoBusinessRule
case model.Delete:
r.Delete = model.HaveNoBusinessRule
}
}