-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsms_clone_repo_test.go
149 lines (131 loc) · 4.75 KB
/
sms_clone_repo_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
package sms_nigeria_go
import (
"errors"
"reflect"
"strings"
"testing"
)
var (
stubSmsClone StubSmsCloneRepository
smsCloneServiceComponent = SmsCloneComponent{
&StubSmsCloneRepository{},
}
smsCloneInfo = SmsCloneNotification{
Sender: sender,
Recipient: recipient,
Username: "Hello",
Password: "assss",
Message: apiToken,
}
smsCloneCredentialInfo = SmsCloneCredential{
Username: sender,
Password: recipient,
}
creditBalance = "500"
)
type StubSmsCloneRepository struct{}
func (stub StubSmsCloneRepository) SmsClone(sms *SmsCloneNotification, route string) (SmsCloneResponse, error) {
if result := stub.ValidateSmsCloneInput(sms); len(result) > 0 {
return SmsCloneResponse{}, nil
}
smsCloneResp := SmsCloneResponse{}
smsCloneResp.BatchCode = "1234"
smsCloneResp.BatchDescription = "Processed"
smsCloneResp.StatusCode = "200"
smsCloneResp.Recipient = "Adeshina"
smsCloneResp.MessageID = "01234"
smsCloneResp.MessageStatus = "Success"
smsCloneResp.StatusDescription = "Sent"
return smsCloneResp, nil
}
func (stub StubSmsCloneRepository) SmsCloneCheckBalance(sms *SmsCloneCredential) (response string, err error) {
if result := stub.ValidateSmsCloneCredentials(sms); len(result) > 0 {
return response, errors.New("validation errors")
}
response = creditBalance
return response, nil
}
func (stub StubSmsCloneRepository) ValidateSmsCloneInput(smsInfo *SmsCloneNotification) (err map[string]interface{}) {
err = smsCloneRepo.ValidateSmsCloneInput(smsInfo)
return err
}
func (stub StubSmsCloneRepository) ValidateSmsCloneCredentials(smsInfo *SmsCloneCredential) (err map[string]interface{}) {
err = smsCloneRepo.ValidateSmsCloneCredentials(smsInfo)
return err
}
func TestSmsCloneUrls(t *testing.T) {
// Testing full URL
actualFullURL := SmsCloneNormalRouteURLCreate + "?api_token=" + apiToken +
"&from=" + sender + "&to=" + recipient + "&body=" +
strings.Replace(body, " ", "%20", -1)
correctFullURL := SmsCloneNormalRouteURLCreate + "?api_token=ABCDE&from=Adeshina&to=09099999&body=Hello.%20How%20are%20you%20doing"
t.Run("returns TRUE on correct full url", func(t *testing.T) {
result := compareUrls(actualFullURL, correctFullURL)
assertCorrectURL(t, result, true)
})
wrongFullURL := "https://smsclone.com/api/v1/sms/create?api_token=/"
t.Run("returns FALSE on wrong full url", func(t *testing.T) {
result := compareUrls(actualFullURL, wrongFullURL)
assertCorrectURL(t, result, false)
})
}
func TestSmsCloneValidation(t *testing.T) {
//Testing validation
var validationErr map[string]interface{}
t.Run("returns true for passed validation", func(t *testing.T) {
validationErr = stubSmsClone.ValidateSmsCloneInput(&smsCloneInfo)
assertPassedValidation(t, len(validationErr), 0)
})
t.Run("returns true for failed validation", func(t *testing.T) {
validationErr = stubSmsClone.ValidateSmsCloneInput(&SmsCloneNotification{})
assertFailedValidation(t, len(validationErr), 5)
})
}
func TestSmsCloneCheckBalance(t *testing.T) {
//Testing account balance
t.Run("returns true for passed check balance", func(t *testing.T) {
result, _ := stubSmsClone.SmsCloneCheckBalance(&smsCloneCredentialInfo)
assertCheckBalanceSmsClone(t, result, creditBalance)
})
t.Run("returns true for failed balance check", func(t *testing.T) {
result, _ := stubSmsClone.SmsCloneCheckBalance(&SmsCloneCredential{})
assertCheckBalanceSmsClone(t, result, "")
})
}
func TestSmsCloneHTTPResponse(t *testing.T) {
smsCloneResp := SmsCloneResponse{}
smsCloneResp.BatchCode = "1234"
smsCloneResp.BatchDescription = "Processed"
smsCloneResp.StatusCode = "200"
smsCloneResp.Recipient = "Adeshina"
smsCloneResp.MessageID = "01234"
smsCloneResp.MessageStatus = "Success"
smsCloneResp.StatusDescription = "Sent"
//Testing endpoint
t.Run("returns true on non-empty bulkSmsNigeriaResp struct", func(t *testing.T) {
result, _ := stubSmsClone.SmsClone(&smsCloneInfo, "")
assertEndpointCorrectResponseSmsClone(t, result, smsCloneResp)
})
t.Run("returns true on empty bulkSmsNigeriaResp struct", func(t *testing.T) {
result, _ := stubSmsClone.SmsClone(&SmsCloneNotification{}, "")
assertEndpointWrongResponseSmsClone(t, result, SmsCloneResponse{})
})
}
func assertEndpointCorrectResponseSmsClone(t *testing.T, got, want SmsCloneResponse) {
t.Helper()
if !reflect.DeepEqual(got, want) {
t.Errorf("result of full endpoint contact is wrong, got %v want %v", got, want)
}
}
func assertCheckBalanceSmsClone(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("result of balance check is wrong, got %v want %v", got, want)
}
}
func assertEndpointWrongResponseSmsClone(t *testing.T, got, want SmsCloneResponse) {
t.Helper()
if reflect.DeepEqual(got, want) == false {
t.Errorf("result of endpoint result comparison is wrong, got %v want %v", got, want)
}
}