Skip to content

Commit

Permalink
load dotenv and access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Romarionijim committed Apr 12, 2024
1 parent 1fa2dfe commit 22536d4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
8 changes: 4 additions & 4 deletions infra/api/endpoints/ApiEndpoints.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export enum ApiEndpoints {
//========================================
//--------Pet Store Endpoints-------------
//--------PET STORE endpoints-------------
PET = 'pet',
UPLOAD_IMAGE = 'uploadImage',
//=======================================
//-----------pokemon endpoint------------
//-----------POKEMON endpoints------------
POKEMON = 'pokemon',
//=======================================
//-----------go rest api endpoints-------
GO_REST_API = '',
//-----------GO REST API endpoints-------
USERS_ENDPOINT = 'users',
}
36 changes: 9 additions & 27 deletions infra/api/entities/gorestapi/Users.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
import { APIResponse } from "@playwright/test";
import { ApiClient, RequestMethods } from "../../apiClient/ApiClient";
import { ApiClient, RequestMethods, StatusCode } from "../../apiClient/ApiClient";
import Randomizer from "../../helpers/faker/Randomizer";
import { ApplicationUrl } from "../../helpers/urls/ApplicationUrl";
import { ApiEndpoints } from "../../endpoints/ApiEndpoints";

export class Users extends ApiClient {
private usersEnpoint = `${ApplicationUrl.GO_REST_API}/users`
private usersEnpoint = `${ApplicationUrl.GO_REST_API}/${ApiEndpoints.USERS_ENDPOINT}`

public async getUsers() {
let response = await this.get(this.usersEnpoint)
return response;
}

private async getGender(gender: string) {
public async getGender(gender: string) {
let response = await this.get(this.usersEnpoint)
let responseObject = await response?.json()
let genderFilter = responseObject.filter((el: any) => el.gender === gender).length
return genderFilter
}

public async getMaleUsers() {
let res = await this.getGender('male')
return res
}

public async getFemaleUsers() {
let res = await this.getGender('female')
return res
}

/**
* @description make both genders even for the first default 10 records without pagination
* @returns
*/
public async makeBothGendersEven() {
let response: APIResponse | undefined
let maleUsers = await this.getMaleUsers();
let femaleUsers = await this.getFemaleUsers();
let maleUsers = await this.getGender('male');
let femaleUsers = await this.getGender('female');
try {
let differrence = Math.abs(maleUsers - femaleUsers)
if (maleUsers === femaleUsers) {
return response;
return;
} else if (maleUsers > femaleUsers) {
for (let i = 0; i < differrence; i++) {
let femaleData = {
Expand All @@ -64,6 +55,8 @@ export class Users extends ApiClient {
}
}



return response;
} catch (error) {
throw new Error(`an error occured in makeBothGendersEven function: ${error}`)
Expand Down Expand Up @@ -150,15 +143,4 @@ export class Users extends ApiClient {
}
return extensions
}

private async replaceEmailExtension(email: string, newExtension: string) {
let emailWithoutDomain = email.substring(0, email.lastIndexOf('@'))
let emailDomain = email.split('@').pop()
let domainWithoutExtension = emailDomain?.substring(0, emailDomain.lastIndexOf('.'))
let emailExtension = emailDomain?.substring(emailDomain.lastIndexOf('.'))
let extensionSwap = emailExtension?.replace(emailExtension, newExtension)
let newEmail = `${emailWithoutDomain}@${domainWithoutExtension}${extensionSwap}`
return newEmail

}
}
3 changes: 2 additions & 1 deletion infra/api/entities/pokemon/PokemonApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { APIRequestContext, APIResponse, expect } from "@playwright/test";
import { ApiClient, RequestMethods, StatusCode } from "../../apiClient/ApiClient";
import { ApplicationUrl } from "../../helpers/urls/ApplicationUrl";
import { ApiEndpoints } from "../../endpoints/ApiEndpoints";

export class PokemonApi extends ApiClient {
private POKEMON_BASE_URL = ApplicationUrl.POKEMON_URL;
private POKEMON_ENDPOINT = `${this.POKEMON_BASE_URL}/pokemon`;
private POKEMON_ENDPOINT = `${this.POKEMON_BASE_URL}/${ApiEndpoints.POKEMON}`;

public async getPokemon() {
let response = await this.get(this.POKEMON_ENDPOINT)
Expand Down
2 changes: 1 addition & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defineConfig, devices } from '@playwright/test';
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
Expand Down
15 changes: 8 additions & 7 deletions tests/api_tests/goRestApi/GoRestApiTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ test.describe('Api tests for GoRestApi endpoints', async () => {
/**
* @description there is a bug with this endpoint - it does not authorize any generated toke=n whatsoever
*/
test.skip('gender equality', { tag: ['@GO_REST_API'] }, async () => {
await test.step('make an apir equest to make both male and female genders equal', async () => {
let response = await users.makeBothGendersEven();
expect(response?.status()).toBe(StatusCode.OK)
let maleGender = await users.getMaleUsers()
let femaleGender = await users.getFemaleUsers()
expect(maleGender.length).toEqual(femaleGender.length)
test('gender equality', { tag: ['@GO_REST_API'] }, async () => {
await test.step('make an api request to make both male and female genders equal', async () => {
await users.makeBothGendersEven();
let maleGender = await users.getGender('male')
let femaleGender = await users.getGender('female')
expect(maleGender).toEqual(femaleGender)
})
})

test('replace email extension of users', { tag: ['@GO_REST_API'] }, async () => {
await test.step('extract extension of each user email and replace each extension with co.il', async () => {
let response = await users.replaceEmailExtensionForUsers()
expect(response?.status()).toBe(StatusCode.OK)
})
await test.step('validate previous extenstions were replaced with co.il extension', async () => {
let actualEmailExtensions = await users.getCurrentUserEmailExtension()
let expectedExtensions = new Array(actualEmailExtensions.length).fill('.co.il')
expect(actualEmailExtensions).toEqual(expectedExtensions)
Expand Down
2 changes: 1 addition & 1 deletion tests/api_tests/petStore/PetStoreCrudTests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.describe.serial('CRUD API tests for the Pet Store API', async () => {
let response = await petStoreCrudActions.getPet(id)
let responseJson: Ipet = await response?.json()
expect(response?.status()).toBe(StatusCode.OK)
expect(responseJson.name).toBe('doggie')
expect(responseJson.name).toBe('ZAP')
})
})

Expand Down

0 comments on commit 22536d4

Please sign in to comment.