diff --git a/spec/author_bio_spec.lua b/spec/author_bio_spec.lua new file mode 100644 index 0000000..edb67d7 --- /dev/null +++ b/spec/author_bio_spec.lua @@ -0,0 +1,118 @@ +_G.spy = spy +local mock_env = require("spec.env_mock") +local rng = require("spec.fuzzgen") + +describe("smr biography",function() + setup(mock_env.setup) + teardown(mock_env.teardown) + it("should allow users to set their biography",function() + local claim_post = require("endpoints.claim_post") + local login_post = require("endpoints.login_post") + local index_get = require("endpoints.index_get") + local bio_get = require("endpoints.bio_get") + local bio_post = require("endpoints.bio_post") + local db = require("db") + 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 = username + } + } + 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 home_req_get = { + method = "GET", + host = username .. ".test.host", + path = "/", + cookies = { + session = sessionid + } + } + index_get(home_req_get) + local edit_bio_button = '= 300 and code <= 400, "Should receive a redirect after posting, got:" .. tostring(code)) + assert(paste_req_post.response_headers, "Should have received some response headers") + assert(paste_req_post.response_headers.Location, "Should have received a location in response headers") + local redirect = paste_req_post.response_headers.Location:match("(/[^/]*)$") + local read_req_get = { + method = "GET", + host = username .. ".test.host", + path = redirect, + cookies = { + session = sessionid + }, + args = {} + } + read_get(read_req_get) + local response = read_req_get.response + assert( + response:find([[post title]]), + "Failed to find post title in response." + ) + assert( + response:find('By ' .. username .. ''), + "Failed to find the author name after a paste." + ) + assert( + response:find([[post text]]), + "Failed to find post text in response." + ) + ]=] + end) +end) diff --git a/src/lua/endpoints/bio_get.lua b/src/lua/endpoints/bio_get.lua index 16bc9dc..3a4c845 100644 --- a/src/lua/endpoints/bio_get.lua +++ b/src/lua/endpoints/bio_get.lua @@ -12,7 +12,7 @@ local config = require("config") local stmnt_bio local oldconfigure = configure function configure(...) - stmnt_bio = assert(db.conn:prepare(queries.select_bio)) + stmnt_bio = assert(db.conn:prepare(queries.select_author_bio)) return oldconfigure(...) end @@ -37,7 +37,7 @@ local function bio_edit_get(req) stmnt_bio:bind_names{ authorid = authorid } - local err = util.do_sql(stmnt_edit) + local err = util.do_sql(stmnt_bio) if err == sql.DONE then --No rows, we're logged in but an author with our id doesn't --exist? Something has gone wrong. @@ -66,4 +66,4 @@ found, please report this error. http_response(req,200,ret) end -return edit_get +return bio_edit_get diff --git a/src/lua/init.lua b/src/lua/init.lua index 78de6fb..28e5380 100644 --- a/src/lua/init.lua +++ b/src/lua/init.lua @@ -11,41 +11,17 @@ local et = require("etlua") local sql = require("lsqlite3") local zlib = require("zlib") ---stub for detouring +--stubs for detouring function configure(...) end --smr code +require("global") local cache = require("cache") local pages = require("pages") local util = require("util") local config = require("config") local db = require("db") ---Pages -local endpoint_names = { - read = {"get","post"}, - preview = {"post"}, - index = {"get"}, - paste = {"get","post"}, - download = {"get"}, - login = {"get","post"}, - logout = {"get"}, - edit = {"get","post"}, - claim = {"get","post"}, - search = {"get"}, - archive = {"get"}, - api = {"get"}, - delete = {"post"}, - bio = {"get","post"}, -} -local endpoints = {} -for name, methods in pairs(endpoint_names) do - for _,method in pairs(methods) do - local epn = string.format("%s_%s",name,method) - endpoints[epn] = require("endpoints." .. epn) - end -end - print("Hello from init.lua") local oldconfigure = configure function configure(...) @@ -59,65 +35,65 @@ function configure(...) end print("Created configure function") +-- TODO: Fill this out local http_methods = {"GET","POST"} local http_m_rev = {} +for k,v in pairs(http_methods) do + http_m_rev[v] = true +end +--Endpoints, all this stuff gets required here. for funcname, spec in pairs({ home = { - GET = endpoints.index_get, + GET = require("endpoints.index_get"), }, claim = { - GET = endpoints.claim_get, - POST = endpoints.claim_post, + GET = require("endpoints.claim_get"), + POST = require("endpoints.claim_post"), }, paste = { - GET = endpionts.paste_get, - POST = endpoints.paste_post, + GET = require("endpoints.paste_get"), + POST = require("endpoints.paste_post"), }, read = { - GET = endpoints.read_get, - POST = endpoints.read_post + GET = require("endpoints.read_get"), + POST = require("endpoints.read_post"), }, login = { - GET = endpoints.login_get, - POST = endpoints.login_post, + GET = require("endpoints.login_get"), + POST = require("endpoints.login_post"), }, logout = { - GET = endpoints.logout_get, + GET = require("endpoints.logout_get"), }, edit = { - GET = endpoints.edit_get, - POST = endpoints.edit_post, + GET = require("endpoints.edit_get"), + POST = require("endpoints.edit_post"), }, delete = { - POST = endpoints.delete_post, + POST = require("endpoints.delete_post"), }, edit_bio = { - GET = endpoints.bio_edit_get, - POST = endpoints.bio_post, + GET = require("endpoints.bio_get"), + POST = require("endpoints.bio_post"), }, download = { - GET = endpoints.download_get, + GET = require("endpoints.download_get"), }, preview = { - POST = endpoints.preview_post, + POST = require("endpoints.preview_post"), }, search = { - GET = endpoints.search_get, + GET = require("endpoints.search_get"), }, archive = { - GET = endpoints.archive_get, + GET = require("endpoints.archive_get"), }, api = { - GET = endpoints.api_get, - POST = endpoints.api_post, + GET = require("endpoints.api_get"), }, }) do assert(_G[funcname] == nil, "Tried to overwrite an endpoint, please define endpoints exactly once") - -- TODO: Fill this out - for k,v in pairs(http_methods) do - http_m_rev[v] = true - end for k,v in pairs(spec) do assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'") end diff --git a/src/lua/pages.lua b/src/lua/pages.lua index f5c63ec..46e47e9 100644 --- a/src/lua/pages.lua +++ b/src/lua/pages.lua @@ -18,6 +18,7 @@ local pagenames = { "author_edit", "search", "error", + "edit_bio", } local pages = {} for k,v in pairs(pagenames) do