github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/utils/inbucket.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package utils 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "io" 10 "net/http" 11 "os" 12 "strings" 13 "time" 14 ) 15 16 const ( 17 INBUCKET_API = "/api/v1/mailbox/" 18 ) 19 20 // OutputJSONHeader holds the received Header to test sending emails (inbucket) 21 type JSONMessageHeaderInbucket []struct { 22 Mailbox string 23 ID string `json:"Id"` 24 From, Subject, Date string 25 To []string 26 Size int 27 } 28 29 // OutputJSONMessage holds the received Message fto test sending emails (inbucket) 30 type JSONMessageInbucket struct { 31 Mailbox string 32 ID string `json:"Id"` 33 From, Subject, Date string 34 Size int 35 Header map[string][]string 36 Body struct { 37 Text string 38 HTML string `json:"Html"` 39 } 40 } 41 42 func ParseEmail(email string) string { 43 pos := strings.Index(email, "@") 44 parsedEmail := email[0:pos] 45 return parsedEmail 46 } 47 48 func GetMailBox(email string) (results JSONMessageHeaderInbucket, err error) { 49 50 parsedEmail := ParseEmail(email) 51 52 url := fmt.Sprintf("%s%s%s", getInbucketHost(), INBUCKET_API, parsedEmail) 53 req, err := http.NewRequest("GET", url, nil) 54 if err != nil { 55 return nil, err 56 } 57 58 client := &http.Client{} 59 60 resp, err := client.Do(req) 61 if err != nil { 62 return nil, err 63 } 64 defer resp.Body.Close() 65 66 if resp.Body == nil { 67 return nil, fmt.Errorf("No Mailbox") 68 } 69 70 var record JSONMessageHeaderInbucket 71 err = json.NewDecoder(resp.Body).Decode(&record) 72 switch { 73 case err == io.EOF: 74 return nil, fmt.Errorf("Error: %s", err) 75 case err != nil: 76 return nil, fmt.Errorf("Error: %s", err) 77 } 78 if len(record) == 0 { 79 return nil, fmt.Errorf("No mailbox") 80 } 81 82 return record, nil 83 } 84 85 func GetMessageFromMailbox(email, id string) (results JSONMessageInbucket, err error) { 86 87 parsedEmail := ParseEmail(email) 88 89 var record JSONMessageInbucket 90 91 url := fmt.Sprintf("%s%s%s/%s", getInbucketHost(), INBUCKET_API, parsedEmail, id) 92 req, err := http.NewRequest("GET", url, nil) 93 if err != nil { 94 return record, err 95 } 96 97 client := &http.Client{} 98 99 resp, err := client.Do(req) 100 if err != nil { 101 return record, err 102 } 103 defer resp.Body.Close() 104 105 err = json.NewDecoder(resp.Body).Decode(&record) 106 return record, err 107 } 108 109 func DeleteMailBox(email string) (err error) { 110 111 parsedEmail := ParseEmail(email) 112 113 url := fmt.Sprintf("%s%s%s", getInbucketHost(), INBUCKET_API, parsedEmail) 114 req, err := http.NewRequest("DELETE", url, nil) 115 if err != nil { 116 return err 117 } 118 119 client := &http.Client{} 120 121 resp, err := client.Do(req) 122 if err != nil { 123 return err 124 } 125 defer resp.Body.Close() 126 127 return nil 128 } 129 130 func RetryInbucket(attempts int, callback func() error) (err error) { 131 for i := 0; ; i++ { 132 err = callback() 133 if err == nil { 134 return nil 135 } 136 137 if i >= (attempts - 1) { 138 break 139 } 140 141 time.Sleep(5 * time.Second) 142 143 fmt.Println("retrying...") 144 } 145 return fmt.Errorf("After %d attempts, last error: %s", attempts, err) 146 } 147 148 func getInbucketHost() (host string) { 149 150 inbucket_host := os.Getenv("CI_HOST") 151 if inbucket_host == "" { 152 inbucket_host = "dockerhost" 153 } 154 155 inbucket_port := os.Getenv("CI_INBUCKET_PORT") 156 if inbucket_port == "" { 157 inbucket_port = "9000" 158 } 159 return fmt.Sprintf("http://%s:%s", inbucket_host, inbucket_port) 160 }