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

View File

@ -6,6 +6,20 @@ import (
"testing" "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) { func AssertContains(t *testing.T, containing string, search string) {
t.Helper() t.Helper()
if !strings.Contains(containing, search) { 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) { func AssertNoError(t *testing.T, err error, message string) {
t.Helper() t.Helper()
if err != nil { if err != nil {

View File

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