Repository: cloudflare/cloudflare-go
Pull request #1213 (2023-02-22 17:39:55)

 Fix dex_tests using incorrect types and field mappings 
The initial release to support the dex_tests feature (https://github.com/cloudflare/cloudflare-go/pull/1209/files) contained managed network types and json struct mappings, which would cause missing test ids in the device dex test struct and likely 500 errors.

This PR fixes the code which would have caused those issues. 

## Has your change been tested?

A test, which was not in the initial release, was added for the update function. It fails with the previous incorrect types and mappings, and passes with the updated code. 

## Types of changes

What sort of change does your code introduce/modify?

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/



Patch:
diff --git a/.changelog/1213.txt b/.changelog/1213.txt
new file mode 100644
index 000000000..124db5e91
--- /dev/null
+++ b/.changelog/1213.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+dex_test: use dex test types and json struct mappings instead of managed networks
+```
diff --git a/devices_dex.go b/devices_dex.go
index d04a84028..f95cb9b5c 100644
--- a/devices_dex.go
+++ b/devices_dex.go
@@ -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"`
@@ -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"`
@@ -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.


Test patch:
diff --git a/devices_dex_test.go b/devices_dex_test.go
index 0db33d442..f5901f791 100644
--- a/devices_dex_test.go
+++ b/devices_dex_test.go
@@ -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()