Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Arquisoft/wiq_es05b into…
Browse files Browse the repository at this point in the history
… develop
  • Loading branch information
algarfer committed Apr 25, 2024
2 parents 240e6fa + 730bc1d commit bea0d80
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 104 deletions.
File renamed without changes.
47 changes: 0 additions & 47 deletions users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
50 changes: 50 additions & 0 deletions users/authservice/routes/authRoutes.test.js
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 });
});
});
60 changes: 60 additions & 0 deletions users/userservice/routes/usersRoutes.test.js
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);
});
*/
});
57 changes: 0 additions & 57 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
*/
});

0 comments on commit bea0d80

Please sign in to comment.