@@ -2,97 +2,72 @@ package store
22
33import (
44 "context"
5+ "testing"
6+ "time"
7+
58 "github.com/openfga/cli/internal/fga"
69 mockclient "github.com/openfga/cli/internal/mocks"
710 "github.com/openfga/cli/internal/storetest"
811 "github.com/openfga/go-sdk/client"
912 "go.uber.org/mock/gomock"
10- "testing"
11- "time"
1213)
1314
1415func TestImportStore (t * testing.T ) {
1516 t .Parallel ()
1617
17- mockCtrl := gomock .NewController (t )
18- defer mockCtrl .Finish ()
19-
20- mockFgaClient := mockclient .NewMockSdkClient (mockCtrl )
21- clientConfig := fga.ClientConfig {}
22-
2318 expectedAssertions := []client.ClientAssertion {{
2419 User : "user:anne" ,
2520 Relation : "reader" ,
2621 Object : "document:doc1" ,
2722 Expectation : true ,
2823 }}
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 }
5926
6027 importStoreTests := []struct {
6128 name string
6229 mockWriteAssertions bool
63- mockGetStore bool
6430 mockCreateStore bool
6531 mockWriteModel bool
6632 testStore storetest.StoreData
67- storeId string
6833 }{
6934 {
7035 name : "import store with assertions" ,
7136 mockWriteAssertions : true ,
72- mockGetStore : false ,
7337 mockWriteModel : true ,
7438 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+ },
7757 },
7858 {
7959 name : "create new store without assertions" ,
8060 mockWriteAssertions : false ,
8161 mockCreateStore : true ,
82- mockGetStore : false ,
8362 mockWriteModel : false ,
84- testStore : storetest.StoreData {
85- Name : "test-store" ,
86- },
87- storeId : "" ,
63+ testStore : storetest.StoreData {Name : "test-store" },
8864 },
8965 {
9066 name : "create new store without check assertions" ,
9167 mockCreateStore : true ,
9268 mockWriteModel : true ,
9369 mockWriteAssertions : false ,
9470 testStore : storetest.StoreData {
95- Name : "test-store" ,
9671 Model : `type user
9772 type document
9873 relations
@@ -102,41 +77,84 @@ func TestImportStore(t *testing.T) {
10277 Name : "Test" ,
10378 ListObjects : []storetest.ModelTestListObjects {
10479 {
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" }},
11083 },
11184 },
11285 },
11386 },
11487 },
115- storeId : "" ,
11688 },
11789 {
11890 name : "do not write assertions if imported store does not have a model" ,
11991 mockCreateStore : true ,
12092 mockWriteAssertions : false ,
12193 testStore : storetest.StoreData {
122- Name : "test-store" ,
12394 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" },
13696 },
13797 },
138- storeId : "" ,
13998 },
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+ }{
140158 {
141159 name : "update store with assertions" ,
142160 mockWriteAssertions : true ,
@@ -163,7 +181,6 @@ func TestImportStore(t *testing.T) {
163181 },
164182 },
165183 },
166- storeId : storeID ,
167184 },
168185 {
169186 name : "update store without assertions" ,
@@ -177,56 +194,83 @@ func TestImportStore(t *testing.T) {
177194 relations
178195 define reader: [user]` ,
179196 },
180- storeId : storeID ,
181197 },
182198 }
183199
184200 for _ , test := range importStoreTests {
185-
186201 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 ()
187210
188211 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 )
194213 } else {
195214 mockFgaClient .EXPECT ().WriteAssertions (context .Background ()).Times (0 )
196215 }
197216
198217 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 )
211219 }
212220
213221 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 )
224223 }
225224
225+ _ , err := importStore (& clientConfig , mockFgaClient , & test .testStore , "" , storeID , 1 , 1 , "" )
226226 if err != nil {
227227 t .Errorf ("expected no error, got %v" , err )
228228 }
229-
230229 })
231230 }
232231}
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