-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidate_test.go
More file actions
230 lines (202 loc) · 7.04 KB
/
validate_test.go
File metadata and controls
230 lines (202 loc) · 7.04 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
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
220
221
222
223
224
225
226
227
228
229
230
package zerobouncego
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
// mockValidateRequest mock responses of GET/validate
func mockValidateRequest() {
httpmock.RegisterResponder("GET", `=~^(.*)`+ENDPOINT_VALIDATE+`(.*)\z`,
func(req *http.Request) (*http.Response, error) {
request_query := req.URL.Query()
if request_query.Get("api_key") == "" {
return nil, errors.New("'api_key' missing from request arguments")
}
email_address := request_query.Get("email")
mock_response := MOCK_VALIDATE_RESPONSE[email_address]
if mock_response == "" {
return nil, fmt.Errorf("no mock for email address %s", email_address)
}
return httpmock.NewStringResponse(200, mock_response), nil
},
)
}
// mockBatchValidateRequest mock responses of GET/validatebatch
func mockBatchValidateRequest() {
type BatchValidateRequestPayload struct {
ApiKey string `json:"api_key"`
Emails *[]EmailToValidate `json:"email_batch"`
}
httpmock.RegisterResponder("POST", `=~^(.*)`+ENDPOINT_BATCH_VALIDATE+`(.*)\z`,
func(req *http.Request) (*http.Response, error) {
var error_ error
var email_responses []string
var error_responses []string
const missing_email_address_value = `{
"error": "Missing email_batch key: email_address.",
"email_address": "unknown"
}`
const missing_email_batch_param = `{
"Message": "Missing parameter: email_batch."
}`
const invalid_api_key = `{
"email_address": "all",
"error": "Invalid API Key or your account ran out of credits"
}`
request_body := &BatchValidateRequestPayload{}
defer req.Body.Close()
request_body_raw, error_ := io.ReadAll(req.Body)
if error_ != nil {
return nil, error_
}
error_ = json.NewDecoder(strings.NewReader(string(request_body_raw))).Decode(request_body)
if error_ != nil {
return nil, error_
}
// normally, errors request errors should be returned when this cases occur
if request_body.Emails == nil {
return httpmock.NewStringResponse(400, missing_email_batch_param), nil
}
if request_body.ApiKey == "" {
return httpmock.NewStringResponse(200, `{"email_batch": [], "errors": [`+invalid_api_key+`]}`), nil
}
if len(*request_body.Emails) == 0 {
return httpmock.NewStringResponse(400, missing_email_address_value), nil
}
// map request emails to expected responses
for _, validation_email := range *request_body.Emails {
email_address := validation_email.EmailAddress
if email_address == "" {
error_responses = append(error_responses, missing_email_address_value)
} else if MOCK_VALIDATE_RESPONSE[email_address] == "" {
return nil, fmt.Errorf("an email address that was not mocked was used: %s", email_address)
} else {
email_responses = append(email_responses, MOCK_VALIDATE_RESPONSE[email_address])
}
}
// return response
final_response := `{"email_batch": [` + strings.Join(email_responses, ",") + `], "errors": [` + strings.Join(error_responses, ",") + `]}` + "\n"
return httpmock.NewStringResponse(200, final_response), nil
},
)
}
func TestMockValidationNoApiKeySet(t *testing.T) {
Initialize("")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockValidateRequest()
_, error_ := ValidateWithTimeout("valid@example.com", SANDBOX_IP, "10")
assert.NotNil(t, error_)
assert.Contains(t, error_.Error(), "api_key")
}
// TestMockValidation test the `Validate` function on each example email
func TestMockValidationOk(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockValidateRequest()
for _, test_case := range emailsToValidate {
email_response, error_ := ValidateWithTimeout(test_case.Email, SANDBOX_IP, "10")
assert.Nil(t, error_)
assert.Equalf(t, test_case.Status, email_response.Status, "failed for email %s", email_response.Address)
assert.Equalf(t, test_case.SubStatus, email_response.SubStatus, "failed for email %s", email_response.Address)
}
}
func TestResponseSubStatusGold(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockValidateRequest()
response, error_ := ValidateWithTimeout("gold@example.com", SANDBOX_IP, "10")
assert.Nil(t, error_)
assert.Equal(t, "gold@example.com", response.Address)
assert.Equal(t, "valid", response.Status)
assert.Equal(t, "gold", response.SubStatus)
}
func TestResponseSubStatusRoleBasedAcceptAll(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockValidateRequest()
response, error_ := ValidateWithTimeout("role_based_accept_all@example.com", SANDBOX_IP, "10")
assert.Nil(t, error_)
assert.Equal(t, "role_based_accept_all@example.com", response.Address)
assert.Equal(t, "valid", response.Status)
assert.Equal(t, "role_based_accept_all", response.SubStatus)
}
func TestMockBulkValidationNoApiKey(t *testing.T) {
Initialize("")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockBatchValidateRequest()
bulk_response, error_ := ValidateBatch(EmailsToValidate())
if error_ != nil {
t.Error(error_.Error())
}
if !assert.Len(t, bulk_response.EmailBatch, 0) {
t.FailNow()
}
if !assert.Len(t, bulk_response.Errors, 1) {
t.FailNow()
}
response_error := bulk_response.Errors[0]
if !assert.Equal(t, response_error.EmailAddress, "all") {
t.FailNow()
}
if !assert.Contains(t, response_error.Error, "Invalid API Key") {
t.FailNow()
}
}
func TestMockBulkValidationNoEmails(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockBatchValidateRequest()
_, error_ := ValidateBatch([]EmailToValidate{})
assert.NotNil(t, error_)
}
func TestMockBulkValidationValidAndErroneousMail(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockBatchValidateRequest()
response, error_ := ValidateBatch([]EmailToValidate{
{EmailAddress: "valid@example.com"}, {},
})
if !assert.Nil(t, error_) {
t.FailNow()
}
assert.Len(t, response.EmailBatch, 1)
assert.Len(t, response.Errors, 1)
}
// TestMockValidation test the `ValidateBatch` function on all example emails
func TestMockBulkValidationOk(t *testing.T) {
Initialize("mock_key")
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockBatchValidateRequest()
emails_to_validate := EmailsToValidate()
response, error_ := ValidateBatch(emails_to_validate)
if error_ != nil {
t.Errorf(error_.Error())
}
for error_response := range response.Errors {
t.Error(error_response)
}
assert.Len(t, response.EmailBatch, len(emails_to_validate))
emailToTest := make(map[string]SingleTest)
for _, single_test := range emailsToValidate {
emailToTest[single_test.Email] = single_test
}
for _, email_response := range response.EmailBatch {
test_details := emailToTest[email_response.Address]
assert.Equalf(t, email_response.Status, test_details.Status, "failed for email %s", email_response.Address)
assert.Equalf(t, email_response.SubStatus, test_details.SubStatus, "failed for email %s", email_response.Address)
}
}