Skip to content

Commit

Permalink
finished users entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Romario authored and Romario committed Dec 4, 2023
1 parent 3551679 commit ee47064
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
34 changes: 24 additions & 10 deletions infra/api/entities/gorestapi/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@ export class Users extends ApiRequests {
}
}

private async getUserStatus(status: string) {
let users = await this.get(this.usersEnpoint)
let usersJsonObject = await users?.json()
let inactiveUsesrs = usersJsonObject.filter((user: { status: string; }) => user.status === status)
return inactiveUsesrs
}

public async getInActiveUsers() {
let queryParams = {
status: 'inactive'
}
let response = await this.get(this.usersEnpoint, { queryParams })
let inActiveUsersObject = await response?.json()
return inActiveUsersObject;
let res = await this.getUserStatus('inactive')
return res
}

public async deleteInactiveUsers() {
Expand Down Expand Up @@ -124,7 +127,7 @@ export class Users extends ApiRequests {
for (let user of usersObject) {
let emailExtension = await this.extractEmailExtension(user.email)
if (emailExtension && emailExtension !== 'co.il') {
let newEmail = emailExtension.replace(emailExtension, 'co.il')
let newEmail = await this.replaceEmailExtension(user.email, '.co.il')
let newEmailProperty = { email: newEmail }
response = await this.patch(`${this.usersEnpoint}/${user.id}`, newEmailProperty, { authoriaztionRequired: true })
}
Expand All @@ -139,11 +142,22 @@ export class Users extends ApiRequests {
let extensions: string[] = []
let users = await this.getUsers()
let usersJsonObject = await users?.json()
let userEmails = usersJsonObject.email
for (let email of userEmails) {
let extension = await this.extractEmailExtension(email)
let userEmails = usersJsonObject
for (let user of userEmails) {
let extension = await this.extractEmailExtension(user.email)
extensions.push(extension!)
}
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

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { test, request, expect } from '@playwright/test'
import { Users } from '../../infra/api/entities/gorestapi/Users'
import { StatusCode } from '../../infra/api/apiRequests/ApiRequests';
import { IUser } from '../../infra/api/helpers/interfaces/ApiObjectsInterfaces';
import { StatusCode } from '../../../infra/api/apiRequests/ApiRequests';
import { IUser } from '../../../infra/api/helpers/interfaces/ApiObjectsInterfaces';
import { Users } from '../../../infra/api/entities/gorestapi/Users';

test.describe('Api tests for GoRestApi endpoints', async () => {
let users: Users;
Expand All @@ -25,18 +25,9 @@ test.describe('Api tests for GoRestApi endpoints', async () => {
let response = await users.getUsers();
expect(response?.status()).toBe(StatusCode.OK)
expect(response?.body()).toBeTruthy()
let userObject = await users.getTypeOfUserProperies()
expect(userObject).toEqual([
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string'],
['number', 'string', 'string', 'string', 'string']])
let actualObjectProperties = await users.getTypeOfUserProperies()
let expectedObjectProperties = new Array(actualObjectProperties.length).fill(['number', 'string', 'string', 'string', 'string'])
expect(actualObjectProperties).toEqual(expectedObjectProperties)
})
})

Expand All @@ -58,8 +49,18 @@ test.describe('Api tests for GoRestApi endpoints', async () => {
let response = await users.replaceEmailExtensionForUsers()
expect(response?.status()).toBe(StatusCode.OK)
let actualEmailExtensions = await users.getCurrentUserEmailExtension()
let expectedExtensions = new Array(actualEmailExtensions.length).fill('co.il')
let expectedExtensions = new Array(actualEmailExtensions.length).fill('.co.il')
expect(actualEmailExtensions).toEqual(expectedExtensions)
})
})

test('delete inactive users', async () => {
await test.step('make a request to delete all users that have an inactive status', async () => {
let response = await users.deleteInactiveUsers()
expect(response?.status()).toBe(StatusCode.UNAUTHORIZED)
let actualInactiveUsers = await users.getInActiveUsers()
let expectedInactiveUsersLength = actualInactiveUsers.length
expect(expectedInactiveUsersLength).toBe(0)
})
})
})

0 comments on commit ee47064

Please sign in to comment.