owl-blogs/repository_test.go

277 lines
7.2 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-07-20 17:17:42 +00:00
"os"
"path"
"testing"
)
func TestCanCreateRepository(t *testing.T) {
repoName := testRepoName()
2022-08-03 14:55:48 +00:00
_, err := owl.CreateRepository(repoName)
2022-07-20 17:17:42 +00:00
if err != nil {
2022-07-23 07:11:25 +00:00
t.Error("Error creating repository: ", err.Error())
2022-07-20 17:17:42 +00:00
}
}
func TestCannotCreateExistingRepository(t *testing.T) {
repoName := testRepoName()
2022-08-03 14:55:48 +00:00
owl.CreateRepository(repoName)
_, err := owl.CreateRepository(repoName)
2022-07-20 17:17:42 +00:00
if err == nil {
t.Error("No error returned when creating existing repository")
}
}
func TestCanCreateANewUser(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-07-20 17:17:42 +00:00
if _, err := os.Stat(path.Join(user.Dir(), "")); err != nil {
t.Error("User directory not created")
}
}
func TestCannotRecreateExisitingUser(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
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-07-20 17:17:42 +00:00
if err == nil {
t.Error("No error returned when creating existing user")
}
}
func TestCreateUserAddsVersionFile(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-07-20 17:17:42 +00:00
if _, err := os.Stat(path.Join(user.Dir(), "/meta/VERSION")); err != nil {
t.Error("Version file not created")
}
}
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-07-20 17:17:42 +00:00
if _, err := os.Stat(path.Join(user.Dir(), "/meta/base.html")); err != nil {
t.Error("Base html file not created")
}
}
2022-07-27 19:53:56 +00:00
func TestCreateUserAddConfigYml(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-27 19:53:56 +00:00
user, _ := repo.CreateUser(randomUserName())
if _, err := os.Stat(path.Join(user.Dir(), "/meta/config.yml")); err != nil {
t.Error("Config file not created")
}
}
2022-07-20 17:17:42 +00:00
func TestCreateUserAddsPublicFolder(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
2022-07-20 17:17:42 +00:00
if _, err := os.Stat(path.Join(user.Dir(), "/public")); err != nil {
t.Error("Public folder not created")
}
}
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
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
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()
2022-07-24 14:19:21 +00:00
if len(users) != 2 {
t.Error("Wrong number of users returned, expected 2, got ", len(users))
2022-07-20 17:17:42 +00:00
}
2022-07-20 17:35:31 +00:00
for _, user := range users {
2022-07-24 14:19:21 +00:00
if user.Name() != user1.Name() && user.Name() != user2.Name() {
t.Error("User found: " + user.Name())
2022-07-20 17:35:31 +00:00
}
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()
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(repoName)
2022-07-23 07:00:10 +00:00
// Open the repository
2022-08-03 14:55:48 +00:00
repo2, err := owl.OpenRepository(repoName)
2022-07-23 07:00:10 +00:00
if err != nil {
2022-07-23 07:11:25 +00:00
t.Error("Error opening repository: ", err.Error())
2022-07-23 07:00:10 +00:00
}
if repo2.Dir() != repo.Dir() {
t.Error("Repository directories do not match")
}
}
func TestCannotOpenNonExisitingRepo(t *testing.T) {
2022-08-03 14:55:48 +00:00
_, err := owl.OpenRepository(testRepoName())
2022-07-23 07:00:10 +00:00
if err == nil {
t.Error("No error returned when opening non-existing repository")
}
}
2022-07-23 15:19:47 +00:00
func TestGetUser(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-23 15:19:47 +00:00
user, _ := repo.CreateUser(randomUserName())
// Get the user
user2, err := repo.GetUser(user.Name())
if err != nil {
t.Error("Error getting user: ", err.Error())
}
if user2.Name() != user.Name() {
t.Error("User names do not match")
}
}
func TestCannotGetNonexistingUser(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-23 15:19:47 +00:00
_, err := repo.GetUser(randomUserName())
if err == nil {
t.Error("No error returned when getting non-existing user")
}
}
2022-07-24 13:34:52 +00:00
func TestGetStaticDirOfRepo(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-24 13:34:52 +00:00
// Get the user
staticDir := repo.StaticDir()
if staticDir == "" {
t.Error("Static dir not returned")
}
}
func TestNewRepoGetsStaticFiles(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-24 13:34:52 +00:00
if _, err := os.Stat(repo.StaticDir()); err != nil {
t.Error("Static directory not found")
}
dir, _ := os.Open(repo.StaticDir())
defer dir.Close()
files, _ := dir.Readdirnames(-1)
if len(files) == 0 {
t.Error("No static files found")
}
}
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, _ := owl.CreateRepository(testRepoName())
file, err := os.Open(path.Join(repo.StaticDir(), "pico.min.css"))
if err != nil {
t.Error("Error opening pico.min.css")
}
// check that the file has content
stat, _ := file.Stat()
if stat.Size() == 0 {
t.Error("pico.min.css is empty")
}
}
2022-07-24 18:29:31 +00:00
func TestNewRepoGetsBaseHtml(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-24 18:29:31 +00:00
if _, err := os.Stat(path.Join(repo.Dir(), "/base.html")); err != nil {
t.Error("Base html file not found")
}
}
func TestCanGetRepoTemplate(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-24 18:29:31 +00:00
// Get the user
template, err := repo.Template()
if err != nil {
t.Error("Error getting template: ", err.Error())
}
if template == "" {
t.Error("Template not returned")
}
}
func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName)
created_repo.CreateUser(userName)
created_repo.CreateUser(randomUserName())
created_repo.CreateUser(randomUserName())
// Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName)
users, _ := repo.Users()
if len(users) != 1 {
t.Error("Wrong number of users returned, expected 1, got ", len(users))
}
if users[0].Name() != userName {
t.Error("User name does not match")
}
}
func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName)
created_repo.CreateUser(userName)
// Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName)
user, _ := repo.GetUser(userName)
if user.UrlPath() != "/" {
t.Error("User url is not '/'. Got: ", user.UrlPath())
}
}
2022-08-06 17:38:13 +00:00
func TestCanGetMapWithAllPostAliases(t *testing.T) {
repo, _ := owl.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
post, _ := user.CreateNewPost("test-1")
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.Posts()
if len(posts) != 1 {
t.Error("Wrong number of posts returned, expected 1, got ", len(posts))
}
var aliases map[string]*owl.Post
aliases, err := repo.PostAliases()
if err != nil {
t.Error("Error getting post aliases: ", err.Error())
}
if len(aliases) != 2 {
t.Error("Wrong number of aliases returned, expected 2, got ", len(aliases))
t.Error("Aliases: ", aliases)
}
if aliases["/foo/bar"] == nil {
t.Error("Alias '/foo/bar' not found")
}
if aliases["/foo/baz"] == nil {
t.Error("Alias '/foo/baz' not found")
}
}