forked from mocktools/go-smtp-mock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_rset_test.go
219 lines (182 loc) · 8.47 KB
/
handler_rset_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package smtpmock
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewHandlerRset(t *testing.T) {
t.Run("returns new handlerRset", func(t *testing.T) {
session, message, configuration := new(session), new(Message), new(configuration)
handler := newHandlerRset(session, message, configuration)
assert.Same(t, session, handler.session)
assert.Same(t, message, handler.message)
assert.Same(t, configuration, handler.configuration)
})
}
func TestHandlerRsetRun(t *testing.T) {
t.Run("when successful RSET request", func(t *testing.T) {
request := "RSET"
session, message, configuration := new(sessionMock), new(Message), createConfiguration()
receivedMessage := configuration.msgRsetReceived
message.helo = true
handler := newHandlerRset(session, message, configuration)
session.On("clearError").Once().Return(nil)
session.On("writeResponse", receivedMessage, configuration.responseDelayRset).Once().Return(nil)
handler.run(request)
assert.True(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, receivedMessage, message.rsetResponse)
})
t.Run("when failure RSET request, invalid command sequence", func(t *testing.T) {
request := "RSET"
session, message, configuration := new(sessionMock), new(Message), createConfiguration()
errorMessage := configuration.msgInvalidCmdRsetSequence
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("clearError").Once().Return(nil)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
handler.run(request)
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
t.Run("when failure RSET request, invalid command", func(t *testing.T) {
request := "RSET "
session, message, configuration := new(sessionMock), new(Message), createConfiguration()
errorMessage := configuration.msgInvalidCmdRsetArg
message.helo = true
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("clearError").Once().Return(nil)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
handler.run(request)
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
}
func TestHandlerRsetClearMessage(t *testing.T) {
t.Run("when not multiple message receiving condition erases all message data except HELO/EHLO command context", func(t *testing.T) {
notEmptyMessage := createNotEmptyMessage()
handler := newHandlerRset(new(session), notEmptyMessage, new(configuration))
clearedMessage := &Message{
heloRequest: notEmptyMessage.heloRequest,
heloResponse: notEmptyMessage.heloResponse,
helo: notEmptyMessage.helo,
}
handler.clearMessage()
assert.Same(t, notEmptyMessage, handler.message)
assert.Equal(t, clearedMessage, handler.message)
handler.message.rsetRequest = "42"
handler.clearMessage()
assert.Equal(t, clearedMessage, handler.message)
})
t.Run("when multiple message receiving condition does not updated message", func(t *testing.T) {
configuration, message := &configuration{multipleMessageReceiving: true}, createNotEmptyMessage()
handler := newHandlerRset(new(session), message, configuration)
handler.clearMessage()
assert.Equal(t, message, handler.message)
})
}
func TestHandlerRsetWriteResult(t *testing.T) {
request, response := "request context", "response context"
configuration, session := createConfiguration(), &sessionMock{}
t.Run("when successful request received", func(t *testing.T) {
message := new(Message)
handler := newHandlerRset(session, message, configuration)
session.On("writeResponse", response, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.writeResult(true, request, response))
assert.True(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, response, message.rsetResponse)
})
t.Run("when failed request received", func(t *testing.T) {
message, err := new(Message), errors.New(response)
handler := newHandlerRset(session, message, configuration)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", response, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.writeResult(false, request, response))
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, response, message.rsetResponse)
})
}
func TestHandlerRsetIsInvalidCmdSequence(t *testing.T) {
request, configuration, session := "some request", createConfiguration(), &sessionMock{}
t.Run("when helo previous command was failure ", func(t *testing.T) {
message, errorMessage := new(Message), configuration.msgInvalidCmdRsetSequence
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.isInvalidCmdSequence(request))
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
t.Run("when helo previous command was successful ", func(t *testing.T) {
message := new(Message)
handler := newHandlerRset(session, message, configuration)
message.helo = true
assert.False(t, handler.isInvalidCmdSequence(request))
assert.False(t, message.rset)
assert.Empty(t, message.rsetRequest)
assert.Empty(t, message.rsetResponse)
})
}
func TestHandlerRsetIsInvalidCmdArg(t *testing.T) {
configuration, session := createConfiguration(), &sessionMock{}
t.Run("when request includes invalid RSET command", func(t *testing.T) {
request, message, errorMessage := "RSET ", new(Message), configuration.msgInvalidCmdRsetArg
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.isInvalidCmdArg(request))
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
t.Run("when request includes valid RSET command", func(t *testing.T) {
message := new(Message)
handler := newHandlerRset(session, message, configuration)
assert.False(t, handler.isInvalidCmdArg("RSET"))
assert.False(t, message.rset)
assert.Empty(t, message.rsetRequest)
assert.Empty(t, message.rsetResponse)
})
}
func TestHandlerRsetIsInvalidRequest(t *testing.T) {
configuration := createConfiguration()
t.Run("when request includes invalid RSET command sequence, the previous command is not successful", func(t *testing.T) {
request := "RSET"
session, message, errorMessage := new(sessionMock), new(Message), configuration.msgInvalidCmdRsetSequence
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.isInvalidRequest(request))
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
t.Run("when request includes invalid RSET command", func(t *testing.T) {
request := "RSET "
session, message, errorMessage := new(sessionMock), new(Message), configuration.msgInvalidCmdRsetArg
message.helo = true
handler, err := newHandlerRset(session, message, configuration), errors.New(errorMessage)
session.On("addError", err).Once().Return(nil)
session.On("writeResponse", errorMessage, configuration.responseDelayRset).Once().Return(nil)
assert.True(t, handler.isInvalidRequest(request))
assert.False(t, message.rset)
assert.Equal(t, request, message.rsetRequest)
assert.Equal(t, errorMessage, message.rsetResponse)
})
t.Run("when valid RSET request", func(t *testing.T) {
request := "RSET"
session, message := new(sessionMock), new(Message)
message.helo = true
handler := newHandlerRset(session, message, configuration)
assert.False(t, handler.isInvalidRequest(request))
assert.False(t, message.rset)
assert.Empty(t, message.rsetRequest)
assert.Empty(t, message.rsetResponse)
})
}