-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement mentor availability endpoint (#64)
Co-authored-by: Yoshitha Rathnayake <[email protected]>
- Loading branch information
1 parent
538f1fb
commit c95e3bc
Showing
6 changed files
with
159 additions
and
3 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
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,82 @@ | ||
import { startServer } from '../../app' | ||
import type { Express } from 'express' | ||
import supertest from 'supertest' | ||
import Profile from '../../entities/profile.entity' | ||
import { ProfileTypes } from '../../enums' | ||
import { dataSource } from '../../configs/dbConfig' | ||
import bcrypt from 'bcrypt' | ||
import { mockAdmin, mockUser } from '../../../mocks' | ||
|
||
const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000 | ||
let server: Express | ||
let agent: supertest.SuperAgentTest | ||
let adminAgent: supertest.SuperAgentTest | ||
|
||
describe('Get all users route', () => { | ||
beforeAll(async () => { | ||
server = await startServer(port) | ||
agent = supertest.agent(server) | ||
adminAgent = supertest.agent(server) | ||
|
||
const defaultUser = { | ||
...mockUser | ||
} | ||
|
||
await supertest(server) | ||
.post('/api/auth/register') | ||
.send(defaultUser) | ||
.expect(201) | ||
|
||
await agent.post('/api/auth/login').send(defaultUser).expect(200) | ||
|
||
const adminUser = { | ||
...mockAdmin | ||
} | ||
|
||
const profileRepository = dataSource.getRepository(Profile) | ||
|
||
const hashedPassword = await bcrypt.hash(adminUser.password, 10) | ||
const newProfile = profileRepository.create({ | ||
primary_email: adminUser.email, | ||
password: hashedPassword, | ||
contact_email: '', | ||
first_name: '', | ||
last_name: '', | ||
image_url: '', | ||
linkedin_url: '', | ||
type: ProfileTypes.ADMIN | ||
}) | ||
|
||
await profileRepository.save(newProfile) | ||
|
||
await adminAgent.post('/api/auth/login').send(adminUser).expect(200) | ||
}, 5000) | ||
|
||
it('should return a 401 when a valid access token is not provided', async () => { | ||
await supertest(server).get('/api/admin/users').expect(401) | ||
}) | ||
|
||
it('should return a 403 if user is not admin', async () => { | ||
await agent.get('/api/admin/users').expect(403) | ||
}) | ||
|
||
it('should return a 200 with all users if user is admin', async () => { | ||
const response = await adminAgent.get('/api/admin/users').expect(200) | ||
|
||
const userProfiles = response.body.profiles | ||
|
||
userProfiles.forEach((userProfile: Partial<Profile>) => { | ||
expect(userProfile).toHaveProperty('created_at') | ||
expect(userProfile).toHaveProperty('updated_at') | ||
expect(userProfile).toHaveProperty('primary_email') | ||
expect(userProfile).toHaveProperty('contact_email') | ||
expect(userProfile).toHaveProperty('first_name') | ||
expect(userProfile).toHaveProperty('last_name') | ||
expect(userProfile).toHaveProperty('image_url') | ||
expect(userProfile).toHaveProperty('linkedin_url') | ||
expect(userProfile).toHaveProperty('type') | ||
expect(userProfile).toHaveProperty('uuid') | ||
expect(userProfile).not.toHaveProperty('password') | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import express from 'express' | ||
import { requireAuth } from './../../controllers/auth.controller' | ||
import { mentorApplicationHandler } from './../../controllers/mentor.controller' | ||
import { | ||
mentorApplicationHandler, | ||
mentorAvailabilityHandler | ||
} from './../../controllers/mentor.controller' | ||
|
||
const mentorRouter = express.Router() | ||
|
||
mentorRouter.post('/', requireAuth, mentorApplicationHandler) | ||
mentorRouter.put('/me/availability', requireAuth, mentorAvailabilityHandler) | ||
|
||
export default mentorRouter |
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,8 @@ | ||
import { dataSource } from '../configs/dbConfig' | ||
import Profile from '../entities/profile.entity' | ||
|
||
export const getAllUsers = async (): Promise<Profile[] | undefined> => { | ||
const profileRepository = dataSource.getRepository(Profile) | ||
const allUsers = await profileRepository.find() | ||
return allUsers | ||
} |
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