Skip to content

Commit

Permalink
chore: test(unfinished), github module for project add request, refac…
Browse files Browse the repository at this point in the history
…tor services to use class

Signed-off-by: Desmond Obisi <[email protected]>
  • Loading branch information
DesmondSanctity committed Nov 10, 2024
1 parent c0073e1 commit 71e21a6
Show file tree
Hide file tree
Showing 22 changed files with 6,144 additions and 1,391 deletions.
58 changes: 58 additions & 0 deletions __tests__/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import request from 'supertest';
import { describe, test, expect, beforeAll } from '@jest/globals';
import app from '../src/app.js';
import { User } from '../src/models/index.js';
import { generateToken } from '../src/configs/jwt.config.js';

describe('Auth endpoints', () => {
let adminToken;

beforeAll(async () => {
const admin = await User.create({
fullname: 'Admin User',
email: '[email protected]',
password: 'password123',
isSuperAdmin: true,
});
adminToken = generateToken(admin);
});

describe('POST /api/auth/login', () => {
test('should login successfully with valid credentials', async () => {
const res = await request(app).post('/api/auth/login').send({
email: '[email protected]',
password: 'password123',
});

expect(res.status).toBe(200);
expect(res.body.status).toBe('success');
expect(res.body.data).toHaveProperty('token');
expect(res.body.data.user).toHaveProperty('email', '[email protected]');
});

test('should fail with invalid credentials', async () => {
const res = await request(app).post('/api/auth/login').send({
email: '[email protected]',
password: 'wrongpassword',
});

expect(res.status).toBe(401);
expect(res.body.status).toBe('error');
});
});

describe('POST /api/auth/invitations', () => {
test('should create invitation successfully', async () => {
const res = await request(app)
.post('/api/auth/invitations')
.set('Authorization', `Bearer ${adminToken}`)
.send({
invitee_email: '[email protected]',
isSuperAdmin: false,
});

expect(res.status).toBe(201);
expect(res.body.data.invitation).toHaveProperty('invitee_email', '[email protected]');
});
});
});
67 changes: 67 additions & 0 deletions __tests__/invitation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, test, expect, beforeAll } from '@jest/globals';
import request from 'supertest';
import app from '../src/app.js';
import { User, Invitation } from '../src/models/index.js';
import { generateToken } from '../src/configs/jwt.config.js';

describe('Invitation endpoints', () => {
let adminToken = null;

beforeAll(async () => {
const admin = await User.create({
fullname: 'Admin User',
email: '[email protected]',
password: 'password123',
isSuperAdmin: true,
});
adminToken = generateToken(admin);

await Invitation.create({
inviter_id: admin.id,
invitee_email: '[email protected]',
token: 'test-token',
expires_at: new Date(Date.now() + 24 * 60 * 60 * 1000),
});
});

describe('GET /api/invitations', () => {
test('should get all invitations', async () => {
const res = await request(app)
.get('/api/invitations')
.set('Authorization', `Bearer ${adminToken}`);

expect(res.status).toBe(200);
expect(Array.isArray(res.body.data)).toBe(true);
});

test('should fail without auth token', async () => {
const res = await request(app).get('/api/invitations');
expect(res.status).toBe(401);
});
});

describe('POST /api/invitations/accept', () => {
test('should accept invitation successfully', async () => {
const res = await request(app).post('/api/invitations/accept').send({
token: 'test-token',
fullname: 'New User',
password: 'password123',
confirmPassword: 'password123',
});

expect(res.status).toBe(200);
expect(res.body.data).toHaveProperty('token');
});

test('should fail with invalid token', async () => {
const res = await request(app).post('/api/invitations/accept').send({
token: 'invalid-token',
fullname: 'New User',
password: 'password123',
confirmPassword: 'password123',
});

expect(res.status).toBe(400);
});
});
});
10 changes: 10 additions & 0 deletions __tests__/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { afterAll, beforeAll } from '@jest/globals';
import sequelize from '../src/configs/db.config.js';

beforeAll(async () => {
await sequelize.sync({ force: true });
});

afterAll(async () => {
await sequelize.close();
});
50 changes: 50 additions & 0 deletions __tests__/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import request from 'supertest';
import { describe, test, expect, beforeAll } from '@jest/globals';
import app from '../src/app.js';
import { User } from '../src/models/index.js';
import { generateToken } from '../src/configs/jwt.config.js';

describe('User endpoints', () => {
let adminToken;
let testUserId;

beforeAll(async () => {
const admin = await User.create({
fullname: 'Admin User',
email: '[email protected]',
password: 'password123',
isSuperAdmin: true,
});
adminToken = generateToken(admin);

const testUser = await User.create({
fullname: 'Test User',
email: '[email protected]',
password: 'password123',
});
testUserId = testUser.id;
});

describe('GET /api/users', () => {
test('should get all users', async () => {
const res = await request(app).get('/api/users').set('Authorization', `Bearer ${adminToken}`);

expect(res.status).toBe(200);
expect(Array.isArray(res.body.data)).toBe(true);
});
});

describe('PUT /api/users/:id', () => {
test('should update user successfully', async () => {
const res = await request(app)
.put(`/api/users/${testUserId}`)
.set('Authorization', `Bearer ${adminToken}`)
.send({
fullname: 'Updated Name',
});

expect(res.status).toBe(200);
expect(res.body.data.fullname).toBe('Updated Name');
});
});
});
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]],
};
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default [
sourceType: 'module',
globals: {
...globals.node,
...globals.jest,
},
},
plugins: {
Expand Down
10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
testEnvironment: 'node',
transform: {
'^.+\\.js$': 'babel-jest',
},
moduleFileExtensions: ['js'],
testMatch: ['**/__tests__/**/*.test.js'],
setupFilesAfterEnv: ['./__tests__/setup.js'],
transformIgnorePatterns: ['node_modules/(?!@babel/runtime)'],
};
Loading

0 comments on commit 71e21a6

Please sign in to comment.