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 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'); }); });
it('should report error when logged user is not the same as the requested one', async () => { const id = (await User.findOne({ email: dbUsers.branStark.email }))._id; return request(app) .patch(`/v1/users/${id}`) .set('Authorization', `Bearer ${userAccessToken}`) .expect(httpStatus.FORBIDDEN) .then((res) => { expect(res.body.code).to.be.equal(httpStatus.FORBIDDEN); expect(res.body.message).to.be.equal('Forbidden'); }); });
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 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); }); });
test("Should not update invalid user fields", async () => { await request(app) .patch("/users/me") .set("Authorization", `Bearer ${userOne.tokens[0].token}`) .send({ location: "Some Location" }) .expect(400); });
it("should set the body to undefined if optional and unspecified", (done) => { let api = _.cloneDeep(spec.samples.petStore); api.paths["/pets/{PetName}"].patch.parameters[0].required = false; swagger(api, (err, middleware) => { let express = helper.express(middleware.metadata(), middleware.parseRequest()); helper.supertest(express) .patch("/api/pets/fido") .end(helper.checkSpyResults(done)); express.patch("/api/pets/fido", helper.spy((req, res, next) => { expect(req.body).to.equal(undefined); })); }); });
describe("PATCH /things/:name", () => { test("Update a thing's name", async () => { let response = await request(app) .patch("/things/Example") .send({ name: "New Name" }); expect(response.body).toEqual({ ...exampleThing, name: "New Name" }); }); });
it("should throw an error if the body param is required and unspecified", (done) => { swagger(spec.samples.petStore, (err, middleware) => { let express = helper.express(middleware.metadata(), middleware.parseRequest()); helper.supertest(express) .patch("/api/pets/fido") .end(helper.checkSpyResults(done)); express.use("/api/pets/fido", helper.spy((err, req, res, next) => { expect(err).to.be.an.instanceOf(Error); expect(err.message).to.contain('Missing required body parameter "PetData"'); })); }); });
it("should throw an error if the body param is required and unspecified, even if there's a default value", (done) => { let api = _.cloneDeep(spec.samples.petStore); api.paths["/pets/{PetName}"].patch.parameters[0].schema.default = '{"name": "Fluffy", "type": "cat"}'; swagger(api, (err, middleware) => { let express = helper.express(middleware.metadata(), middleware.parseRequest()); helper.supertest(express) .patch("/api/pets/fido") .end(helper.checkSpyResults(done)); express.use("/api/pets/fido", helper.spy((err, req, res, next) => { expect(err).to.be.an.instanceOf(Error); expect(err.message).to.contain('Missing required body parameter "PetData"'); })); }); });
it("should not respond to PATCH requests", (done) => { swagger(spec.files.externalRefs, (err, middleware) => { let express = helper.express(middleware.files()); helper.supertest(express) .patch("/api-docs/external-refs.yaml") .expect(404) .end(done); }); });
it("should not respond to PATCH requests", (done) => { swagger(spec.files.petStore, (err, middleware) => { let express = helper.express(middleware.files()); helper.supertest(express) .patch("/api-docs") .expect(404) .end(done); }); });
it('should report error when logged customer is not the same as the requested one', async () => { const id = (await Customer.findOne({ email: dbCustomers.masterAccount.email }))._id; return request(app) .patch(`/v1/customers/${id}`) .set('Authorization', `Bearer ${customerAccessToken}`) .expect(httpStatus.FORBIDDEN) .then((res) => { expect(res.body.code).to.be.equal(httpStatus.FORBIDDEN); expect(res.body.message).to.be.equal('Forbidden'); }); });
test ('Should throw error when trying to update an invalid user field', async () => { await request(app) .patch('/users/me') .set('Authorization', `Bearer ${userOne.tokens[0].token}`) .send({ location: 'Knowhere'}) .expect(400); });
it('should report error "Customer does not exist" when customer does not exists', () => { return request(app) .patch('/v1/customers/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('Customer does not exist'); }); });