diff --git a/jordi/jordi-service.test.js b/jordi/jordi-service.draft.js similarity index 100% rename from jordi/jordi-service.test.js rename to jordi/jordi-service.draft.js diff --git a/users/authservice/auth-service.test.js b/users/authservice/auth-service.test.js index ad8162c9..758ee4fc 100644 --- a/users/authservice/auth-service.test.js +++ b/users/authservice/auth-service.test.js @@ -109,50 +109,3 @@ describe("[Auth Service] - /validate/:token", () => { }) }) -const express = require('express'); -const routes = require('./routes/authRoutes'); -const jwt = require('jsonwebtoken'); - -const mockUserRepository = { - findUserByUsername: jest.fn(), -}; - -let app2 = express(); -app2.use(express.json()); -routes(app2, 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(app2).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(app2).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(app2).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(app2).get('/validate/invalid'); - expect(res.statusCode).toEqual(200); - expect(res.body).toEqual({ valid: false }); - }); -}); diff --git a/users/authservice/routes/authRoutes.test.js b/users/authservice/routes/authRoutes.test.js new file mode 100644 index 00000000..8aa19b11 --- /dev/null +++ b/users/authservice/routes/authRoutes.test.js @@ -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 }); + }); +}); diff --git a/users/userservice/routes/usersRoutes.test.js b/users/userservice/routes/usersRoutes.test.js new file mode 100644 index 00000000..a1d3221f --- /dev/null +++ b/users/userservice/routes/usersRoutes.test.js @@ -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); + }); + */ +}); \ No newline at end of file diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 3710fcda..74c11a7c 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -102,60 +102,3 @@ describe('[User Service] - /user/:userId', () => { }) -const express = require('express'); -const routes = require('./routes/usersRoutes'); - -const mockUserRepository = { - getUser: jest.fn(), - insertUser: jest.fn(), - checkValidId: jest.fn(), -}; - -const app2 = express(); -app2.use(express.json()); -routes(app2, 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(app2).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(app2).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); - }); - */ -}); \ No newline at end of file