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

apiteste #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -42,4 +42,4 @@
"url": "https://github.com/ugioni/integration-tests-jest/issues"
},
"homepage": "https://github.com/ugioni/integration-tests-jest#readme"
}
}
151 changes: 151 additions & 0 deletions test/Api_Company.spec.ts
Original file line number Diff line number Diff line change
@@ -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
}
});
});
});