From 9d14e1a8fcabe9f5685f78da70901755908bcf2f Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Mon, 1 Aug 2022 19:50:29 +0200 Subject: [PATCH] creating media dir --- post.go | 4 ++++ post_test.go | 14 +++++++++++++- user.go | 2 ++ user_test.go | 11 +++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/post.go b/post.go index b457b42..ddeb697 100644 --- a/post.go +++ b/post.go @@ -21,6 +21,10 @@ func (post Post) Dir() string { return path.Join(post.user.Dir(), "public", post.id) } +func (post Post) MediaDir() string { + return path.Join(post.Dir(), "media") +} + func (post Post) UrlPath() string { return post.user.Path() + "/" + post.id } diff --git a/post_test.go b/post_test.go index 8c38957..8465582 100644 --- a/post_test.go +++ b/post_test.go @@ -1,6 +1,9 @@ package kiss_test -import "testing" +import ( + "path" + "testing" +) func TestCanGetPostTitle(t *testing.T) { user := getTestUser() @@ -10,3 +13,12 @@ func TestCanGetPostTitle(t *testing.T) { t.Error("Wrong Title. Got: " + result) } } + +func TestMediaDir(t *testing.T) { + user := getTestUser() + post, _ := user.CreateNewPost("testpost") + result := post.MediaDir() + if result != path.Join(post.Dir(), "media") { + t.Error("Wrong MediaDir. Got: " + result) + } +} diff --git a/user.go b/user.go index 374adb9..7ec8c6d 100644 --- a/user.go +++ b/user.go @@ -94,6 +94,8 @@ func (user User) CreateNewPost(title string) (Post, error) { // create post file os.Mkdir(post_dir, 0755) os.WriteFile(post.ContentFile(), []byte(initial_content), 0644) + // create media dir + os.Mkdir(post.MediaDir(), 0755) return post, nil } diff --git a/user_test.go b/user_test.go index b531996..a60527e 100644 --- a/user_test.go +++ b/user_test.go @@ -24,6 +24,17 @@ func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { } } +func TestCreateNewPostCreatesMediaDir(t *testing.T) { + // Create a new user + repo, _ := kiss.CreateRepository(testRepoName()) + 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 TestCreateNewPostMultipleCalls(t *testing.T) { // Create a new user repo, _ := kiss.CreateRepository(testRepoName())