Skip to content

Commit c86e0f1

Browse files
committed
signup fixed
1 parent d6ccbad commit c86e0f1

File tree

6 files changed

+30
-119
lines changed

6 files changed

+30
-119
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,8 @@ CLIENTID:your_google_oauth_cliendId
6363
## Tests
6464

6565
- The aplication was tested with [Jest](https://jestjs.io/) and [Supertest](https://www.npmjs.com/package/supertest)
66-
6766
```
68-
npm test
67+
npm run test
6968
7069
```
7170

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"type": "module",
77
"scripts": {
8-
"test": "jest",
8+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
99
"start": "node index.js",
1010
"dev": "nodemon index.js"
1111
},

server/application/users/usersControllers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
import userService from './usersService.js';
22
const maxAge = 24 * 60 * 60 * 1000;
33

4-
const usersController = {
5-
signIn,
6-
signUp,
7-
getUser,
8-
getStat,
9-
googleToken,
10-
findByEmail,
11-
};
12-
134
function signIn(req, res, next) {
14-
console.log("aca");
5+
console.log('aca');
156
userService
167
.signIn(req.body)
178
.then(({ user, sendToken }) => {
@@ -31,6 +22,7 @@ console.log("aca");
3122
}
3223

3324
function signUp(req, res, next) {
25+
console.log('controller');
3426
userService
3527
.signUp(req.body)
3628
.then((user) => res.json(user))
@@ -65,4 +57,12 @@ function googleToken(req, res, next) {
6557
.catch((error) => next(error));
6658
}
6759

60+
const usersController = {
61+
signIn,
62+
signUp,
63+
getUser,
64+
getStat,
65+
googleToken,
66+
findByEmail,
67+
};
6868
export default usersController;

server/application/users/usersService.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,21 @@ async function checkLength(item, num) {
6666

6767
async function signUp(user) {
6868
const allowedFields = ['email', 'password', 'username'];
69-
7069
const filteredUser = Object.keys(user) //check if user contains allowedFields
7170
.filter((key) => allowedFields.includes(key))
7271
.reduce((obj, key) => {
7372
obj[key] = user[key];
7473
return obj;
7574
}, {});
75+
await checkLength(filteredUser.password, 8);
7676

77-
const validatePass = await checkLength(filteredUser.password, 8);
78-
if (validatePass) {
77+
try {
7978
const encryptPassword = await encrypt.hashPassword(filteredUser.password);
8079
const createUser = new User({ ...filteredUser, password: encryptPassword });
8180
const newUser = await createUser.save();
8281
return newUser;
83-
} else {
84-
throw new BadRequestError('password does not match');
82+
} catch (error) {
83+
throw new BadRequestError(error.message);
8584
}
8685
}
8786

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect } from 'chai';
2+
import usersController from '../../application/users/usersControllers';
3+
4+
describe('this tests should return succesfull responses', () => {
5+
it('should return an user with status code 200', async () => {
6+
const body = {
7+
email: '12345@hotmail.com',
8+
password:
9+
'@ztCU^d3Q9%JXkN!toR&fVniRupNySHo3w^6tP*VfG9vuoH9u#zod42JC6Y6Poq',
10+
};
11+
const response = usersController.signIn(body);
12+
console.log(response);
13+
});
14+
});
Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1 @@
1-
import request from 'supertest';
2-
import { Router } from 'express';
3-
import cartController from './shopingCartController.js';
4-
import authMiddleware from '../helpers/jwt.js';
5-
import adminCheck from '../helpers/adminCheck.js';
6-
import { cleanBody } from '../helpers/sanitizer.js';
71

8-
const router = Router();
9-
10-
// Mock authMiddleware and adminCheck
11-
jest.mock('../helpers/jwt.js', () => jest.fn());
12-
jest.mock('../helpers/adminCheck.js', () => jest.fn());
13-
14-
// Mock cartController methods
15-
cartController.createCart = jest.fn();
16-
cartController.getAll = jest.fn();
17-
cartController.getUserCart = jest.fn();
18-
cartController.editCart = jest.fn();
19-
cartController.deleteCart = jest.fn();
20-
21-
// Mock router methods
22-
router.post = jest.fn();
23-
router.get = jest.fn();
24-
router.put = jest.fn();
25-
router.delete = jest.fn();
26-
27-
describe('Cart Routes', () => {
28-
beforeEach(() => {
29-
jest.clearAllMocks();
30-
});
31-
32-
it('should call the appropriate methods when POST /create is called', async () => {
33-
router.post('/create', authMiddleware, cleanBody, cartController.createCart);
34-
35-
const req = { body: { products: [] }, user: { id: 'user_id' } };
36-
const res = { json: jest.fn() };
37-
const next = jest.fn();
38-
39-
await request(router).post('/create').send(req.body).set('Authorization', 'Bearer token');
40-
41-
expect(authMiddleware).toHaveBeenCalled();
42-
expect(cleanBody).toHaveBeenCalled();
43-
expect(cartController.createCart).toHaveBeenCalledWith(req, res, next);
44-
});
45-
46-
it('should call the appropriate methods when GET /getAll is called', async () => {
47-
router.get('/getAll', authMiddleware, adminCheck, cartController.getAll);
48-
49-
const req = { query: { page: '1', size: '10' } };
50-
const res = { json: jest.fn() };
51-
const next = jest.fn();
52-
53-
await request(router).get('/getAll').query(req.query).set('Authorization', 'Bearer token');
54-
55-
expect(authMiddleware).toHaveBeenCalled();
56-
expect(adminCheck).toHaveBeenCalled();
57-
expect(cartController.getAll).toHaveBeenCalledWith(req, res, next);
58-
});
59-
60-
it('should call the appropriate methods when GET /:userId is called', async () => {
61-
router.get('/:userId', authMiddleware, cartController.getUserCart);
62-
63-
const req = { params: { userId: 'user_id' } };
64-
const res = { json: jest.fn() };
65-
const next = jest.fn();
66-
67-
await request(router).get(`/${req.params.userId}`).set('Authorization', 'Bearer token');
68-
69-
expect(authMiddleware).toHaveBeenCalled();
70-
expect(cartController.getUserCart).toHaveBeenCalledWith(req, res, next);
71-
});
72-
73-
it('should call the appropriate methods when PUT /update/:cartId is called', async () => {
74-
router.put('/update/:cartId', authMiddleware, cleanBody, cartController.editCart);
75-
76-
const req = { body: {}, params: { cartId: 'cart_id' }, user: { id: 'user_id' } };
77-
const res = { json: jest.fn() };
78-
const next = jest.fn();
79-
80-
await request(router)
81-
.put(`/update/${req.params.cartId}`)
82-
.send(req.body)
83-
.set('Authorization', 'Bearer token');
84-
85-
expect(authMiddleware).toHaveBeenCalled();
86-
expect(cleanBody).toHaveBeenCalled();
87-
expect(cartController.editCart).toHaveBeenCalledWith(req, res, next);
88-
});
89-
90-
it('should call the appropriate methods when DELETE /delete/:cartId is called', async () => {
91-
router.delete('/delete/:cartId', authMiddleware, cartController.deleteCart);
92-
93-
const req ={ params: { cartId: 'cart_id' }, user: { id: 'user_id' } };
94-
const res = { json: jest.fn() };
95-
const next = jest.fn();
96-
97-
await request(router).delete(`/delete/${req.params.cartId}`).set('Authorization', 'Bearer token');
98-
99-
expect(authMiddleware).toHaveBeenCalled();
100-
expect(cartController.deleteCart).toHaveBeenCalledWith(req, res, next);
101-
});
102-
});

0 commit comments

Comments
 (0)