|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { action } from '../dashboard.transactions.$transactionId.$action' |
| 3 | +import { TRANSACTION_STATUS } from '~/models/enum' |
| 4 | +import { requireUser } from '~/services/auth.server' |
| 5 | +import { getFirstCourse } from '~/models/course' |
| 6 | +import { requireCourseAuthor } from '~/utils/permissions' |
| 7 | +import { |
| 8 | + getTransactionById, |
| 9 | + updateTransactionStatus, |
| 10 | +} from '~/models/transaction' |
| 11 | +import { |
| 12 | + activateSubscription, |
| 13 | + deactivateSubscription, |
| 14 | +} from '~/models/subscription' |
| 15 | + |
| 16 | +vi.mock('~/services/auth.server', () => ({ |
| 17 | + requireUser: vi.fn(), |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('~/models/course', () => ({ |
| 21 | + getFirstCourse: vi.fn(), |
| 22 | +})) |
| 23 | + |
| 24 | +vi.mock('~/utils/permissions', () => ({ |
| 25 | + requireCourseAuthor: vi.fn(), |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('~/models/transaction', () => ({ |
| 29 | + getTransactionById: vi.fn(), |
| 30 | + updateTransactionStatus: vi.fn(), |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('~/models/subscription', () => ({ |
| 34 | + activateSubscription: vi.fn(), |
| 35 | + deactivateSubscription: vi.fn(), |
| 36 | +})) |
| 37 | + |
| 38 | +function buildRequest(status: string, notes = 'Catatan verifikasi') { |
| 39 | + const formData = new URLSearchParams({ status, notes }) |
| 40 | + |
| 41 | + return new Request( |
| 42 | + 'http://localhost:3000/dashboard/transactions/tx-1/verify', |
| 43 | + { |
| 44 | + method: 'POST', |
| 45 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 46 | + body: formData, |
| 47 | + } |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +async function runAction(status: string) { |
| 52 | + return action({ |
| 53 | + request: buildRequest(status), |
| 54 | + params: { transactionId: 'tx-1' }, |
| 55 | + context: {}, |
| 56 | + } as never) |
| 57 | +} |
| 58 | + |
| 59 | +describe('dashboard.transactions.$transactionId.$action action', () => { |
| 60 | + beforeEach(() => { |
| 61 | + vi.resetAllMocks() |
| 62 | + |
| 63 | + vi.mocked(requireUser).mockResolvedValue({ id: 'author-1' } as never) |
| 64 | + vi.mocked(getFirstCourse).mockResolvedValue({ id: 'course-1' } as never) |
| 65 | + vi.mocked(requireCourseAuthor).mockReturnValue(true) |
| 66 | + vi.mocked(getTransactionById).mockResolvedValue({ |
| 67 | + id: 'tx-1', |
| 68 | + status: TRANSACTION_STATUS.SUBMITTED, |
| 69 | + userId: 'user-1', |
| 70 | + } as never) |
| 71 | + vi.mocked(updateTransactionStatus).mockResolvedValue({ |
| 72 | + id: 'tx-1', |
| 73 | + status: TRANSACTION_STATUS.VERIFIED, |
| 74 | + userId: 'user-1', |
| 75 | + } as never) |
| 76 | + vi.mocked(activateSubscription).mockResolvedValue({ id: 'sub-1' } as never) |
| 77 | + vi.mocked(deactivateSubscription).mockResolvedValue({ |
| 78 | + id: 'sub-1', |
| 79 | + } as never) |
| 80 | + }) |
| 81 | + |
| 82 | + it('rejects tampered status payloads that are outside allowlist', async () => { |
| 83 | + try { |
| 84 | + await runAction(TRANSACTION_STATUS.SUBMITTED) |
| 85 | + throw new Error('Expected action to throw Response') |
| 86 | + } catch (error) { |
| 87 | + expect(error).toBeInstanceOf(Response) |
| 88 | + const response = error as Response |
| 89 | + expect(response.status).toBe(400) |
| 90 | + await expect(response.json()).resolves.toBe( |
| 91 | + 'Status transaksi tidak valid.' |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + expect(updateTransactionStatus).not.toHaveBeenCalled() |
| 96 | + }) |
| 97 | + |
| 98 | + it('rejects forbidden transition from VERIFIED to REJECTED', async () => { |
| 99 | + vi.mocked(getTransactionById).mockResolvedValue({ |
| 100 | + id: 'tx-1', |
| 101 | + status: TRANSACTION_STATUS.VERIFIED, |
| 102 | + userId: 'user-1', |
| 103 | + } as never) |
| 104 | + |
| 105 | + try { |
| 106 | + await runAction(TRANSACTION_STATUS.REJECTED) |
| 107 | + throw new Error('Expected action to throw Response') |
| 108 | + } catch (error) { |
| 109 | + expect(error).toBeInstanceOf(Response) |
| 110 | + const response = error as Response |
| 111 | + expect(response.status).toBe(400) |
| 112 | + await expect(response.json()).resolves.toBe( |
| 113 | + 'Transaksi yang sudah diverifikasi tidak dapat diubah menjadi ditolak.' |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + expect(updateTransactionStatus).not.toHaveBeenCalled() |
| 118 | + }) |
| 119 | + |
| 120 | + it('accepts VERIFIED status and updates transaction', async () => { |
| 121 | + const response = await runAction(TRANSACTION_STATUS.VERIFIED) |
| 122 | + |
| 123 | + expect(response).toBeInstanceOf(Response) |
| 124 | + if (!(response instanceof Response)) throw new Error('Expected Response') |
| 125 | + expect(response.status).toBe(302) |
| 126 | + expect(response.headers.get('Location')).toBe( |
| 127 | + '/dashboard/transactions/tx-1' |
| 128 | + ) |
| 129 | + expect(updateTransactionStatus).toHaveBeenCalledWith( |
| 130 | + 'tx-1', |
| 131 | + 'Catatan verifikasi', |
| 132 | + TRANSACTION_STATUS.VERIFIED, |
| 133 | + [TRANSACTION_STATUS.SUBMITTED, TRANSACTION_STATUS.REJECTED] |
| 134 | + ) |
| 135 | + }) |
| 136 | +}) |
0 commit comments