Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API tests for complete order and cancel order actions #34

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"happo-playwright": "^1.1.0",
"happo.io": "^7.2.1",
"playwright": "^1.22.1",
"serverest": "2.25.3",
"wait-on": "^6.0.1"
},
"scripts": {
Expand Down
115 changes: 98 additions & 17 deletions tests/api/cart.api.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,122 @@
const { test, expect } = require('@playwright/test')
const { POST_SUCESS, DELETE_SUCESS, SEM_CARRINHO, TOKEN_INVALID, ESTOQUE_REABASTECIDO } = require('serverest/src/utils/constants')
const { StatusCodes } = require('http-status-codes')

const { getAuthToken, getProductBody, getProductId, getCartBody, getCartId } = require('../../lib/helpers')

test.describe.parallel('Carts API', () => {
let authorization

test.beforeAll(async ({ request }) => {
authorization = await getAuthToken(request)
})

test('retrieves the list of carts', async ({ request }) => {
const response = await request.get('/carrinhos')

await expect(response).toBeOK()

expect(response.status()).toEqual(StatusCodes.OK)
})

test('retrieves an existing cart by its id', async ({ request }) => {
const productId = await getProductId(request, authorization, getProductBody())
const cartId = await getCartId(request, authorization, getCartBody(productId))

const response = await request.get(`/carrinhos/${cartId}`)

expect(response.status()).toEqual(StatusCodes.OK)
})

test('creates a cart successfully', async ({ request }) => {
const id = await getProductId(request, authorization, getProductBody())
const authorization = await getAuthToken(request)
const productId = await getProductId(request, authorization, getProductBody())

const response = await request.post('/carrinhos', {
data: getCartBody(id),
data: getCartBody(productId),
headers: { 'Authorization': authorization }
})

await expect(response).toBeOK()
expect(response.status()).toEqual(StatusCodes.CREATED)

const cart = await response.json()

const responseBody = JSON.parse(await response.text())

await expect(responseBody.message).toBe('Cadastro realizado com sucesso')
expect(cart.message).toEqual(POST_SUCESS)
})

test('retrieves an existing cart by its id', async ({ request }) => {
test('deletes an order successfully', async ({ request }) => {
const idProduct = await getProductId(request, authorization, getProductBody())
await getCartId(request, authorization, getCartBody(idProduct))
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved

const response = await request.delete('/carrinhos/concluir-compra', {
headers: { 'Authorization': authorization }
})

expect(response.status()).toEqual(StatusCodes.OK)

const cart = await response.json()

expect(await cart.message).toEqual(DELETE_SUCESS)
})

test('deletes an order without a cart', async ({ request }) => {
const response = await request.delete('/carrinhos/concluir-compra', {
headers: { 'Authorization': authorization }
})

expect(response.status()).toEqual(StatusCodes.OK)

const cart = await response.json()

expect(cart.message).toEqual(SEM_CARRINHO)
})

test('fails to delete an order if authorization token is not provided', async ({ request }) => {
const response = await request.delete('/carrinhos/concluir-compra', {
headers: { 'Authorization': '' }
})

expect(response.status()).toEqual(StatusCodes.UNAUTHORIZED)

const cart = await response.json()

expect(cart.message).toEqual(TOKEN_INVALID)
})

test('cancels an order successfully', async ({ request }) => {
const idProduct = await getProductId(request, authorization, getProductBody())
const _id = await getCartId(request, authorization, getCartBody(idProduct))

const response = await request.get(`/carrinhos/${_id}`)

await expect(response).toBeOK()
await getCartId(request, authorization, getCartBody(idProduct))

const response = await request.delete('/carrinhos/cancelar-compra', {
headers: { 'Authorization': authorization }
})

expect(response.status()).toEqual(StatusCodes.OK)

const cart = await response.json()

expect(cart.message).toEqual(`${DELETE_SUCESS}. ${ESTOQUE_REABASTECIDO}`)
})

test('cancels an order without a cart', async ({ request }) => {
const response = await request.delete('/carrinhos/cancelar-compra', {
headers: { 'Authorization': authorization }
})

expect(response.status()).toEqual(StatusCodes.OK)

const cart = await response.json()

expect(cart.message).toEqual(SEM_CARRINHO)
})

test('fails to cancel an order if authorization token is not provided', async ({ request }) => {
const response = await request.delete('/carrinhos/cancelar-compra', {
headers: { 'Authorization': '' }
})

expect(response.status()).toEqual(StatusCodes.UNAUTHORIZED)

const cart = await response.json()

expect(cart.message).toEqual(TOKEN_INVALID)
})
})
Loading