github.com/mailgun/mailgun-go/v3@v3.6.4/mock_domains.go (about) 1 package mailgun 2 3 import ( 4 "net/http" 5 "time" 6 7 "github.com/go-chi/chi" 8 ) 9 10 type domainContainer struct { 11 Domain Domain `json:"domain"` 12 ReceivingDNSRecords []DNSRecord `json:"receiving_dns_records"` 13 SendingDNSRecords []DNSRecord `json:"sending_dns_records"` 14 Connection *DomainConnection `json:"connection,omitempty"` 15 Tracking *DomainTracking `json:"tracking,omitempty"` 16 TagLimits *TagLimits `json:"limits,omitempty"` 17 } 18 19 func (ms *MockServer) addDomainRoutes(r chi.Router) { 20 21 ms.domainList = append(ms.domainList, domainContainer{ 22 Domain: Domain{ 23 CreatedAt: RFC2822Time(time.Now().UTC()), 24 Name: "mailgun.test", 25 SMTPLogin: "postmaster@mailgun.test", 26 SMTPPassword: "4rtqo4p6rrx9", 27 Wildcard: true, 28 SpamAction: SpamActionDisabled, 29 State: "active", 30 }, 31 Connection: &DomainConnection{ 32 RequireTLS: true, 33 SkipVerification: true, 34 }, 35 TagLimits: &TagLimits{ 36 Limit: 50000, 37 Count: 5000, 38 }, 39 Tracking: &DomainTracking{ 40 Click: TrackingStatus{Active: true}, 41 Open: TrackingStatus{Active: true}, 42 Unsubscribe: TrackingStatus{ 43 Active: false, 44 HTMLFooter: "\n<br>\n<p><a href=\"%unsubscribe_url%\">unsubscribe</a></p>\n", 45 TextFooter: "\n\nTo unsubscribe click: <%unsubscribe_url%>\n\n", 46 }, 47 }, 48 ReceivingDNSRecords: []DNSRecord{ 49 { 50 Priority: "10", 51 RecordType: "MX", 52 Valid: "valid", 53 Value: "mxa.mailgun.org", 54 }, 55 { 56 Priority: "10", 57 RecordType: "MX", 58 Valid: "valid", 59 Value: "mxb.mailgun.org", 60 }, 61 }, 62 SendingDNSRecords: []DNSRecord{ 63 { 64 RecordType: "TXT", 65 Valid: "valid", 66 Name: "domain.com", 67 Value: "v=spf1 include:mailgun.org ~all", 68 }, 69 { 70 RecordType: "TXT", 71 Valid: "valid", 72 Name: "domain.com", 73 Value: "k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUA....", 74 }, 75 { 76 RecordType: "CNAME", 77 Valid: "valid", 78 Name: "email.domain.com", 79 Value: "mailgun.org", 80 }, 81 }, 82 }) 83 84 r.Get("/domains", ms.listDomains) 85 r.Post("/domains", ms.createDomain) 86 r.Get("/domains/{domain}", ms.getDomain) 87 r.Put("/domains/{domain}/verify", ms.getDomain) 88 r.Delete("/domains/{domain}", ms.deleteDomain) 89 //r.Get("/domains/{domain}/credentials", ms.getCredentials) 90 //r.Post("/domains/{domain}/credentials", ms.createCredentials) 91 //r.Put("/domains/{domain}/credentials/{login}", ms.updateCredentials) 92 //r.Delete("/domains/{domain}/credentials/{login}", ms.deleteCredentials) 93 r.Get("/domains/{domain}/connection", ms.getConnection) 94 r.Put("/domains/{domain}/connection", ms.updateConnection) 95 r.Get("/domains/{domain}/tracking", ms.getTracking) 96 r.Put("/domains/{domain}/tracking/click", ms.updateClickTracking) 97 r.Put("/domains/{domain}/tracking/open", ms.updateOpenTracking) 98 r.Put("/domains/{domain}/tracking/unsubscribe", ms.updateUnsubTracking) 99 r.Get("/domains/{domain}/limits/tag", ms.getTagLimits) 100 } 101 102 func (ms *MockServer) listDomains(w http.ResponseWriter, r *http.Request) { 103 var list []Domain 104 for _, domain := range ms.domainList { 105 list = append(list, domain.Domain) 106 } 107 108 skip := stringToInt(r.FormValue("skip")) 109 limit := stringToInt(r.FormValue("limit")) 110 if limit == 0 { 111 limit = 100 112 } 113 114 if skip > len(list) { 115 skip = len(list) 116 } 117 118 end := limit + skip 119 if end > len(list) { 120 end = len(list) 121 } 122 123 // If we are at the end of the list 124 if skip == end { 125 toJSON(w, domainsListResponse{ 126 TotalCount: len(list), 127 Items: []Domain{}, 128 }) 129 return 130 } 131 132 toJSON(w, domainsListResponse{ 133 TotalCount: len(list), 134 Items: list[skip:end], 135 }) 136 } 137 138 func (ms *MockServer) getDomain(w http.ResponseWriter, r *http.Request) { 139 for _, d := range ms.domainList { 140 if d.Domain.Name == chi.URLParam(r, "domain") { 141 d.Connection = nil 142 toJSON(w, d) 143 return 144 } 145 } 146 w.WriteHeader(http.StatusNotFound) 147 toJSON(w, okResp{Message: "domain not found"}) 148 } 149 150 func (ms *MockServer) createDomain(w http.ResponseWriter, r *http.Request) { 151 ms.domainList = append(ms.domainList, domainContainer{ 152 Domain: Domain{ 153 CreatedAt: RFC2822Time(time.Now()), 154 Name: r.FormValue("name"), 155 SMTPLogin: r.FormValue("smtp_login"), 156 SMTPPassword: r.FormValue("smtp_password"), 157 Wildcard: stringToBool(r.FormValue("wildcard")), 158 SpamAction: SpamAction(r.FormValue("spam_action")), 159 State: "active", 160 }, 161 }) 162 toJSON(w, okResp{Message: "Domain has been created"}) 163 } 164 165 func (ms *MockServer) deleteDomain(w http.ResponseWriter, r *http.Request) { 166 result := ms.domainList[:0] 167 for _, domain := range ms.domainList { 168 if domain.Domain.Name == chi.URLParam(r, "domain") { 169 continue 170 } 171 result = append(result, domain) 172 } 173 174 if len(result) != len(ms.domainList) { 175 toJSON(w, okResp{Message: "success"}) 176 ms.domainList = result 177 return 178 } 179 180 w.WriteHeader(http.StatusNotFound) 181 toJSON(w, okResp{Message: "domain not found"}) 182 } 183 184 func (ms *MockServer) getConnection(w http.ResponseWriter, r *http.Request) { 185 for _, d := range ms.domainList { 186 if d.Domain.Name == chi.URLParam(r, "domain") { 187 resp := domainConnectionResponse{ 188 Connection: *d.Connection, 189 } 190 toJSON(w, resp) 191 return 192 } 193 } 194 w.WriteHeader(http.StatusNotFound) 195 toJSON(w, okResp{Message: "domain not found"}) 196 } 197 198 func (ms *MockServer) updateConnection(w http.ResponseWriter, r *http.Request) { 199 for i, d := range ms.domainList { 200 if d.Domain.Name == chi.URLParam(r, "domain") { 201 ms.domainList[i].Connection = &DomainConnection{ 202 RequireTLS: stringToBool(r.FormValue("require_tls")), 203 SkipVerification: stringToBool(r.FormValue("skip_verification")), 204 } 205 toJSON(w, okResp{Message: "Domain connection settings have been updated, may take 10 minutes to fully propagate"}) 206 return 207 } 208 } 209 w.WriteHeader(http.StatusNotFound) 210 toJSON(w, okResp{Message: "domain not found"}) 211 } 212 213 func (ms *MockServer) getTracking(w http.ResponseWriter, r *http.Request) { 214 for _, d := range ms.domainList { 215 if d.Domain.Name == chi.URLParam(r, "domain") { 216 resp := domainTrackingResponse{ 217 Tracking: *d.Tracking, 218 } 219 toJSON(w, resp) 220 return 221 } 222 } 223 w.WriteHeader(http.StatusNotFound) 224 toJSON(w, okResp{Message: "domain not found"}) 225 } 226 227 func (ms *MockServer) updateClickTracking(w http.ResponseWriter, r *http.Request) { 228 for i, d := range ms.domainList { 229 if d.Domain.Name == chi.URLParam(r, "domain") { 230 ms.domainList[i].Tracking.Click.Active = stringToBool(r.FormValue("active")) 231 toJSON(w, okResp{Message: "Domain tracking settings have been updated"}) 232 return 233 } 234 } 235 w.WriteHeader(http.StatusNotFound) 236 toJSON(w, okResp{Message: "domain not found"}) 237 } 238 239 func (ms *MockServer) updateOpenTracking(w http.ResponseWriter, r *http.Request) { 240 for i, d := range ms.domainList { 241 if d.Domain.Name == chi.URLParam(r, "domain") { 242 ms.domainList[i].Tracking.Open.Active = stringToBool(r.FormValue("active")) 243 toJSON(w, okResp{Message: "Domain tracking settings have been updated"}) 244 return 245 } 246 } 247 w.WriteHeader(http.StatusNotFound) 248 toJSON(w, okResp{Message: "domain not found"}) 249 } 250 251 func (ms *MockServer) updateUnsubTracking(w http.ResponseWriter, r *http.Request) { 252 for i, d := range ms.domainList { 253 if d.Domain.Name == chi.URLParam(r, "domain") { 254 ms.domainList[i].Tracking.Unsubscribe.Active = stringToBool(r.FormValue("active")) 255 if len(r.FormValue("html_footer")) != 0 { 256 ms.domainList[i].Tracking.Unsubscribe.HTMLFooter = r.FormValue("html_footer") 257 } 258 if len(r.FormValue("text_footer")) != 0 { 259 ms.domainList[i].Tracking.Unsubscribe.TextFooter = r.FormValue("text_footer") 260 } 261 toJSON(w, okResp{Message: "Domain tracking settings have been updated"}) 262 return 263 } 264 } 265 w.WriteHeader(http.StatusNotFound) 266 toJSON(w, okResp{Message: "domain not found"}) 267 } 268 269 func (ms *MockServer) getTagLimits(w http.ResponseWriter, r *http.Request) { 270 for _, d := range ms.domainList { 271 if d.Domain.Name == chi.URLParam(r, "domain") { 272 if d.TagLimits == nil { 273 w.WriteHeader(http.StatusNotFound) 274 toJSON(w, okResp{Message: "no limits defined for domain"}) 275 return 276 } 277 toJSON(w, d.TagLimits) 278 return 279 } 280 } 281 w.WriteHeader(http.StatusNotFound) 282 toJSON(w, okResp{Message: "domain not found"}) 283 }