Skip to content

Commit

Permalink
fix(go-sdk): ulid for auth model id and store id to be validated (#133)
Browse files Browse the repository at this point in the history
* fix(go-sdk): ulid for auth model id and store id to be validated

Go SDK should validate auth model id and store id to ensure they are valid ulid
Part of openfga/go-sdk#19

* fix: update based on code review feedback

* fix(go-sdk): validation connection and model for batch operations (#135)

Close openfga/go-sdk#21
  • Loading branch information
adriantam committed Jun 20, 2023
1 parent 1af2c62 commit d4ac29b
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 36 deletions.
10 changes: 9 additions & 1 deletion config/clients/go/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
"destinationFilename": "oauth2/token.go",
"templateType": "SupportingFiles"
},
"oauth2/token_test.go": {}
"oauth2/token_test.go": {},
"utils/utils.mustache": {
"destinationFilename": "utils/utils.go",
"templateType": "SupportingFiles"
},
"utils/utils_test.mustache": {
"destinationFilename": "utils/utils_test.go",
"templateType": "SupportingFiles"
}
}
}
5 changes: 5 additions & 0 deletions config/clients/go/template/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"time"
{{#imports}} "{{import}}"
{{/imports}}

"{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/utils"
)

// Linger please
Expand Down Expand Up @@ -121,6 +123,9 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
if a.client.cfg.StoreId == "" {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("Configuration.StoreId is required and must be specified to call this method")
}
if !utils.IsWellFormedUlidString(a.client.cfg.StoreId) {
return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("Configuration.StoreId is invalid")
}
{{/pathParams.0}}
localVarPath := "{{{path}}}"{{#pathParams}}
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", _neturl.PathEscape({{#-first}}a.client.cfg.StoreId{{/-first}}{{^-first}}parameterToString(r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"){{/-first}}), -1){{/pathParams}}
Expand Down
33 changes: 22 additions & 11 deletions config/clients/go/template/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
}
})

t.Run("Providing invalid storeid should result in error", func(t *testing.T) {
_, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "invalid",
})

if err == nil {
t.Fatalf("Expect error when invalid storeid is provided")
}
})

t.Run("In ApiToken credential method, apiToken is required in the Credentials Config", func(t *testing.T) {
_, err := NewConfiguration(Configuration{
ApiHost: "https://api.{{sampleApiDomain}}",
Expand All @@ -77,7 +88,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
t.Run("should issue a successful network call when using ApiToken credential method", func(t *testing.T) {
configuration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand Down Expand Up @@ -120,7 +131,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
t.Run("In ClientCredentials method, providing no client id, secret, audience or issuer should error", func(t *testing.T) {
_, err := NewConfiguration(Configuration{
ApiHost: "https://api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand All @@ -135,7 +146,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

_, err = NewConfiguration(Configuration{
ApiHost: "https://api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand All @@ -152,7 +163,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

_, err = NewConfiguration(Configuration{
ApiHost: "https://api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand All @@ -169,7 +180,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

_, err = NewConfiguration(Configuration{
ApiHost: "https://api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodApiToken,
Config: &credentials.Config{
Expand All @@ -186,7 +197,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {

_, err = NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodClientCredentials,
Config: &credentials.Config{
Expand All @@ -206,7 +217,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
t.Run("should issue a network call to get the token at the first request if client id is provided", func(t *testing.T) {
configuration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodClientCredentials,
Config: &credentials.Config{
Expand Down Expand Up @@ -274,7 +285,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
t.Run("should not issue a network call to get the token at the first request if the clientId is not provided", func(t *testing.T) {
configuration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
Credentials: &credentials.Credentials{
Method: credentials.CredentialsMethodNone,
Config: &credentials.Config{ClientCredentialsApiTokenIssuer: "tokenissuer.api.example"},
Expand Down Expand Up @@ -382,7 +393,7 @@ func Test{{appShortName}}ApiConfiguration(t *testing.T) {
func Test{{appShortName}}Api(t *testing.T) {
configuration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
})
if err != nil {
t.Fatalf("%v", err)
Expand Down Expand Up @@ -1118,7 +1129,7 @@ func Test{{appShortName}}Api(t *testing.T) {

updatedConfiguration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
RetryParams: &RetryParams{
MaxRetry: 3,
MinWaitInMs: 5,
Expand Down Expand Up @@ -1184,7 +1195,7 @@ func Test{{appShortName}}Api(t *testing.T) {
)
updatedConfiguration, err := NewConfiguration(Configuration{
ApiHost: "api.{{sampleApiDomain}}",
StoreId: "6c181474-aaa1-4df7-8929-6e7b3a992754",
StoreId: "01GXSB9YR785C4FYS3C0RTG7B2",
RetryParams: &RetryParams{
MaxRetry: 2,
MinWaitInMs: 5,
Expand Down
Loading

0 comments on commit d4ac29b

Please sign in to comment.