forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeluxeApiSpec.ts
206 lines (190 loc) · 6.95 KB
/
deluxeApiSpec.ts
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
/*
* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
import frisby = require('frisby')
import config from 'config'
const jsonHeader = { 'content-type': 'application/json' }
const REST_URL = 'http://localhost:3000/rest'
const API_URL = 'http://localhost:3000/api'
async function login ({ email, password }: { email: string, password: string }) {
// @ts-expect-error FIXME promise return handling broken
const loginRes = await frisby
.post(`${REST_URL}/user/login`, {
email,
password
}).catch((res: any) => {
if (res.json?.type && res.json.status === 'totp_token_required') {
return res
}
throw new Error(`Failed to login '${email}'`)
})
return loginRes.json.authentication
}
describe('/rest/deluxe-membership', () => {
it('GET deluxe membership status for customers', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'bender@' + config.get<string>('application.domain'),
password: 'OhG0dPlease1nsertLiquor!'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.get(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' }
})
.expect('status', 200)
.expect('json', 'data', { membershipCost: 49 })
})
})
it('GET deluxe membership status for deluxe members throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'ciso@' + config.get<string>('application.domain'),
password: 'mDLx?94T~1CfVfZMzw@sJ9f?s3L6lbMqE70FfI8^54jbNikY5fymx7c!YbJb'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.get(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' }
})
.expect('status', 400)
.expect('json', 'error', 'You are already a deluxe member!')
})
})
it('GET deluxe membership status for admin throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'admin@' + config.get<string>('application.domain'),
password: 'admin123'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.get(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' }
})
.expect('status', 400)
.expect('json', 'error', 'You are not eligible for deluxe membership!')
})
})
it('GET deluxe membership status for accountant throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'accountant@' + config.get<string>('application.domain'),
password: 'i am an awesome accountant'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.get(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' }
})
.expect('status', 400)
.expect('json', 'error', 'You are not eligible for deluxe membership!')
})
})
it('POST upgrade deluxe membership status for customers', async () => {
const { token } = await login({
email: `bender@${config.get<string>('application.domain')}`,
password: 'OhG0dPlease1nsertLiquor!'
})
const { json } = await frisby.get(API_URL + '/Cards', {
headers: { Authorization: 'Bearer ' + token, 'content-type': 'application/json' }
})
.expect('status', 200)
.promise()
await frisby.post(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + token, 'content-type': 'application/json' },
body: {
paymentMode: 'card',
paymentId: json.data[0].id.toString()
}
})
.expect('status', 200)
.expect('json', 'status', 'success')
.promise()
})
it('POST deluxe membership status with wrong card id throws error', async () => {
const { token } = await login({
email: `jim@${config.get<string>('application.domain')}`,
password: 'ncc-1701'
})
await frisby.post(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + token, 'content-type': 'application/json' },
body: {
paymentMode: 'card',
paymentId: 1337
}
})
.expect('status', 400)
.expect('json', 'error', 'Invalid Card')
.promise()
})
it('POST deluxe membership status for deluxe members throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'ciso@' + config.get<string>('application.domain'),
password: 'mDLx?94T~1CfVfZMzw@sJ9f?s3L6lbMqE70FfI8^54jbNikY5fymx7c!YbJb'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.post(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' },
body: {
paymentMode: 'wallet'
}
})
.expect('status', 400)
.expect('json', 'error', 'Something went wrong. Please try again!')
})
})
it('POST deluxe membership status for admin throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'admin@' + config.get<string>('application.domain'),
password: 'admin123'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.post(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' },
body: {
paymentMode: 'wallet'
}
})
.expect('status', 400)
.expect('json', 'error', 'Something went wrong. Please try again!')
})
})
it('POST deluxe membership status for accountant throws error', () => {
return frisby.post(REST_URL + '/user/login', {
headers: jsonHeader,
body: {
email: 'accountant@' + config.get<string>('application.domain'),
password: 'i am an awesome accountant'
}
})
.expect('status', 200)
.then(({ json: jsonLogin }) => {
return frisby.post(REST_URL + '/deluxe-membership', {
headers: { Authorization: 'Bearer ' + jsonLogin.authentication.token, 'content-type': 'application/json' },
body: {
paymentMode: 'wallet'
}
})
.expect('status', 400)
.expect('json', 'error', 'Something went wrong. Please try again!')
})
})
})