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

Fix responses for email routing catch all rules #1070

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1070.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
email_routing_rules: Fix response for email routing catch all rule.
```
15 changes: 9 additions & 6 deletions email_routing_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ type EmailRoutingCatchAllRule struct {
Actions []EmailRoutingRuleAction `json:"actions,omitempty"`
}

type EmailRoutingCatchAllRuleResponse struct {
Result EmailRoutingCatchAllRule `json:"result"`
Response
}

// ListEmailRoutingRules Lists existing routing rules.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-list-routing-rules
Expand Down Expand Up @@ -227,21 +232,19 @@ func (api *API) GetEmailRoutingCatchAllRule(ctx context.Context, rc *ResourceCon
if rc.Identifier == "" {
return EmailRoutingCatchAllRule{}, ErrMissingZoneID
}

uri := fmt.Sprintf("/zones/%s/email/routing/rules/catch_all", rc.Identifier)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return EmailRoutingCatchAllRule{}, err
}

var r EmailRoutingCatchAllRule
var r EmailRoutingCatchAllRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return r, nil
return r.Result, nil
}

// UpdateEmailRoutingCatchAllRule Enable or disable catch-all routing rule, or change action to forward to specific destination address.
Expand All @@ -259,11 +262,11 @@ func (api *API) UpdateEmailRoutingCatchAllRule(ctx context.Context, rc *Resource
return EmailRoutingCatchAllRule{}, err
}

var r EmailRoutingCatchAllRule
var r EmailRoutingCatchAllRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return r, nil
return r.Result, nil
}
80 changes: 46 additions & 34 deletions email_routing_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,22 +280,28 @@ func TestEmailRouting_GetAllRule(t *testing.T) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"tag": "a7e6fb77503c41d8a7f3113c6918f10c",
"name": "Rule send to user@example.net",
"enabled": true,
"matchers": [
{
"type": "all"
}
],
"actions": [
{
"type": "forward",
"value": [
"destinationaddress@example.net"
]
}
]
"result": {
"tag": "a7e6fb77503c41d8a7f3113c6918f10c",
"name": "Send to user@example.net rule.",
"matchers": [
{
"type": "all"
}
],
"actions": [
{
"type": "forward",
"value": [
"destinationaddress@example.net"
]
}
],
"enabled": true,
"priority": 2147483647
},
"success": true,
"errors": [],
"messages": []
}`)
})

Expand All @@ -306,7 +312,7 @@ func TestEmailRouting_GetAllRule(t *testing.T) {

want := EmailRoutingCatchAllRule{
Tag: "a7e6fb77503c41d8a7f3113c6918f10c",
Name: "Rule send to user@example.net",
Name: "Send to user@example.net rule.",
Enabled: BoolPtr(true),
Matchers: []EmailRoutingRuleMatcher{
{
Expand Down Expand Up @@ -335,22 +341,28 @@ func TestEmailRouting_UpdateAllRule(t *testing.T) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"tag": "a7e6fb77503c41d8a7f3113c6918f10c",
"name": "Rule send to user@example.net",
"enabled": true,
"matchers": [
{
"type": "all"
}
],
"actions": [
{
"type": "forward",
"value": [
"destinationaddress@example.net"
]
}
]
"result": {
"tag": "a7e6fb77503c41d8a7f3113c6918f10c",
"name": "Send to user@example.net rule.",
"matchers": [
{
"type": "all"
}
],
"actions": [
{
"type": "forward",
"value": [
"destinationaddress@example.net"
]
}
],
"enabled": true,
"priority": 2147483647
},
"success": true,
"errors": [],
"messages": []
}`)
})

Expand All @@ -361,7 +373,7 @@ func TestEmailRouting_UpdateAllRule(t *testing.T) {

want := EmailRoutingCatchAllRule{
Tag: "a7e6fb77503c41d8a7f3113c6918f10c",
Name: "Rule send to user@example.net",
Name: "Send to user@example.net rule.",
Enabled: BoolPtr(true),
Matchers: []EmailRoutingRuleMatcher{
{
Expand Down