Skip to content

Commit

Permalink
contiued with the pet store api
Browse files Browse the repository at this point in the history
  • Loading branch information
Romario authored and Romario committed Nov 30, 2023
1 parent 8311ce3 commit 8b8e690
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 28 deletions.
Binary file added .DS_Store
Binary file not shown.
72 changes: 62 additions & 10 deletions infra/api/apiRequests/ApiRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,80 @@ export class ApiRequests {
return response
}

public async get<T>(url: string, queryParams?: { [key: string]: T | any }) {
let response = await this.makeRequest(RequestMethods.GET, url, { requestData: queryParams })
/**
* @description function that supports pagination via page pagination or by limit and offset pagination
*/
private async paginateRequest<T>(method: RequestMethods, url: string, options?: { pagePagination?: boolean, limitOffsetPagination?: boolean, queryParams?: { [key: string]: any }, pageNumber?: number, limit?: number, offset?: number, requestData?: { [key: string]: T }, authoriaztionRequired?: boolean }) {
let existingQueryParams = { ...options?.queryParams }
let response: APIResponse | undefined
try {
while (true) {
if (options?.pagePagination && options?.pageNumber !== undefined) {
let page = options?.pageNumber
existingQueryParams['page'] = page
response = await this.makeRequest(method, url, { queryParams: existingQueryParams, requestData: options.requestData, authoriaztionRequired: options.authoriaztionRequired })
let responseObject = await response?.json()
if (!responseObject || responseObject.length === 0) {
break
}
options.pageNumber = page
page++
}
if (options?.limitOffsetPagination) {

existingQueryParams['limit'] = options.limit
existingQueryParams['offset'] = options.offset
response = await this.makeRequest(method, url, { queryParams: existingQueryParams, requestData: options.requestData, authoriaztionRequired: options.authoriaztionRequired })
let responseObject = await response?.json()
if (!responseObject || responseObject.length === 0) {
break
}
options.limit! += options.offset!
}
}
return response;

} catch (error) {
throw new Error(`none of the conditions in the paginateRequest function were satisfied `)
}
}

/**
* @description make request by deciding if you want to include pagination or without paginating the request
*/
private async httpRequest<T>(method: RequestMethods, url: string, options?: { queryParams?: { [key: string]: T | any }, requestData?: { [key: string]: T }, authoriaztionRequired?: boolean, isMultiPart?: boolean, multipartObject?: { [key: string]: any }, pagePagination?: boolean, limitOffsetPagination?: boolean, pageNumber?: number, limit?: number, offset?: number, paginateRequest?: boolean }) {
let response: APIResponse | undefined
if (options?.paginateRequest) {
response = await this.paginateRequest(method, url, { queryParams: options?.queryParams, requestData: options?.requestData, authoriaztionRequired: options?.authoriaztionRequired })
} else {
response = await this.makeRequest(method, url, { queryParams: options?.queryParams, requestData: options?.requestData, authoriaztionRequired: options?.authoriaztionRequired, isMultiPart: options?.isMultiPart, multipartObject: options?.multipartObject })
}

return response;
}

public async get<T>(url: string, queryParams?: { [key: string]: T | any }, options?: { paginate?: boolean }) {
let response = await this.httpRequest(RequestMethods.GET, url, { requestData: queryParams, paginateRequest: options?.paginate })
return response;
}

public async post<T>(url: string, options?: { data?: { [key: string]: T }, isMultiPart?: boolean, multiPartData?: { [key: string]: T } }) {
let response = await this.makeRequest(RequestMethods.POST, url, { isMultiPart: options?.isMultiPart, requestData: options?.data, multipartObject: options?.multiPartData })
public async post<T>(url: string, data: { [key: string]: T }, options?: { isMultiPart?: boolean, multiPartData?: { [key: string]: T }, paginate?: boolean }) {
let response = await this.httpRequest(RequestMethods.POST, url, { isMultiPart: options?.isMultiPart, requestData: data, multipartObject: options?.multiPartData, paginateRequest: options?.paginate })
return response;
}

public async put<T>(url: string, data?: { [key: string]: T }) {
let response = await this.makeRequest(RequestMethods.PUT, url, { requestData: data });
public async put<T>(url: string, data: { [key: string]: T }, options?: { paginate?: boolean }) {
let response = await this.httpRequest(RequestMethods.PUT, url, { requestData: data, paginateRequest: options?.paginate });
return response;
}

public async patch<T>(url: string, data?: { [key: string]: T }) {
let response = await this.makeRequest(RequestMethods.PATCH, url, { requestData: data });
public async patch<T>(url: string, data?: { [key: string]: T }, options?: { paginate?: boolean }) {
let response = await this.httpRequest(RequestMethods.PATCH, url, { requestData: data, paginateRequest: options?.paginate });
return response;
}

public async delete<T>(url: string) {
let response = await this.makeRequest(RequestMethods.DELETE, url);
public async delete<T>(url: string, options?: { paginate?: boolean }) {
let response = await this.httpRequest(RequestMethods.DELETE, url, { paginateRequest: options?.paginate });
return response;
}
}
16 changes: 13 additions & 3 deletions infra/api/entities/petStore/PetStoreCrudActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIRequestContext } from "@playwright/test";
import { APIRequestContext, APIResponse } from "@playwright/test";
import { ApiRequests } from "../../apiRequests/ApiRequests";
import { ApiEndpoints } from "../../endpoints/ApiEndpoints";
import { ApplicationUrl } from "../../helpers/urls/ApplicationUrl";
Expand All @@ -10,13 +10,13 @@ export class PetStoreCrudActions extends ApiRequests {

private petStorePetEndpoint = `${ApplicationUrl.PET_STORE_URL}/${ApiEndpoints.PET}`

public async getPet(petId: number) {
public async getPet(petId: number): Promise<APIResponse | undefined> {
let response = await this.get(`${this.petStorePetEndpoint}/${petId}`)
return response;
}

public async createNewPet<T>(petData: { [key: string]: T }) {
let response = await this.post(this.petStorePetEndpoint, { data: petData })
let response = await this.post(this.petStorePetEndpoint, petData)
return response;
}

Expand Down Expand Up @@ -54,4 +54,14 @@ export class PetStoreCrudActions extends ApiRequests {
// return res;

}

public async updatePet<T>(updatedData: { [key: string]: T }) {
let response = await this.put(this.petStorePetEndpoint, updatedData)
return response;
}

public async deletePet(petId: number) {
let response = await this.delete(`${this.petStorePetEndpoint}/${petId}`)
return response;
}
}
60 changes: 45 additions & 15 deletions tests/api_tests/petStore/PetStoreCrudTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import Randomizer from '../../../infra/api/helpers/faker/Randomizer';

test.describe('CRUD API tests for the Pet Store API', async () => {
let petStoreCrudActions: PetStoreCrudActions;
let petId: number = 10;
let createdPedtId: number = 3193;
let id: number = 10;
let petId: number = 3193;


test.beforeEach(async ({ request }) => {
Expand All @@ -17,7 +17,7 @@ test.describe('CRUD API tests for the Pet Store API', async () => {

test('get a specific pet for sanity checkup @PET_STORE', async () => {
await test.step('make an api request to a specific pet ID', async () => {
let response = await petStoreCrudActions.getPet(petId)
let response = await petStoreCrudActions.getPet(id)
let responseJson: Ipet = await response?.json()
expect(response?.status()).toBe(StatusCode.OK)
expect(responseJson.name).toBe('doggie')
Expand All @@ -27,12 +27,12 @@ test.describe('CRUD API tests for the Pet Store API', async () => {
test('create a new pet @PET_STORE', async () => {
await test.step('create a new pet via post request', async () => {
let petData = {
id: Randomizer.getRandomLongNumber(),
id: petId,
category: {
id: Randomizer.getRandomLongNumber(),
name: Randomizer.getRandomName()
},
name: Randomizer.getDogNameBreed(),
name: 'Pikachu',
photoUrls: ['https://ibb.co/wLWCrSX'],
tags: [
{
Expand All @@ -52,29 +52,59 @@ test.describe('CRUD API tests for the Pet Store API', async () => {

test('validate the pet existance', async () => {
await test.step('validate the pet that was created from previous test now exists', async () => {
let response = await petStoreCrudActions.getPet(createdPedtId)
let response = await petStoreCrudActions.getPet(petId)
let responseBody: Ipet = await response?.json();
expect(response).toBeTruthy()
expect(response?.status()).toBe(StatusCode.OK)
expect(responseBody.id).toEqual(createdPedtId)
expect(responseBody.name).toEqual('Shiloh Shepherd')
expect(responseBody.id).toEqual(petId)
expect(responseBody.name).toEqual('Pikachu')
})
})

test.skip('create pet image', async () => {
await test.step('upload another image to the pet that was created in the previous test', async () => {
let imageFileName: string = 'pug.png'
let response = await petStoreCrudActions.uploadPetImage(createdPedtId, imageFileName);
let response = await petStoreCrudActions.uploadPetImage(petId, imageFileName);
expect(response?.status()).toBe(StatusCode.OK);

})
})

// (property) multipart?: {
// [key: string]: string | number | boolean | ReadStream | {
// name: string;
// mimeType: string;
// };
// } | undefined
test('update pet', async () => {
await test.step('update the newly created pet that was created in previous test', async () => {
let petData = {
id: petId,
category: {
id: Randomizer.getRandomLongNumber(),
name: Randomizer.getRandomName()
},
name: 'Pokey',
photoUrls: ['https://ibb.co/0Z9v02Z'],
tags: [
{
id: Randomizer.getRandomLongNumber(),
name: Randomizer.getRandomName(),
}
],
status: 'available'
}
let response = await petStoreCrudActions.updatePet(petData)
let responseBody: Ipet = await response?.json();
expect(response?.status()).toBe(StatusCode.OK)
expect(responseBody.name).toEqual('Pokey');
})
})

test('delete pet', async () => {
await test.step('delete the pet that was created and updated in previous tests', async () => {
let response = await petStoreCrudActions.deletePet(petId)
expect(response?.status()).toBe(StatusCode.OK)
})

await test.step('retrieve the deleted pet and validate it does not exist by validating response returns 404', async () => {
let deletedPet = await petStoreCrudActions.getPet(petId)
expect(deletedPet?.status()).toBe(StatusCode.NOT_FOUND);
})
})

})

0 comments on commit 8b8e690

Please sign in to comment.