More work on author biographies

Start work on unit tests for author biographies
Fix a bug in the biography get endpoint
Add the biography get page to the page list
This commit is contained in:
Robin Malley 2022-11-19 23:58:56 +00:00
parent 3431daee0b
commit e25d2fd06a
4 changed files with 149 additions and 54 deletions

118
spec/author_bio_spec.lua Normal file
View File

@ -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 = '<a href="/_bio"'
assert(
home_req_get.response:find(edit_bio_button),
"After logging in the user should have a button to" ..
" edit their biography. Looking for " .. edit_bio_button
.. " but didn't find it in " .. home_req_get.response
)
local edit_bio_req_get = {
method = "GET",
host = username .. ".test.host",
path = "/_bio",
cookies = {
session = sessionid
},
args = {}
}
bio_get(edit_bio_req_get)
assert(edit_bio_req_get.responsecode == 200)
--[=[
local paste_req_post = {
method = "POST",
host = username .. ".test.host",
path = "/_paste",
cookies = {
session = sessionid
},
args = {
title = "post title",
text = "post text",
markup = "plain",
tags = "",
}
}
paste_post(paste_req_post)
for row in db.conn:rows("SELECT COUNT(*) FROM posts") do
assert(row[1] == 1, "Expected exactly 1 post in sample db")
end
local code = paste_req_post.responsecode
assert(code >= 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 <a href="https://' .. username .. '.test.host">' .. username .. '</a>'),
"Failed to find the author name after a paste."
)
assert(
response:find([[post text]]),
"Failed to find post text in response."
)
]=]
end)
end)

View File

@ -12,7 +12,7 @@ local config = require("config")
local stmnt_bio local stmnt_bio
local oldconfigure = configure local oldconfigure = configure
function configure(...) function configure(...)
stmnt_bio = assert(db.conn:prepare(queries.select_bio)) stmnt_bio = assert(db.conn:prepare(queries.select_author_bio))
return oldconfigure(...) return oldconfigure(...)
end end
@ -37,7 +37,7 @@ local function bio_edit_get(req)
stmnt_bio:bind_names{ stmnt_bio:bind_names{
authorid = authorid authorid = authorid
} }
local err = util.do_sql(stmnt_edit) local err = util.do_sql(stmnt_bio)
if err == sql.DONE then if err == sql.DONE then
--No rows, we're logged in but an author with our id doesn't --No rows, we're logged in but an author with our id doesn't
--exist? Something has gone wrong. --exist? Something has gone wrong.
@ -66,4 +66,4 @@ found, please report this error.
http_response(req,200,ret) http_response(req,200,ret)
end end
return edit_get return bio_edit_get

View File

@ -11,41 +11,17 @@ local et = require("etlua")
local sql = require("lsqlite3") local sql = require("lsqlite3")
local zlib = require("zlib") local zlib = require("zlib")
--stub for detouring --stubs for detouring
function configure(...) end function configure(...) end
--smr code --smr code
require("global")
local cache = require("cache") local cache = require("cache")
local pages = require("pages") local pages = require("pages")
local util = require("util") local util = require("util")
local config = require("config") local config = require("config")
local db = require("db") 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") print("Hello from init.lua")
local oldconfigure = configure local oldconfigure = configure
function configure(...) function configure(...)
@ -59,65 +35,65 @@ function configure(...)
end end
print("Created configure function") print("Created configure function")
-- TODO: Fill this out
local http_methods = {"GET","POST"} local http_methods = {"GET","POST"}
local http_m_rev = {} 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({ for funcname, spec in pairs({
home = { home = {
GET = endpoints.index_get, GET = require("endpoints.index_get"),
}, },
claim = { claim = {
GET = endpoints.claim_get, GET = require("endpoints.claim_get"),
POST = endpoints.claim_post, POST = require("endpoints.claim_post"),
}, },
paste = { paste = {
GET = endpionts.paste_get, GET = require("endpoints.paste_get"),
POST = endpoints.paste_post, POST = require("endpoints.paste_post"),
}, },
read = { read = {
GET = endpoints.read_get, GET = require("endpoints.read_get"),
POST = endpoints.read_post POST = require("endpoints.read_post"),
}, },
login = { login = {
GET = endpoints.login_get, GET = require("endpoints.login_get"),
POST = endpoints.login_post, POST = require("endpoints.login_post"),
}, },
logout = { logout = {
GET = endpoints.logout_get, GET = require("endpoints.logout_get"),
}, },
edit = { edit = {
GET = endpoints.edit_get, GET = require("endpoints.edit_get"),
POST = endpoints.edit_post, POST = require("endpoints.edit_post"),
}, },
delete = { delete = {
POST = endpoints.delete_post, POST = require("endpoints.delete_post"),
}, },
edit_bio = { edit_bio = {
GET = endpoints.bio_edit_get, GET = require("endpoints.bio_get"),
POST = endpoints.bio_post, POST = require("endpoints.bio_post"),
}, },
download = { download = {
GET = endpoints.download_get, GET = require("endpoints.download_get"),
}, },
preview = { preview = {
POST = endpoints.preview_post, POST = require("endpoints.preview_post"),
}, },
search = { search = {
GET = endpoints.search_get, GET = require("endpoints.search_get"),
}, },
archive = { archive = {
GET = endpoints.archive_get, GET = require("endpoints.archive_get"),
}, },
api = { api = {
GET = endpoints.api_get, GET = require("endpoints.api_get"),
POST = endpoints.api_post,
}, },
}) do }) do
assert(_G[funcname] == nil, "Tried to overwrite an endpoint, please define endpoints exactly once") 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 for k,v in pairs(spec) do
assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'") assert(http_m_rev[k], "Unknown http method '" .. k .. "' defined for endpoint '" .. funcname .. "'")
end end

View File

@ -18,6 +18,7 @@ local pagenames = {
"author_edit", "author_edit",
"search", "search",
"error", "error",
"edit_bio",
} }
local pages = {} local pages = {}
for k,v in pairs(pagenames) do for k,v in pairs(pagenames) do