refactoring to have web config (single user, unsafe) in config of repo

This commit is contained in:
Niko Abeler 2022-09-05 20:50:46 +02:00
parent 2614ee3f15
commit ef53bfa358
16 changed files with 100 additions and 135 deletions

View File

@ -1,6 +1,7 @@
package web_test package web_test
import ( import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -9,7 +10,7 @@ import (
) )
func TestRedirectOnAliases(t *testing.T) { func TestRedirectOnAliases(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -47,7 +48,7 @@ func TestRedirectOnAliases(t *testing.T) {
} }
func TestNoRedirectOnNonExistingAliases(t *testing.T) { func TestNoRedirectOnNonExistingAliases(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -78,7 +79,7 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) {
} }
func TestNoRedirectIfValidPostUrl(t *testing.T) { func TestNoRedirectIfValidPostUrl(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
post2, _ := user.CreateNewPost("post-2") post2, _ := user.CreateNewPost("post-2")
@ -109,7 +110,7 @@ func TestNoRedirectIfValidPostUrl(t *testing.T) {
} }
func TestRedirectIfInvalidPostUrl(t *testing.T) { func TestRedirectIfInvalidPostUrl(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -139,7 +140,7 @@ func TestRedirectIfInvalidPostUrl(t *testing.T) {
} }
func TestRedirectIfInvalidUserUrl(t *testing.T) { func TestRedirectIfInvalidUserUrl(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -169,7 +170,7 @@ func TestRedirectIfInvalidUserUrl(t *testing.T) {
} }
func TestRedirectIfInvalidMediaUrl(t *testing.T) { func TestRedirectIfInvalidMediaUrl(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -199,9 +200,8 @@ func TestRedirectIfInvalidMediaUrl(t *testing.T) {
} }
func TestDeepAliasInSingleUserMode(t *testing.T) { func TestDeepAliasInSingleUserMode(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{SingleUser: "test-1"})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
repo.SetSingleUser(user)
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
content := "---\n" content := "---\n"

View File

@ -11,8 +11,8 @@ import (
) )
func getUserFromRepo(repo *owl.Repository, ps httprouter.Params) (owl.User, error) { func getUserFromRepo(repo *owl.Repository, ps httprouter.Params) (owl.User, error) {
if repo.SingleUserName() != "" { if config, _ := repo.Config(); config.SingleUser != "" {
return repo.GetUser(repo.SingleUserName()) return repo.GetUser(config.SingleUser)
} }
userName := ps.ByName("user") userName := ps.ByName("user")
user, err := repo.GetUser(userName) user, err := repo.GetUser(userName)

View File

@ -27,13 +27,13 @@ func testRepoName() string {
return "/tmp/" + randomName() return "/tmp/" + randomName()
} }
func getTestRepo() owl.Repository { func getTestRepo(config owl.RepoConfig) owl.Repository {
repo, _ := owl.CreateRepository(testRepoName()) repo, _ := owl.CreateRepository(testRepoName(), config)
return repo return repo
} }
func TestMultiUserRepoIndexHandler(t *testing.T) { func TestMultiUserRepoIndexHandler(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.CreateUser("user_1") repo.CreateUser("user_1")
repo.CreateUser("user_2") repo.CreateUser("user_2")
@ -64,7 +64,7 @@ func TestMultiUserRepoIndexHandler(t *testing.T) {
} }
func TestMultiUserUserIndexHandler(t *testing.T) { func TestMultiUserUserIndexHandler(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
user.CreateNewPost("post-1") user.CreateNewPost("post-1")
@ -91,7 +91,7 @@ func TestMultiUserUserIndexHandler(t *testing.T) {
} }
func TestMultiUserPostHandler(t *testing.T) { func TestMultiUserPostHandler(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -112,7 +112,7 @@ func TestMultiUserPostHandler(t *testing.T) {
} }
func TestMultiUserPostMediaHandler(t *testing.T) { func TestMultiUserPostMediaHandler(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")

View File

@ -1,6 +1,7 @@
package web_test package web_test
import ( import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -9,7 +10,7 @@ import (
) )
func TestPostHandlerReturns404OnDrafts(t *testing.T) { func TestPostHandlerReturns404OnDrafts(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")

View File

@ -1,6 +1,7 @@
package web_test package web_test
import ( import (
"h4kor/owl-blogs"
main "h4kor/owl-blogs/cmd/owl/web" main "h4kor/owl-blogs/cmd/owl/web"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -9,7 +10,7 @@ import (
) )
func TestMultiUserUserRssIndexHandler(t *testing.T) { func TestMultiUserUserRssIndexHandler(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
user.CreateNewPost("post-1") user.CreateNewPost("post-1")

View File

@ -34,20 +34,10 @@ func SingleUserRouter(repo *owl.Repository) http.Handler {
return router return router
} }
func StartServer(repoPath string, port int, unsafe bool, user string) { func StartServer(repoPath string, port int) {
var repo owl.Repository var repo owl.Repository
var err error var err error
if user != "" {
println("Single user mode")
println("Repository:", repoPath)
println("User:", user)
repo, err = owl.OpenSingleUserRepo(repoPath, user)
} else {
println("Multi user mode")
println("Repository:", repoPath)
repo, err = owl.OpenRepository(repoPath) repo, err = owl.OpenRepository(repoPath)
}
repo.SetAllowRawHtml(unsafe)
if err != nil { if err != nil {
println("Error opening repository: ", err.Error()) println("Error opening repository: ", err.Error())
@ -55,13 +45,12 @@ func StartServer(repoPath string, port int, unsafe bool, user string) {
} }
var router http.Handler var router http.Handler
if user == "" { if config, _ := repo.Config(); config.SingleUser != "" {
println("Multi user mode Router used")
router = Router(&repo)
} else {
println("Single user mode Router used")
router = SingleUserRouter(&repo) router = SingleUserRouter(&repo)
} else {
router = Router(&repo)
} }
println("Listening on port", port) println("Listening on port", port)
http.ListenAndServe(":"+strconv.Itoa(port), router) http.ListenAndServe(":"+strconv.Itoa(port), router)

View File

@ -12,9 +12,8 @@ import (
) )
func getSingleUserTestRepo() (owl.Repository, owl.User) { func getSingleUserTestRepo() (owl.Repository, owl.User) {
repo, _ := owl.CreateRepository(testRepoName()) repo, _ := owl.CreateRepository(testRepoName(), owl.RepoConfig{SingleUser: "test-1"})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
repo.SetSingleUser(user)
return repo, user return repo, user
} }

View File

@ -42,7 +42,7 @@ func assertStatus(t *testing.T, rr *httptest.ResponseRecorder, expStatus int) {
} }
func TestWebmentionHandleAccepts(t *testing.T) { func TestWebmentionHandleAccepts(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -60,7 +60,7 @@ func TestWebmentionHandleAccepts(t *testing.T) {
func TestWebmentionWrittenToPost(t *testing.T) { func TestWebmentionWrittenToPost(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -89,7 +89,7 @@ func TestWebmentionWrittenToPost(t *testing.T) {
// (Most commonly this means checking that the source and target schemes are http or https). // (Most commonly this means checking that the source and target schemes are http or https).
func TestWebmentionSourceValidation(t *testing.T) { func TestWebmentionSourceValidation(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -106,7 +106,7 @@ func TestWebmentionSourceValidation(t *testing.T) {
func TestWebmentionTargetValidation(t *testing.T) { func TestWebmentionTargetValidation(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -125,7 +125,7 @@ func TestWebmentionTargetValidation(t *testing.T) {
func TestWebmentionSameTargetAndSource(t *testing.T) { func TestWebmentionSameTargetAndSource(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")
@ -144,7 +144,7 @@ func TestWebmentionSameTargetAndSource(t *testing.T) {
// This check SHOULD happen synchronously to reject invalid Webmentions before more in-depth verification begins. // This check SHOULD happen synchronously to reject invalid Webmentions before more in-depth verification begins.
// What a "valid resource" means is up to the receiver. // What a "valid resource" means is up to the receiver.
func TestValidationOfTarget(t *testing.T) { func TestValidationOfTarget(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("test-1") user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1") post, _ := user.CreateNewPost("post-1")

View File

@ -1 +0,0 @@
domain: "http://localhost:8080"

View File

@ -50,13 +50,13 @@ func randomUserName() string {
} }
func getTestUser() owl.User { func getTestUser() owl.User {
repo, _ := owl.CreateRepository(testRepoName()) repo, _ := owl.CreateRepository(testRepoName(), owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
return user return user
} }
func getTestRepo() owl.Repository { func getTestRepo(config owl.RepoConfig) owl.Repository {
repo, _ := owl.CreateRepository(testRepoName()) repo, _ := owl.CreateRepository(testRepoName(), config)
return repo return repo
} }

View File

@ -144,7 +144,7 @@ func (post Post) RenderedContent() bytes.Buffer {
} }
options := goldmark.WithRendererOptions() options := goldmark.WithRendererOptions()
if post.user.repo.AllowRawHtml() { if config, _ := post.user.repo.Config(); config.AllowRawHtml {
options = goldmark.WithRendererOptions( options = goldmark.WithRendererOptions(
html.WithUnsafe(), html.WithUnsafe(),
) )

View File

@ -89,7 +89,7 @@ func TestDraftInMetaData(t *testing.T) {
} }
func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) { func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
content := "---\n" content := "---\n"
@ -107,8 +107,7 @@ func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
} }
func TestRawHTMLIfAllowedByRepo(t *testing.T) { func TestRawHTMLIfAllowedByRepo(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{AllowRawHtml: true})
repo.SetAllowRawHtml(true)
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
content := "---\n" content := "---\n"
@ -126,8 +125,7 @@ func TestRawHTMLIfAllowedByRepo(t *testing.T) {
} }
func TestLoadMeta(t *testing.T) { func TestLoadMeta(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{AllowRawHtml: true})
repo.SetAllowRawHtml(true)
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
@ -170,7 +168,7 @@ func TestLoadMeta(t *testing.T) {
/// ///
func TestPersistWebmention(t *testing.T) { func TestPersistWebmention(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
webmention := owl.WebmentionIn{ webmention := owl.WebmentionIn{
@ -191,7 +189,7 @@ func TestPersistWebmention(t *testing.T) {
} }
func TestAddWebmentionCreatesFile(t *testing.T) { func TestAddWebmentionCreatesFile(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.HttpClient = &MockHttpRetriever{} repo.HttpClient = &MockHttpRetriever{}
repo.Parser = &MockHttpParser{} repo.Parser = &MockHttpParser{}
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
@ -209,7 +207,7 @@ func TestAddWebmentionCreatesFile(t *testing.T) {
} }
func TestAddWebmentionNotOverwritingFile(t *testing.T) { func TestAddWebmentionNotOverwritingFile(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.HttpClient = &MockHttpRetriever{} repo.HttpClient = &MockHttpRetriever{}
repo.Parser = &MockHttpParser{} repo.Parser = &MockHttpParser{}
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
@ -239,7 +237,7 @@ func TestAddWebmentionNotOverwritingFile(t *testing.T) {
} }
func TestAddWebmentionAddsParsedTitle(t *testing.T) { func TestAddWebmentionAddsParsedTitle(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.HttpClient = &MockHttpRetriever{} repo.HttpClient = &MockHttpRetriever{}
repo.Parser = &MockHttpParser{} repo.Parser = &MockHttpParser{}
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
@ -262,7 +260,7 @@ func TestAddWebmentionAddsParsedTitle(t *testing.T) {
} }
func TestApprovedWebmentions(t *testing.T) { func TestApprovedWebmentions(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
webmention := owl.WebmentionIn{ webmention := owl.WebmentionIn{
@ -305,7 +303,7 @@ func TestApprovedWebmentions(t *testing.T) {
} }
func TestScanningForLinks(t *testing.T) { func TestScanningForLinks(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
@ -328,7 +326,7 @@ func TestScanningForLinks(t *testing.T) {
} }
func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) { func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
@ -354,7 +352,7 @@ func TestScanningForLinksDoesNotAddDuplicates(t *testing.T) {
} }
func TestCanSendWebmention(t *testing.T) { func TestCanSendWebmention(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.HttpClient = &MockHttpRetriever{} repo.HttpClient = &MockHttpRetriever{}
repo.Parser = &MockHttpParser{} repo.Parser = &MockHttpParser{}
user, _ := repo.CreateUser("testuser") user, _ := repo.CreateUser("testuser")

View File

@ -107,7 +107,7 @@ func TestRenderIndexPageWithBrokenBaseTemplate(t *testing.T) {
} }
func TestRenderUserList(t *testing.T) { func TestRenderUserList(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
repo.CreateUser("user1") repo.CreateUser("user1")
repo.CreateUser("user2") repo.CreateUser("user2")

View File

@ -17,18 +17,17 @@ var VERSION = "0.0.1"
type Repository struct { type Repository struct {
name string name string
single_user_mode bool
active_user string
allow_raw_html bool
HttpClient HttpClient HttpClient HttpClient
Parser HtmlParser Parser HtmlParser
} }
type RepoConfig struct { type RepoConfig struct {
Domain string `yaml:"domain"` Domain string `yaml:"domain"`
SingleUser string `yaml:"single_user"`
AllowRawHtml bool `yaml:"allow_raw_html"`
} }
func CreateRepository(name string) (Repository, error) { func CreateRepository(name string, config RepoConfig) (Repository, error) {
newRepo := Repository{name: name, Parser: OwlHtmlParser{}, HttpClient: OwlHttpClient{}} newRepo := Repository{name: name, Parser: OwlHtmlParser{}, HttpClient: OwlHttpClient{}}
// check if repository already exists // check if repository already exists
if dirExists(newRepo.Dir()) { if dirExists(newRepo.Dir()) {
@ -39,6 +38,13 @@ func CreateRepository(name string) (Repository, error) {
os.Mkdir(newRepo.UsersDir(), 0755) os.Mkdir(newRepo.UsersDir(), 0755)
os.Mkdir(newRepo.StaticDir(), 0755) os.Mkdir(newRepo.StaticDir(), 0755)
// create config file
if config.Domain == "" {
config.Domain = "http://localhost:8080"
}
config_data, _ := yaml.Marshal(config)
os.WriteFile(path.Join(newRepo.Dir(), "config.yml"), config_data, 0644)
// copy all files from static_files embed.FS to StaticDir // copy all files from static_files embed.FS to StaticDir
staticFiles, _ := embed_files.ReadDir("embed/initial/static") staticFiles, _ := embed_files.ReadDir("embed/initial/static")
for _, file := range staticFiles { for _, file := range staticFiles {
@ -71,36 +77,6 @@ func OpenRepository(name string) (Repository, error) {
return repo, nil return repo, nil
} }
func OpenSingleUserRepo(name string, user_name string) (Repository, error) {
repo, err := OpenRepository(name)
if err != nil {
return Repository{}, err
}
user, err := repo.GetUser(user_name)
if err != nil {
return Repository{}, err
}
repo.SetSingleUser(user)
return repo, nil
}
func (repo Repository) AllowRawHtml() bool {
return repo.allow_raw_html
}
func (repo *Repository) SetAllowRawHtml(allow bool) {
repo.allow_raw_html = allow
}
func (repo *Repository) SetSingleUser(user User) {
repo.single_user_mode = true
repo.active_user = user.name
}
func (repo Repository) SingleUserName() string {
return repo.active_user
}
func (repo Repository) Dir() string { func (repo Repository) Dir() string {
return repo.name return repo.name
} }
@ -114,7 +90,8 @@ func (repo Repository) UsersDir() string {
} }
func (repo Repository) UserUrlPath(user User) string { func (repo Repository) UserUrlPath(user User) string {
if repo.single_user_mode { config, _ := repo.Config()
if config.SingleUser != "" {
return "/" return "/"
} }
return "/user/" + user.name + "/" return "/user/" + user.name + "/"
@ -136,8 +113,9 @@ func (repo Repository) Template() (string, error) {
} }
func (repo Repository) Users() ([]User, error) { func (repo Repository) Users() ([]User, error) {
if repo.single_user_mode { config, _ := repo.Config()
return []User{{repo: &repo, name: repo.active_user}}, nil if config.SingleUser != "" {
return []User{{repo: &repo, name: config.SingleUser}}, nil
} }
userNames := listDir(repo.UsersDir()) userNames := listDir(repo.UsersDir())

View File

@ -9,7 +9,7 @@ import (
func TestCanCreateRepository(t *testing.T) { func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName() repoName := testRepoName()
_, err := owl.CreateRepository(repoName) _, err := owl.CreateRepository(repoName, owl.RepoConfig{})
if err != nil { if err != nil {
t.Error("Error creating repository: ", err.Error()) t.Error("Error creating repository: ", err.Error())
} }
@ -18,8 +18,8 @@ func TestCanCreateRepository(t *testing.T) {
func TestCannotCreateExistingRepository(t *testing.T) { func TestCannotCreateExistingRepository(t *testing.T) {
repoName := testRepoName() repoName := testRepoName()
owl.CreateRepository(repoName) owl.CreateRepository(repoName, owl.RepoConfig{})
_, err := owl.CreateRepository(repoName) _, err := owl.CreateRepository(repoName, owl.RepoConfig{})
if err == nil { if err == nil {
t.Error("No error returned when creating existing repository") t.Error("No error returned when creating existing repository")
} }
@ -27,7 +27,7 @@ func TestCannotCreateExistingRepository(t *testing.T) {
func TestCanCreateANewUser(t *testing.T) { func TestCanCreateANewUser(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil {
t.Error("User directory not created") t.Error("User directory not created")
@ -36,7 +36,7 @@ func TestCanCreateANewUser(t *testing.T) {
func TestCannotRecreateExisitingUser(t *testing.T) { func TestCannotRecreateExisitingUser(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
userName := randomUserName() userName := randomUserName()
repo.CreateUser(userName) repo.CreateUser(userName)
_, err := repo.CreateUser(userName) _, err := repo.CreateUser(userName)
@ -47,7 +47,7 @@ func TestCannotRecreateExisitingUser(t *testing.T) {
func TestCreateUserAddsVersionFile(t *testing.T) { func TestCreateUserAddsVersionFile(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil {
t.Error("Version file not created") t.Error("Version file not created")
@ -56,7 +56,7 @@ func TestCreateUserAddsVersionFile(t *testing.T) {
func TestCreateUserAddsBaseHtmlFile(t *testing.T) { func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil {
t.Error("Base html file not created") t.Error("Base html file not created")
@ -65,7 +65,7 @@ func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
func TestCreateUserAddConfigYml(t *testing.T) { func TestCreateUserAddConfigYml(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml")); err != nil {
t.Error("Config file not created") t.Error("Config file not created")
@ -74,7 +74,7 @@ func TestCreateUserAddConfigYml(t *testing.T) {
func TestCreateUserAddsPublicFolder(t *testing.T) { func TestCreateUserAddsPublicFolder(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil { if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil {
t.Error("Public folder not created") t.Error("Public folder not created")
@ -83,7 +83,7 @@ func TestCreateUserAddsPublicFolder(t *testing.T) {
func TestCanListRepoUsers(t *testing.T) { func TestCanListRepoUsers(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user1, _ := repo.CreateUser(randomUserName()) user1, _ := repo.CreateUser(randomUserName())
user2, _ := repo.CreateUser(randomUserName()) user2, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
@ -101,7 +101,7 @@ func TestCanListRepoUsers(t *testing.T) {
func TestCanOpenRepository(t *testing.T) { func TestCanOpenRepository(t *testing.T) {
// Create a new user // Create a new user
repoName := testRepoName() repoName := testRepoName()
repo, _ := owl.CreateRepository(repoName) repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{})
// Open the repository // Open the repository
repo2, err := owl.OpenRepository(repoName) repo2, err := owl.OpenRepository(repoName)
if err != nil { if err != nil {
@ -121,7 +121,7 @@ func TestCannotOpenNonExisitingRepo(t *testing.T) {
func TestGetUser(t *testing.T) { func TestGetUser(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Get the user // Get the user
user2, err := repo.GetUser(user.Name()) user2, err := repo.GetUser(user.Name())
@ -135,7 +135,7 @@ func TestGetUser(t *testing.T) {
func TestCannotGetNonexistingUser(t *testing.T) { func TestCannotGetNonexistingUser(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
_, err := repo.GetUser(randomUserName()) _, err := repo.GetUser(randomUserName())
if err == nil { if err == nil {
t.Error("No error returned when getting non-existing user") t.Error("No error returned when getting non-existing user")
@ -144,7 +144,7 @@ func TestCannotGetNonexistingUser(t *testing.T) {
func TestGetStaticDirOfRepo(t *testing.T) { func TestGetStaticDirOfRepo(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
// Get the user // Get the user
staticDir := repo.StaticDir() staticDir := repo.StaticDir()
if staticDir == "" { if staticDir == "" {
@ -154,7 +154,7 @@ func TestGetStaticDirOfRepo(t *testing.T) {
func TestNewRepoGetsStaticFiles(t *testing.T) { func TestNewRepoGetsStaticFiles(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
if _, err := os.Stat(repo.StaticDir()); err != nil { if _, err := os.Stat(repo.StaticDir()); err != nil {
t.Error("Static directory not found") t.Error("Static directory not found")
} }
@ -169,7 +169,7 @@ func TestNewRepoGetsStaticFiles(t *testing.T) {
func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) { func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css")) file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css"))
if err != nil { if err != nil {
t.Error("Error opening pico.min.css") t.Error("Error opening pico.min.css")
@ -183,7 +183,7 @@ func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) {
func TestNewRepoGetsBaseHtml(t *testing.T) { func TestNewRepoGetsBaseHtml(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil { if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil {
t.Error("Base html file not found") t.Error("Base html file not found")
} }
@ -191,7 +191,7 @@ func TestNewRepoGetsBaseHtml(t *testing.T) {
func TestCanGetRepoTemplate(t *testing.T) { func TestCanGetRepoTemplate(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
// Get the user // Get the user
template, err := repo.Template() template, err := repo.Template()
if err != nil { if err != nil {
@ -206,13 +206,13 @@ func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
// Create a new user // Create a new user
repoName := testRepoName() repoName := testRepoName()
userName := randomUserName() userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName) created_repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{SingleUser: userName})
created_repo.CreateUser(userName) created_repo.CreateUser(userName)
created_repo.CreateUser(randomUserName()) created_repo.CreateUser(randomUserName())
created_repo.CreateUser(randomUserName()) created_repo.CreateUser(randomUserName())
// Open the repository // Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName) repo, _ := owl.OpenRepository(repoName)
users, _ := repo.Users() users, _ := repo.Users()
if len(users) != 1 { if len(users) != 1 {
@ -227,11 +227,11 @@ func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) {
// Create a new user // Create a new user
repoName := testRepoName() repoName := testRepoName()
userName := randomUserName() userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName) created_repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{SingleUser: userName})
created_repo.CreateUser(userName) created_repo.CreateUser(userName)
// Open the repository // Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName) repo, _ := owl.OpenRepository(repoName)
user, _ := repo.GetUser(userName) user, _ := repo.GetUser(userName)
if user.UrlPath() != "/" { if user.UrlPath() != "/" {
t.Error("User url is not '/'. Got: ", user.UrlPath()) t.Error("User url is not '/'. Got: ", user.UrlPath())
@ -239,7 +239,7 @@ func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) {
} }
func TestCanGetMapWithAllPostAliases(t *testing.T) { func TestCanGetMapWithAllPostAliases(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
post, _ := user.CreateNewPost("test-1") post, _ := user.CreateNewPost("test-1")
@ -276,7 +276,7 @@ func TestCanGetMapWithAllPostAliases(t *testing.T) {
} }
func TestAliasesHaveCorrectPost(t *testing.T) { func TestAliasesHaveCorrectPost(t *testing.T) {
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
post1, _ := user.CreateNewPost("test-1") post1, _ := user.CreateNewPost("test-1")
post2, _ := user.CreateNewPost("test-2") post2, _ := user.CreateNewPost("test-2")

View File

@ -11,7 +11,7 @@ import (
func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
@ -26,7 +26,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
func TestCreateNewPostCreatesMediaDir(t *testing.T) { func TestCreateNewPostCreatesMediaDir(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
@ -49,7 +49,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
func TestCreateNewPostMultipleCalls(t *testing.T) { func TestCreateNewPostMultipleCalls(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
@ -66,7 +66,7 @@ func TestCreateNewPostMultipleCalls(t *testing.T) {
func TestCanListUserPosts(t *testing.T) { func TestCanListUserPosts(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
@ -83,7 +83,7 @@ func TestCanListUserPosts(t *testing.T) {
func TestCannotListUserPostsInSubdirectories(t *testing.T) { func TestCannotListUserPostsInSubdirectories(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
@ -120,7 +120,7 @@ func TestCannotListUserPostsInSubdirectories(t *testing.T) {
func TestCannotListUserPostsWithoutIndexMd(t *testing.T) { func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
user.CreateNewPost("testpost") user.CreateNewPost("testpost")
@ -149,7 +149,7 @@ func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) { func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
@ -170,7 +170,7 @@ func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
func TestListUsersDraftsExcludedRealWorld(t *testing.T) { func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
// Create a new user // Create a new user
repo := getTestRepo() repo := getTestRepo(owl.RepoConfig{})
user, _ := repo.CreateUser(randomUserName()) user, _ := repo.CreateUser(randomUserName())
// Create a new post // Create a new post
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")