it('should return a new accessToken when refreshToken and email match', async () => { await RefreshToken.create(refreshToken); return request(app) .post('/v1/auth/refresh-token') .send({ email: dbUser.email, refreshToken: refreshToken.token }) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.have.a.property('accessToken'); expect(res.body).to.have.a.property('refreshToken'); expect(res.body).to.have.a.property('expiresIn'); }); });
it('should not update the role of the user (not admin)', async () => { const id = (await User.findOne({ email: dbUsers.jonSnow.email }))._id; const role = 'admin'; return request(app) .patch(`/v1/users/${id}`) .set('Authorization', `Bearer ${userAccessToken}`) .send({ role }) .expect(httpStatus.OK) .then((res) => { expect(res.body.role).to.not.be.equal(role); }); });
it('should not update user when no parameters were given', async () => { delete dbUsers.branStark.password; const id = (await User.findOne(dbUsers.branStark))._id; return request(app) .patch(`/v1/users/${id}`) .set('Authorization', `Bearer ${adminAccessToken}`) .send() .expect(httpStatus.OK) .then((res) => { expect(res.body).to.include(dbUsers.branStark); }); });
it('should get all users with pagination', () => { return request(app) .get('/v1/users') .set('Authorization', `Bearer ${adminAccessToken}`) .query({ page: 2, perPage: 1 }) .expect(httpStatus.OK) .then(async (res) => { delete dbUsers.jonSnow.password; expect(res.body).to.be.an('array'); expect(res.body[0]).to.be.an('object'); expect(res.body).to.have.lengthOf(1); expect(res.body[0].name).to.be.equal('Jon Snow'); }); });
.get('/v1/users/profile') .set('Authorization', `Bearer ${userAccessToken}`) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.include(dbUsers.jonSnow);
it('should get all users', () => { return request(app) .get('/v1/users') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.OK) .then(async (res) => { const bran = await format(dbUsers.branStark); const john = await format(dbUsers.jonSnow); // before comparing it is necessary to convert String to Date res.body[0].createdAt = new Date(res.body[0].createdAt); res.body[1].createdAt = new Date(res.body[1].createdAt); const includesBranStark = some(res.body, bran); const includesjonSnow = some(res.body, john); expect(res.body).to.be.an('array'); expect(res.body).to.have.lengthOf(2); expect(includesBranStark).to.be.true; expect(includesjonSnow).to.be.true; }); });
it('should update password and send confirmation email when email and reset token are valid', async () => { await PasswordResetToken.create(resetToken); sandbox .stub(emailProvider, 'sendPasswordChangeEmail') .callsFake(() => Promise.resolve('email sent')); return request(app) .post('/v1/auth/reset-password') .send({ email: dbUser.email, password: 'updatedPassword2', resetToken: resetToken.resetToken, }) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.be.equal('Password Updated'); }); });
.post('/v1/auth/send-password-reset') .send({ email: dbUser.email }) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.be.equal('success');
.get(`/v1/users/${id}`) .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.OK) .then((res) => { expect(res.body).to.include(dbUsers.branStark);
.post('/v1/auth/facebook') .send({ access_token: '123' }) .expect(httpStatus.OK) .then((res) => { expect(res.body.token).to.have.a.property('accessToken'); .post('/v1/auth/facebook') .send({ access_token: '123' }) .expect(httpStatus.OK) .then((res) => { expect(res.body.token).to.have.a.property('accessToken');
it('should filter users', () => { return request(app) .get('/v1/users') .set('Authorization', `Bearer ${adminAccessToken}`) .query({ email: dbUsers.jonSnow.email }) .expect(httpStatus.OK) .then(async (res) => { delete dbUsers.jonSnow.password; const john = await format(dbUsers.jonSnow); // before comparing it is necessary to convert String to Date res.body[0].createdAt = new Date(res.body[0].createdAt); const includesjonSnow = some(res.body, john); expect(res.body).to.be.an('array'); expect(res.body).to.have.lengthOf(1); expect(includesjonSnow).to.be.true; }); });
it('should replace user', async () => { delete dbUsers.branStark.password; const id = (await User.findOne(dbUsers.branStark))._id; return request(app) .put(`/v1/users/${id}`) .set('Authorization', `Bearer ${adminAccessToken}`) .send(user) .expect(httpStatus.OK) .then((res) => { delete user.password; expect(res.body).to.include(user); expect(res.body.role).to.be.equal('user'); }); });
it('should update user', async () => { delete dbUsers.branStark.password; const id = (await User.findOne(dbUsers.branStark))._id; const { name } = user; return request(app) .patch(`/v1/users/${id}`) .set('Authorization', `Bearer ${adminAccessToken}`) .send({ name }) .expect(httpStatus.OK) .then((res) => { expect(res.body.name).to.be.equal(name); expect(res.body.email).to.be.equal(dbUsers.branStark.email); }); });
it('should not replace the role of the user (not admin)', async () => { const id = (await User.findOne({ email: dbUsers.jonSnow.email }))._id; const role = 'admin'; return request(app) .put(`/v1/users/${id}`) .set('Authorization', `Bearer ${userAccessToken}`) .send(admin) .expect(httpStatus.OK) .then((res) => { expect(res.body.role).to.not.be.equal(role); }); });
.post('/v1/auth/google') .send({ access_token: '123' }) .expect(httpStatus.OK) .then((res) => { expect(res.body.token).to.have.a.property('accessToken'); .post('/v1/auth/google') .send({ access_token: '123' }) .expect(httpStatus.OK) .then((res) => { expect(res.body.token).to.have.a.property('accessToken');