-
Notifications
You must be signed in to change notification settings - Fork 26
/
contact_test.go
78 lines (69 loc) · 2.1 KB
/
contact_test.go
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
package main
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.goblog.app/app/pkgs/contenttype"
"go.goblog.app/app/pkgs/mocksmtp"
)
func Test_contact(t *testing.T) {
// Start the SMTP server
port, rd, cancel, err := mocksmtp.StartMockSMTPServer()
require.NoError(t, err)
defer cancel()
// Init everything
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
app.cfg.Blogs = map[string]*configBlog{
"en": {
Lang: "en",
// Config for contact
Contact: &configContact{
Enabled: true,
SMTPPort: port,
SMTPHost: "127.0.0.1",
SMTPUser: "user",
SMTPPassword: "pass",
EmailTo: "to@example.org",
EmailFrom: "from@example.org",
EmailSubject: "Neue Kontaktnachricht",
},
},
}
app.cfg.DefaultBlog = "en"
_ = app.initConfig(false)
app.initSessions()
_ = app.initTemplateStrings()
// Make contact form request
rec := httptest.NewRecorder()
data := url.Values{}
data.Add("name", "Test User")
data.Add("email", "test@example.net")
data.Add("website", "https://test.example.com")
data.Add("message", "This is a test contact message")
req := httptest.NewRequest(http.MethodPost, "/contact", strings.NewReader(data.Encode()))
req.Header.Add(contentType, contenttype.WWWForm)
app.sendContactSubmission(rec, req.WithContext(context.WithValue(req.Context(), blogKey, "en")))
require.Equal(t, http.StatusOK, rec.Code)
// Wait a second
time.Sleep(500 * time.Millisecond)
// Check sent mail
assert.Contains(t, rd.Usernames, "user")
assert.Contains(t, rd.Passwords, "pass")
assert.Contains(t, rd.Froms, "from@example.org")
assert.Contains(t, rd.Rcpts, "to@example.org")
if assert.Len(t, rd.Datas, 1) {
assert.Contains(t, string(rd.Datas[0]), "This is a test contact message")
assert.Contains(t, string(rd.Datas[0]), "test@example.net")
assert.Contains(t, string(rd.Datas[0]), "https://test.example.com")
assert.Contains(t, string(rd.Datas[0]), "Test User")
assert.Contains(t, string(rd.Datas[0]), "Neue Kontaktnachricht")
}
}