it('should report error "User does not exist" when user does not exists', () => { return request(app) .put('/v1/users/palmeiras1914') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NOT_FOUND) .then((res) => { expect(res.body.code).to.be.equal(404); expect(res.body.message).to.be.equal('User does not exist'); }); });
/** * Get user * * @param {ObjectId} id - The objectId of user. * @returns {Promise<User, APIError>} */ async get(id) { try { let user; if (mongoose.Types.ObjectId.isValid(id)) { user = await this.findById(id).exec(); } if (user) { return user; } throw new APIError({ message: 'User does not exist', status: httpStatus.NOT_FOUND, }); } catch (error) { throw error; } }
.delete('/v1/users/palmeiras1914') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NOT_FOUND) .then((res) => { expect(res.body.code).to.be.equal(404);
.get('/v1/users/56c787ccc67fc16ccc1a5e92') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NOT_FOUND) .then((res) => { expect(res.body.code).to.be.equal(404); .get('/v1/users/palmeiras1914') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NOT_FOUND) .then((res) => { expect(res.body.code).to.be.equal(404);
it('should report error "User does not exist" when user does not exists', () => { return request(app) .patch('/v1/users/palmeiras1914') .set('Authorization', `Bearer ${adminAccessToken}`) .expect(httpStatus.NOT_FOUND) .then((res) => { expect(res.body.code).to.be.equal(404); expect(res.body.message).to.be.equal('User does not exist'); }); });
getByUrl(firebaseUrl) { return this.findOne({ firebaseUrl }) .exec() .then((doc) => { debug('GET %O', doc); if (doc) { return doc; } const err = new APIError('No such file exists', httpStatus.NOT_FOUND, true); return Promise.reject(err); }); }
// catch 404 and forward to error handler app.use((req, res, next) => { const err = new APIError('API not found', httpStatus.NOT_FOUND); return next(err); });
async findUser (email) { const user = await this.findOne({ email }).exec() if(!user) throw new APIError(`No user associated with ${email}`, httpStatus.NOT_FOUND) return user; }
it('notFound middleware should generate not found error', () => { notFound(req, res); expect(statusStub).toBeCalledWith(httpStatus.NOT_FOUND); expect(jsonStub).toHaveBeenCalledTimes(1); expect(endStub).toHaveBeenCalledTimes(1); });
server.on('VersionNotAllowed', (req, res) => { res.send( httpStatusCodes.NOT_FOUND, new Error('Unsupported API version requested', 'INVALID_VERSION') ); });
it('should generate not found error', () => { const sut = APIError.notFound(); expect(sut).toHaveProperty('message'); expect(sut).toHaveProperty('errors'); expect(sut).toHaveProperty('route'); expect(sut).toHaveProperty('stack'); expect(sut).toHaveProperty('status'); expect(sut).toHaveProperty('isPublic'); expect(sut.message).toEqual(expect.any(String)); expect(sut.route).toEqual(expect.any(String)); expect(sut.status).toEqual(httpStatus.NOT_FOUND); expect(sut.isPublic).toEqual(expect.any(Boolean)); });
it('should return 404 ( no user with such email )', (done) => { reqs.businessUser .resetPasswordInit('some.other@mail.com') .then((res) => { expect(res.status) .to .be .eq(httpStatus.NOT_FOUND); done(); }) .catch(done); });
async findAndGenerateToken (payload) { const { email, password } = payload if (!email) throw new APIError('Email must be provided for login') const user = await this.findOne({ email }).exec() if (!user) throw new APIError(`No user associated with ${email}`, httpStatus.NOT_FOUND) const passwordOK = await user.passwordMatches(password) if (!passwordOK) throw new APIError(`Password mismatch`, httpStatus.UNAUTHORIZED) return user }
app.use((req, res, next) => { res.status(httpStatus.NOT_FOUND).send('Page not found!'); next(); });
server.on('InvalidVersion', (req, res) => { res.send( httpStatusCodes.NOT_FOUND, new Error('Unsupported API version requested', 'INVALID_VERSION') ); });