Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes update seats with wrong body: expected array, sent single object #1480

Merged
merged 8 commits into from
Jan 31, 2024
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
```
38 changes: 37 additions & 1 deletion access_seats.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type UpdateAccessUserSeatResponse struct {
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 +53,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 []UpdateAccessUserSeatParams) ([]AccessUpdateAccessUserSeatResult, error) {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
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, []UpdateAccessUserSeatParams{{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, []UpdateAccessUserSeatParams{
{
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)
}
}
Loading