Skip to content

Commit

Permalink
Merge pull request #1213 from dhens/master
Browse files Browse the repository at this point in the history
 Fix dex_tests using incorrect types and field mappings
  • Loading branch information
jacobbednarz authored Feb 24, 2023
2 parents 7005ef3 + 0ccc674 commit 87f504e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/1213.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
dex_test: use dex test types and json struct mappings instead of managed networks
```
18 changes: 9 additions & 9 deletions devices_dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type DeviceDexTestListResponse struct {
type ListDeviceDexTestParams struct{}

type CreateDeviceDexTestParams struct {
TestID string `json:"network_id,omitempty"`
TestID string `json:"test_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Interval string `json:"interval"`
Expand All @@ -47,7 +47,7 @@ type CreateDeviceDexTestParams struct {
}

type UpdateDeviceDexTestParams struct {
TestID string `json:"network_id,omitempty"`
TestID string `json:"test_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Interval string `json:"interval"`
Expand Down Expand Up @@ -105,25 +105,25 @@ func (api *API) CreateDeviceDexTest(ctx context.Context, rc *ResourceContainer,
// UpdateDeviceDexTest Updates a Device Dex Test.
//
// API reference: https://developers.cloudflare.com/api/operations/device-dex-test-update-device-dex-test
func (api *API) UpdateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params UpdateDeviceDexTestParams) (DeviceManagedNetwork, error) {
func (api *API) UpdateDeviceDexTest(ctx context.Context, rc *ResourceContainer, params UpdateDeviceDexTestParams) (DeviceDexTest, error) {
if rc.Level != AccountRouteLevel {
return DeviceManagedNetwork{}, ErrRequiredAccountLevelResourceContainer
return DeviceDexTest{}, ErrRequiredAccountLevelResourceContainer
}

uri := fmt.Sprintf("/%s/%s/devices/dex_tests/%s", rc.Level, rc.Identifier, params.TestID)

res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return DeviceManagedNetwork{}, err
return DeviceDexTest{}, err
}

var deviceManagedNetworksResponse DeviceManagedNetworkResponse
var deviceDexTestsResponse DeviceDexTestResponse

if err := json.Unmarshal(res, &deviceManagedNetworksResponse); err != nil {
return DeviceManagedNetwork{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
if err := json.Unmarshal(res, &deviceDexTestsResponse); err != nil {
return DeviceDexTest{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return deviceManagedNetworksResponse.Result, err
return deviceDexTestsResponse.Result, err
}

// GetDeviceDexTest gets a single Device Dex Test.
Expand Down
63 changes: 63 additions & 0 deletions devices_dex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,69 @@ func TestCreateDeviceDexTest(t *testing.T) {
}
}

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

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"test_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
"name": "http test dash",
"description": "dex test description",
"interval": "0h30m0s",
"enabled": true,
"data": {
"host": "https://dash.cloudflare.com",
"kind": "http",
"method": "GET"
},
"updated": "2023-01-30T19:59:44.401278Z",
"created": "2023-01-30T19:59:44.401278Z"
}
}`)
}

want := DeviceDexTest{
TestID: testID,
Name: "http test dash",
Description: "dex test description",
Interval: "0h30m0s",
Enabled: true,
Data: &DeviceDexTestData{
"kind": "http",
"method": "GET",
"host": "https://dash.cloudflare.com",
},
Updated: dexTimestamp,
Created: dexTimestamp,
}

mux.HandleFunc("/accounts/"+testAccountID+"/devices/dex_tests/"+testID, handler)

actual, err := client.UpdateDeviceDexTest(context.Background(), AccountIdentifier(testAccountID), UpdateDeviceDexTestParams{
TestID: testID,
Name: "http test dash",
Description: "dex test description",
Interval: "0h30m0s",
Enabled: true,
Data: &DeviceDexTestData{
"kind": "http",
"method": "GET",
"host": "https://dash.cloudflare.com",
},
})

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestDeleteDeviceDexTest(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 87f504e

Please sign in to comment.