Skip to content

Commit

Permalink
Merge pull request #1070 from Cyb3r-Jak3/email-routing-catch-all-resp…
Browse files Browse the repository at this point in the history
…onses
  • Loading branch information
jacobbednarz committed Aug 30, 2022
2 parents f7aac0a + 41b0e51 commit 0dc767b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
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

0 comments on commit 0dc767b

Please sign in to comment.