Skip to content

Commit

Permalink
Merge pull request #310 from Szasza/feature/301-adding-web-scheme-as-…
Browse files Browse the repository at this point in the history
…primary-domain-attribute

Adding web_scheme as a primary domain attribute
  • Loading branch information
thrawn01 committed Jul 27, 2023
2 parents 6190171 + c241db0 commit 782a41e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
26 changes: 26 additions & 0 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Domain struct {
Wildcard bool `json:"wildcard"`
SpamAction SpamAction `json:"spam_action"`
State string `json:"state"`
WebScheme string `json:"web_scheme"`
}

// DNSRecord structures describe intended records to properly configure your domain for use with Mailgun.
Expand Down Expand Up @@ -419,6 +420,31 @@ func (mg *MailgunImpl) UpdateDomainTrackingWebPrefix(ctx context.Context, domain
return err
}

// UpdateDomainOptions options for updating a domain
type UpdateDomainOptions struct {
WebScheme string
}

// UpdateDomain updates a domain's attributes.
// Currently only the web_scheme update is supported, spam_action and wildcard are to be added.
func (mg *MailgunImpl) UpdateDomain(ctx context.Context, name string, opts *UpdateDomainOptions) error {
r := newHTTPRequest(generatePublicApiUrl(mg, domainsEndpoint) + "/" + name)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.APIKey())

payload := newUrlEncodedPayload()

if opts != nil {
if opts.WebScheme != "" {
payload.addValue("web_scheme", string(opts.WebScheme))
}
}

_, err := makePutRequest(ctx, r, payload)

return err
}

func boolToString(b bool) string {
if b {
return "true"
Expand Down
9 changes: 7 additions & 2 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,19 @@ func TestGetSingleDomainNotExist(t *testing.T) {
ensure.DeepEqual(t, ure.Actual, http.StatusNotFound)
}

func TestAddDeleteDomain(t *testing.T) {
func TestAddUpdateDeleteDomain(t *testing.T) {
mg := mailgun.NewMailgun(testDomain, testKey)
mg.SetAPIBase(server.URL())
ctx := context.Background()

// First, we need to add the domain.
_, err := mg.CreateDomain(ctx, "mx.mailgun.test",
&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag, Password: "supersecret", WebScheme: "https"})
&mailgun.CreateDomainOptions{SpamAction: mailgun.SpamActionTag, Password: "supersecret", WebScheme: "http"})
ensure.Nil(t, err)

// Then, we update it.
err = mg.UpdateDomain(ctx, "mx.mailgun.test",
&mailgun.UpdateDomainOptions{WebScheme: "https"})
ensure.Nil(t, err)

// Next, we delete it.
Expand Down
16 changes: 16 additions & 0 deletions mock_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (ms *mockServer) addDomainRoutes(r chi.Router) {
Wildcard: true,
SpamAction: SpamActionDisabled,
State: "active",
WebScheme: "http",
},
Connection: &DomainConnection{
RequireTLS: true,
Expand Down Expand Up @@ -84,6 +85,7 @@ func (ms *mockServer) addDomainRoutes(r chi.Router) {
r.Get("/domains", ms.listDomains)
r.Post("/domains", ms.createDomain)
r.Get("/domains/{domain}", ms.getDomain)
r.Put("/domains/{domain}", ms.updateDomain)
r.Put("/domains/{domain}/verify", ms.getDomain)
r.Delete("/domains/{domain}", ms.deleteDomain)
//r.Get("/domains/{domain}/credentials", ms.getCredentials)
Expand Down Expand Up @@ -168,11 +170,25 @@ func (ms *mockServer) createDomain(w http.ResponseWriter, r *http.Request) {
Wildcard: stringToBool(r.FormValue("wildcard")),
SpamAction: SpamAction(r.FormValue("spam_action")),
State: "active",
WebScheme: "http",
},
})
toJSON(w, okResp{Message: "Domain has been created"})
}

func (ms *mockServer) updateDomain(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()

for _, domain := range ms.domainList {
if domain.Domain.Name == chi.URLParam(r, "domain") {
domain.Domain.WebScheme = r.FormValue("web_scheme")
}
}

toJSON(w, okResp{Message: "Domain has been updated"})
}

func (ms *mockServer) deleteDomain(w http.ResponseWriter, r *http.Request) {
defer ms.mutex.Unlock()
ms.mutex.Lock()
Expand Down
3 changes: 0 additions & 3 deletions tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mailgun_test

import (
"context"
"log"
"testing"
"time"

Expand Down Expand Up @@ -40,7 +39,6 @@ func TestTags(t *testing.T) {
var page []mailgun.Tag
for it.Next(ctx, &page) {
ensure.True(t, len(page) != 0)
log.Printf("Tags: %+v\n", page)
}
ensure.Nil(t, it.Err())

Expand All @@ -50,7 +48,6 @@ func TestTags(t *testing.T) {
var tags []mailgun.Tag
for cursor.Next(ctx, &tags) {
ensure.DeepEqual(t, len(tags), 1)
log.Printf("Tags: %+v\n", tags)
}
ensure.Nil(t, cursor.Err())

Expand Down

0 comments on commit 782a41e

Please sign in to comment.