owl-blogs/repository_test.go

258 lines
8.1 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl_test
2022-07-20 17:17:42 +00:00
import (
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-11-06 13:17:14 +00:00
"h4kor/owl-blogs/test/assertions"
2022-07-20 17:17:42 +00:00
"os"
"path"
"testing"
)
func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName()
_, err := owl.CreateRepository(repoName, owl.RepoConfig{})
assertions.AssertNoError(t, err, "Error creating repository: ")
2022-07-20 17:17:42 +00:00
}
func TestCannotCreateExistingRepository(t *testing.T) {
repoName := testRepoName()
owl.CreateRepository(repoName, owl.RepoConfig{})
_, err := owl.CreateRepository(repoName, owl.RepoConfig{})
2022-11-03 18:23:13 +00:00
assertions.AssertError(t, err, "No error returned when creating existing repository")
2022-07-20 17:17:42 +00:00
}
func TestCanCreateANewUser(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(user.Dir(), ""))
assertions.AssertNoError(t, err, "Error creating user: ")
2022-07-20 17:17:42 +00:00
}
func TestCannotRecreateExisitingUser(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:17:42 +00:00
userName := randomUserName()
2022-07-20 17:35:31 +00:00
repo.CreateUser(userName)
_, err := repo.CreateUser(userName)
2022-11-03 18:23:13 +00:00
assertions.AssertError(t, err, "No error returned when creating existing user")
2022-07-20 17:17:42 +00:00
}
func TestCreateUserAddsVersionFile(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION"))
assertions.AssertNoError(t, err, "Version file not created")
2022-07-20 17:17:42 +00:00
}
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(user.Dir(), "/meta/base.html"))
assertions.AssertNoError(t, err, "Base html file not created")
2022-07-20 17:17:42 +00:00
}
2022-07-27 19:53:56 +00:00
func TestCreateUserAddConfigYml(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-27 19:53:56 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml"))
assertions.AssertNoError(t, err, "Config file not created")
2022-07-27 19:53:56 +00:00
}
2022-07-20 17:17:42 +00:00
func TestCreateUserAddsPublicFolder(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(user.Dir(), "/public"))
assertions.AssertNoError(t, err, "Public folder not created")
2022-07-20 17:17:42 +00:00
}
2022-07-24 14:19:21 +00:00
func TestCanListRepoUsers(t *testing.T) {
2022-07-20 17:17:42 +00:00
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user1, _ := repo.CreateUser(randomUserName())
user2, _ := repo.CreateUser(randomUserName())
2022-07-20 17:17:42 +00:00
// Create a new post
2022-07-20 17:35:31 +00:00
users, _ := repo.Users()
assertions.AssertLen(t, users, 2)
2022-07-20 17:35:31 +00:00
for _, user := range users {
2022-11-03 18:23:13 +00:00
assertions.AssertNot(
t,
user.Name() != user1.Name() && user.Name() != user2.Name(),
"User found: "+user.Name(),
)
2022-07-20 17:17:42 +00:00
}
}
2022-07-23 07:00:10 +00:00
func TestCanOpenRepository(t *testing.T) {
// Create a new user
repoName := testRepoName()
repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{})
2022-07-23 07:00:10 +00:00
// Open the repository
2022-08-03 14:55:48 +00:00
repo2, err := owl.OpenRepository(repoName)
assertions.AssertNoError(t, err, "Error opening repository: ")
2022-11-03 18:23:13 +00:00
assertions.Assert(t, repo2.Dir() == repo.Dir(), "Repository directories do not match")
2022-07-23 07:00:10 +00:00
}
func TestCannotOpenNonExisitingRepo(t *testing.T) {
2022-08-03 14:55:48 +00:00
_, err := owl.OpenRepository(testRepoName())
2022-11-03 18:23:13 +00:00
assertions.AssertError(t, err, "No error returned when opening non-existing repository")
2022-07-23 07:00:10 +00:00
}
2022-07-23 15:19:47 +00:00
func TestGetUser(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-23 15:19:47 +00:00
user, _ := repo.CreateUser(randomUserName())
// Get the user
user2, err := repo.GetUser(user.Name())
assertions.AssertNoError(t, err, "Error getting user: ")
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user2.Name() == user.Name(), "User names do not match")
2022-07-23 15:19:47 +00:00
}
func TestCannotGetNonexistingUser(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-23 15:19:47 +00:00
_, err := repo.GetUser(randomUserName())
2022-11-03 18:23:13 +00:00
assertions.AssertError(t, err, "No error returned when getting non-existing user")
2022-07-23 15:19:47 +00:00
}
2022-07-24 13:34:52 +00:00
func TestGetStaticDirOfRepo(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-24 13:34:52 +00:00
// Get the user
staticDir := repo.StaticDir()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, staticDir != "", "Static dir is empty")
2022-07-24 13:34:52 +00:00
}
func TestNewRepoGetsStaticFiles(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-11-03 18:23:13 +00:00
_, err := os.Stat(repo.StaticDir())
assertions.AssertNoError(t, err, "Static dir not created")
2022-07-24 13:34:52 +00:00
dir, _ := os.Open(repo.StaticDir())
defer dir.Close()
files, _ := dir.Readdirnames(-1)
2022-11-03 18:23:13 +00:00
assertions.AssertLen(t, files, 1)
2022-07-24 13:34:52 +00:00
}
2022-07-24 18:29:31 +00:00
2022-08-05 20:13:14 +00:00
func TestNewRepoGetsStaticFilesPicoCSSWithContent(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-08-05 20:13:14 +00:00
file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css"))
assertions.AssertNoError(t, err, "Error opening pico.min.css")
2022-08-05 20:13:14 +00:00
// check that the file has content
stat, _ := file.Stat()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, stat.Size() > 0, "pico.min.css is empty")
2022-08-05 20:13:14 +00:00
}
2022-07-24 18:29:31 +00:00
func TestNewRepoGetsBaseHtml(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-11-03 18:23:13 +00:00
_, err := os.Stat(path.Join(repo.Dir(), "/base.html"))
assertions.AssertNoError(t, err, "Base html file not found")
2022-07-24 18:29:31 +00:00
}
func TestCanGetRepoTemplate(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-24 18:29:31 +00:00
// Get the user
template, err := repo.Template()
assertions.AssertNoError(t, err, "Error getting template: ")
2022-11-03 18:23:13 +00:00
assertions.Assert(t, template != "", "Template is empty")
2022-07-24 18:29:31 +00:00
}
func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{SingleUser: userName})
created_repo.CreateUser(userName)
created_repo.CreateUser(randomUserName())
created_repo.CreateUser(randomUserName())
// Open the repository
repo, _ := owl.OpenRepository(repoName)
users, _ := repo.Users()
assertions.AssertLen(t, users, 1)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, users[0].Name() == userName, "User name does not match")
}
func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName, owl.RepoConfig{SingleUser: userName})
created_repo.CreateUser(userName)
// Open the repository
repo, _ := owl.OpenRepository(repoName)
user, _ := repo.GetUser(userName)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.UrlPath() == "/", "User url path is not /")
}
2022-08-06 17:38:13 +00:00
func TestCanGetMapWithAllPostAliases(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
2022-08-06 17:38:13 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-12-05 19:47:52 +00:00
post, _ := user.CreateNewPost(owl.PostMeta{Title: "test-1"}, "")
2022-08-06 17:38:13 +00:00
content := "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/bar\n"
content += " - /foo/baz\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 1)
2022-08-06 17:38:13 +00:00
2022-12-05 18:51:42 +00:00
var aliases map[string]owl.IPost
2022-08-06 17:38:13 +00:00
aliases, err := repo.PostAliases()
assertions.AssertNoError(t, err, "Error getting post aliases: ")
assertions.AssertMapLen(t, aliases, 2)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, aliases["/foo/bar"] != nil, "Alias '/foo/bar' not found")
assertions.Assert(t, aliases["/foo/baz"] != nil, "Alias '/foo/baz' not found")
2022-08-06 17:38:13 +00:00
}
2022-08-21 10:27:28 +00:00
func TestAliasesHaveCorrectPost(t *testing.T) {
repo := getTestRepo(owl.RepoConfig{})
2022-08-21 10:27:28 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-12-05 19:47:52 +00:00
post1, _ := user.CreateNewPost(owl.PostMeta{Title: "test-1"}, "")
post2, _ := user.CreateNewPost(owl.PostMeta{Title: "test-2"}, "")
2022-08-21 10:27:28 +00:00
content := "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/1\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test\n"
content += "aliases: \n"
content += " - /foo/2\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.PublishedPosts()
assertions.AssertLen(t, posts, 2)
2022-08-21 10:27:28 +00:00
2022-12-05 18:51:42 +00:00
var aliases map[string]owl.IPost
2022-08-21 10:27:28 +00:00
aliases, err := repo.PostAliases()
assertions.AssertNoError(t, err, "Error getting post aliases: ")
assertions.AssertMapLen(t, aliases, 2)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, aliases["/foo/1"].Id() == post1.Id(), "Alias '/foo/1' does not point to post 1")
assertions.Assert(t, aliases["/foo/2"].Id() == post2.Id(), "Alias '/foo/2' does not point to post 2")
2022-08-21 10:27:28 +00:00
}