Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests #22

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Playwright Tests
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
jobs:
test:
services:
postgres:
image: postgres:13
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Install Playwright Browsers
run: npm run build && npx playwright install --with-deps
env:
DATABASE_URL: ${{ secrets.DATABASE_URL }}
POSTGRES_URL_NON_POOLING: ${{ secrets.POSTGRES_URL_NON_POOLING }}
VERCEL_EMAIL_API_KEY: ${{ secrets.VERCEL_EMAIL_API_KEY }}
VERCEL_EMAIL_SENDER: ${{ secrets.VERCEL_EMAIL_SENDER }}
- name: Run Playwright tests
run: npx playwright test --workers=1 --reporter=list
env:
# The hostname used to communicate with the PostgreSQL service container
POSTGRES_HOST: postgres
# The default PostgreSQL port
POSTGRES_PORT: 5432
DATABASE_URL: ${{ secrets.DATABASE_URL }}

- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts
storybook*
storybook*
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
<div align="center">
<img alt="vercel" src="https://vercelbadge.vercel.app/api/shech2/management-system?style=for-the-badge">
<img alt="GitHub issues" src="https://img.shields.io/github/issues/shech2/management-system?style=for-the-badge">
<img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/shech2/management-system?style=for-the-badge">
<img alt="contributors" src="https://img.shields.io/github/contributors/shech2/management-system?style=for-the-badge">
</div>


## Getting Started

Expand Down
68 changes: 64 additions & 4 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "npm run prisma:generate && npm run build-storybook && NODE_ENV=production next build",
"build": "npm run prisma:generate && prisma migrate dev && npm run build-storybook && NODE_ENV=production next build",
"start": "next start",
"format": "npx prettier --write .",
"lint": "next lint",
Expand All @@ -23,6 +23,7 @@
"bcrypt": "^5.1.1",
"crypto": "^1.0.1",
"date-fns": "^3.3.0",
"dotenv": "^16.4.1",
"jsonwebtoken": "^9.0.2",
"next": "14.0.4",
"next-auth": "^4.24.5",
Expand All @@ -38,6 +39,7 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@playwright/test": "^1.41.2",
"@storybook/addon-essentials": "^7.6.10",
"@storybook/addon-interactions": "^7.6.10",
"@storybook/addon-links": "^7.6.10",
Expand Down
80 changes: 80 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';
import path from 'path';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
dotenv.config({ path: path.resolve(__dirname, '.', '.env.local') });

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
/* Run tests in files in parallel */
fullyParallel: false,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://localhost:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },

// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run dev',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
},
});
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ generator client {
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("POSTGRES_URL_NON_POOLING") // uses a direct connection
}

enum Provider {
Expand Down
1 change: 1 addition & 0 deletions src/api/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const SendInviteEmail = async (
subject: string,
): Promise<CreateEmailResponse | null> => {
try {
console.log('Sending email to:', email);
// TODO: change from address to actual platform email - need to purchasedomain. currently not developed
const sendResponse = await resend.emails.send({
from: SenderString,
Expand Down
11 changes: 11 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Role } from '@prisma/client';

export const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

export const baseURL = 'http://localhost:3000';

export enum Routes {
AUTH = '/auth',
DASHBOARD = '/dashboard',
Expand All @@ -11,3 +15,10 @@ export enum Endpoints {
AUTH = '/auth',
TASKS = '/tasks',
}
export const User = {
email: '[email protected]',
password: '123123',
name: 'test',
teamName: 'test',
Role: Role.ADMIN,
};
65 changes: 65 additions & 0 deletions tests/Auth/auth.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Routes, baseURL, User } from '@/consts';
import { test, expect } from '@playwright/test';
import prisma from '../../lib/prismadb';
import bcrypt from 'bcrypt';
import { Provider } from '@prisma/client';

test.beforeEach(async ({ page }) => {
await page.goto(baseURL);
});

test('load login page', async ({ page }) => {
await page.goto(baseURL + Routes.AUTH);
expect(page.url()).toBe(baseURL + Routes.AUTH);
expect(await page.title()).toBe('Management System');
});

test('check db', async () => {
const users = await prisma.user.findMany();
expect(users).toBeTruthy();
});

test('create static user in db', async () => {
const passwordHash = bcrypt.hashSync(User.password, 10);
const user = await prisma.user.create({
data: {
email: User.email,
fullName: User.name,
passwordHash: passwordHash,
lastLogin: new Date(),
role: User.Role,
provider: Provider.EMAIL,
},
select: {
id: true,
email: true,
fullName: true,
passwordHash: true,
lastLogin: true,
teamId: true,
role: true,
},
});
const team = await prisma.team.create({
data: {
name: User.teamName,
users: {
connect: {
id: user.id,
},
},
},
});
});

test('login test without signup', async ({ page }) => {
// Expect a title "to contain" a substring.
await page.getByPlaceholder('Email').fill(User.email);
await page.getByPlaceholder('Password').fill(User.password);
await page.getByRole('button', { name: 'Log in' }).click();
await page.waitForURL(Routes.DASHBOARD);
expect(page.url()).toBe(baseURL + Routes.DASHBOARD);
expect(await page.getByRole('button', { name: 'Logout' }).textContent()).toBe(
'Logout',
);
});
Loading