Skip to content

Commit

Permalink
release(v0.3.4): with a fix for write model with conditions (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Jan 22, 2024
2 parents 3ecb638 + f5a35e2 commit 97890db
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 24 deletions.
6 changes: 6 additions & 0 deletions config/clients/go/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v0.3.4

### [0.3.4](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.3.3...v0.3.4) (2024-01-22)

- fix: WriteAuthorizationModel was not passing conditions to API

## v0.3.3

### [0.3.3](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.3.2...v0.3.3) (2023-12-21)
Expand Down
7 changes: 5 additions & 2 deletions config/clients/go/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"sdkId": "go",
"gitRepoId": "go-sdk",
"packageName": "openfga",
"packageVersion": "0.3.3",
"packageVersion": "0.3.4",
"packageDescription": "Go SDK for OpenFGA",
"packageDetailedDescription": "This is an autogenerated Go SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).",
"fossaComplianceNoticeId": "41c01c64-f74a-414a-9e39-7aeca87bc47b",
Expand Down Expand Up @@ -79,7 +79,10 @@
"example/Makefile": {},
"example/README.md": {},
"example/example1/example1.go": {},
"example/example1/go.mod": {},
"example/example1/go.mod.mustache": {
"destinationFilename": "example/example1/go.mod",
"templateType": "SupportingFiles"
},
"example/example1/go.sum": {}
}
}
5 changes: 1 addition & 4 deletions config/clients/go/template/client/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -813,10 +813,7 @@ func (request *SdkClientWriteAuthorizationModelRequest) GetContext() _context.Co
}

func (client *{{appShortName}}Client) WriteAuthorizationModelExecute(request SdkClientWriteAuthorizationModelRequestInterface) (*ClientWriteAuthorizationModelResponse, error) {
data, _, err := client.{{appShortName}}Api.WriteAuthorizationModel(request.GetContext()).Body(fgaSdk.WriteAuthorizationModelRequest{
TypeDefinitions: request.GetBody().TypeDefinitions,
SchemaVersion: request.GetBody().SchemaVersion,
}).Execute()
data, _, err := client.{{appShortName}}Api.WriteAuthorizationModel(request.GetContext()).Body(*request.GetBody()).Execute()
if err != nil {
return nil, err
}
Expand Down
218 changes: 217 additions & 1 deletion config/clients/go/template/client/client_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func Test{{appShortName}}Client(t *testing.T) {
Method: http.MethodGet,
}

var expectedResponse openfga.ListStoresResponse
var expectedResponse {{packageName}}.ListStoresResponse
if err := json.Unmarshal([]byte(test.JsonResponse), &expectedResponse); err != nil {
t.Fatalf("%v", err)
}
Expand Down Expand Up @@ -520,6 +520,222 @@ func Test{{appShortName}}Client(t *testing.T) {
}
})

t.Run("WriteAuthorizationModelWithCondition", func(t *testing.T) {
test := TestDefinition{
Name: "WriteAuthorizationModelWithCondition",
JsonResponse: `{"authorization_model_id":"01GXSA8YR785C4FYS3C0RTG7B1"}`,
ResponseStatus: http.StatusOK,
Method: http.MethodPost,
RequestPath: "authorization-models",
}
requestBody := ClientWriteAuthorizationModelRequest{
SchemaVersion: "1.1",
TypeDefinitions: []{{packageName}}.TypeDefinition{
{
Type: "user",
Relations: &map[string]{{packageName}}.Userset{},
},
{
Type: "document",
Relations: &map[string]{{packageName}}.Userset{
"writer": {This: &map[string]interface{}{}},
"viewer": {Union: &{{packageName}}.Usersets{
Child: []{{packageName}}.Userset{
{This: &map[string]interface{}{}},
{ComputedUserset: &{{packageName}}.ObjectRelation{
Object: {{packageName}}.PtrString(""),
Relation: {{packageName}}.PtrString("writer"),
}},
},
}},
},
Metadata: &{{packageName}}.Metadata{
Relations: &map[string]{{packageName}}.RelationMetadata{
"writer": {
DirectlyRelatedUserTypes: &[]{{packageName}}.RelationReference{
{Type: "user"},
{Type: "user", Condition: {{packageName}}.PtrString("ViewCountLessThan200")},
},
},
"viewer": {
DirectlyRelatedUserTypes: &[]{{packageName}}.RelationReference{
{Type: "user"},
},
},
},
},
},
},
Conditions: &map[string]{{packageName}}.Condition{
"ViewCountLessThan200": {
Name: "ViewCountLessThan200",
Expression: "ViewCount < 200",
Parameters: &map[string]{{packageName}}.ConditionParamTypeRef{
"ViewCount": {
TypeName: {{packageName}}.INT,
},
"Type": {
TypeName: {{packageName}}.STRING,
},
"Name": {
TypeName: {{packageName}}.STRING,
},
},
},
},
}

var expectedResponse {{packageName}}.WriteAuthorizationModelResponse
if err := json.Unmarshal([]byte(test.JsonResponse), &expectedResponse); err != nil {
t.Fatalf("%v", err)
}

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterMatcherResponder(
test.Method,
fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, fgaClient.GetConfig().StoreId, test.RequestPath),
httpmock.BodyContainsString(`"ViewCountLessThan200"`),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil
}
return resp, nil
},
)
options := ClientWriteAuthorizationModelOptions{}
got, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
t.Fatalf("%v", err)
}

_, err = got.MarshalJSON()
if err != nil {
t.Fatalf("%v", err)
}

