-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from sadbeam/DE-1201/subaccounts-support
DE-1203 subaccounts & X-Mailgun-On-Behalf-Of http header
- Loading branch information
Showing
7 changed files
with
572 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -605,6 +605,45 @@ func TestAddOverrideHeader(t *testing.T) { | |
ensure.StringContains(t, mg.GetCurlOutput(), "Host:") | ||
} | ||
|
||
func TestOnBehalfOfSubaccount(t *testing.T) { | ||
const ( | ||
exampleDomain = "testDomain" | ||
exampleAPIKey = "testAPIKey" | ||
toUser = "[email protected]" | ||
exampleMessage = "Queue. Thank you" | ||
exampleID = "<[email protected]>" | ||
) | ||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
ensure.DeepEqual(t, req.Method, http.MethodPost) | ||
ensure.DeepEqual(t, req.URL.Path, fmt.Sprintf("/v3/%s/messages", exampleDomain)) | ||
ensure.DeepEqual(t, req.Header.Get("CustomHeader"), "custom-value") | ||
ensure.DeepEqual(t, req.Host, "example.com") | ||
ensure.DeepEqual(t, req.Header.Get(mailgun.OnBehalfOfHeader), "mailgun.subaccount") | ||
|
||
rsp := fmt.Sprintf(`{"message":"%s", "id":"%s"}`, exampleMessage, exampleID) | ||
fmt.Fprint(w, rsp) | ||
})) | ||
defer srv.Close() | ||
|
||
mg := mailgun.NewMailgun(exampleDomain, exampleAPIKey) | ||
mg.SetAPIBase(srv.URL + "/v3") | ||
mg.AddOverrideHeader("Host", "example.com") | ||
mg.AddOverrideHeader("CustomHeader", "custom-value") | ||
mg.SetOnBehalfOfSubaccount("mailgun.subaccount") | ||
ctx := context.Background() | ||
|
||
m := mg.NewMessage(fromUser, exampleSubject, exampleText, toUser) | ||
m.SetRequireTLS(true) | ||
m.SetSkipVerification(true) | ||
|
||
msg, id, err := mg.Send(ctx, m) | ||
ensure.Nil(t, err) | ||
ensure.DeepEqual(t, msg, exampleMessage) | ||
ensure.DeepEqual(t, id, exampleID) | ||
|
||
ensure.StringContains(t, mg.GetCurlOutput(), "Host:") | ||
} | ||
|
||
func TestCaptureCurlOutput(t *testing.T) { | ||
const ( | ||
exampleDomain = "testDomain" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package mailgun | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
func (ms *mockServer) addSubaccountRoutes(r chi.Router) { | ||
ms.subaccountList = append(ms.subaccountList, Subaccount{ | ||
Id: "enabled.subaccount", | ||
Name: "mailgun.test", | ||
Status: "enabled", | ||
}, Subaccount{ | ||
Id: "disabled.subaccount", | ||
Name: "mailgun.test", | ||
Status: "disabled", | ||
}) | ||
|
||
r.Get("/accounts/subaccounts", ms.listSubaccounts) | ||
r.Post("/accounts/subaccounts", ms.createSubaccount) | ||
|
||
r.Get("/accounts/subaccounts/{subaccountID}", ms.getSubaccount) | ||
r.Post("/accounts/subaccounts/{subaccountID}/enable", ms.enableSubaccount) | ||
r.Post("/accounts/subaccounts/{subaccountID}/disable", ms.disableSubaccount) | ||
} | ||
|
||
func (ms *mockServer) listSubaccounts(w http.ResponseWriter, r *http.Request) { | ||
defer ms.mutex.Unlock() | ||
ms.mutex.Lock() | ||
|
||
var list subaccountsListResponse | ||
for _, subaccount := range ms.subaccountList { | ||
list.Items = append(list.Items, subaccount) | ||
} | ||
|
||
skip := stringToInt(r.FormValue("skip")) | ||
limit := stringToInt(r.FormValue("limit")) | ||
if limit == 0 { | ||
limit = 100 | ||
} | ||
|
||
if skip > len(list.Items) { | ||
skip = len(list.Items) | ||
} | ||
|
||
end := limit + skip | ||
if end > len(list.Items) { | ||
end = len(list.Items) | ||
} | ||
|
||
// If we are at the end of the list | ||
if skip == end { | ||
toJSON(w, subaccountsListResponse{ | ||
Total: len(list.Items), | ||
Items: []Subaccount{}, | ||
}) | ||
return | ||
} | ||
|
||
toJSON(w, subaccountsListResponse{ | ||
Total: len(list.Items), | ||
Items: list.Items[skip:end], | ||
}) | ||
} | ||
|
||
func (ms *mockServer) getSubaccount(w http.ResponseWriter, r *http.Request) { | ||
defer ms.mutex.Unlock() | ||
ms.mutex.Lock() | ||
|
||
for _, s := range ms.subaccountList { | ||
if s.Id == chi.URLParam(r, "subaccountID") { | ||
toJSON(w, SubaccountResponse{Item: s}) | ||
return | ||
} | ||
} | ||
w.WriteHeader(http.StatusNotFound) | ||
toJSON(w, okResp{Message: "Not Found"}) | ||
} | ||
|
||
func (ms *mockServer) createSubaccount(w http.ResponseWriter, r *http.Request) { | ||
defer ms.mutex.Unlock() | ||
ms.mutex.Lock() | ||
|
||
ms.subaccountList = append(ms.subaccountList, Subaccount{ | ||
Id: "test", | ||
Name: r.FormValue("name"), | ||
Status: "active", | ||
}) | ||
toJSON(w, okResp{Message: "Subaccount has been created"}) | ||
} | ||
|
||
func (ms *mockServer) enableSubaccount(w http.ResponseWriter, r *http.Request) { | ||
defer ms.mutex.Unlock() | ||
ms.mutex.Lock() | ||
|
||
for _, subaccount := range ms.subaccountList { | ||
if subaccount.Id == chi.URLParam(r, "subaccountID") && subaccount.Status == "disabled" { | ||
subaccount.Status = "enabled" | ||
toJSON(w, SubaccountResponse{Item: subaccount}) | ||
return | ||
} | ||
if subaccount.Id == chi.URLParam(r, "subaccountID") && subaccount.Status == "enabled" { | ||
toJSON(w, okResp{Message: "subaccount is already enabled"}) | ||
return | ||
} | ||
} | ||
toJSON(w, okResp{Message: "Not Found"}) | ||
} | ||
|
||
func (ms *mockServer) disableSubaccount(w http.ResponseWriter, r *http.Request) { | ||
defer ms.mutex.Unlock() | ||
ms.mutex.Lock() | ||
|
||
for _, subaccount := range ms.subaccountList { | ||
if subaccount.Id == chi.URLParam(r, "subaccountID") && subaccount.Status == "enabled" { | ||
subaccount.Status = "disabled" | ||
toJSON(w, SubaccountResponse{Item: subaccount}) | ||
return | ||
} | ||
if subaccount.Id == chi.URLParam(r, "subaccountID") && subaccount.Status == "disabled" { | ||
toJSON(w, okResp{Message: "subaccount is already disabled"}) | ||
return | ||
} | ||
} | ||
toJSON(w, okResp{Message: "Not Found"}) | ||
} |
Oops, something went wrong.