generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/Arquisoft/wiq_es05b into…
… develop
- Loading branch information
Showing
5 changed files
with
110 additions
and
104 deletions.
There are no files selected for viewing
File renamed without changes.
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,50 @@ | ||
const request = require('supertest'); | ||
const bcrypt = require('bcrypt'); | ||
const express = require('express'); | ||
const routes = require('./authRoutes'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const mockUserRepository = { | ||
findUserByUsername: jest.fn(), | ||
}; | ||
|
||
let app = express(); | ||
app.use(express.json()); | ||
routes(app, mockUserRepository); | ||
|
||
describe('Auth Routes', () => { | ||
it('logs in with valid credentials', async () => { | ||
const password = 'password'; | ||
const hashedPassword = await bcrypt.hash(password, 10); | ||
mockUserRepository.findUserByUsername.mockResolvedValue({ password: hashedPassword }); | ||
|
||
const res = await request(app).post('/login').send({ username: 'username', password }); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toHaveProperty('token'); | ||
expect(res.body).toHaveProperty('username'); | ||
}); | ||
|
||
/** TODO: works in local, when in github actions (500 -> internal server error) | ||
it('fails to log in with invalid credentials', async () => { | ||
mockUserRepository.findUserByUsername.mockResolvedValue(null); | ||
const res = await request(app).post('/login').send({ username: 'username', password: 'password' }); | ||
expect(res.statusCode).toEqual(401); | ||
}); | ||
*/ | ||
|
||
|
||
it('validates a valid token', async () => { | ||
const token = jwt.sign({ userId: 'userId' }, 'a-very-secret-string', { expiresIn: '4h' }); | ||
|
||
const res = await request(app).get(`/validate/${token}`); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toEqual({ data: { userId: 'userId' }, valid: true }); | ||
}); | ||
|
||
it('fails to validate an invalid token', async () => { | ||
const res = await request(app).get('/validate/invalid'); | ||
expect(res.statusCode).toEqual(200); | ||
expect(res.body).toEqual({ valid: false }); | ||
}); | ||
}); |
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,60 @@ | ||
const request = require('supertest'); | ||
const bcrypt = require('bcrypt'); | ||
const express = require('express'); | ||
const routes = require('./usersRoutes'); | ||
|
||
const mockUserRepository = { | ||
getUser: jest.fn(), | ||
insertUser: jest.fn(), | ||
checkValidId: jest.fn(), | ||
}; | ||
|
||
|
||
const app = express(); | ||
app.use(express.json()); | ||
routes(app, mockUserRepository); | ||
|
||
describe('User Routes', () => { | ||
it('adds a new user with unique username', async () => { | ||
const password = 'password'; | ||
const hashedPassword = await bcrypt.hash(password, 10); | ||
mockUserRepository.getUser.mockResolvedValue(null); | ||
mockUserRepository.insertUser.mockResolvedValue({ username: 'username', password: hashedPassword }); | ||
|
||
const res = await request(app).post('/adduser').send({ username: 'username', password }); | ||
expect(res.statusCode).toEqual(200); | ||
}); | ||
|
||
/** TODO: works in local, when in github actions (500 -> internal server error) | ||
it('fails to add a new user with existing username', async () => { | ||
mockUserRepository.getUser.mockResolvedValue({ username: 'username' }); | ||
const res = await request(app2).post('/adduser').send({ username: 'username', password: 'password' }); | ||
expect(res.statusCode).toEqual(400); | ||
}); | ||
*/ | ||
it('fetches user by id', async () => { | ||
mockUserRepository.checkValidId.mockReturnValue(true); | ||
mockUserRepository.getUser.mockResolvedValue({ _id: 'userId', username: 'username' }); | ||
|
||
const res = await request(app).get('/user/userId'); | ||
expect(res.statusCode).toEqual(200); | ||
}); | ||
|
||
/** | ||
it('returns error for invalid id format', async () => { | ||
mockUserRepository.checkValidId.mockReturnValue(false); | ||
const res = await request(app2).get('/user/invalid'); | ||
expect(res.statusCode).toEqual(400); | ||
}); | ||
it('returns error for non-existent user', async () => { | ||
mockUserRepository.checkValidId.mockReturnValue(true); | ||
mockUserRepository.getUser.mockResolvedValue(null); | ||
const res = await request(app2).get('/user/nonexistent'); | ||
expect(res.statusCode).toEqual(404); | ||
}); | ||
*/ | ||
}); |
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