diff --git a/tests/userTests.spec.ts b/tests/userTests.spec.ts new file mode 100644 index 0000000..9e03507 --- /dev/null +++ b/tests/userTests.spec.ts @@ -0,0 +1,21 @@ +import { test, expect } from '@playwright/test'; +import { loginWithCredsFromEnv, CheckUserInformation, CreateTestUser } from './utilities'; +import { validUserName, validPassword } from '../playwright.config'; + + +test.describe('Login and UserInfo tests', () => { + test('Create User', async () => { + const responseStatus = await CreateTestUser({ username: validUserName, password: validPassword }) + expect(responseStatus.status()).toBe(200); + }); + + test('Check Login', async () => { + const responseStatus = await loginWithCredsFromEnv(validUserName, validPassword); + expect(responseStatus.status()).toBe(200); + }); + + test('Check User', async () => { + const responseStatus = await CheckUserInformation(validUserName) + expect(responseStatus.status()).toBe(200); + }); +}) \ No newline at end of file diff --git a/tests/utilities.ts b/tests/utilities.ts new file mode 100644 index 0000000..94c71ca --- /dev/null +++ b/tests/utilities.ts @@ -0,0 +1,48 @@ +import { test, expect, request } from '@playwright/test'; + +export async function CreateTestUser({id = 11225, username = "TestUser", firstName = "Auto", lastName = "Testing", email = "test.user@example.com", password = "12345678Aa!", phone = "0661112233", userStatus = 0} = {}) { + const context = await request.newContext(); + const response = await context.post('https://petstore.swagger.io/v2/user', { + data: { + "id": id, + "username": username, + "firstName": firstName, + "lastName": lastName, + "email": email, + "password": password, + "phone": phone, + "userStatus": userStatus + } + }) + const responseBody = await response.json(); + console.log('Creation response:', responseBody); + return response +} + +export async function loginWithCredsFromEnv(username: string | undefined, password: string | undefined) { + const context = await request.newContext(); + if (!username || !password) { + throw new Error('Username or password is undefined'); + } + const response = await context.get('https://petstore.swagger.io/v2/user/login', { + + params: { + username: username, + password: password, + }, + }); + const responseBody = await response.json(); + console.log('Login response:', responseBody); + return response +} + +export async function CheckUserInformation(username: string | undefined) { + const context = await request.newContext(); + const response = await context.get(`https://petstore.swagger.io/v2/user/${username}`) + const responseBody = await response.json(); + console.log(responseBody) + + const userInfo = await response.json(); + console.log('User Information:', userInfo); + return response +} \ No newline at end of file