@@ -49,6 +49,7 @@ const (
49
49
// createStore creates a new store with the given client configuration and store data.
50
50
func createStore (
51
51
clientConfig * fga.ClientConfig ,
52
+ fgaClient client.SdkClient ,
52
53
storeData * storetest.StoreData ,
53
54
format authorizationmodel.ModelFormat ,
54
55
fileName string ,
@@ -58,7 +59,7 @@ func createStore(
58
59
storeDataName = strings .TrimSuffix (path .Base (fileName ), ".fga.yaml" )
59
60
}
60
61
61
- createStoreAndModelResponse , err := CreateStoreWithModel (* clientConfig , storeDataName , storeData .Model , format )
62
+ createStoreAndModelResponse , err := CreateStoreWithModel (fgaClient , storeDataName , storeData .Model , format )
62
63
if err != nil {
63
64
return nil , err
64
65
}
@@ -122,13 +123,18 @@ func importStore(
122
123
return nil , err
123
124
}
124
125
125
- if len (storeData .Tuples ) == 0 {
126
- return response , nil
126
+ if len (storeData .Tuples ) != 0 {
127
+ err = importTuples (fgaClient , storeData .Tuples , maxTuplesPerWrite , maxParallelRequests )
128
+ if err != nil {
129
+ return nil , err
130
+ }
127
131
}
128
132
129
- err = importTuples (fgaClient , storeData .Tuples , maxTuplesPerWrite , maxParallelRequests )
130
- if err != nil {
131
- return nil , err
133
+ if len (storeData .Tests ) != 0 && response .Model != nil {
134
+ err = importAssertions (fgaClient , storeData .Tests , response .Store .Id , response .Model .AuthorizationModelId )
135
+ if err != nil {
136
+ return nil , err
137
+ }
132
138
}
133
139
134
140
return response , nil
@@ -143,7 +149,7 @@ func createOrUpdateStore(
143
149
fileName string ,
144
150
) (* CreateStoreAndModelResponse , error ) {
145
151
if storeID == "" {
146
- return createStore (clientConfig , storeData , format , fileName )
152
+ return createStore (clientConfig , fgaClient , storeData , format , fileName )
147
153
}
148
154
149
155
return updateStore (clientConfig , fgaClient , storeData , format , storeID )
@@ -183,6 +189,53 @@ func importTuples(
183
189
return nil
184
190
}
185
191
192
+ func importAssertions (
193
+ fgaClient client.SdkClient ,
194
+ modelTests []storetest.ModelTest ,
195
+ storeID string ,
196
+ modelID string ,
197
+ ) error {
198
+ var assertions []client.ClientAssertion
199
+
200
+ for _ , modelTest := range modelTests {
201
+ if len (modelTest .Check ) > 0 {
202
+ checkAssertions := getCheckAssertions (modelTest .Check )
203
+ assertions = append (assertions , checkAssertions ... )
204
+ }
205
+ }
206
+
207
+ if len (assertions ) > 0 {
208
+ writeOptions := client.ClientWriteAssertionsOptions {
209
+ AuthorizationModelId : & modelID ,
210
+ StoreId : & storeID ,
211
+ }
212
+
213
+ _ , err := fgaClient .WriteAssertions (context .Background ()).Body (assertions ).Options (writeOptions ).Execute ()
214
+ if err != nil {
215
+ return fmt .Errorf ("failed to import assertions: %w" , err )
216
+ }
217
+ }
218
+
219
+ return nil
220
+ }
221
+
222
+ func getCheckAssertions (checkTests []storetest.ModelTestCheck ) []client.ClientAssertion {
223
+ var assertions []client.ClientAssertion
224
+
225
+ for _ , checkTest := range checkTests {
226
+ for relation , expectation := range checkTest .Assertions {
227
+ assertions = append (assertions , client.ClientAssertion {
228
+ User : checkTest .User ,
229
+ Relation : relation ,
230
+ Object : checkTest .Object ,
231
+ Expectation : expectation ,
232
+ })
233
+ }
234
+ }
235
+
236
+ return assertions
237
+ }
238
+
186
239
func createProgressBar (total int ) * progressbar.ProgressBar {
187
240
return progressbar .NewOptions (total ,
188
241
progressbar .OptionSetWriter (os .Stderr ),
0 commit comments