summaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authorCarlos Tadeu Panato Junior <ctadeu@gmail.com>2017-01-17 15:01:41 +0100
committerHarrison Healey <harrisonmhealey@gmail.com>2017-01-17 09:01:41 -0500
commitdc54e640c296d50c51858fa50256b3aed9e0a46c (patch)
treeb5b9dc2b044535570cb885307de366136c2b3419 /utils
parent97a51b0e31692b7f86a9971ad36d62b04100fffb (diff)
downloadchat-dc54e640c296d50c51858fa50256b3aed9e0a46c.tar.gz
chat-dc54e640c296d50c51858fa50256b3aed9e0a46c.tar.bz2
chat-dc54e640c296d50c51858fa50256b3aed9e0a46c.zip
Add inbucket docker image to allow local and automated testing of emails (#4901)
* add docker container for inbucket * Add way to get the emails using inbucket and add a test for reset password * add config setting to send emails * update TestEmailTest update * add another test and fix wrong assert * update per review fix lint change senders email * Revert config.json to default values for EmailSettings section * update test * add setup to make the test run
Diffstat (limited to 'utils')
-rw-r--r--utils/inbucket.go115
1 files changed, 115 insertions, 0 deletions
diff --git a/utils/inbucket.go b/utils/inbucket.go
new file mode 100644
index 000000000..1c747cee8
--- /dev/null
+++ b/utils/inbucket.go
@@ -0,0 +1,115 @@
+package utils
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "strings"
+)
+
+const (
+ INBUCKET_HOST = "http://dockerhost:9000"
+ INBUCKET_API = "/api/v1/mailbox/"
+)
+
+// OutputJSONHeader holds the received Header to test sending emails (inbucket)
+type JSONMessageHeaderInbucket []struct {
+ Mailbox string
+ ID string `json:"Id"`
+ From, Subject, Date string
+ To []string
+ Size int
+}
+
+// OutputJSONMessage holds the received Message fto test sending emails (inbucket)
+type JSONMessageInbucket struct {
+ Mailbox string
+ ID string `json:"Id"`
+ From, Subject, Date string
+ Size int
+ Header map[string][]string
+ Body struct {
+ Text string
+ HTML string `json:"Html"`
+ }
+}
+
+func ParseEmail(email string) string {
+ pos := strings.Index(email, "@")
+ parsedEmail := email[0:pos]
+ return parsedEmail
+}
+
+func GetMailBox(email string) (results JSONMessageHeaderInbucket, err error) {
+
+ parsedEmail := ParseEmail(email)
+
+ url := fmt.Sprintf("%s%s%s", INBUCKET_HOST, INBUCKET_API, parsedEmail)
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return nil, err
+ }
+
+ client := &http.Client{}
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ var record JSONMessageHeaderInbucket
+ if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
+ fmt.Println(err)
+ return nil, err
+ }
+ return record, nil
+}
+
+func GetMessageFromMailbox(email, id string) (results JSONMessageInbucket, err error) {
+
+ parsedEmail := ParseEmail(email)
+
+ var record JSONMessageInbucket
+
+ url := fmt.Sprintf("%s%s%s/%s", INBUCKET_HOST, INBUCKET_API, parsedEmail, id)
+ req, err := http.NewRequest("GET", url, nil)
+ if err != nil {
+ return record, err
+ }
+
+ client := &http.Client{}
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return record, err
+ }
+ defer resp.Body.Close()
+
+ if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
+ fmt.Println(err)
+ return record, err
+ }
+ return record, nil
+}
+
+func DeleteMailBox(email string) (err error) {
+
+ parsedEmail := ParseEmail(email)
+
+ url := fmt.Sprintf("%s%s%s", INBUCKET_HOST, INBUCKET_API, parsedEmail)
+ req, err := http.NewRequest("DELETE", url, nil)
+ if err != nil {
+ return err
+ }
+
+ client := &http.Client{}
+
+ resp, err := client.Do(req)
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+
+ return nil
+}