-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtransaction-flows.spec.ts
More file actions
161 lines (135 loc) · 4.89 KB
/
transaction-flows.spec.ts
File metadata and controls
161 lines (135 loc) · 4.89 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
import { Course, PrismaClient, Transaction, User } from '@prisma/client'
import { readFixture } from '../app/utils/fixtures'
import { generateId } from '../app/utils/nanoid'
import { stripLeadingPlus } from '../app/utils/misc'
import { getWhatsAppLinkForConfirmation } from '../app/utils/whatsapp'
import { transactionBuilder } from '../app/models/__mocks__/transaction'
import { userBuilder } from '../app/models/__mocks__/user'
import { test, expect } from './base-test'
import { authFixtures, getDataFixturePath, isStagingEnv } from './fixtures'
process.env.DATABASE_URL = process.env.DATABASE_URL ?? 'file:./test.db'
const db = new PrismaClient()
test.skip(isStagingEnv, 'Skipping on staging - creates local transaction data')
test.skip(
({ browserName, noscript }) => browserName !== 'chromium' || noscript,
'Transaction flow tests mutate shared local database fixtures'
)
test.afterAll(async () => {
await db.$disconnect()
})
async function readUserFixture(name: string): Promise<User> {
return JSON.parse(await readFixture(getDataFixturePath('users', name)))
}
async function getCourse(): Promise<Course> {
const course = await db.course.findFirst()
if (!course) {
throw new Error('Expected a seeded course fixture')
}
return course
}
test.describe('Transaction confirmation process', () => {
test.use({ storageState: authFixtures.memberEdit })
test.beforeEach(async () => {
const member = await readUserFixture('member-edit')
await db.transaction.deleteMany({ where: { userId: member.id } })
})
test('submits payment details and generates the WhatsApp confirmation link', async ({
page,
baseURL,
}) => {
const author = await readUserFixture('author')
await page.goto('/dashboard/purchase/confirm')
await page.getByRole('textbox', { name: /nama bank/i }).fill('Bank Jago')
await page
.getByRole('textbox', { name: /nomor rekening/i })
.fill('123-456-789')
await page
.getByRole('textbox', { name: /nama pemilik rekening/i })
.fill('Member Edit')
await page.getByRole('textbox', { name: /nominal/i }).fill('200000')
await page.getByRole('button', { name: /konfirmasi pembayaran/i }).click()
await page.waitForURL(/\/dashboard\/purchase\/verify\/[^/]+$/)
const verifyPurchaseLink = page.getByRole('link', {
name: /verifikasi pembelian/i,
})
await expect(verifyPurchaseLink).toBeVisible()
await expect(verifyPurchaseLink).toHaveAttribute(
'href',
/\/dashboard\/purchase\/verify\/[^/]+$/
)
const verifyPurchaseHref = await verifyPurchaseLink.getAttribute('href')
const transactionId = (verifyPurchaseHref as string).split('/').at(-1)
const href = getWhatsAppLinkForConfirmation(
author.phoneNumber,
transactionId as string,
baseURL
)
const url = new URL(href)
expect(url.origin).toBe('https://api.whatsapp.com')
expect(url.pathname).toBe('/send')
expect(url.searchParams.get('phone')).toBe(
stripLeadingPlus(author.phoneNumber)
)
expect(url.searchParams.get('text')).toContain(
`${baseURL}/dashboard/transactions/${transactionId}`
)
})
})
test.describe('Transaction verification process', () => {
test.use({ storageState: authFixtures.author })
let member: User
let transaction: Transaction
test.beforeEach(async () => {
const course = await getCourse()
member = await db.user.create({
data: {
id: generateId(),
...userBuilder({ overrides: { phoneNumber: '+6281234567890' } }),
},
})
transaction = await db.transaction.create({
data: {
id: generateId(),
userId: member.id,
courseId: course.id,
authorId: course.authorId,
...transactionBuilder(),
},
})
})
test('verifies a submitted transaction and exposes the member WhatsApp link', async ({
page,
}) => {
await page.goto(`/dashboard/transactions/${transaction.id}`)
const contactWhatsAppButton = page.getByRole('link', {
name: /kontak whatsapp/i,
})
await expect(contactWhatsAppButton).toBeVisible()
await expect(contactWhatsAppButton).toHaveAttribute(
'href',
`https://wa.me/${stripLeadingPlus(member.phoneNumber)}`
)
const verifyTransactionLink = page.getByRole('link', {
name: /verifikasi transaksi/i,
})
await expect(verifyTransactionLink).toHaveAttribute(
'href',
`/dashboard/transactions/${transaction.id}/verify`
)
const response = await page.request.post(
`/dashboard/transactions/${transaction.id}/verify`,
{
form: {
status: 'VERIFIED',
notes: 'Valid payment',
},
}
)
expect(response.ok()).toBe(true)
await page.goto(`/dashboard/transactions/${transaction.id}`)
await expect(page.locator('id=transaction-status')).toContainText(
'Terverifikasi'
)
await expect(page.locator('id=notes')).toContainText('Valid payment')
})
})