-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain_test.go
More file actions
148 lines (124 loc) · 4.46 KB
/
main_test.go
File metadata and controls
148 lines (124 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
//<gen>go_send_email_deps
import (
"context"
"github.com/antihax/optional"
sasl "github.com/emersion/go-sasl"
smtp "github.com/emersion/go-smtp"
mailslurp "github.com/mailslurp/mailslurp-client-go"
"github.com/stretchr/testify/assert"
"log"
"os"
"strings"
"testing"
)
//</gen>
//<gen>go_send_email_get_client
var apiKey = os.Getenv("API_KEY")
func getMailSlurpClient(t *testing.T) (*mailslurp.APIClient, context.Context) {
assert.NotNil(t, apiKey)
// create a context with your api key
ctx := context.WithValue(context.Background(), mailslurp.ContextAPIKey, mailslurp.APIKey{Key: apiKey})
// create mailslurp client
config := mailslurp.NewConfiguration()
client := mailslurp.NewAPIClient(config)
return client, ctx
}
//</gen>
// how to send insecurely with mailslurp
//<gen>go_send_email_insecure
func Test_CanSendEmail_Insecure(t *testing.T) {
// create a context with your api key
client, ctx := getMailSlurpClient(t)
// create an inbox using the inbox controller
opts := &mailslurp.CreateInboxOpts{
InboxType: optional.NewString("SMTP_INBOX"),
}
// create two inboxes for testing
inbox1, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
//<gen>golang_smtp_access
smtpAccess, _, _ := client.InboxControllerApi.GetImapSmtpAccess(ctx, &mailslurp.GetImapSmtpAccessOpts{
InboxId: optional.NewInterface(inbox1.Id),
})
inbox2, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
// create a plain auth client with smtp access details
auth := sasl.NewPlainClient("", smtpAccess.SmtpUsername, smtpAccess.SmtpPassword)
// dial connection to the smtp server
c, err := smtp.Dial("mx.mailslurp.com:2525")
assert.NoError(t, err, "Expect client dial")
defer c.Close()
// issue hello smtp command
log.Println("Say hello")
err = c.Hello("test")
assert.NoError(t, err, "Expect hello")
// issue auth smtp command
log.Println("Set auth")
err = c.Auth(auth)
assert.NoError(t, err, "Expect auth")
// send the email
log.Println("Send email")
to := []string{inbox2.EmailAddress}
msg := strings.NewReader("To: " + inbox2.EmailAddress + "\r\n" +
"Subject: Hello Insecure Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
err = c.SendMail(inbox1.EmailAddress, to, msg)
assert.NoError(t, err, "Expect insecure smtp send to work")
//</gen>
// fetch the email for inbox2
log.Println("Wait for email to arrive")
waitOpts := &mailslurp.WaitForLatestEmailOpts{
InboxId: optional.NewInterface(inbox2.Id),
Timeout: optional.NewInt64(30000),
UnreadOnly: optional.NewBool(true),
}
email, _, err := client.WaitForControllerApi.WaitForLatestEmail(ctx, waitOpts)
// assert email contents
log.Println("Email received: " + *email.Subject)
assert.NoError(t, err)
assert.Contains(t, *email.Subject, "Hello Insecure Gophers")
assert.Contains(t, *email.Body, "This is the email body")
}
//</gen>
// send using TLS
//<gen>go_send_email_tls
func Test_CanSendEmail_TLS(t *testing.T) {
// create a context with your api key
client, ctx := getMailSlurpClient(t)
// create an inbox using the inbox controller
opts := &mailslurp.CreateInboxOpts{
InboxType: optional.NewString("SMTP_INBOX"),
}
// create two inboxes for testing
inbox1, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
smtpAccess, _, _ := client.InboxControllerApi.GetImapSmtpAccess(ctx, &mailslurp.GetImapSmtpAccessOpts{
InboxId: optional.NewInterface(inbox1.Id),
})
inbox2, _, _ := client.InboxControllerApi.CreateInbox(ctx, opts)
// send email from inbox1 to inbox2
auth := sasl.NewPlainClient("", smtpAccess.SmtpUsername, smtpAccess.SmtpPassword)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{inbox2.EmailAddress}
msg := strings.NewReader("To: " + inbox2.EmailAddress + "\r\n" +
"Subject: Hello Gophers!\r\n" +
"\r\n" +
"This is the email body.\r\n")
// not TLS mailslurp uses a different host
err := smtp.SendMail("mailslurp.mx:587", auth, inbox1.EmailAddress, to, msg)
if err != nil {
log.Fatal(err)
assert.NoError(t, err, "Expect smtp send to work")
}
// fetch the email for inbox2
waitOpts := &mailslurp.WaitForLatestEmailOpts{
InboxId: optional.NewInterface(inbox2.Id),
Timeout: optional.NewInt64(30000),
UnreadOnly: optional.NewBool(true),
}
email, _, err := client.WaitForControllerApi.WaitForLatestEmail(ctx, waitOpts)
assert.NoError(t, err)
assert.Contains(t, *email.Subject, "Hello Gophers")
assert.Contains(t, *email.Body, "This is the email body")
}
//</gen>