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 2 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
104 changes: 91 additions & 13 deletions tests/api/cart.api.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,119 @@
const { test, expect } = require('@playwright/test')
const { POST_SUCESS } = require('serverest/src/utils/constants')

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()
})

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

const response = await request.get(`/carrinhos/${_id}`)
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved

await expect(response).toBeOK()
})

test('creates a cart successfully', async ({ request }) => {
const id = await getProductId(request, authorization, getProductBody())
const authorization = await getAuthToken(request)
const idProduct = await getProductId(request, authorization, getProductBody())
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved

const response = await request.post('/carrinhos', {
data: getCartBody(id),
data: getCartBody(idProduct),
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
headers: { 'Authorization': authorization }
})
const responseBody = JSON.parse(await response.text())

await expect(response).toBeOK()
expect(responseBody.message).toEqual(POST_SUCESS)
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
})

test('delete an order successfully', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
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 }
})

await expect(response).toBeOK()

const responseBody = JSON.parse(await response.text())
await expect(responseBody.message).toBe('Cadastro realizado com sucesso')

await expect(responseBody.message).toBe('Registro excluído com sucesso')
})

test('retrieves an existing cart by its id', async ({ request }) => {
test('delete an order without a cart', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
const response = await request.delete('/carrinhos/concluir-compra', {
headers: { 'Authorization': authorization }
})

await expect(response).toBeOK()

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

await expect(responseBody.message).toBe('Não foi encontrado carrinho para esse usuário')
})

test('delete an order without a token', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
const response = await request.delete('/carrinhos/concluir-compra', {
headers: { 'Authorization': '' }
})

await expect(response).not.toBeOK()

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

await expect(responseBody.message).toBe('Token de acesso ausente, inválido, expirado ou usuário do token não existe mais')
})

test('cancel an order successfully', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
const idProduct = await getProductId(request, authorization, getProductBody())
const _id = await getCartId(request, authorization, getCartBody(idProduct))

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

await getCartId(request, authorization, getCartBody(idProduct))

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

await expect(response).toBeOK()

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

await expect(responseBody.message).toBe('Registro excluído com sucesso. Estoque dos produtos reabastecido')
})

test('cancel an order without a cart', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
const response = await request.delete('/carrinhos/cancelar-compra', {
headers: { 'Authorization': authorization }
})

await expect(response).toBeOK()

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

await expect(responseBody.message).toBe('Não foi encontrado carrinho para esse usuário')
})

test('cancel an order without a token', async ({ request }) => {
uLucasFraga marked this conversation as resolved.
Show resolved Hide resolved
const response = await request.delete('/carrinhos/cancelar-compra', {
headers: { 'Authorization': '' }
})

await expect(response).not.toBeOK()

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

await expect(responseBody.message).toBe('Token de acesso ausente, inválido, expirado ou usuário do token não existe mais')
})
})
Loading