This repository has been archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration_test.go
170 lines (131 loc) · 3.91 KB
/
registration_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
type DummyStore struct {
storeMethod func(reg Registration) (err error)
}
func (s DummyStore) Store(reg Registration) (err error) {
return s.storeMethod(reg)
}
func NewDummyReg() *Registration {
dummyStore := DummyStore{func(reg Registration) (err error) { return nil }}
return &Registration{
"",
"",
"",
time.Now(),
dummyStore,
""}
}
func TestFormDisplay(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
reg := NewDummyReg()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusOK)
}
if !strings.Contains(resp.Body.String(), "<form") {
t.Error("Registration does not contain form element!")
}
}
func TestFormProcessBadRequest(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/", nil)
if err != nil {
t.Fatal(err)
}
reg := NewDummyReg()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusBadRequest)
}
// test bad mail format
resp = httptest.NewRecorder()
req, err = http.NewRequest("POST", "/", strings.NewReader("name=Test&surname=Test&email=Test"))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
reg = NewDummyReg()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusBadRequest)
}
}
func TestFormProcessOkRequest(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/", strings.NewReader("name=Test&surname=Test&[email protected]"))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
reg := NewDummyReg()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusOK)
}
}
func TestStoreCall(t *testing.T) {
storeCalled := false
defer func() {
if storeCalled == false {
t.Error("Store not called!")
}
}()
storeChecker := func(reg Registration) (err error) {
storeCalled = true
return nil
}
dummyStore := DummyStore{storeChecker}
reg := &Registration{Storer: dummyStore}
req, err := http.NewRequest("POST", "/", strings.NewReader("name=Test&surname=Test&[email protected]"))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp := httptest.NewRecorder()
reg.ServeHTTP(resp, req)
}
func TestStoreError(t *testing.T) {
storeFailer := func(reg Registration) (err error) {
return errors.New("Store failed!")
}
dummyStore := DummyStore{storeFailer}
reg := &Registration{Storer: dummyStore}
req, err := http.NewRequest("POST", "/", strings.NewReader("name=Test&surname=Test&[email protected]"))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp := httptest.NewRecorder()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusInternalServerError {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusInternalServerError)
}
}
func TestSuccessMsgDisplay(t *testing.T) {
resp := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/", strings.NewReader("name=Test&surname=Test&[email protected]"))
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
reg := NewDummyReg()
reg.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Server replied with status %v expected %v", resp.Code, http.StatusOK)
}
if !strings.Contains(resp.Body.String(), registrationSuccessful) {
t.Errorf("Registration does not contain success message, expected %q", registrationSuccessful)
}
}