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

Add unit & integration tests and ES lint #10

Merged
merged 13 commits into from
Jun 24, 2024
4 changes: 4 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_URL=postgresql://testuser:testpassword@localhost:5433/testdb?schema=public
NEXTAUTH_SECRET=secret
NODE_ENV="test"
DATABASE_PASSWORD="test"
18 changes: 17 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@
"plugin:tailwindcss/recommended",
"prettier"
],
"plugins": ["prettier"],
"rules": {
"react/no-unescaped-entities": "off",
"react-hooks/exhaustive-deps": "off"
"react-hooks/exhaustive-deps": "off",
"prettier/prettier": [
"error",
{
"semi": true,
"trailingComma": "all",
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"endOfLine": "auto",
"bracketSpacing": true
}
]
},
"env": {
"jest": true
}
}
28 changes: 28 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Build the app and run tests
run-name: ${{ github.actor }} is creating a pull request
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
- name: Install dependencies
run: npm install
# - name: Build
# run: npm run build
- name: Run tests
run: npm test
- name: Notify Discord on failure
if: failure()
uses: containrrr/shoutrrr-action@v1
with:
url: ${{ secrets.NOTIFICATION_URL }}
title: "Build failed for ${{ github.actor }}"
message: |
Build failed or tests did not pass
Pull request: ${{ github.event.pull_request.html_url }}
23 changes: 23 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated files
.next/
dist/
out/

# Dependency directories
node_modules/

# Configuration and lock files
package-lock.json

# Environment files
.env

# Docker-related files
docker-compose.yml

# GitHub Actions workflows
.github/

# Others
tsconfig.json
jest.config.ts
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"semi": true,
"trailingComma": "all",
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"endOfLine": "auto",
"bracketSpacing": true
}
71 changes: 38 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,58 @@ Patrigma is a Progressive Web Application (PWA) designed to enhance social inter
### Installation

1. Clone the repository:

```bash
git clone https://github.com/your-username/patrigma.git
cd patrigma
```

2. Install dependencies:
```bash
npm install
```
```bash
npm install
```
3. Set up the environment variables:
```env
DATABASE_URL="your-database-url"
MAPBOX_KEY="your-mapbox-api-key"
NEXT_PUBLIC_MAPBOX_KEY="your-public-mapbox-api-key"
JWT_SECRET="mysecret"
```
4. Apply database migrations:

Dev environment :
```bash
npx prisma migrate dev
```

Prod environment :
```bash
npx prisma migrate deploy
```
```env
DATABASE_URL="your-database-url"
MAPBOX_KEY="your-mapbox-api-key"
NEXT_PUBLIC_MAPBOX_KEY="your-public-mapbox-api-key"
JWT_SECRET="mysecret"
```
4. Apply database migrations:

Dev environment :

```bash
npx prisma migrate dev
```

Prod environment :

```bash
npx prisma migrate deploy
```

5. Seeding database (optionnal):
```bash
npx prisma db seed
```

```bash
npx prisma db seed
```

6. Run the development server:
```bash
npm run dev
```
Open http://localhost:3000 with your browser to see the result.
`bash
npm run dev
`
Open http://localhost:3000 with your browser to see the result.

### Deployment

To deploy Patrigma, follow these steps:

1. Build the application for production:
```bash
npm run build
```
```bash
npm run build
```
2. Start the production server:
```bash
npm run start
```
```bash
npm run start
```
70 changes: 70 additions & 0 deletions __test__/auth/apiClient/registerUserApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @jest-environment node
*/

import { testApiHandler } from "next-test-api-route-handler";

import * as registerHandler from "../../../src/app/api/auth/register/route";

describe("/api/auth/register", () => {
it("registers a new user successfully", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01"),
}),
});
const data = await res.json();

expect(res.status).toBe(201);
expect(data).toEqual(
expect.objectContaining({
user: expect.objectContaining({
email: "[email protected]",
username: "johndoe",
name: "John",
lastName: "Doe",
}),
}),
);
},
});
});

it("returns error for already used email", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: "Alice",
lastName: "Prisma",
email: "[email protected]",
username: "aliceUserName",
password: "alicePassword",
dateOfBirth: new Date("1990-01-01"),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
message: "Email already in use",
}),
);
},
});
});
});
48 changes: 48 additions & 0 deletions __test__/auth/registerSchema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// __tests__/auth/registerSchema.test.ts
import registerSchema from "@/validators/registerSchema";

describe("registerSchema", () => {
it("should validate correct input", () => {
const validData = {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date(1990, 1, 1),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
};

expect(() => registerSchema.parse(validData)).not.toThrow();
});

it("should throw error for invalid email", () => {
const invalidData = {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date(1990, 1, 1),
email: "john.doe",
password: "Password@123",
confirmPassword: "Password@123",
};

expect(() => registerSchema.parse(invalidData)).toThrow();
});

it("should throw error for password mismatch", () => {
const invalidData = {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date(1990, 1, 1),
email: "[email protected]",
password: "Password@123",
confirmPassword: "DifferentPassword@123",
};

expect(() => registerSchema.parse(invalidData)).toThrow();
});

// Add more tests for other invalid cases as needed
});
2 changes: 1 addition & 1 deletion components.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
"components": "@/components",
"utils": "@/lib/utils"
}
}
}
20 changes: 20 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,23 @@ services:
POSTGRES_USER: patrigma
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: patrigma_db
volumes:
- postgres_data:/var/lib/postgresql/data

test_db:
image: postgres:latest
container_name: patrigma_test_db
ports:
- "5433:5432"
environment:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpassword
POSTGRES_DB: testdb
healthcheck:
test: ["CMD-SHELL", "pg_isready -U testuser -h localhost -p 5432"]
interval: 10s
timeout: 5s
retries: 5

volumes:
postgres_data:
Loading
Loading