owl-blogs/user_test.go

317 lines
9.5 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-11-06 13:17:14 +00:00
"h4kor/owl-blogs/test/assertions"
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
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
2022-10-13 19:00:28 +00:00
files, err := os.ReadDir(path.Join(user.Dir(), "public"))
assertions.AssertNoError(t, err, "Error reading directory")
2022-11-02 21:12:58 +00:00
assertions.AssertLen(t, files, 1)
2022-07-20 17:35:31 +00:00
}
2022-08-01 17:50:29 +00:00
func TestCreateNewPostCreatesMediaDir(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-08-01 17:50:29 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-11-02 21:12:58 +00:00
_, err := os.Stat(post.MediaDir())
assertions.AssertNot(t, os.IsNotExist(err), "Media directory not created")
2022-08-01 17:50:29 +00:00
}
func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
user := getTestUser()
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
posts, _ := user.Posts()
2022-08-17 19:57:55 +00:00
post, _ := user.GetPost(posts[0].Id())
meta := post.Meta()
2022-11-02 21:12:58 +00:00
assertions.AssertNot(t, meta.Date.IsZero(), "Date not set")
}
2022-07-20 17:35:31 +00:00
func TestCreateNewPostMultipleCalls(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 17:35:31 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
2022-10-13 19:00:28 +00:00
files, err := os.ReadDir(path.Join(user.Dir(), "public"))
assertions.AssertNoError(t, err, "Error reading directory")
2022-11-03 18:23:13 +00:00
assertions.AssertEqual(t, len(files), 3)
2022-07-20 17:35:31 +00:00
}
2022-07-20 19:12:18 +00:00
func TestCanListUserPosts(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-07-20 19:12:18 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
user.CreateNewPost("testpost", false)
2022-07-20 19:12:18 +00:00
posts, err := user.Posts()
assertions.AssertNoError(t, err, "Error reading posts")
assertions.AssertLen(t, posts, 3)
2022-07-20 19:12:18 +00:00
}
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
repo := getTestRepo(owl.RepoConfig{})
2022-07-23 15:19:47 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
2022-07-23 15:19:47 +00:00
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-17 19:57:55 +00:00
postIds := []string{}
for _, p := range posts {
postIds = append(postIds, p.Id())
}
if !contains(postIds, "foo") {
2022-08-03 16:03:10 +00:00
t.Error("Does not contain post: foo. Found:")
for _, p := range posts {
2022-08-17 19:57:55 +00:00
t.Error("\t" + p.Id())
2022-08-03 16:03:10 +00:00
}
}
2022-08-17 19:57:55 +00:00
if contains(postIds, "foo/bar") {
2022-08-03 16:03:10 +00:00
t.Error("Invalid post found: foo/bar. Found:")
2022-07-23 15:19:47 +00:00
for _, p := range posts {
2022-08-17 19:57:55 +00:00
t.Error("\t" + p.Id())
2022-07-23 15:19:47 +00:00
}
}
2022-08-03 16:03:10 +00:00
}
func TestCannotListUserPostsWithoutIndexMd(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-08-03 16:03:10 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
2022-08-03 16:03:10 +00:00
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()
2022-08-17 19:57:55 +00:00
postIds := []string{}
for _, p := range posts {
postIds = append(postIds, p.Id())
}
if contains(postIds, "foo") {
2022-08-03 16:03:10 +00:00
t.Error("Contains invalid post: foo. Found:")
2022-07-23 15:19:47 +00:00
for _, p := range posts {
2022-08-17 19:57:55 +00:00
t.Error("\t" + p.Id())
2022-07-23 15:19:47 +00:00
}
}
}
2022-08-20 20:35:51 +00:00
func TestListUserPostsDoesNotIncludeDrafts(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-08-20 20:35:51 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-08-20 20:35:51 +00:00
content := ""
content += "---\n"
content += "title: test\n"
content += "draft: true\n"
content += "---\n"
content += "\n"
content += "Write your post here.\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
assertions.AssertLen(t, posts, 0)
2022-08-22 06:01:44 +00:00
}
func TestListUsersDraftsExcludedRealWorld(t *testing.T) {
// Create a new user
repo := getTestRepo(owl.RepoConfig{})
2022-08-22 06:01:44 +00:00
user, _ := repo.CreateUser(randomUserName())
// Create a new post
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
2022-08-22 06:01:44 +00:00
content := ""
content += "---\n"
content += "title: Articles September 2019\n"
content += "author: h4kor\n"
content += "type: post\n"
content += "date: -001-11-30T00:00:00+00:00\n"
content += "draft: true\n"
content += "url: /?p=426\n"
content += "categories:\n"
content += " - Uncategorised\n"
content += "\n"
content += "---\n"
content += "<https://nesslabs.com/time-anxiety>\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
assertions.AssertLen(t, posts, 0)
2022-08-20 20:35:51 +00:00
}
2022-07-21 17:44:07 +00:00
func TestCanLoadPost(t *testing.T) {
user := getTestUser()
// Create a new post
2022-10-13 18:33:00 +00:00
user.CreateNewPost("testpost", false)
2022-07-21 17:44:07 +00:00
posts, _ := user.Posts()
2022-08-17 19:57:55 +00:00
post, _ := user.GetPost(posts[0].Id())
2022-11-03 18:23:13 +00:00
assertions.Assert(t, post.Title() == "testpost", "Post title is not correct")
2022-07-21 17:44:07 +00:00
}
2022-08-03 17:41:13 +00:00
func TestUserUrlPath(t *testing.T) {
user := getTestUser()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.UrlPath() == "/user/"+user.Name()+"/", "Wrong url path")
2022-08-03 17:41:13 +00:00
}
2022-08-13 16:47:27 +00:00
func TestUserFullUrl(t *testing.T) {
user := getTestUser()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.FullUrl() == "http://localhost:8080/user/"+user.Name()+"/", "Wrong url path")
2022-08-13 16:47:27 +00:00
}
2022-08-17 19:57:55 +00:00
func TestPostsSortedByPublishingDateLatestFirst(t *testing.T) {
user := getTestUser()
// Create a new post
2022-10-13 18:33:00 +00:00
post1, _ := user.CreateNewPost("testpost", false)
post2, _ := user.CreateNewPost("testpost2", false)
2022-08-17 19:57:55 +00:00
content := "---\n"
content += "title: Test Post\n"
content += "date: Wed, 17 Aug 2022 10:50:02 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test Post 2\n"
content += "date: Wed, 17 Aug 2022 20:50:06 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id")
assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id")
2022-08-17 19:57:55 +00:00
}
func TestPostsSortedByPublishingDateLatestFirst2(t *testing.T) {
user := getTestUser()
// Create a new post
posts := []*owl.Post{}
for i := 59; i >= 0; i-- {
2022-10-13 18:33:00 +00:00
post, _ := user.CreateNewPost("testpost", false)
content := "---\n"
content += "title: Test Post\n"
content += fmt.Sprintf("date: Wed, 17 Aug 2022 10:%02d:02 +0000\n", i)
content += "---\n"
content += "This is a test"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
2022-10-13 19:03:16 +00:00
posts = append(posts, post)
}
retPosts, _ := user.Posts()
for i, p := range retPosts {
2022-11-03 18:23:13 +00:00
assertions.Assert(t, p.Id() == posts[i].Id(), "Wrong Id")
}
}
2022-08-17 19:57:55 +00:00
func TestPostsSortedByPublishingDateBrokenAtBottom(t *testing.T) {
user := getTestUser()
// Create a new post
2022-10-13 18:33:00 +00:00
post1, _ := user.CreateNewPost("testpost", false)
post2, _ := user.CreateNewPost("testpost2", false)
2022-08-17 19:57:55 +00:00
content := "---\n"
content += "title: Test Post\n"
content += "date: Wed, 17 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post1.ContentFile(), []byte(content), 0644)
content = "---\n"
content += "title: Test Post 2\n"
content += "date: Wed, 17 Aug 2022 20:50:06 +0000\n"
content += "---\n"
content += "This is a test"
os.WriteFile(post2.ContentFile(), []byte(content), 0644)
posts, _ := user.Posts()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, posts[0].Id() == post2.Id(), "Wrong Id")
assertions.Assert(t, posts[1].Id() == post1.Id(), "Wrong Id")
2022-08-17 19:57:55 +00:00
}
2022-09-10 13:22:18 +00:00
func TestAvatarEmptyIfNotExist(t *testing.T) {
user := getTestUser()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.AvatarUrl() == "", "Avatar should be empty")
2022-09-10 13:22:18 +00:00
}
func TestAvatarSetIfFileExist(t *testing.T) {
user := getTestUser()
os.WriteFile(path.Join(user.MediaDir(), "avatar.png"), []byte("test"), 0644)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.AvatarUrl() != "", "Avatar should not be empty")
2022-09-10 13:22:18 +00:00
}
2022-10-13 18:58:16 +00:00
func TestPostNameIllegalFileName(t *testing.T) {
user := getTestUser()
_, err := user.CreateNewPost("testpost?///", false)
assertions.AssertNoError(t, err, "Should not have failed")
2022-10-13 18:58:16 +00:00
}
2022-11-01 20:14:03 +00:00
func TestFaviconIfNotExist(t *testing.T) {
user := getTestUser()
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.FaviconUrl() == "", "Favicon should be empty")
2022-11-01 20:14:03 +00:00
}
func TestFaviconSetIfFileExist(t *testing.T) {
user := getTestUser()
os.WriteFile(path.Join(user.MediaDir(), "favicon.ico"), []byte("test"), 0644)
2022-11-03 18:23:13 +00:00
assertions.Assert(t, user.FaviconUrl() != "", "Favicon should not be empty")
2022-11-01 20:14:03 +00:00
}
2022-11-03 19:25:53 +00:00
func TestResetUserPassword(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
assertions.Assert(t, user.Config().PassworHash != "", "Password Hash should not be empty")
assertions.Assert(t, user.Config().PassworHash != "test", "Password Hash should not be test")
}
2022-11-03 19:29:16 +00:00
func TestVerifyPassword(t *testing.T) {
user := getTestUser()
user.ResetPassword("test")
assertions.Assert(t, user.VerifyPassword("test"), "Password should be correct")
assertions.Assert(t, !user.VerifyPassword("test2"), "Password should be incorrect")
assertions.Assert(t, !user.VerifyPassword(""), "Password should be incorrect")
assertions.Assert(t, !user.VerifyPassword("Test"), "Password should be incorrect")
assertions.Assert(t, !user.VerifyPassword("TEST"), "Password should be incorrect")
assertions.Assert(t, !user.VerifyPassword("0000000"), "Password should be incorrect")
}