-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added tests for routes PATCH /stripe/*
- Loading branch information
1 parent
7eb8cc3
commit 8235c66
Showing
7 changed files
with
134 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Router } from 'express'; | ||
import paymentAcceptedWebhook from "./paymentAcceptedWebhook"; | ||
import paymentExpiredWebhook from "./paymentExpiredWebhook"; | ||
|
||
const router = Router(); | ||
|
||
router.post('/accepted', paymentAcceptedWebhook); | ||
router.post('/expired', paymentExpiredWebhook); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import request from 'supertest'; | ||
import { TransactionState } from '@prisma/client'; | ||
import { expect } from 'chai'; | ||
import app from '../../src/app'; | ||
import { sandbox } from '../setup'; | ||
import * as cartOperations from '../../src/operations/carts'; | ||
import database from '../../src/services/database'; | ||
import { Error, User, Cart } from '../../src/types'; | ||
import { createFakeUser, createFakeCart } from '../utils'; | ||
import { updateCart } from '../../src/operations/carts'; | ||
|
||
describe('POST /stripe/accepted', () => { | ||
let user: User; | ||
let cart: Cart; | ||
|
||
before(async () => { | ||
user = await createFakeUser(); | ||
cart = await createFakeCart({ userId: user.id, items: [] }); | ||
}); | ||
|
||
after(async () => { | ||
await database.user.deleteMany(); | ||
await database.cart.deleteMany(); | ||
}); | ||
|
||
const generateCart = (transactionId: string) => ({ | ||
object: 'event', | ||
type: 'checkout.session.accepted', | ||
data: { | ||
object: { | ||
object: 'checkout.session', | ||
id: transactionId, | ||
}, | ||
}, | ||
}); | ||
|
||
it('should fail with an internal server error', async () => { | ||
sandbox.stub(cartOperations, 'fetchCartFromTransactionId').throws('Unexpected error'); | ||
await request(app).post('/stripe/accepted').send(generateCart('plz throw')).expect(500, { error: Error.InternalServerError }); | ||
}); | ||
|
||
it('should fail as the transaction id does not exist', () => | ||
request(app).post('/stripe/accepted').send(generateCart('I AM A H4X0R')).expect(404, { error: Error.CartNotFound })); | ||
|
||
it('should change the transactionState of the cart from pending to paid', async () => { | ||
await updateCart(cart.id, 'supersecret', TransactionState.pending); | ||
await request(app).post('/stripe/accepted').send(generateCart('supersecret')).expect(200, { api: 'ok' }); | ||
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } }); | ||
expect(databaseCart.transactionState).to.equal(TransactionState.paid); | ||
}); | ||
|
||
describe('test for initial transactionState = `paid` | `expired` | `refunded`', () => { | ||
for (const transactionState of [TransactionState.paid, TransactionState.expired, TransactionState.refunded]) { | ||
it(`should not change the transactionState of the cart as it already equals ${transactionState}`, async () => { | ||
await updateCart(cart.id, 'supersecret', transactionState); | ||
await request(app).post('/stripe/expired').send(generateCart('supersecret')).expect(200, { api: 'ok' }); | ||
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } }); | ||
expect(databaseCart.transactionState).to.equal(transactionState); | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import request from 'supertest'; | ||
import { TransactionState } from '@prisma/client'; | ||
import { expect } from 'chai'; | ||
import app from '../../src/app'; | ||
import { sandbox } from '../setup'; | ||
import * as cartOperations from '../../src/operations/carts'; | ||
import database from '../../src/services/database'; | ||
import { Error, User, Cart } from '../../src/types'; | ||
import { createFakeUser, createFakeCart } from '../utils'; | ||
import { updateCart } from '../../src/operations/carts'; | ||
|
||
describe('POST /stripe/expired', () => { | ||
let user: User; | ||
let cart: Cart; | ||
|
||
before(async () => { | ||
user = await createFakeUser(); | ||
cart = await createFakeCart({ userId: user.id, items: [] }); | ||
}); | ||
|
||
after(async () => { | ||
await database.cart.deleteMany(); | ||
await database.user.deleteMany(); | ||
}); | ||
|
||
const generateCart = (transactionId: string) => ({ | ||
object: 'event', | ||
type: 'checkout.session.expired', | ||
data: { | ||
object: { | ||
object: 'checkout.session', | ||
id: transactionId, | ||
}, | ||
}, | ||
}); | ||
|
||
it('should fail with an internal server error', async () => { | ||
sandbox.stub(cartOperations, 'fetchCartFromTransactionId').throws('Unexpected error'); | ||
await request(app).post('/stripe/expired').send(generateCart('plz throw')).expect(500, { error: Error.InternalServerError }); | ||
}); | ||
|
||
it('should fail as the transaction id does not exist', () => | ||
request(app).post('/stripe/expired').send(generateCart('I AM A H4X0R')).expect(404, { error: Error.CartNotFound })); | ||
|
||
it('should change the transactionState of the cart from `pending` to `expired`', async () => { | ||
await updateCart(cart.id, 'supersecret', TransactionState.pending); | ||
await request(app).post('/stripe/expired').send(generateCart('supersecret')).expect(200, { api: 'ok' }); | ||
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } }); | ||
expect(databaseCart.transactionState).to.equal(TransactionState.expired); | ||
}); | ||
|
||
describe('test for initial transactionState = `paid` | `expired` | `refunded`', () => { | ||
for (const transactionState of [TransactionState.paid, TransactionState.expired, TransactionState.refunded]) { | ||
it(`should not change the transactionState of the cart as it already equals ${transactionState}`, async () => { | ||
await updateCart(cart.id, 'supersecret', transactionState); | ||
await request(app).post('/stripe/expired').send(generateCart('supersecret')).expect(200, { api: 'ok' }); | ||
const databaseCart = await database.cart.findUnique({ where: { id: cart.id } }); | ||
expect(databaseCart.transactionState).to.equal(transactionState); | ||
}); | ||
} | ||
}); | ||
}); |