Skip to content

Commit

Permalink
fix: ensure settings are passed with properties in tenant set (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbell authored Nov 3, 2023
1 parent ca0f0c4 commit ae1eb47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
19 changes: 16 additions & 3 deletions knock/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,24 @@ func (ts *tenantsService) Get(ctx context.Context, getTenantRequest *GetTenantRe
func (ts *tenantsService) Set(ctx context.Context, setTenantRequest *SetTenantRequest) (*Tenant, error) {
path := tenantAPIPath(setTenantRequest.ID)

if len(setTenantRequest.Properties) == 0 {
return nil, &Error{msg: "Must set at least one property"}
if len(setTenantRequest.Properties) == 0 || len(setTenantRequest.Settings) == 0 {
return nil, &Error{msg: "Must set at least one property or settings"}
}

req, err := ts.client.newRequest(http.MethodPut, path, setTenantRequest.Properties, nil)
// Copy the properties ready to be sent to the API
properties := make(map[string]interface{})

for k, v := range setTenantRequest.Properties {
properties[k] = v
}

if len(setTenantRequest.Settings) > 0 {
// For simplicity we add "settings" under a key in properties, because that's what
// we want to send to the API anyway.
properties["settings"] = setTenantRequest.Settings
}

req, err := ts.client.newRequest(http.MethodPut, path, properties, nil)
if err != nil {
return nil, errors.Wrap(err, "error creating request for set tenant")
}
Expand Down
19 changes: 19 additions & 0 deletions knock/tenants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package knock

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"io"

qt "github.com/frankban/quicktest"
)
Expand All @@ -14,7 +16,24 @@ func TestTenants_Set(t *testing.T) {

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)

bodyBytes, _ := io.ReadAll(r.Body)
requestData := make(map[string]interface{})
json.Unmarshal([]byte(bodyBytes), &requestData)
expected := map[string]interface{}{
"name": "cool-tenant-1",
"settings": map[string]interface{}{
"branding": map[string]interface{}{
"primary_color": "#FFFFFF",
},
},
}

// Make sure the request looks as expected
c.Assert(expected, qt.DeepEquals, requestData)

out := `{"__typename":"Tenant","created_at":null,"id":"cool-tenant2","properties":{"name":"cool-tenant-1"},"settings":{"branding":{"primary_color":"#FFFFFF"}},"updated_at":"2022-05-26T13:59:20.701Z"}`

_, err := w.Write([]byte(out))
c.Assert(err, qt.IsNil)
}))
Expand Down

0 comments on commit ae1eb47

Please sign in to comment.