WIP more cleaning up tests

This commit is contained in:
Niko Abeler 2022-11-02 22:12:58 +01:00
parent c92ab958a6
commit 4aef1ca2ee
3 changed files with 33 additions and 43 deletions

View File

@ -6,7 +6,6 @@ import (
"os"
"path"
"strconv"
"strings"
"sync"
"testing"
"time"
@ -16,62 +15,42 @@ func TestCanGetPostTitle(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
result := post.Title()
if result != "testpost" {
t.Error("Wrong Title. Got: " + result)
}
assertions.AssertEqual(t, result, "testpost")
}
func TestMediaDir(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
result := post.MediaDir()
if result != path.Join(post.Dir(), "media") {
t.Error("Wrong MediaDir. Got: " + result)
}
assertions.AssertEqual(t, result, path.Join(post.Dir(), "media"))
}
func TestPostUrlPath(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/"
if !(post.UrlPath() == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
assertions.AssertEqual(t, post.UrlPath(), expected)
}
func TestPostFullUrl(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
expected := "http://localhost:8080/user/" + user.Name() + "/posts/" + post.Id() + "/"
if !(post.FullUrl() == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.FullUrl())
}
assertions.AssertEqual(t, post.FullUrl(), expected)
}
func TestPostUrlMediaPath(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/data.png"
if !(post.UrlMediaPath("data.png") == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
assertions.AssertEqual(t, post.UrlMediaPath("data.png"), expected)
}
func TestPostUrlMediaPathWithSubDir(t *testing.T) {
user := getTestUser()
post, _ := user.CreateNewPost("testpost", false)
expected := "/user/" + user.Name() + "/posts/" + post.Id() + "/media/foo/data.png"
if !(post.UrlMediaPath("foo/data.png") == expected) {
t.Error("Wrong url path")
t.Error("Expected: " + expected)
t.Error(" Got: " + post.UrlPath())
}
assertions.AssertEqual(t, post.UrlMediaPath("foo/data.png"), expected)
}
func TestDraftInMetaData(t *testing.T) {
@ -85,10 +64,7 @@ func TestDraftInMetaData(t *testing.T) {
content += "Write your post here.\n"
os.WriteFile(post.ContentFile(), []byte(content), 0644)
meta := post.Meta()
if !meta.Draft {
t.Error("Draft should be true")
}
assertions.AssertEqual(t, meta.Draft, true)
}
func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
@ -104,9 +80,7 @@ func TestNoRawHTMLIfDisallowedByRepo(t *testing.T) {
os.WriteFile(post.ContentFile(), []byte(content), 0644)
html := post.RenderedContent()
html_str := html.String()
if strings.Contains(html_str, "<script>") {
t.Error("HTML should not be allowed")
}
assertions.AssertNotContains(t, html_str, "<script>")
}
func TestRawHTMLIfAllowedByRepo(t *testing.T) {

View File

@ -6,6 +6,20 @@ import (
"testing"
)
func Assert(t *testing.T, condition bool, message string) {
t.Helper()
if !condition {
t.Errorf(message)
}
}
func AssertNot(t *testing.T, condition bool, message string) {
t.Helper()
if condition {
t.Errorf(message)
}
}
func AssertContains(t *testing.T, containing string, search string) {
t.Helper()
if !strings.Contains(containing, search) {
@ -13,6 +27,13 @@ func AssertContains(t *testing.T, containing string, search string) {
}
}
func AssertNotContains(t *testing.T, containing string, search string) {
t.Helper()
if strings.Contains(containing, search) {
t.Errorf("Expected '%s' to not contain '%s'", containing, search)
}
}
func AssertNoError(t *testing.T, err error, message string) {
t.Helper()
if err != nil {

View File

@ -17,9 +17,7 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) {
user.CreateNewPost("testpost", false)
files, err := os.ReadDir(path.Join(user.Dir(), "public"))
assertions.AssertNoError(t, err, "Error reading directory")
if len(files) < 1 {
t.Error("Post not created")
}
assertions.AssertLen(t, files, 1)
}
func TestCreateNewPostCreatesMediaDir(t *testing.T) {
@ -28,9 +26,8 @@ func TestCreateNewPostCreatesMediaDir(t *testing.T) {
user, _ := repo.CreateUser(randomUserName())
// Create a new post
post, _ := user.CreateNewPost("testpost", false)
if _, err := os.Stat(post.MediaDir()); os.IsNotExist(err) {
t.Error("Media directory not created")
}
_, err := os.Stat(post.MediaDir())
assertions.AssertNot(t, os.IsNotExist(err), "Media directory not created")
}
func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
@ -40,9 +37,7 @@ func TestCreateNewPostAddsDateToMetaBlock(t *testing.T) {
posts, _ := user.Posts()
post, _ := user.GetPost(posts[0].Id())
meta := post.Meta()
if meta.Date.IsZero() {
t.Errorf("Found no date. Got: %v", meta.Date)
}
assertions.AssertNot(t, meta.Date.IsZero(), "Date not set")
}
func TestCreateNewPostMultipleCalls(t *testing.T) {