Skip to content

Commit

Permalink
Merge pull request #1480 from auyer/fixes-update-seats
Browse files Browse the repository at this point in the history
Fixes update seats with wrong body: expected array, sent single object
  • Loading branch information
jacobbednarz committed Jan 31, 2024
2 parents 4d9d951 + 6c9c526 commit 76563b7
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changelog/1480.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
access_seats: UpdateAccessUserSeat: fix parameters not being an array when sending to the api. This caused an error when updating a user's seat
```

```release-note:enhancement
access_seats: Add `UpdateAccessUsersSeats` with an array as input for multiple operations
```
45 changes: 44 additions & 1 deletion access_seats.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ type UpdateAccessUserSeatParams struct {
GatewaySeat *bool `json:"gateway_seat"`
}

// UpdateAccessUsersSeatsParams represents the update payload for multiple access seats.
type UpdateAccessUsersSeatsParams []struct {
SeatUID string `json:"seat_uid,omitempty"`
AccessSeat *bool `json:"access_seat"`
GatewaySeat *bool `json:"gateway_seat"`
}

// AccessUserSeatResponse represents the response from the access user seat endpoints.
type UpdateAccessUserSeatResponse struct {
Response
Result []AccessUpdateAccessUserSeatResult `json:"result"`
ResultInfo `json:"result_info"`
}

// UpdateAccessUserSeat updates a Access User Seat.
// UpdateAccessUserSeat updates a single Access User Seat.
//
// API documentation: https://developers.cloudflare.com/api/operations/zero-trust-seats-update-a-user-seat
func (api *API) UpdateAccessUserSeat(ctx context.Context, rc *ResourceContainer, params UpdateAccessUserSeatParams) ([]AccessUpdateAccessUserSeatResult, error) {
Expand All @@ -53,6 +60,42 @@ func (api *API) UpdateAccessUserSeat(ctx context.Context, rc *ResourceContainer,
rc.Identifier,
)

// this requests expects an array of params, but this method only accepts a single param
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, []UpdateAccessUserSeatParams{params})
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

var updateAccessUserSeatResponse UpdateAccessUserSeatResponse
err = json.Unmarshal(res, &updateAccessUserSeatResponse)
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return updateAccessUserSeatResponse.Result, nil
}

// UpdateAccessUsersSeats updates many Access User Seats.
//
// API documentation: https://developers.cloudflare.com/api/operations/zero-trust-seats-update-a-user-seat
func (api *API) UpdateAccessUsersSeats(ctx context.Context, rc *ResourceContainer, params UpdateAccessUsersSeatsParams) ([]AccessUpdateAccessUserSeatResult, error) {
if rc.Level != AccountRouteLevel {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf(errInvalidResourceContainerAccess, rc.Level)
}

for _, param := range params {
if param.SeatUID == "" {
return []AccessUpdateAccessUserSeatResult{}, errMissingAccessSeatUID
}
}

uri := fmt.Sprintf(
"/%s/%s/access/seats",
rc.Level,
rc.Identifier,
)

// this requests expects an array of params, but this method only accepts a single param
res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params)
if err != nil {
return []AccessUpdateAccessUserSeatResult{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
Expand Down
100 changes: 100 additions & 0 deletions access_seats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloudflare

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
Expand All @@ -11,6 +12,7 @@ import (
)

var testAccessGroupSeatUID = "access-group-seat-uid"
var testAccessGroupSeatUID2 = "access-group-seat-uid2"

func TestUpdateAccessUserSeat_ZoneIsNotSupported(t *testing.T) {
setup()
Expand All @@ -28,12 +30,28 @@ func TestUpdateAccessUserSeat_MissingUID(t *testing.T) {
assert.EqualError(t, err, "missing required access seat UID")
}

func TestUpdateAccessUsersSeats_MissingUID(t *testing.T) {
setup()
defer teardown()

_, err := client.UpdateAccessUsersSeats(context.Background(), testAccountRC, UpdateAccessUsersSeatsParams{{GatewaySeat: BoolPtr(false), SeatUID: "seat_id"}, {SeatUID: "", AccessSeat: BoolPtr(true)}})
assert.EqualError(t, err, "missing required access seat UID")
}

func TestUpdateAccessUserSeat(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)

req := []UpdateAccessUserSeatParams{}

// Try to decode the request body into the struct.
err := json.NewDecoder(r.Body).Decode(&req)
assert.NoError(t, err, "Failed to decode request body into UpdateAccessUserSeatParams")
assert.Equal(t, len(req), 1, "Expected 1 seat to be updated, got %d", len(req))

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"errors": [],
Expand Down Expand Up @@ -80,3 +98,85 @@ func TestUpdateAccessUserSeat(t *testing.T) {
assert.Equal(t, want, actual)
}
}

func TestUpdateAccessUsersSeats(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method)

req := []UpdateAccessUserSeatParams{}

// Try to decode the request body into the struct.
err := json.NewDecoder(r.Body).Decode(&req)
assert.NoError(t, err, "Failed to decode request body into UpdateAccessUserSeatParams")
assert.Equal(t, len(req), 2, "Expected 2 seat to be updated, got %d", len(req))

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"errors": [],
"messages": [],
"result": [
{
"access_seat": false,
"created_at": "2014-01-01T05:20:00.12345Z",
"gateway_seat": false,
"seat_uid": "%s",
"updated_at": "2014-01-01T05:20:00.12345Z"
},
{
"access_seat": false,
"created_at": "2014-01-01T05:20:00.12345Z",
"gateway_seat": false,
"seat_uid": "%s",
"updated_at": "2014-01-01T05:20:00.12345Z"
}
],
"success": true,
"result_info": {
"count": 1,
"page": 1,
"per_page": 20,
"total_count": 2000
}
}
`, testAccessGroupSeatUID, testAccessGroupSeatUID2)
}

mux.HandleFunc("/accounts/"+testAccountID+"/access/seats", handler)
createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
updatedAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
want := []AccessUpdateAccessUserSeatResult{
{
AccessSeat: BoolPtr(false),
CreatedAt: &createdAt,
GatewaySeat: BoolPtr(false),
SeatUID: testAccessGroupSeatUID,
UpdatedAt: &updatedAt,
},
{
AccessSeat: BoolPtr(false),
CreatedAt: &createdAt,
GatewaySeat: BoolPtr(false),
SeatUID: testAccessGroupSeatUID2,
UpdatedAt: &updatedAt,
},
}

actual, err := client.UpdateAccessUsersSeats(context.Background(), testAccountRC, UpdateAccessUsersSeatsParams{
{
SeatUID: testAccessGroupSeatUID,
AccessSeat: BoolPtr(false),
GatewaySeat: BoolPtr(false),
},
{
SeatUID: testAccessGroupSeatUID2,
AccessSeat: BoolPtr(false),
GatewaySeat: BoolPtr(false),
},
})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

0 comments on commit 76563b7

Please sign in to comment.