forked from form3tech-oss/interview-accountapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_client_integration_test.go
227 lines (190 loc) · 7.26 KB
/
account_client_integration_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
220
221
222
223
224
225
226
227
package interview_accountapi
import (
"fmt"
"github.com/google/uuid"
"os"
"testing"
)
func Test_Integration_Fetch_IdIsNotUuid(t *testing.T) {
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
account, httpErr := client.Fetch("blah")
assertHttpError(t, httpErr, &HTTPError{
Message: "id must be a valid uuid",
})
assertAccountData(t, account, nil)
}
func Test_Integration_Fetch_404(t *testing.T) {
id, _ := uuid.NewUUID()
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
account, httpErr := client.Fetch(id.String())
expectedPayload := []byte(`{"error_message":"record ` + id.String() + ` does not exist"}`)
assertHttpError(t, httpErr, &HTTPError{
StatusCode: 404,
Message: "Unexpected response code returned for Get operation, expected 200, got 404",
ResponsePayload: &expectedPayload,
})
assertAccountData(t, account, nil)
}
func Test_Integration_CreateBadRequest(t *testing.T) {
class := "Unexpected Classification"
accountMatchingOptOut := false
country := "CA"
jointAccount := true
status := "pending"
switched := true
requestAccount := &AccountData{
Attributes: &AccountAttributes{
AccountClassification: &class,
AccountMatchingOptOut: &accountMatchingOptOut,
AccountNumber: "A1234567",
AlternativeNames: []string{"x", "y", "z"},
BankID: "GBDSC",
BankIDCode: "BIDC",
BaseCurrency: "CAD",
Bic: "AAAAAABB",
Country: &country,
Iban: "II00",
JointAccount: &jointAccount,
Name: []string{"a", "b", "c"},
SecondaryIdentification: "Driver's License",
Status: &status,
Switched: &switched,
},
ID: uuid.NewString(),
OrganisationID: uuid.NewString(),
Type: "accounts",
}
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
createRespAccount, httpErr := client.Create(requestAccount)
responsePayload := []byte(`{"error_message":"validation failure list:\nvalidation failure list:\nvalidation failure list:\naccount_classification in body should be one of [Personal Business]"}`)
assertHttpError(t, httpErr, &HTTPError{
StatusCode: 400,
ResponsePayload: &responsePayload,
Message: "Unexpected response code returned for Post operation, expected 201, got 400",
})
assertAccountData(t, createRespAccount, nil)
}
func Test_Integration_CreateDuplicateConstraint(t *testing.T) {
requestAccount := getValidAccountData()
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
createRespAccount, httpErr := client.Create(requestAccount)
assertHttpError(t, httpErr, nil)
assertAccountData(t, createRespAccount, requestAccount)
createRespAccount, httpErr = client.Create(requestAccount)
responsePayload := []byte(`{"error_message":"Account cannot be created as it violates a duplicate constraint"}`)
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
Message: "Unexpected response code returned for Post operation, expected 201, got 409",
StatusCode: 409,
ResponsePayload: &responsePayload,
})
assertAccountData(t, createRespAccount, nil)
}
func Test_Integration_CreateFetchDelete(t *testing.T) {
requestAccount := getValidAccountData()
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
createRespAccount, httpErr := client.Create(requestAccount)
assertHttpError(t, httpErr, nil)
assertAccountData(t, createRespAccount, requestAccount)
fetchRespAccount, httpErr := client.Fetch(createRespAccount.ID)
assertHttpError(t, httpErr, nil)
assertAccountData(t, fetchRespAccount, requestAccount)
httpErr = client.Delete(fetchRespAccount.ID, *fetchRespAccount.Version)
assertHttpError(t, httpErr, nil)
httpErr = client.Delete(fetchRespAccount.ID, *fetchRespAccount.Version)
emptyByteSlice := make([]byte, 0)
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
Message: "Unexpected response code returned for Delete operation, expected 204, got 404",
StatusCode: 404,
ResponsePayload: &emptyByteSlice,
})
expectedPayload := []byte(`{"error_message":"record ` + requestAccount.ID + ` does not exist"}`)
fetchRespAccount, httpErr = client.Fetch(createRespAccount.ID)
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
ResponsePayload: &expectedPayload,
StatusCode: 404,
Message: "Unexpected response code returned for Get operation, expected 200, got 404",
})
assertAccountData(t, fetchRespAccount, nil)
}
func Test_Integration_Delete(t *testing.T) {
requestAccount := getValidAccountData()
id := requestAccount.ID
version := requestAccount.Version
clientFactory := AccountsHttpClientFactory{}
client, _ := clientFactory.MakeClient(getBaseUrl())
httpErr := client.Delete(id, *version) // there is nothing to delete, 404 is the correct answer
emptyByteSlice := make([]byte, 0)
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
StatusCode: 404,
Message: "Unexpected response code returned for Delete operation, expected 204, got 404",
ResponsePayload: &emptyByteSlice,
})
client.Create(requestAccount)
wrongVersion := int64(1)
responsePayload := []byte(`{"error_message":"invalid version"}`)
httpErr = client.Delete(id, wrongVersion)
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
StatusCode: 409,
Message: "Unexpected response code returned for Delete operation, expected 204, got 409",
ResponsePayload: &responsePayload,
})
httpErr = client.Delete(id, *version)
assertHttpError(t, httpErr, nil)
httpErr = client.Delete(id, *version) // there is nothing to delete, 404 is the correct answer
assertHttpError(t, httpErr, &HTTPError{
Cause: nil,
StatusCode: 404,
Message: "Unexpected response code returned for Delete operation, expected 204, got 404",
ResponsePayload: &emptyByteSlice,
})
}
func getValidAccountData() *AccountData {
accountMatchingOptOut := false
country := "CA"
jointAccount := true
status := "pending"
switched := true
version := int64(0)
id := uuid.NewString()
requestAccount := &AccountData{
Attributes: &AccountAttributes{
AccountMatchingOptOut: &accountMatchingOptOut,
AccountNumber: "A1234567",
AlternativeNames: []string{"x", "y", "z"},
BankID: "GBDSC",
BankIDCode: "BIDC",
BaseCurrency: "CAD",
Bic: "AAAAAABB",
Country: &country,
Iban: "II00",
JointAccount: &jointAccount,
Name: []string{"a", "b", "c"},
SecondaryIdentification: "Driver's License",
Status: &status,
Switched: &switched,
},
ID: id,
OrganisationID: uuid.NewString(),
Type: "accounts",
Version: &version,
}
return requestAccount
}
func getBaseUrl() string {
baseServiceUrl := os.Getenv("ACCOUNTS_SERVICE_BASE_URL")
if baseServiceUrl == "" {
baseServiceUrl = "http://localhost:8080"
}
fmt.Println("Base Service URL = " + baseServiceUrl)
return baseServiceUrl
}