Skip to content

Commit ae1eb47

Browse files
authored
fix: ensure settings are passed with properties in tenant set (#25)
1 parent ca0f0c4 commit ae1eb47

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

knock/tenants.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,24 @@ func (ts *tenantsService) Get(ctx context.Context, getTenantRequest *GetTenantRe
118118
func (ts *tenantsService) Set(ctx context.Context, setTenantRequest *SetTenantRequest) (*Tenant, error) {
119119
path := tenantAPIPath(setTenantRequest.ID)
120120

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

125-
req, err := ts.client.newRequest(http.MethodPut, path, setTenantRequest.Properties, nil)
125+
// Copy the properties ready to be sent to the API
126+
properties := make(map[string]interface{})
127+
128+
for k, v := range setTenantRequest.Properties {
129+
properties[k] = v
130+
}
131+
132+
if len(setTenantRequest.Settings) > 0 {
133+
// For simplicity we add "settings" under a key in properties, because that's what
134+
// we want to send to the API anyway.
135+
properties["settings"] = setTenantRequest.Settings
136+
}
137+
138+
req, err := ts.client.newRequest(http.MethodPut, path, properties, nil)
126139
if err != nil {
127140
return nil, errors.Wrap(err, "error creating request for set tenant")
128141
}

knock/tenants_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package knock
22

33
import (
44
"context"
5+
"encoding/json"
56
"net/http"
67
"net/http/httptest"
78
"testing"
9+
"io"
810

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

1517
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1618
w.WriteHeader(200)
19+
20+
bodyBytes, _ := io.ReadAll(r.Body)
21+
requestData := make(map[string]interface{})
22+
json.Unmarshal([]byte(bodyBytes), &requestData)
23+
expected := map[string]interface{}{
24+
"name": "cool-tenant-1",
25+
"settings": map[string]interface{}{
26+
"branding": map[string]interface{}{
27+
"primary_color": "#FFFFFF",
28+
},
29+
},
30+
}
31+
32+
// Make sure the request looks as expected
33+
c.Assert(expected, qt.DeepEquals, requestData)
34+
1735
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"}`
36+
1837
_, err := w.Write([]byte(out))
1938
c.Assert(err, qt.IsNil)
2039
}))

0 commit comments

Comments
 (0)