-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapper_slice_test.go
202 lines (160 loc) · 4.91 KB
/
mapper_slice_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
package wm
import (
"github.com/Hoss-Mobility/wm/internal"
"github.com/stretchr/testify/assert"
"testing"
)
var secretsSlice = []internal.SecretItem{secretItem, secretItem, secretItem}
func TestSliceToWebAdmin(t *testing.T) {
role := "admin"
webModelSlice, err := SliceToWeb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if webModelSlice == nil {
t.Fatalf("Error creating webModelSlice")
}
deref := *webModelSlice
// admin is allowed to see all fields
for i, item := range secretsSlice {
assert.Equal(t, item.Name, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Equal(t, item.SecretInfo, deref[i].SecretInfo)
assert.Equal(t, item.TopSecret, deref[i].TopSecret)
assert.Equal(t, item.CanOnlyBeWrittenTo, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToWebDeveloper(t *testing.T) {
role := "developer"
webModelSlice, err := SliceToWeb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if webModelSlice == nil {
t.Fatalf("Error creating webModelSlice")
}
deref := *webModelSlice
// developer is not allowed to see all fields
for i, item := range secretsSlice {
assert.Equal(t, item.Name, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Equal(t, item.SecretInfo, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Empty(t, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToWebStaff(t *testing.T) {
role := "staff"
webModelSlice, err := SliceToWeb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if webModelSlice == nil {
t.Fatalf("Error creating webModelSlice")
}
deref := *webModelSlice
// staff is not allowed to see all fields
for i, item := range secretsSlice {
assert.Equal(t, item.Name, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Empty(t, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Empty(t, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToWebUnauthorized(t *testing.T) {
role := "unauthorized"
webModelSlice, err := SliceToWeb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if webModelSlice == nil {
t.Fatalf("Error creating webModelSlice")
}
deref := *webModelSlice
// unauthorized is not allowed to see any fields
for i, _ := range secretsSlice {
assert.Empty(t, deref[i].Name)
assert.Empty(t, deref[i].Comment)
assert.Empty(t, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Empty(t, deref[i].CanOnlyBeWrittenTo)
}
}
// To DB
func TestSliceToDbAdmin(t *testing.T) {
role := "admin"
dbModelSlice, err := SliceToDb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if dbModelSlice == nil {
t.Fatalf("Error creating dbModelSlice")
}
deref := *dbModelSlice
// admin is allowed to see all fields
for i, item := range secretsSlice {
assert.Equal(t, item.Name, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Equal(t, item.SecretInfo, deref[i].SecretInfo)
assert.Equal(t, item.TopSecret, deref[i].TopSecret)
assert.Equal(t, item.CanOnlyBeWrittenTo, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToDbDeveloper(t *testing.T) {
role := "developer"
dbModelSlice, err := SliceToDb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if dbModelSlice == nil {
t.Fatalf("Error creating dbModelSlice")
}
deref := *dbModelSlice
// developer is not allowed to see all fields
for i, item := range secretsSlice {
assert.Equal(t, item.Name, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Empty(t, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Equal(t, item.CanOnlyBeWrittenTo, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToDbStaff(t *testing.T) {
role := "staff"
dbModelSlice, err := SliceToDb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if dbModelSlice == nil {
t.Fatalf("Error creating dbModelSlice")
}
deref := *dbModelSlice
// staff is not allowed to see all fields
for i, item := range secretsSlice {
assert.Empty(t, deref[i].Name)
assert.Equal(t, item.Comment, deref[i].Comment)
assert.Empty(t, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Equal(t, item.CanOnlyBeWrittenTo, deref[i].CanOnlyBeWrittenTo)
}
}
func TestSliceToDbUnauthorized(t *testing.T) {
role := "unauthorized"
dbModelSlice, err := SliceToDb(secretsSlice, role)
if err != nil {
t.Fatalf("Error creating webModel: %v", err)
}
if dbModelSlice == nil {
t.Fatalf("Error creating dbModelSlice")
}
deref := *dbModelSlice
// unauthorized is not allowed to see any fields
for i, _ := range secretsSlice {
assert.Empty(t, deref[i].Name)
assert.Empty(t, deref[i].Comment)
assert.Empty(t, deref[i].SecretInfo)
assert.Empty(t, deref[i].TopSecret)
assert.Empty(t, deref[i].CanOnlyBeWrittenTo)
}
}