-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: test(unfinished), github module for project add request, refac…
…tor services to use class Signed-off-by: Desmond Obisi <[email protected]>
- Loading branch information
1 parent
c0073e1
commit 71e21a6
Showing
22 changed files
with
6,144 additions
and
1,391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
presets: [['@babel/preset-env', { targets: { node: 'current' } }]], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)'], | ||
}; |
Oops, something went wrong.