From ea2c3254a5208f5284f219c16c2bd6b200dd449d Mon Sep 17 00:00:00 2001 From: Guilherme Marques Silveira Date: Wed, 2 Oct 2024 21:09:14 -0300 Subject: [PATCH] apiteste --- package.json | 16 ++--- test/Api_Company.spec.ts | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 8 deletions(-) create mode 100644 test/Api_Company.spec.ts diff --git a/package.json b/package.json index 456945a..d2f3628 100644 --- a/package.json +++ b/package.json @@ -4,24 +4,24 @@ "description": "API testing with a simple integration between JestJS and PactumJS.", "main": "index.js", "devDependencies": { + "@faker-js/faker": "^9.0.3", "@types/jest": "^29.5.13", "@typescript-eslint/eslint-plugin": "^7.13.1", "@typescript-eslint/parser": "^7.13.1", + "eslint": "^8.56.0", "eslint-config-airbnb-base": "^15.0.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-import": "^2.29.1", "eslint-plugin-prettier": "^5.1.3", - "prettier": "^3.3.3", - "rimraf": "^6.0.1", - "mkdirp": "^3.0.1", - "eslint": "^8.56.0", + "http-status-codes": "^2.2.0", "jest": "^29.7.0", "jest-html-reporters": "^3.1.7", + "mkdirp": "^3.0.1", "pactum": "^3.7.1", + "prettier": "^3.3.3", + "rimraf": "^6.0.1", "ts-jest": "^29.2.5", - "typescript": "^5.6.0", - "@faker-js/faker": "^9.0.3", - "http-status-codes": "^2.2.0" + "typescript": "^5.6.0" }, "scripts": { "clean": "rimraf ./output && mkdirp ./output", @@ -42,4 +42,4 @@ "url": "https://github.com/ugioni/integration-tests-jest/issues" }, "homepage": "https://github.com/ugioni/integration-tests-jest#readme" -} \ No newline at end of file +} diff --git a/test/Api_Company.spec.ts b/test/Api_Company.spec.ts new file mode 100644 index 0000000..1810f8e --- /dev/null +++ b/test/Api_Company.spec.ts @@ -0,0 +1,151 @@ +import pactum from 'pactum'; +import { StatusCodes } from 'http-status-codes'; + +describe('Company API validation', () => { + const baseUrl = 'https://api-desafio-qa.onrender.com/company'; + + pactum.request.setDefaultTimeout(30000); + let createdCompanyId: number; + + describe('Verifying endpoints using POST method', () => { + it('Should return the same data as the JSON sent', async () => { + const companyData = { + name: 'Joao', + cnpj: '15566565500199', + state: 'Teste1', + city: 'Criciuma', + address: 'Teste2', + sector: 'Tecnologia' + }; + + try { + const response = await pactum + .spec() + .post(baseUrl) + .withHeaders('Content-Type', 'application/json') + .withJson(companyData) + .expectStatus(StatusCodes.CREATED) + .returns('body'); + + console.log('Response:', response); + createdCompanyId = response.id; + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + + it('Should return a bad request when sending invalid data', async () => { + const invalidCompanyData = { + name: '', + cnpj: '123456', + state: 'Teste1', + city: 'Criciuma', + address: 'Teste2', + sector: 'Tecnologia' + }; + + try { + await pactum + .spec() + .post(baseUrl) + .withHeaders('Content-Type', 'application/json') + .withJson(invalidCompanyData) + .expectStatus(StatusCodes.BAD_REQUEST); + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + }); + + describe('Verifying endpoints using GET method', () => { + it('Should retrieve the company details by ID', async () => { + try { + const response = await pactum + .spec() + .get(`${baseUrl}/${createdCompanyId}`) + .expectStatus(StatusCodes.OK) + .returns('body'); + + console.log('Company Details:', response); + expect(response).toEqual(expect.objectContaining({ + id: createdCompanyId, + name: expect.any(String), + address: expect.any(String), + services: expect.any(Array) + })); + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + + it('Should retrieve the products of the company by ID', async () => { + try { + const response = await pactum + .spec() + .get(`${baseUrl}/${createdCompanyId}/products`) + .expectStatus(StatusCodes.OK) + .returns('body'); + + console.log('Company Products:', response); + expect(response).toEqual(expect.any(Array)); // Verifica se a resposta é um array + // Você pode adicionar mais validações conforme necessário + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + }); + + describe('Verifying endpoints using PUT method', () => { + it('Should update the company details', async () => { + const updatedCompanyData = { + name: 'Joao Silva Updated', + cnpj: '15566565500199', // Certifique-se de que o CNPJ ainda seja válido + state: 'Teste1 Updated', + city: 'Criciuma Updated', + address: 'Teste2 Updated', + sector: 'Tecnologia Updated' + }; + + try { + const response = await pactum + .spec() + .put(`${baseUrl}/${createdCompanyId}`) + .withHeaders('Content-Type', 'application/json') + .withJson(updatedCompanyData) + .expectStatus(StatusCodes.OK) // Espera o status 200 (OK) + .returns('body'); + + console.log('Updated Company Response:', response); + expect(response).toEqual(expect.objectContaining(updatedCompanyData)); // Verifica se os dados atualizados estão corretos + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + }); + + describe('Verifying endpoints using DELETE method', () => { + it('Should delete the company by ID', async () => { + try { + await pactum + .spec() + .delete(`${baseUrl}/${createdCompanyId}`) + .expectStatus(StatusCodes.NO_CONTENT); // Espera o status 204 (No Content) + + console.log(`Company with ID ${createdCompanyId} deleted successfully.`); + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + + it('Should return a not found error when trying to retrieve the deleted company', async () => { + try { + await pactum + .spec() + .get(`${baseUrl}/${createdCompanyId}`) + .expectStatus(StatusCodes.NOT_FOUND); // Espera o status 404 (Not Found) + } catch (error) { + console.error('Error:', error); // Log do erro para análise + } + }); + }); +});