|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/openfga/cli/internal/fga" |
| 6 | + mockclient "github.com/openfga/cli/internal/mocks" |
| 7 | + "github.com/openfga/cli/internal/storetest" |
| 8 | + "github.com/openfga/go-sdk/client" |
| 9 | + "go.uber.org/mock/gomock" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestImportStore(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + t.Run("imports assertions into store", func(t *testing.T) { |
| 17 | + mockCtrl := gomock.NewController(t) |
| 18 | + defer mockCtrl.Finish() |
| 19 | + |
| 20 | + mockFgaClient := mockclient.NewMockSdkClient(mockCtrl) |
| 21 | + clientConfig := fga.ClientConfig{} |
| 22 | + |
| 23 | + mockWriteAssertions := mockclient.NewMockSdkClientWriteAssertionsRequestInterface(mockCtrl) |
| 24 | + mockCreateStore := mockclient.NewMockSdkClientCreateStoreRequestInterface(mockCtrl) |
| 25 | + mockWriteModel := mockclient.NewMockSdkClientWriteAuthorizationModelRequestInterface(mockCtrl) |
| 26 | + |
| 27 | + expectedAssertions := []client.ClientAssertion{{ |
| 28 | + User: "user:anne", |
| 29 | + Relation: "reader", |
| 30 | + Object: "document:doc1", |
| 31 | + Expectation: true, |
| 32 | + }} |
| 33 | + |
| 34 | + modelID := "model-1" |
| 35 | + storeID := "store-1" |
| 36 | + expectedOptions := client.ClientWriteAssertionsOptions{ |
| 37 | + AuthorizationModelId: &modelID, |
| 38 | + StoreId: &storeID, |
| 39 | + } |
| 40 | + |
| 41 | + mockFgaClient.EXPECT().CreateStore(context.Background()).Return(mockCreateStore) |
| 42 | + mockCreateStore.EXPECT().Body(gomock.Any()).Return(mockCreateStore) |
| 43 | + mockCreateStore.EXPECT().Execute().Return(&client.ClientCreateStoreResponse{Id: "store-1"}, nil) |
| 44 | + |
| 45 | + mockFgaClient.EXPECT().SetStoreId("store-1") |
| 46 | + |
| 47 | + mockFgaClient.EXPECT().WriteAuthorizationModel(context.Background()).Return(mockWriteModel) |
| 48 | + mockWriteModel.EXPECT().Body(gomock.Any()).Return(mockWriteModel) |
| 49 | + mockWriteModel.EXPECT().Execute().Return(&client.ClientWriteAuthorizationModelResponse{AuthorizationModelId: "model-1"}, nil) |
| 50 | + |
| 51 | + mockFgaClient.EXPECT().WriteAssertions(context.Background()).Return(mockWriteAssertions) |
| 52 | + mockWriteAssertions.EXPECT().Body(expectedAssertions).Return(mockWriteAssertions) |
| 53 | + mockWriteAssertions.EXPECT().Options(expectedOptions).Return(mockWriteAssertions) |
| 54 | + mockWriteAssertions.EXPECT().Execute().Return(nil, nil) |
| 55 | + |
| 56 | + testStore := &storetest.StoreData{ |
| 57 | + Name: "test-store", |
| 58 | + Model: `type user |
| 59 | + type document |
| 60 | + relations |
| 61 | + define reader: [user]`, |
| 62 | + Tests: []storetest.ModelTest{ |
| 63 | + { |
| 64 | + Name: "Test", |
| 65 | + Check: []storetest.ModelTestCheck{ |
| 66 | + { |
| 67 | + User: "user:anne", |
| 68 | + Object: "document:doc1", |
| 69 | + Assertions: map[string]bool{ |
| 70 | + "reader": true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + _, err := importStore(&clientConfig, mockFgaClient, testStore, "", "", 1, 1, "") |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + t.Errorf("expected no error, got %v", err) |
| 81 | + } |
| 82 | + }) |
| 83 | +} |
0 commit comments