-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuser_diff_test.go
239 lines (216 loc) · 6.15 KB
/
user_diff_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package yext
import (
"reflect"
"testing"
)
var (
exampleUser = &User{
Id: String("[email protected]"),
FirstName: String("dang"),
LastName: String("dangle"),
PhoneNumber: String("2025562637"),
EmailAddress: String("[email protected]"),
UserName: String("[email protected]"),
Password: String("terriblepassword"),
ACLs: []ACL{
ACL{
Role: Role{
Id: String("3"),
Name: String("Example Role"),
},
On: "12345",
AccessOn: ACCESS_FOLDER,
},
},
}
secondUser *User
)
func TestStringChanges(t *testing.T) {
secondUser = exampleUser.Copy()
secondUser.UserName = String("someotherdang")
secondUser.FirstName = String("john")
secondUser.LastName = String("dang")
secondUser.PhoneNumber = String("5555555555")
secondUser.EmailAddress = String("[email protected]")
secondUser.Password = String("someotherpassword")
d, isDiff := exampleUser.Diff(secondUser)
if !isDiff {
t.Errorf("Expected diff true, was false, diff result: %v", d)
} else {
if d.GetPassword() != secondUser.GetPassword() {
t.Errorf("Expected Password to be '%s' but was '%s', diff result: %v", d.GetPassword(), secondUser.GetPassword(), d)
}
if d.GetUserName() != secondUser.GetUserName() {
t.Errorf("Expected UserName to be '%s' but was '%s', diff result: %v", d.GetUserName(), secondUser.GetUserName(), d)
}
if d.GetFirstName() != secondUser.GetFirstName() {
t.Errorf("Expected FirstName to be '%s' but was '%s', diff result: %v", d.GetFirstName(), secondUser.GetFirstName(), d)
}
if d.GetLastName() != secondUser.GetLastName() {
t.Errorf("Expected LastName to be '%s' but was '%s', diff result: %v", d.GetLastName(), secondUser.GetLastName(), d)
}
if d.GetPhoneNumber() != secondUser.GetPhoneNumber() {
t.Errorf("Expected PhoneNumber to be '%s' but was '%s', diff result: %v", d.GetPhoneNumber(), secondUser.GetPhoneNumber(), d)
}
if d.GetEmailAddress() != secondUser.GetEmailAddress() {
t.Errorf("Expected EmailAddress to be '%s' but was '%s', diff result: %v", d.GetEmailAddress(), secondUser.GetEmailAddress(), d)
}
}
}
func TestAppendACL(t *testing.T) {
expectACL := ACL{
Role: Role{
Id: String("123"),
Name: String("Crazy Role"),
},
On: "123456",
AccessOn: ACCESS_ACCOUNT,
}
secondUser = exampleUser.Copy()
secondUser.ACLs = append(secondUser.ACLs, expectACL)
d, isDiff := exampleUser.Diff(secondUser)
if !isDiff {
t.Errorf("Expected diff true, was false, diff result: %v", d)
} else {
if len(d.ACLs) != len(secondUser.ACLs) {
t.Errorf("Expected ACL to be added, got %v, diff result: %v", d.ACLs, d)
}
hasCorrectACL := false
for _, acl := range d.ACLs {
if acl.On == expectACL.On {
hasCorrectACL = true
}
}
if !hasCorrectACL {
t.Errorf("Expected ACLS to contain %v but didn't, diff result: %v", expectACL, d)
}
}
}
func TestDeleteACL(t *testing.T) {
secondUser = exampleUser.Copy()
secondUser.ACLs = []ACL{}
d, isDiff := exampleUser.Diff(secondUser)
if !isDiff {
t.Errorf("Expected diff true, was false, diff result: %v", d)
} else {
if len(d.ACLs) != len(secondUser.ACLs) {
t.Errorf("Expected ACL to be deleted, got %v, diff result: %v", d.ACLs, d)
}
}
}
func TestModifyACL(t *testing.T) {
secondUser = exampleUser.Copy()
acl := secondUser.ACLs[0]
acl.Role.Name = String("Dingle Role")
acl.On = "987456"
secondUser.ACLs[0] = acl
d, isDiff := exampleUser.Diff(secondUser)
if !isDiff {
t.Errorf("Expected diff true, was false, diff result: %v", d)
} else if d == nil {
t.Errorf("Expected non nill diff: '%v' res: '%v'", d, isDiff)
} else {
if d.ACLs == nil {
t.Errorf("Expected ACLS!%v", d)
}
if secondUser.ACLs == nil {
t.Errorf("Should have had ACLS!%v", secondUser)
}
if len(d.ACLs) != len(secondUser.ACLs) {
t.Errorf("Expected ACL to be the same length, got %v, diff result: %v", d.ACLs, d)
}
if d.ACLs[0].GetName() != acl.GetName() {
t.Errorf("Expected ACL Name to be modified, got %v, diff result: %v", d.ACLs, d)
}
if d.ACLs[0].On != acl.On {
t.Errorf("Expected ACL On to be modified, got %v, diff result: %v", d.ACLs, d)
}
}
}
func TestDiff(t *testing.T) {
type test struct {
A, B *User
IsDiff bool
Diff *User
}
tests := []test{
test{
A: &User{},
B: &User{},
IsDiff: false,
Diff: nil,
},
test{
A: &User{},
B: &User{},
IsDiff: false,
Diff: nil,
},
test{
A: &User{ACLs: []ACL{}},
B: &User{ACLs: []ACL{}},
IsDiff: false,
Diff: nil,
},
test{
A: &User{ACLs: []ACL{ACL{On: "foo", AccessOn: ACCESS_LOCATION, AccountId: "123"}}},
B: &User{ACLs: []ACL{ACL{On: "foo", AccessOn: "LOCATION", AccountId: "123"}}},
IsDiff: false,
Diff: nil,
},
test{
A: &User{ACLs: []ACL{ACL{Role: Role{Id: String("228"), Name: String("Farmers Agent Basic Users")}}}},
B: &User{ACLs: []ACL{ACL{Role: Role{Id: String("228"), Name: String("Farmers Agent Basic Users")}}}},
IsDiff: false,
Diff: nil,
},
test{
A: &User{
Id: String("[email protected]"),
FirstName: String("jane"),
LastName: String("doe"),
PhoneNumber: String("2025562637"),
EmailAddress: String("[email protected]"),
UserName: String("[email protected]"),
Password: String("sekret"),
ACLs: []ACL{
ACL{
Role: Role{
Id: String("3"),
Name: String("Example Role"),
},
On: "12345",
AccessOn: ACCESS_FOLDER,
},
},
},
B: &User{
Id: String("[email protected]"),
FirstName: String("jane"),
LastName: String("doe"),
PhoneNumber: String("2025562637"),
EmailAddress: String("[email protected]"),
UserName: String("[email protected]"),
Password: String("sekret"),
ACLs: []ACL{
ACL{
Role: Role{
Id: String("3"),
Name: String("Example Role"),
},
On: "12345",
AccessOn: ACCESS_FOLDER,
},
},
},
IsDiff: false,
Diff: nil,
},
}
for _, tst := range tests {
diff, isDiff := tst.A.Diff(tst.B)
if isDiff != tst.IsDiff || !reflect.DeepEqual(diff, tst.Diff) {
t.Errorf("\nA: %s\nB: %s\nWanted:%s\nGot: %s", tst.A, tst.B, tst.Diff, diff)
}
}
}