owl-blogs/user_test.go

167 lines
4.3 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl_test
2022-07-20 17:35:31 +00:00
import (
"fmt"
2022-08-03 14:55:48 +00:00
"h4kor/owl-blogs"
2022-07-20 17:35:31 +00:00
"io/ioutil"
2022-07-23 15:19:47 +00:00
"os"
2022-07-20 17:35:31 +00:00
"path"
"testing"
)
func TestCreateNewPostCreatesEntryInPublic(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())
// Create a new post
2022-07-21 17:02:37 +00:00
user.CreateNewPost("testpost")
2022-07-20 17:35:31 +00:00
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
if len(files) < 1 {
t.Error("Post not created")
}
}
2022-08-01 17:50:29 +00:00
func TestCreateNewPostCreatesMediaDir(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-08-01 17:50:29 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
post, _ := user.CreateNewPost("testpost")
if _, err := os.Stat(post.MediaDir()); os.IsNotExist(err) {
t.Error("Media directory not created")
}
}
func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
user := getTestUser()
// Create a new post
user.CreateNewPost("testpost")
posts, _ := user.Posts()
post, _ := user.GetPost(posts[0])
_, meta := post.MarkdownData()
if meta.Date == "" {
t.Error("Found no date. Got: " + meta.Date)
}
}
2022-07-20 17:35:31 +00:00
func TestCreateNewPostMultipleCalls(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())
// Create a new post
2022-07-21 17:02:37 +00:00
user.CreateNewPost("testpost")
user.CreateNewPost("testpost")
user.CreateNewPost("testpost")
2022-07-20 17:35:31 +00:00
files, err := ioutil.ReadDir(path.Join(user.Dir(), "public"))
if err != nil {
t.Error("Error reading directory")
}
if len(files) < 3 {
t.Error(fmt.Sprintf("Only %d posts created", len(files)))
}
}
2022-07-20 19:12:18 +00:00
func TestCanListUserPosts(t *testing.T) {
// Create a new user
2022-08-03 14:55:48 +00:00
repo, _ := owl.CreateRepository(testRepoName())
2022-07-20 19:12:18 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-07-21 17:02:37 +00:00
user.CreateNewPost("testpost")
user.CreateNewPost("testpost")
user.CreateNewPost("testpost")
2022-07-20 19:12:18 +00:00
posts, err := user.Posts()
if err != nil {
t.Error("Error reading posts")
}
if len(posts) != 3 {
t.Error("No posts found")
}
}
2022-07-21 17:44:07 +00:00
2022-08-03 16:03:10 +00:00
func TestCannotListUserPostsInSubdirectories(t *testing.T) {
2022-07-23 15:19:47 +00:00
// 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())
// Create a new post
user.CreateNewPost("testpost")
os.Mkdir(path.Join(user.PostDir(), "foo"), 0755)
os.Mkdir(path.Join(user.PostDir(), "foo/bar"), 0755)
content := ""
content += "---\n"
content += "title: test\n"
content += "---\n"
content += "\n"
content += "Write your post here.\n"
2022-08-03 16:03:10 +00:00
os.WriteFile(path.Join(user.PostDir(), "foo/index.md"), []byte(content), 0644)
2022-07-23 15:19:47 +00:00
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts()
2022-08-03 16:03:10 +00:00
if !contains(posts, "foo") {
t.Error("Does not contain post: foo. Found:")
for _, p := range posts {
t.Error("\t" + p)
}
}
if contains(posts, "foo/bar") {
t.Error("Invalid post found: foo/bar. Found:")
2022-07-23 15:19:47 +00:00
for _, p := range posts {
t.Error("\t" + p)
}
}
2022-08-03 16:03:10 +00:00
}
func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
// Create a new user
repo, _ := owl.CreateRepository(testRepoName())
user, _ := repo.CreateUser(randomUserName())
// Create a new post
user.CreateNewPost("testpost")
os.Mkdir(path.Join(user.PostDir(), "foo"), 0755)
os.Mkdir(path.Join(user.PostDir(), "foo/bar"), 0755)
content := ""
content += "---\n"
content += "title: test\n"
content += "---\n"
content += "\n"
content += "Write your post here.\n"
2022-07-23 15:19:47 +00:00
2022-08-03 16:03:10 +00:00
os.WriteFile(path.Join(user.PostDir(), "foo/bar/index.md"), []byte(content), 0644)
posts, _ := user.Posts()
if contains(posts, "foo") {
t.Error("Contains invalid post: foo. Found:")
2022-07-23 15:19:47 +00:00
for _, p := range posts {
t.Error("\t" + p)
}
}
}
2022-07-21 17:44:07 +00:00
func TestCanLoadPost(t *testing.T) {
user := getTestUser()
// Create a new post
user.CreateNewPost("testpost")
posts, _ := user.Posts()
post, _ := user.GetPost(posts[0])
if post.Title() != "testpost" {
t.Error("Wrong title, Got: " + post.Title())
}
}
2022-08-03 17:41:13 +00:00
func TestUserUrlPath(t *testing.T) {
user := getTestUser()
if !(user.UrlPath() == "/user/"+user.Name()+"/") {
t.Error("Wrong url path, Expected: " + "/user/" + user.Name() + "/" + " Got: " + user.UrlPath())
}
}
2022-08-13 16:47:27 +00:00
func TestUserFullUrl(t *testing.T) {
user := getTestUser()
if !(user.FullUrl() == "http://localhost:8080/user/"+user.Name()+"/") {
t.Error("Wrong url path, Expected: " + "http://localhost:8080/user/" + user.Name() + "/" + " Got: " + user.FullUrl())
}
}