From 16054156a1168779a97181f7a1363cd257a8078b Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Thu, 21 Oct 2021 23:44:23 +0000 Subject: [PATCH] Finished writing cacheing tests Cacheing tests now make sure that logged in users don't cause pages to cache. Also fixed creating a logged in session automatically in the mock environment. --- spec/cacheing_spec.lua | 50 ++++++++++++++++++++++++++++++++++++++++-- spec/env_mock.lua | 48 +++++++++++++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/spec/cacheing_spec.lua b/spec/cacheing_spec.lua index 979fb56..c4fbc6f 100644 --- a/spec/cacheing_spec.lua +++ b/spec/cacheing_spec.lua @@ -34,7 +34,23 @@ describe("smr cacheing",function() local cache = require("cache") renderspy = spy.on(cache,"render") configure() - pending("TODO: complete") + for row in cache.cache:rows("SELECT COUNT(*) FROM cache") do + assert(row[1] == 0, string.format( + "Cache should not have any rows before " .. + "request have been made." + )) + end + local req = mock_env.session() + req.method = "GET" + req.path = "/a" + req.args = {} + read_get(req) + for row in cache.cache:rows("SELECT COUNT(*) FROM cache") do + assert(row[1] == 0, string.format( + "Cache should not cache requests made by " .. + "logged in users." + )) + end end) it("caches one page for domain/id and author.domain/id",function() local read_get = require("endpoints.read_get") @@ -68,7 +84,37 @@ describe("smr cacheing",function() end) describe("author home page",function() it("lists all stories by that author",function() - pending("TODO") + local read_get = require("endpoints.index_get") + local cache = require("cache") + configure() + local req_m = {__index = { + method = "GET", + path = "/a", + args = {} + }} + local base_host = {host="user.test.host"} + for row in cache.cache:rows("SELECT COUNT(*) FROM cache") do + assert(row[1] == 0, string.format( + "Before requesting user homepage " .. + "there should not be any pages in the " .. + "cache." + )) + end + local base_req = setmetatable({host="user.test.host"},req_m) + read_get(base_req) + for row in cache.cache:rows("SELECT COUNT(*) FROM cache") do + assert(row[1] == 1, string.format( + "After reading the autor home page, " .. + " only that page should be cached." + )) + end + read_get(base_req) + for row in cache.cache:rows("SELECT COUNT(*) FROM cache") do + assert(row[1] == 1, string.format( + "After reading the autor home page " .. + " twice only that page should be cached." + )) + end end) end) end) diff --git a/spec/env_mock.lua b/spec/env_mock.lua index 7bb6f87..c4d0873 100644 --- a/spec/env_mock.lua +++ b/spec/env_mock.lua @@ -7,6 +7,11 @@ mock.env = env --Mirror print prior to lua 5.4 --local oldprint = print local ntostring + +-- Modules that get required lazily +local login_post +local fuzzy +local claim_post print_table= function(...) print("Print called") local args = {...} @@ -190,7 +195,7 @@ local session_m = {__index = { print("After requireing login_post edpoint, self.args is " .. tostring(self.args)) self.args.user = who self.args.pass = pass - post_login(self) + login_post(self) error("TODO") end, logout = function(self) @@ -202,16 +207,43 @@ local session_m = {__index = { }} function mock.session(tbl) - if not post_login_required then - post_login = require("endpoints.login_post") - post_login_required = true + if post_login == nil then + login_post = require("endpoints.login_post") + fuzzy = require("spec.fuzzgen") + claim_post = require("endpoints.claim_post") + configure() end - tbl = tbl or {} - tbl.args = tbl.args or {} - local req = tbl or { + local username = fuzzy.subdomain() + local claim_req = { + method = "POST", host = "test.host", + path = "/_claim", + args = { + user = username + } } - return setmetatable(req,session_m) + claim_post(claim_req) + local login_req = { + method = "POST", + host = "test.host", + path = "/_login", + args = { + user = username + }, + file = { + pass = claim_req.response + } + } + login_post(login_req) + local cookie = login_req.response_headers["set-cookie"] + local sessionid = cookie:match("session=([^;]+)") + local req = { + host = "test.host", + cookies = { + session = sessionid + } + } + return req, username end return mock