Skip to content

Commit

Permalink
fix(api-playwright): move to uuid random generation
Browse files Browse the repository at this point in the history
  • Loading branch information
geromegrignon committed Oct 24, 2023
1 parent 4361079 commit 6c71b72
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/api-testing-playwright/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npx nx e2e api-testing-playwright
run with ui mode:

```shell
npx nx e2e api-testing-playwright --uiMode
npx nx e2e api-testing-playwright --ui
```

filter tests by tags included:
Expand Down
9 changes: 5 additions & 4 deletions apps/api-testing-playwright/src/app.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from '@playwright/test';
import { uuid } from 'uuidv4';

test('should retrieve articles', async ({ request }) => {
const response = await request.get('/api/articles');
Expand All @@ -8,9 +9,9 @@ test('should retrieve articles', async ({ request }) => {
});

test('CRUD article', async ({ request }) => {
const username = `ggn-username-${new Date().getTime()}`;
const email = `ggn-email-${new Date().getTime()}`;
const password = `ggn-password-${new Date().getTime()}`;
const username = `ggn-username-${uuid()}`;
const email = `ggn-email-${uuid()}`;
const password = `ggn-password-${uuid()}`;
const accountCreationResponse = await request.post('/api/users', {
data: {
user: {
Expand All @@ -24,7 +25,7 @@ test('CRUD article', async ({ request }) => {
const { user } = await accountCreationResponse.json();
const token = user.token;

const title = `ggn-title-${new Date().getTime()}`;
const title = `ggn-title-${uuid()}`;
const articleCreationResponse = await request.post('/api/articles', {
headers: {
Authorization: `Token ${token}`,
Expand Down
19 changes: 10 additions & 9 deletions apps/api-testing-playwright/src/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { expect, test } from '@playwright/test';
import { createUser, login } from './utils/user.util';
import { uuid } from 'uuidv4';

// TODO: change endpoint from users to users/create ?
test.describe('@POST create user', () => {
test('OK @201', async ({ request }) => {
// Given
const username = `ggn-username-${new Date().getTime()}`;
const email = `ggn-email-${new Date().getTime()}`;
const password = `ggn-password-${new Date().getTime()}`;
const username = `ggn-username-${uuid()}`;
const email = `ggn-email-${uuid()}`;
const password = `ggn-password-${uuid()}`;

// When
const response = await createUser(request, username, email, password);

// Then
expect(response.ok()).toBeTruthy();
expect(response.status()).toEqual(200);
expect(response.status()).toEqual(201);
const { user } = await response.json();
expect(user.username).toEqual(username);
expect(user.email).toEqual(email);
Expand All @@ -23,8 +24,8 @@ test.describe('@POST create user', () => {

test('KO @422', async ({ request }) => {
// Given
const username = `ggn-username-${new Date().getTime()}`;
const email = `ggn-email-${new Date().getTime()}`;
const username = `ggn-username-${uuid()}`;
const email = `ggn-email-${uuid()}`;
const password = null;

// When
Expand All @@ -38,9 +39,9 @@ test.describe('@POST create user', () => {
test.describe('@POST login', () => {
test('OK @200', async ({ request }) => {
// Given
const username = `ggn-username-${new Date().getTime()}`;
const email = `ggn-email-${new Date().getTime()}`;
const password = `ggn-password-${new Date().getTime()}`;
const username = `ggn-username-${uuid()}`;
const email = `ggn-email-${uuid()}`;
const password = `ggn-password-${uuid()}`;
await createUser(request, username, email, password);

// When
Expand Down
7 changes: 4 additions & 3 deletions apps/api-testing-playwright/src/utils/account.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test as base } from '@playwright/test';
import { uuid } from 'uuidv4';

type Account = {
email: string;
Expand All @@ -13,9 +14,9 @@ export const test = base.extend<{ account: Account }>({
const response = await request.post('/api/users', {
data: {
user: {
username: `ggn-username-${new Date().getTime()}`,
email: `ggn-email-${new Date().getTime()}`,
password: `ggn-password-${new Date().getTime()}`,
username: `ggn-username-${uuid()}`,
email: `ggn-email-${uuid()}`,
password: `ggn-password-${uuid()}`,
},
},
});
Expand Down
3 changes: 2 additions & 1 deletion apps/api-testing-playwright/src/utils/article.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIRequestContext } from 'playwright';
import { uuid } from 'uuidv4';

export const createPost = async (request: APIRequestContext, token) => {
return request.post('/api/articles', {
Expand All @@ -7,7 +8,7 @@ export const createPost = async (request: APIRequestContext, token) => {
},
data: {
article: {
title: `ggn-title-${new Date().getTime()}`,
title: `ggn-title-${uuid()}`,
description: 'bar',
body: 'bar',
tagList: ['foo'],
Expand Down
7 changes: 4 additions & 3 deletions apps/api-testing-playwright/src/utils/user.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { APIRequestContext } from 'playwright';
import { APIResponse } from '@playwright/test';
import { uuid } from 'uuidv4';

export const createUser = async (
request: APIRequestContext,
username = `ggn-username-${new Date().getTime()}`,
email = `ggn-email-${new Date().getTime()}`,
password = `ggn-password-${new Date().getTime()}`,
username = `ggn-username-${uuid()}`,
email = `ggn-email-${uuid()}`,
password = `ggn-password-${uuid()}`,
): Promise<APIResponse> => {
return request.post('/api/users', {
data: {
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@mands/nx-playwright": "^0.4.0",
"@nx-plus/docusaurus": "^15.0.0-rc.0",
"@nx/cypress": "17.0.1",
"@nx/eslint": "17.0.1",
"@nx/eslint-plugin": "17.0.1",
"@nx/express": "17.0.1",
"@nx/jest": "17.0.1",
Expand Down Expand Up @@ -142,13 +143,13 @@
"tsx": "^3.12.6",
"typescript": "5.1.6",
"url-loader": "^4.1.1",
"uuidv4": "^6.2.13",
"vite": "4.5.0",
"vite-plugin-eslint": "1.8.1",
"vite-tsconfig-paths": "4.2.0",
"vitest": "0.32.4",
"webpack": "5.89.0",
"webpack-merge": "^5.8.0",
"@nx/eslint": "17.0.1"
"webpack-merge": "^5.8.0"
},
"lint-staged": {
"*.{js,ts}": "eslint --cache --fix",
Expand Down

1 comment on commit 6c71b72

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.