From 37a9bbd63d3d856bebd3f82d1096fbedf1f09788 Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Sun, 12 Sep 2021 16:26:09 +0000 Subject: [PATCH] Use random usernames to unittest Don't replace the database with a unittest database, just use the usual database and user randomly generated usernames so we don't have collisions. --- spec/login_spec.lua | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/spec/login_spec.lua b/spec/login_spec.lua index 1b31c11..ab7479b 100644 --- a/spec/login_spec.lua +++ b/spec/login_spec.lua @@ -1,6 +1,7 @@ _G.spy = spy local mock_env = require("spec.env_mock") +local rng = require("spec.fuzzgen") describe("smr login",function() setup(mock_env.setup) @@ -52,15 +53,14 @@ describe("smr login",function() ) end) it("should give a session cookie when logging in with a user",function() - pending("Look at cleaning mock env") - mock_env.mockdb() local claim_post = require("endpoints.claim_post") local login_post = require("endpoints.login_post") local config = require("config") local db = require("db") + local session = require("session") configure() - local username = "nuser" + local username = rng.subdomain() local claim_req = { method = "POST", host = "test.host", @@ -81,7 +81,9 @@ describe("smr login",function() pass = claim_req.response } } + sessionspy = spy.on(session,"start") login_post(login_req) + assert.spy(sessionspy).was.called() local code = login_req.responsecode assert( code >= 300 and code <= 400, @@ -123,8 +125,6 @@ describe("smr login",function() ) end) it("should allow logged in users the option of posting under their username",function() - pending("Fix up cleaning db for unit tests") - mock_env.mockdb() local claim_post = require("endpoints.claim_post") local login_post = require("endpoints.login_post") local paste_get = require("endpoints.paste_get") @@ -134,12 +134,13 @@ describe("smr login",function() local config = require("config") config.domain = "test.host" configure() + local username = rng.subdomain() local claim_req = { method = "POST", host = "test.host", path = "/_claim", args = { - user = "user" + user = username } } claim_post(claim_req) @@ -148,7 +149,7 @@ describe("smr login",function() host = "test.host", path = "/_login", args = { - user = "user" + user = username }, file = { pass = claim_req.response @@ -159,14 +160,14 @@ describe("smr login",function() local sessionid = cookie:match("session=([^;]+)") local paste_req_get = { method = "GET", - host = "user.test.host", + host = username .. ".test.host", path = "/_paste", cookies = { session = sessionid } } paste_get(paste_req_get) - local option = '' + local option = '' assert( paste_req_get.response:find(option), "After logging in the user should have an option to ".. @@ -175,7 +176,7 @@ describe("smr login",function() ) local paste_req_post = { method = "POST", - host = "user.test.host", + host = username .. ".test.host", path = "/_paste", cookies = { session = sessionid @@ -198,7 +199,7 @@ describe("smr login",function() local redirect = paste_req_post.response_headers.Location:match("(/[^/]*)$") local read_req_get = { method = "GET", - host = "user.test.host", + host = username .. ".test.host", path = redirect, cookies = { session = sessionid @@ -212,7 +213,7 @@ describe("smr login",function() "Failed to find post title in response." ) assert( - response:find([[By user]]), + response:find('By ' .. username .. ''), "Failed to find the author name after a paste." ) assert(