@@ -2,97 +2,72 @@ package store
2
2
3
3
import (
4
4
"context"
5
+ "testing"
6
+ "time"
7
+
5
8
"github.com/openfga/cli/internal/fga"
6
9
mockclient "github.com/openfga/cli/internal/mocks"
7
10
"github.com/openfga/cli/internal/storetest"
8
11
"github.com/openfga/go-sdk/client"
9
12
"go.uber.org/mock/gomock"
10
- "testing"
11
- "time"
12
13
)
13
14
14
15
func TestImportStore (t * testing.T ) {
15
16
t .Parallel ()
16
17
17
- mockCtrl := gomock .NewController (t )
18
- defer mockCtrl .Finish ()
19
-
20
- mockFgaClient := mockclient .NewMockSdkClient (mockCtrl )
21
- clientConfig := fga.ClientConfig {}
22
-
23
18
expectedAssertions := []client.ClientAssertion {{
24
19
User : "user:anne" ,
25
20
Relation : "reader" ,
26
21
Object : "document:doc1" ,
27
22
Expectation : true ,
28
23
}}
29
-
30
- modelID := "model-1"
31
- storeID := "store-1"
32
- sampleTime := time .Now ()
33
- expectedOptions := client.ClientWriteAssertionsOptions {
34
- AuthorizationModelId : & modelID ,
35
- StoreId : & storeID ,
36
- }
37
-
38
- defaultStore := storetest.StoreData {
39
- Name : "test-store" ,
40
- Model : `type user
41
- type document
42
- relations
43
- define reader: [user]` ,
44
- Tests : []storetest.ModelTest {
45
- {
46
- Name : "Test" ,
47
- Check : []storetest.ModelTestCheck {
48
- {
49
- User : "user:anne" ,
50
- Object : "document:doc1" ,
51
- Assertions : map [string ]bool {
52
- "reader" : true ,
53
- },
54
- },
55
- },
56
- },
57
- },
58
- }
24
+ modelID , storeID := "model-1" , "store-1"
25
+ expectedOptions := client.ClientWriteAssertionsOptions {AuthorizationModelId : & modelID , StoreId : & storeID }
59
26
60
27
importStoreTests := []struct {
61
28
name string
62
29
mockWriteAssertions bool
63
- mockGetStore bool
64
30
mockCreateStore bool
65
31
mockWriteModel bool
66
32
testStore storetest.StoreData
67
- storeId string
68
33
}{
69
34
{
70
35
name : "import store with assertions" ,
71
36
mockWriteAssertions : true ,
72
- mockGetStore : false ,
73
37
mockWriteModel : true ,
74
38
mockCreateStore : true ,
75
- testStore : defaultStore ,
76
- storeId : "" ,
39
+ testStore : storetest.StoreData {
40
+ Model : `type user
41
+ type document
42
+ relations
43
+ define reader: [user]` ,
44
+ Tests : []storetest.ModelTest {
45
+ {
46
+ Name : "Test" ,
47
+ Check : []storetest.ModelTestCheck {
48
+ {
49
+ User : "user:anne" ,
50
+ Object : "document:doc1" ,
51
+ Assertions : map [string ]bool {"reader" : true },
52
+ },
53
+ },
54
+ },
55
+ },
56
+ },
77
57
},
78
58
{
79
59
name : "create new store without assertions" ,
80
60
mockWriteAssertions : false ,
81
61
mockCreateStore : true ,
82
- mockGetStore : false ,
83
62
mockWriteModel : false ,
84
- testStore : storetest.StoreData {
85
- Name : "test-store" ,
86
- },
87
- storeId : "" ,
63
+ testStore : storetest.StoreData {Name : "test-store" },
88
64
},
89
65
{
90
66
name : "create new store without check assertions" ,
91
67
mockCreateStore : true ,
92
68
mockWriteModel : true ,
93
69
mockWriteAssertions : false ,
94
70
testStore : storetest.StoreData {
95
- Name : "test-store" ,
96
71
Model : `type user
97
72
type document
98
73
relations
@@ -102,41 +77,84 @@ func TestImportStore(t *testing.T) {
102
77
Name : "Test" ,
103
78
ListObjects : []storetest.ModelTestListObjects {
104
79
{
105
- User : "user:anne" ,
106
- Type : "organization" ,
107
- Assertions : map [string ][]string {
108
- "member" : {"organization:acme" },
109
- },
80
+ User : "user:anne" ,
81
+ Type : "organization" ,
82
+ Assertions : map [string ][]string {"member" : {"organization:acme" }},
110
83
},
111
84
},
112
85
},
113
86
},
114
87
},
115
- storeId : "" ,
116
88
},
117
89
{
118
90
name : "do not write assertions if imported store does not have a model" ,
119
91
mockCreateStore : true ,
120
92
mockWriteAssertions : false ,
121
93
testStore : storetest.StoreData {
122
- Name : "test-store" ,
123
94
Tests : []storetest.ModelTest {
124
- {
125
- Name : "Test" ,
126
- ListObjects : []storetest.ModelTestListObjects {
127
- {
128
- User : "user:anne" ,
129
- Type : "organization" ,
130
- Assertions : map [string ][]string {
131
- "member" : {"organization:acme" },
132
- },
133
- },
134
- },
135
- },
95
+ {Name : "Test" },
136
96
},
137
97
},
138
- storeId : "" ,
139
98
},
99
+ }
100
+
101
+ for _ , test := range importStoreTests {
102
+ t .Run (test .name , func (t * testing.T ) {
103
+ t .Parallel ()
104
+ mockCtrl := gomock .NewController (t )
105
+ mockFgaClient := mockclient .NewMockSdkClient (mockCtrl )
106
+
107
+ defer mockCtrl .Finish ()
108
+
109
+ if test .mockWriteAssertions {
110
+ setupWriteAssertionsMock (mockCtrl , mockFgaClient , expectedAssertions , expectedOptions )
111
+ } else {
112
+ mockFgaClient .EXPECT ().WriteAssertions (context .Background ()).Times (0 )
113
+ }
114
+
115
+ if test .mockWriteModel {
116
+ setupWriteModelMock (mockCtrl , mockFgaClient , modelID )
117
+ }
118
+
119
+ if test .mockCreateStore {
120
+ setupCreateStoreMock (mockCtrl , mockFgaClient , storeID )
121
+ }
122
+
123
+ _ , err := importStore (& fga.ClientConfig {}, mockFgaClient , & test .testStore , "" , "" , 1 , 1 , "" )
124
+ if err != nil {
125
+ t .Errorf ("expected no error, got %v" , err )
126
+ }
127
+ })
128
+ }
129
+ }
130
+
131
+ func TestUpdateStore (t * testing.T ) {
132
+ t .Parallel ()
133
+
134
+ clientConfig := fga.ClientConfig {}
135
+
136
+ expectedAssertions := []client.ClientAssertion {{
137
+ User : "user:anne" ,
138
+ Relation : "reader" ,
139
+ Object : "document:doc1" ,
140
+ Expectation : true ,
141
+ }}
142
+
143
+ modelID := "model-1"
144
+ storeID := "store-1"
145
+ sampleTime := time .Now ()
146
+ expectedOptions := client.ClientWriteAssertionsOptions {
147
+ AuthorizationModelId : & modelID ,
148
+ StoreId : & storeID ,
149
+ }
150
+
151
+ importStoreTests := []struct {
152
+ name string
153
+ mockWriteAssertions bool
154
+ mockGetStore bool
155
+ mockWriteModel bool
156
+ testStore storetest.StoreData
157
+ }{
140
158
{
141
159
name : "update store with assertions" ,
142
160
mockWriteAssertions : true ,
@@ -163,7 +181,6 @@ func TestImportStore(t *testing.T) {
163
181
},
164
182
},
165
183
},
166
- storeId : storeID ,
167
184
},
168
185
{
169
186
name : "update store without assertions" ,
@@ -177,56 +194,83 @@ func TestImportStore(t *testing.T) {
177
194
relations
178
195
define reader: [user]` ,
179
196
},
180
- storeId : storeID ,
181
197
},
182
198
}
183
199
184
200
for _ , test := range importStoreTests {
185
-
186
201
t .Run (test .name , func (t * testing.T ) {
202
+ t .Parallel ()
203
+
204
+ mockCtrl := gomock .NewController (t )
205
+ defer mockCtrl .Finish ()
206
+
207
+ mockFgaClient := mockclient .NewMockSdkClient (mockCtrl )
208
+
209
+ defer mockCtrl .Finish ()
187
210
188
211
if test .mockWriteAssertions {
189
- mockWriteAssertions := mockclient .NewMockSdkClientWriteAssertionsRequestInterface (mockCtrl )
190
- mockFgaClient .EXPECT ().WriteAssertions (context .Background ()).Return (mockWriteAssertions )
191
- mockWriteAssertions .EXPECT ().Body (expectedAssertions ).Return (mockWriteAssertions )
192
- mockWriteAssertions .EXPECT ().Options (expectedOptions ).Return (mockWriteAssertions )
193
- mockWriteAssertions .EXPECT ().Execute ().Return (nil , nil )
212
+ setupWriteAssertionsMock (mockCtrl , mockFgaClient , expectedAssertions , expectedOptions )
194
213
} else {
195
214
mockFgaClient .EXPECT ().WriteAssertions (context .Background ()).Times (0 )
196
215
}
197
216
198
217
if test .mockWriteModel {
199
- mockWriteModel := mockclient .NewMockSdkClientWriteAuthorizationModelRequestInterface (mockCtrl )
200
- mockFgaClient .EXPECT ().WriteAuthorizationModel (context .Background ()).Return (mockWriteModel )
201
- mockWriteModel .EXPECT ().Body (gomock .Any ()).Return (mockWriteModel )
202
- mockWriteModel .EXPECT ().Execute ().Return (& client.ClientWriteAuthorizationModelResponse {AuthorizationModelId : modelID }, nil )
203
- }
204
-
205
- if test .mockCreateStore {
206
- mockCreateStore := mockclient .NewMockSdkClientCreateStoreRequestInterface (mockCtrl )
207
- mockFgaClient .EXPECT ().CreateStore (context .Background ()).Return (mockCreateStore )
208
- mockCreateStore .EXPECT ().Body (gomock .Any ()).Return (mockCreateStore )
209
- mockCreateStore .EXPECT ().Execute ().Return (& client.ClientCreateStoreResponse {Id : storeID }, nil )
210
- mockFgaClient .EXPECT ().SetStoreId (storeID )
218
+ setupWriteModelMock (mockCtrl , mockFgaClient , modelID )
211
219
}
212
220
213
221
if test .mockGetStore {
214
- mockGetStore := mockclient .NewMockSdkClientGetStoreRequestInterface (mockCtrl )
215
- mockFgaClient .EXPECT ().GetStore (context .Background ()).Return (mockGetStore )
216
- mockGetStore .EXPECT ().Execute ().Return (& client.ClientGetStoreResponse {Id : storeID , Name : "test-store" , CreatedAt : sampleTime , UpdatedAt : sampleTime }, nil )
217
- }
218
-
219
- var err error
220
- if storeID != "" {
221
- _ , err = importStore (& clientConfig , mockFgaClient , & test .testStore , "" , test .storeId , 1 , 1 , "" )
222
- } else {
223
- _ , err = importStore (& clientConfig , mockFgaClient , & test .testStore , "" , "" , 1 , 1 , "" )
222
+ setupGetStoreMock (mockCtrl , mockFgaClient , storeID , sampleTime )
224
223
}
225
224
225
+ _ , err := importStore (& clientConfig , mockFgaClient , & test .testStore , "" , storeID , 1 , 1 , "" )
226
226
if err != nil {
227
227
t .Errorf ("expected no error, got %v" , err )
228
228
}
229
-
230
229
})
231
230
}
232
231
}
232
+
233
+ func setupGetStoreMock (
234
+ mockCtrl * gomock.Controller ,
235
+ mockFgaClient * mockclient.MockSdkClient ,
236
+ storeID string ,
237
+ sampleTime time.Time ,
238
+ ) {
239
+ mockGetStore := mockclient .NewMockSdkClientGetStoreRequestInterface (mockCtrl )
240
+ mockFgaClient .EXPECT ().GetStore (context .Background ()).Return (mockGetStore )
241
+ mockGetStore .EXPECT ().Execute ().Return (
242
+ & client.ClientGetStoreResponse {Id : storeID , Name : "test-store" , CreatedAt : sampleTime , UpdatedAt : sampleTime },
243
+ nil ,
244
+ )
245
+ }
246
+
247
+ func setupCreateStoreMock (mockCtrl * gomock.Controller , mockFgaClient * mockclient.MockSdkClient , storeID string ) {
248
+ mockCreateStore := mockclient .NewMockSdkClientCreateStoreRequestInterface (mockCtrl )
249
+ mockFgaClient .EXPECT ().CreateStore (context .Background ()).Return (mockCreateStore )
250
+ mockCreateStore .EXPECT ().Body (gomock .Any ()).Return (mockCreateStore )
251
+ mockCreateStore .EXPECT ().Execute ().Return (& client.ClientCreateStoreResponse {Id : storeID }, nil )
252
+ mockFgaClient .EXPECT ().SetStoreId (storeID )
253
+ }
254
+
255
+ func setupWriteModelMock (mockCtrl * gomock.Controller , mockFgaClient * mockclient.MockSdkClient , modelID string ) {
256
+ mockWriteModel := mockclient .NewMockSdkClientWriteAuthorizationModelRequestInterface (mockCtrl )
257
+ mockFgaClient .EXPECT ().WriteAuthorizationModel (context .Background ()).Return (mockWriteModel )
258
+ mockWriteModel .EXPECT ().Body (gomock .Any ()).Return (mockWriteModel )
259
+ mockWriteModel .EXPECT ().Execute ().Return (
260
+ & client.ClientWriteAuthorizationModelResponse {AuthorizationModelId : modelID },
261
+ nil ,
262
+ )
263
+ }
264
+
265
+ func setupWriteAssertionsMock (
266
+ mockCtrl * gomock.Controller ,
267
+ mockFgaClient * mockclient.MockSdkClient ,
268
+ expectedAssertions []client.ClientAssertion ,
269
+ expectedOptions client.ClientWriteAssertionsOptions ,
270
+ ) {
271
+ mockWriteAssertions := mockclient .NewMockSdkClientWriteAssertionsRequestInterface (mockCtrl )
272
+ mockFgaClient .EXPECT ().WriteAssertions (context .Background ()).Return (mockWriteAssertions )
273
+ mockWriteAssertions .EXPECT ().Body (expectedAssertions ).Return (mockWriteAssertions )
274
+ mockWriteAssertions .EXPECT ().Options (expectedOptions ).Return (mockWriteAssertions )
275
+ mockWriteAssertions .EXPECT ().Execute ().Return (nil , nil )
276
+ }
0 commit comments