fixing broken tests

This commit is contained in:
Michael Shanks 2020-06-18 20:41:37 +01:00
parent bf14aa5ccb
commit 2436ea525d
10 changed files with 51 additions and 41 deletions

View File

@ -6,7 +6,7 @@ const { BUILDER, READ_MODEL } = require("../../utilities/accessLevels")
const router = Router() const router = Router()
router router
.get("/api/models", modelController.fetch) .get("/api/models", authorized(BUILDER), modelController.fetch)
.get( .get(
"/api/models/:id", "/api/models/:id",
authorized(READ_MODEL, ctx => ctx.params.id), authorized(READ_MODEL, ctx => ctx.params.id),

View File

@ -44,7 +44,7 @@ describe("/accesslevels", () => {
it("returns a success message when level is successfully created", async () => { it("returns a success message when level is successfully created", async () => {
const res = await request const res = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user" }) .send({ name: "user" })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -62,7 +62,7 @@ describe("/accesslevels", () => {
it("should list custom levels, plus 2 default levels", async () => { it("should list custom levels, plus 2 default levels", async () => {
const createRes = await request const createRes = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL }] }) .send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL }] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -71,7 +71,7 @@ describe("/accesslevels", () => {
const customLevel = createRes.body const customLevel = createRes.body
const res = await request const res = await request
.get(`/api/${instanceId}/accesslevels`) .get(`/api/accesslevels`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -95,7 +95,7 @@ describe("/accesslevels", () => {
describe("destroy", () => { describe("destroy", () => {
it("should delete custom access level", async () => { it("should delete custom access level", async () => {
const createRes = await request const createRes = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL } ] }) .send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL } ] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -104,12 +104,12 @@ describe("/accesslevels", () => {
const customLevel = createRes.body const customLevel = createRes.body
await request await request
.delete(`/api/${instanceId}/accesslevels/${customLevel._id}/${customLevel._rev}`) .delete(`/api/accesslevels/${customLevel._id}/${customLevel._rev}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect(200) .expect(200)
await request await request
.get(`/api/${instanceId}/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect(404) .expect(404)
}) })
@ -118,7 +118,7 @@ describe("/accesslevels", () => {
describe("patch", () => { describe("patch", () => {
it("should add given permissions", async () => { it("should add given permissions", async () => {
const createRes = await request const createRes = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL }] }) .send({ name: "user", permissions: [ { itemId: model._id, name: READ_MODEL }] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -127,7 +127,7 @@ describe("/accesslevels", () => {
const customLevel = createRes.body const customLevel = createRes.body
await request await request
.patch(`/api/${instanceId}/accesslevels/${customLevel._id}`) .patch(`/api/accesslevels/${customLevel._id}`)
.send({ .send({
_rev: customLevel._rev, _rev: customLevel._rev,
addedPermissions: [ { itemId: model._id, name: WRITE_MODEL } ] addedPermissions: [ { itemId: model._id, name: WRITE_MODEL } ]
@ -137,7 +137,7 @@ describe("/accesslevels", () => {
.expect(200) .expect(200)
const finalRes = await request const finalRes = await request
.get(`/api/${instanceId}/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect(200) .expect(200)
@ -148,7 +148,7 @@ describe("/accesslevels", () => {
it("should remove given permissions", async () => { it("should remove given permissions", async () => {
const createRes = await request const createRes = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ .send({
name: "user", name: "user",
permissions: [ permissions: [
@ -163,7 +163,7 @@ describe("/accesslevels", () => {
const customLevel = createRes.body const customLevel = createRes.body
await request await request
.patch(`/api/${instanceId}/accesslevels/${customLevel._id}`) .patch(`/api/accesslevels/${customLevel._id}`)
.send({ .send({
_rev: customLevel._rev, _rev: customLevel._rev,
removedPermissions: [ { itemId: model._id, name: WRITE_MODEL }] removedPermissions: [ { itemId: model._id, name: WRITE_MODEL }]
@ -173,7 +173,7 @@ describe("/accesslevels", () => {
.expect(200) .expect(200)
const finalRes = await request const finalRes = await request
.get(`/api/${instanceId}/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId, instanceId))
.expect(200) .expect(200)

View File

@ -41,7 +41,7 @@ describe("/applications", () => {
expect(res.body._id).toBeDefined() expect(res.body._id).toBeDefined()
}) })
fit("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request) const otherApplication = await createApplication(request)
const instance = await createInstance(request, otherApplication._id) const instance = await createInstance(request, otherApplication._id)
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({

View File

@ -57,7 +57,7 @@ exports.createModel = async (request, appId, instanceId, model) => {
} }
const res = await request const res = await request
.post(`/api/${instanceId}/models`) .post(`/api/models`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId, instanceId))
.send(model) .send(model)
return res.body return res.body
@ -69,7 +69,7 @@ exports.createView = async (request, appId, instanceId, view) => {
} }
const res = await request const res = await request
.post(`/api/${instanceId}/views`) .post(`/api/views`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId, instanceId))
.send(view) .send(view)
return res.body return res.body
@ -80,10 +80,10 @@ exports.createClientDatabase = async id => await create(id || TEST_CLIENT_ID)
exports.createApplication = async (request, name = "test_application") => { exports.createApplication = async (request, name = "test_application") => {
const res = await request const res = await request
.post("/api/applications") .post("/api/applications")
.set(exports.defaultHeaders())
.send({ .send({
name, name,
}) })
.set(exports.defaultHeaders())
return res.body return res.body
} }
@ -92,10 +92,10 @@ exports.destroyClientDatabase = async () => await destroy(TEST_CLIENT_ID)
exports.createInstance = async (request, appId) => { exports.createInstance = async (request, appId) => {
const res = await request const res = await request
.post(`/api/instances`) .post(`/api/instances`)
.set(exports.defaultHeaders(appId))
.send({ .send({
name: "test-instance", name: "test-instance2",
}) })
.set(exports.defaultHeaders(appId))
return res.body return res.body
} }
@ -180,13 +180,13 @@ const createUserWithPermissions = async (
username username
) => { ) => {
const accessRes = await request const accessRes = await request
.post(`/api/${instanceId}/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "TestLevel", permissions }) .send({ name: "TestLevel", permissions })
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId, instanceId))
const password = `password_${username}` const password = `password_${username}`
await request await request
.post(`/api/${instanceId}/users`) .post(`/api/users`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId, instanceId))
.send({ .send({
name: username, name: username,

View File

@ -33,7 +33,7 @@ describe("/models", () => {
it("returns a success message when the model is successfully created", done => { it("returns a success message when the model is successfully created", done => {
request request
.post(`/api/${instance._id}/models`) .post(`/api/models`)
.send({ .send({
name: "TestModel", name: "TestModel",
key: "name", key: "name",
@ -55,8 +55,9 @@ describe("/models", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "POST", method: "POST",
url: `/api/${instance._id}/models`, url: `/api/models`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
body: { body: {
name: "TestModel", name: "TestModel",
key: "name", key: "name",
@ -78,7 +79,7 @@ describe("/models", () => {
it("returns all the models for that instance in the response body", done => { it("returns all the models for that instance in the response body", done => {
request request
.get(`/api/${instance._id}/models`) .get(`/api/models`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -94,8 +95,9 @@ describe("/models", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "GET", method: "GET",
url: `/api/${instance._id}/models`, url: `/api/models`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
}) })
}) })
}); });
@ -114,7 +116,7 @@ describe("/models", () => {
it("returns a success response when a model is deleted.", async done => { it("returns a success response when a model is deleted.", async done => {
request request
.delete(`/api/${instance._id}/models/${testModel._id}/${testModel._rev}`) .delete(`/api/models/${testModel._id}/${testModel._rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -147,7 +149,7 @@ describe("/models", () => {
}) })
request request
.delete(`/api/${instance._id}/models/${testModel._id}/${testModel._rev}`) .delete(`/api/models/${testModel._id}/${testModel._rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -163,8 +165,9 @@ describe("/models", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "DELETE", method: "DELETE",
url: `/api/${instance._id}/models/${testModel._id}/${testModel._rev}`, url: `/api/models/${testModel._id}/${testModel._rev}`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
}) })
}) })

View File

@ -45,7 +45,7 @@ describe("/records", () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
fit("returns a success message when the record is created", async () => { it("returns a success message when the record is created", async () => {
const res = await createRecord() const res = await createRecord()
expect(res.res.statusMessage).toEqual(`${model.name} created successfully`) expect(res.res.statusMessage).toEqual(`${model.name} created successfully`)
expect(res.body.name).toEqual("Test Contact") expect(res.body.name).toEqual("Test Contact")

View File

@ -54,8 +54,9 @@ describe("/users", () => {
await testPermissionsForEndpoint({ await testPermissionsForEndpoint({
request, request,
method: "GET", method: "GET",
url: `/api/${instance._id}/users`, url: `/api/users`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
permissionName: LIST_USERS, permissionName: LIST_USERS,
}) })
}) })
@ -66,7 +67,7 @@ describe("/users", () => {
it("returns a success message when a user is successfully created", async () => { it("returns a success message when a user is successfully created", async () => {
const res = await request const res = await request
.post(`/api/${instance._id}/users`) .post(`/api/users`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID }) .send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID })
.expect(200) .expect(200)
@ -81,8 +82,9 @@ describe("/users", () => {
request, request,
method: "POST", method: "POST",
body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID }, body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID },
url: `/api/${instance._id}/users`, url: `/api/users`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
permissionName: USER_MANAGEMENT, permissionName: USER_MANAGEMENT,
}) })
}) })

View File

@ -30,7 +30,7 @@ describe("/views", () => {
const createView = async () => const createView = async () =>
await request await request
.post(`/api/${instance._id}/views`) .post(`/api/views`)
.send({ .send({
name: "TestView", name: "TestView",
map: `function(doc) { map: `function(doc) {
@ -62,7 +62,7 @@ describe("/views", () => {
it("should only return custom views", async () => { it("should only return custom views", async () => {
const view = await createView() const view = await createView()
const res = await request const res = await request
.get(`/api/${instance._id}/views`) .get(`/api/views`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)

View File

@ -63,7 +63,7 @@ describe("/workflows", () => {
describe("create", () => { describe("create", () => {
it("returns a success message when the workflow is successfully created", async () => { it("returns a success message when the workflow is successfully created", async () => {
const res = await request const res = await request
.post(`/api/${instance._id}/workflows`) .post(`/api/workflows`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.send(TEST_WORKFLOW) .send(TEST_WORKFLOW)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -77,8 +77,9 @@ describe("/workflows", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "POST", method: "POST",
url: `/api/${instance._id}/workflows`, url: `/api/workflows`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
body: TEST_WORKFLOW body: TEST_WORKFLOW
}) })
}) })
@ -92,7 +93,7 @@ describe("/workflows", () => {
workflow.name = "Updated Name"; workflow.name = "Updated Name";
const res = await request const res = await request
.put(`/api/${instance._id}/workflows`) .put(`/api/workflows`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.send(workflow) .send(workflow)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -107,7 +108,7 @@ describe("/workflows", () => {
it("return all the workflows for an instance", async () => { it("return all the workflows for an instance", async () => {
await createWorkflow(); await createWorkflow();
const res = await request const res = await request
.get(`/api/${instance._id}/workflows`) .get(`/api/workflows`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -119,8 +120,9 @@ describe("/workflows", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "GET", method: "GET",
url: `/api/${instance._id}/workflows`, url: `/api/workflows`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
}) })
}) })
}) })
@ -129,7 +131,7 @@ describe("/workflows", () => {
it("deletes a workflow by its ID", async () => { it("deletes a workflow by its ID", async () => {
await createWorkflow(); await createWorkflow();
const res = await request const res = await request
.delete(`/api/${instance._id}/workflows/${workflow.id}/${workflow.rev}`) .delete(`/api/workflows/${workflow.id}/${workflow.rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(app._id, instance._id))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -142,8 +144,9 @@ describe("/workflows", () => {
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "DELETE", method: "DELETE",
url: `/api/${instance._id}/workflows/${workflow.id}/${workflow._rev}`, url: `/api/workflows/${workflow.id}/${workflow._rev}`,
instanceId: instance._id, instanceId: instance._id,
appId: app._id,
}) })
}) })
}) })

View File

@ -81,6 +81,8 @@ const getAccessLevel = async (instanceId, accessLevelId) => {
const findAccessContext = { const findAccessContext = {
params: { params: {
levelId: accessLevelId, levelId: accessLevelId,
},
user: {
instanceId, instanceId,
}, },
} }