if got.GetAuthorizationModelId() != expectedResponse.GetAuthorizationModelId() {
t.Fatalf("{{appShortName}}Client.%v() / AuthorizationModelId = %v, want %v", test.Name, got.GetAuthorizationModelId(), expectedResponse.GetAuthorizationModelId())
}

// WriteAuthorizationModel without options should work
_, err = fgaClient.WriteAuthorizationModel(context.Background()).Body(requestBody).Execute()
if err != nil {
t.Fatalf("%v", err)
}
})

t.Run("WriteAuthorizationModelWithCondition2", func(t *testing.T) {
test := TestDefinition{
Name: "WriteAuthorizationModelWithCondition2",
JsonResponse: `{"authorization_model_id":"01GXSA8YR785C4FYS3C0RTG7B1"}`,
ResponseStatus: http.StatusOK,
Method: http.MethodPost,
RequestPath: "authorization-models",
}

schemaVersion := "1.1"
typeDefs := []{{packageName}}.TypeDefinition{
{
Type: "user",
Relations: &map[string]{{packageName}}.Userset{},
},
{
Type: "document",
Relations: &map[string]{{packageName}}.Userset{
"writer": {This: &map[string]interface{}{}},
"viewer": {Union: &{{packageName}}.Usersets{
Child: []{{packageName}}.Userset{
{This: &map[string]interface{}{}},
{ComputedUserset: &{{packageName}}.ObjectRelation{
Object: {{packageName}}.PtrString(""),
Relation: {{packageName}}.PtrString("writer"),
}},
},
}},
},
Metadata: &{{packageName}}.Metadata{
Relations: &map[string]{{packageName}}.RelationMetadata{
"writer": {
DirectlyRelatedUserTypes: &[]{{packageName}}.RelationReference{
{Type: "user"},
{Type: "user", Condition: {{packageName}}.PtrString("ViewCountLessThan200")},
},
},
"viewer": {
DirectlyRelatedUserTypes: &[]{{packageName}}.RelationReference{
{Type: "user"},
},
},
},
},
},
}
conditions := map[string]{{packageName}}.Condition{
"ViewCountLessThan200": {
Name: "ViewCountLessThan200",
Expression: "ViewCount < 200",
Parameters: &map[string]{{packageName}}.ConditionParamTypeRef{
"ViewCount": {
TypeName: {{packageName}}.INT,
},
"Type": {
TypeName: {{packageName}}.STRING,
},
"Name": {
TypeName: {{packageName}}.STRING,
},
},
},
}
requestBody := ClientWriteAuthorizationModelRequest{
SchemaVersion: schemaVersion,
TypeDefinitions: typeDefs,
Conditions: &conditions,
}

var expectedResponse {{packageName}}.WriteAuthorizationModelResponse
if err := json.Unmarshal([]byte(test.JsonResponse), &expectedResponse); err != nil {
t.Fatalf("%v", err)
}

httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterMatcherResponder(
test.Method,
fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, fgaClient.GetConfig().StoreId, test.RequestPath),
httpmock.BodyContainsString(`"ViewCountLessThan200"`),
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse)
if err != nil {
return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil
}
return resp, nil
},
)
options := ClientWriteAuthorizationModelOptions{}
got, err := fgaClient.WriteAuthorizationModel(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
t.Fatalf("%v", err)
}

_, err = got.MarshalJSON()
if err != nil {
t.Fatalf("%v", err)
}

if got.GetAuthorizationModelId() != expectedResponse.GetAuthorizationModelId() {
t.Fatalf("{{appShortName}}Client.%v() / AuthorizationModelId = %v, want %v", test.Name, got.GetAuthorizationModelId(), expectedResponse.GetAuthorizationModelId())
}

// WriteAuthorizationModel without options should work
_, err = fgaClient.WriteAuthorizationModel(context.Background()).Body(requestBody).Execute()
if err != nil {
t.Fatalf("%v", err)
}
})

t.Run("ReadAuthorizationModel", func(t *testing.T) {
test := TestDefinition{
Name: "ReadAuthorizationModel",
Expand Down
10 changes: 0 additions & 10 deletions config/clients/go/template/example/example1/go.mod

This file was deleted.

10 changes: 10 additions & 0 deletions config/clients/go/template/example/example1/go.mod.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module example1

go 1.20

require {{gitHost}}/{{gitUserId}}/{{gitRepoId}} v{{packageVersion}}

require golang.org/x/sync v0.6.0 // indirect

// To reference local build, uncomment below and run `go mod tidy`
//replace {{gitHost}}/{{gitUserId}}/{{gitRepoId}} v{{packageVersion}} => ../../
8 changes: 4 additions & 4 deletions config/clients/go/template/example/example1/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/openfga/go-sdk v0.3.2 h1:sed3zQLZTLLGzdqZYQZmiBoSxKWqJD+sk4KtUb+yVpU=
github.com/openfga/go-sdk v0.3.2/go.mod h1:W4SNYMSxptGOtA9aGYxsYUmSC7LaZYP7y9qbT36ouCc=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
github.com/openfga/go-sdk v0.3.3 h1:eMZGCEDW/sz9S3gNxbpO5rNpDezz7cjT+zwjpDgqTt0=
github.com/openfga/go-sdk v0.3.3/go.mod h1:W4SNYMSxptGOtA9aGYxsYUmSC7LaZYP7y9qbT36ouCc=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
2 changes: 1 addition & 1 deletion config/clients/go/template/go.mod.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.20

require (
github.com/jarcoal/httpmock v1.3.1
golang.org/x/sync v0.5.0
golang.org/x/sync v0.6.0
)
4 changes: 2 additions & 2 deletions config/clients/go/template/go.sum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=

0 comments on commit 97890db

Please sign in to comment.