From ff98b4bf4d016a6713dde4aed7830d45e865e98a Mon Sep 17 00:00:00 2001 From: michael shanks Date: Fri, 14 Jun 2019 23:03:01 +0100 Subject: [PATCH] more session testing --- packages/server/middleware/routers.js | 3 +- packages/server/tests/authenticate.js | 56 ++++++++++++++++----------- packages/server/tests/testApp.js | 5 +++ 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/packages/server/middleware/routers.js b/packages/server/middleware/routers.js index 949ea3a48..1de9d68be 100644 --- a/packages/server/middleware/routers.js +++ b/packages/server/middleware/routers.js @@ -69,7 +69,8 @@ module.exports = (config, app) => { }) .get("/:appname/api/users", async (ctx) => { - + ctx.body = await ctx.instance.authApi.getUsers(); + ctx.response.status = StatusCodes.OK; }) .get("/:appname/api/accessLevels", async (ctx) => { diff --git a/packages/server/tests/authenticate.js b/packages/server/tests/authenticate.js index 66a37853b..50c042cb3 100644 --- a/packages/server/tests/authenticate.js +++ b/packages/server/tests/authenticate.js @@ -2,15 +2,6 @@ const statusCodes = require("../utilities/statusCodes"); module.exports = (app) => { - it("should return ok correct username and password supplied", async () => { - - await app.post("/_master/api/authenticate", { - username: app.masterAuth.username, - password: app.masterAuth.password - }) - .expect(statusCodes.OK); - }); - it("should return unauthorized if username is incorrect", async () => { await app.post("/_master/api/authenticate", { username: "unknownuser", @@ -37,34 +28,55 @@ module.exports = (app) => { }); - it("should be able to create new user with authenticated cookie", async () => { + let ownerCookie; + it("should return ok correct username and password supplied", async () => { + const response = await app.post("/_master/api/authenticate", { username: app.masterAuth.username, password: app.masterAuth.password - }); - - const cookie = response.header['set-cookie']; - + }) + .expect(statusCodes.OK); + + ownerCookie = response.header['set-cookie']; + }); + + const testUserName = "test_user"; + const testPassword = "test_user_password"; + it("should be able to create new user with authenticated cookie", async () => { + await app.post("/_master/api/createUser", { user: { - name: "test_user", + name: testUserName, accessLevels:["owner"], enabled:true }, - password: "test_password" + password: testPassword }) - .set("cookie", cookie) + .set("cookie", ownerCookie) .expect(statusCodes.OK); + + }); + + let newUserCookie; + it("should be able to authenticate with new user", async () => { + const responseNewUser = await app.post("/_master/api/authenticate", { - username: "test_user", - password: "test_password" - }); + username: testUserName, + password: testPassword + }) + .expect(statusCodes.OK); - const newUserCookie = responseNewUser.header['set-cookie']; + newUserCookie = responseNewUser.header['set-cookie']; expect(newUserCookie).toBeDefined(); - expect(newUserCookie).not.toEqual(cookie); + expect(newUserCookie).not.toEqual(ownerCookie); + + app.get("/_master/api/users/") + .set("cookie", newUserCookie) + .expect(statusCodes.OK); }); + + }; diff --git a/packages/server/tests/testApp.js b/packages/server/tests/testApp.js index 10f3d2cf3..5c7d01ce2 100644 --- a/packages/server/tests/testApp.js +++ b/packages/server/tests/testApp.js @@ -33,6 +33,7 @@ module.exports = () => { config, server:() => server, post: (url, body) => postRequest(server,url,body), + get: (url) => getRequest(server, url), masterAuth: { username: masterOwnerName, password: masterOwnerPassword @@ -47,6 +48,10 @@ const postRequest = (server, url, body) => .send(body) .set('Accept', 'application/json'); +const getRequest = (server, url) => + request(server) + .get(url) + .set('Accept', 'application/json'); const reInitialize = async () => { try {