-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cy.js
202 lines (192 loc) · 6.89 KB
/
test.cy.js
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
// DO NOT CHANGE THIS FILE!
const apiUrl = `${Cypress.env("apiUrl")}`
function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
describe('Transaction Management Backend - Level 2', () => {
it('Provides a functional healthcheck', () => {
cy.request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/ping`,
}).then((response) => {
expect(response.status).to.eq(200)
})
})
it('Can create and read transactions and accounts with positive amounts', () => {
const accountId = uuid()
let transactionId
cy.request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json"
},
body: {
account_id: accountId,
amount: 7
}
}).then((response) => {
assert.equal(response.status, 201, "Creating a transaction should result with 201 status code")
assert.isDefined(response.body.transaction_id, "A transaction id should be returned")
transactionId = response.body.transaction_id
cy.request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/transactions/${transactionId}`,
}).then((response) => {
assert.equal(response.status, 200, "Getting an existing transaction by its id should give 200 status code")
assert.equal(response.body.transaction_id, transactionId, "Got a transaction of a different id than queried for")
assert.equal(response.body.account_id, accountId, "Account different than expected")
assert.equal(response.body.amount, 7, "Got unexpected transaction amount value")
})
}).request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/accounts/${accountId}`,
}).then((response) => {
assert.equal(response.status, 200, "Getting existing transaction should return 200 OK")
assert.equal(response.body.account_id, accountId, "Got unexpected account_id value")
assert.equal(response.body.balance, 7, "Incorrect account balance returned")
})
})
it('Can create and read transactions and accounts with negative amounts', () => {
const accountId = uuid()
let transactionId
cy.request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: {
account_id: accountId,
amount: 4
}
}).then((response) => {
assert.equal(response.status, 201, "Creating a transaction should result with 201 status code")
assert.isDefined(response.body.transaction_id, "A transaction id must be returned")
transactionId = response.body.transaction_id
}).request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/accounts/${accountId}`,
}).then((response) => {
assert.equal(response.status, 200, "Getting existing account should give 200 OK status code")
assert.equal(response.body.account_id, accountId, "Got unexpected account_id value")
assert.equal(response.body.balance, 4, "Incorrect account balance returned")
}).request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: {
account_id: accountId,
amount: -3
}
}).then((response) => {
assert.equal(response.status, 201, "Creating a transaction should result with 201 status code")
assert.isDefined(response.body.transaction_id, "A transaction id must be returned in the transaction creation response")
transactionId = response.body.transaction_id
}).request({ // read account balance
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/accounts/${accountId}`,
}).then((response) => {
assert.equal(response.status, 200, "Getting existing account should give 200 OK")
assert.equal(response.body.account_id, accountId, "Got unexpected account_id value")
assert.equal(response.body.balance, 1, "Incorrect account balance returned")
})
})
it('Can handle requests for non-existent accounts and transactions', () => {
const accountId = uuid()
const transactionId = uuid()
cy.request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/accounts/${accountId}`,
}).then((response) => {
assert.equal(response.status, 404, "Reading an inexistent account should return 404")
}).request({
failOnStatusCode: false,
method: 'GET',
url: `${apiUrl}/transactions/${transactionId}`,
}).then((response) => {
assert.equal(response.status, 404, "Reading an inexistent transaction should return 404")
})
})
it('Can handle invalid requests', () => {
const accountId = uuid()
cy.request({
failOnStatusCode: false,
method: 'PUT', // wrong method
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: {
account_id: accountId,
amount: 10
}
}).then((response) => {
assert.equal(response.status, 405, "Method not allowed status code should be returned when using a wrong HTTP method")
}).request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/xml", // wrong Content-Type
},
body: {
account_id: accountId,
amount: 10
}
}).then((response) => {
assert.equal(response.status, 415, "Incorrect content-type should be returned when passing a wrong Content-Type header")
}).request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: { // missing account_id
amount: 7
}
}).then((response) => {
assert.equal(response.status, 400, "Bad request status code should be returned in case of missing account_id")
}).request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: { // missing amount
account_id: accountId
}
}).then((response) => {
assert.equal(response.status, 400, "Bad request status code should be returned in case of missing amount")
}).request({
failOnStatusCode: false,
method: 'POST',
url: `${apiUrl}/transactions`,
headers: {
"Content-Type": "application/json",
},
body: {
account_id: 10, // bad format
amount: 7
}
}).then((response) => {
assert.equal(response.status, 400, "Bad request status code should be returned in case of malformed transaction id")
})
})
})