From 760241d965a47f8382bb81409226920a9e3ae41b Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Tue, 19 Jul 2022 21:25:58 +0200 Subject: [PATCH] create posts --- .gitignore | 1 + main.go | 29 ++++++++++++++++++ main_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae1c989 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +users/ diff --git a/main.go b/main.go index d06bd42..e5f91bb 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,8 @@ package main import ( "os" "path" + "time" + "fmt" ) func CreateNewUser(repo string, name string) { @@ -13,10 +15,37 @@ func CreateNewUser(repo string, name string) { user_dir := path.Join(repo, name) os.Mkdir(user_dir, 0755) os.Mkdir(path.Join(user_dir, "meta"), 0755) + // create public folder + os.Mkdir(path.Join(user_dir, "public"), 0755) + + // create Meta files os.WriteFile(path.Join(user_dir, "meta", "VERSION"), []byte("0.0.1"), 0644) os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte("<{{content}}/body>"), 0644) } +func CreateNewPost(repo string, user string, title string) { + timestamp := time.Now().UTC().Unix() + folder_name := fmt.Sprintf("%d-%s", timestamp, title) + post_dir := path.Join(repo, user, "public", folder_name) + + // if post already exists, add -n to the end of the name + i := 0 + for { + if _, err := os.Stat(post_dir); err == nil { + i++ + folder_name = fmt.Sprintf("%d-%s-%d", timestamp, title, i) + post_dir = path.Join(repo, user, "public", folder_name) + } else { + break + } + } + + initial_content := "# " + title + // create post file + os.Mkdir(post_dir, 0755) + os.WriteFile(path.Join(post_dir, "index.md"), []byte(initial_content), 0644) +} + func main() { println("KISS Social") println("Commands") diff --git a/main_test.go b/main_test.go index 1e15085..a4379df 100644 --- a/main_test.go +++ b/main_test.go @@ -1,31 +1,101 @@ package main_test import ( - "os" "testing" + "os" + "fmt" + "path" + "time" + "io/ioutil" + "math/rand" "h4kor/kiss-social" ) + +func testRepo() string { + return "/tmp/test" +} + +func randomUserName() string { + rand.Seed(time.Now().UnixNano()) + var letters = []rune("abcdefghijklmnopqrstuvwxyz") + b := make([]rune, 8) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + return string(b) +} + func TestCanCreateANewUser(t *testing.T) { // Create a new user - main.CreateNewUser("/tmp/test", "testuser") - if _, err := os.Stat("/tmp/test/testuser"); err != nil { + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + if _, err := os.Stat(path.Join(repo, user, "")); err != nil { t.Error("User directory not created") } } func TestCreateUserAddsVersionFile(t *testing.T) { // Create a new user - main.CreateNewUser("/tmp/test", "testuser") - if _, err := os.Stat("/tmp/test/testuser/meta/VERSION"); err != nil { + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + if _, err := os.Stat(path.Join(repo, user, "/meta/VERSION")); err != nil { t.Error("Version file not created") } } func TestCreateUserAddsBaseHtmlFile(t *testing.T) { // Create a new user - main.CreateNewUser("/tmp/test", "testuser") - if _, err := os.Stat("/tmp/test/testuser/meta/base.html"); err != nil { + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + if _, err := os.Stat(path.Join(repo, user, "/meta/base.html")); err != nil { t.Error("Base html file not created") } +} + +func TestCreateUserAddsPublicFolder(t *testing.T) { + // Create a new user + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + if _, err := os.Stat(path.Join(repo, user, "/public")); err != nil { + t.Error("Public folder not created") + } +} + +func TestCreateNewPostCreatesEntryInPublic(t *testing.T) { + // Create a new user + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + // Create a new post + main.CreateNewPost(repo, user, "testpost") + files, err := ioutil.ReadDir(path.Join(repo, user, "public")) + if err != nil { + t.Error("Error reading directory") + } + if len(files) < 1 { + t.Error("Post not created") + } +} + +func TestCreateNewPostMultipleCalls(t *testing.T) { + // Create a new user + repo := testRepo() + user := randomUserName() + main.CreateNewUser(repo, user) + // Create a new post + main.CreateNewPost(repo, user, "testpost") + main.CreateNewPost(repo, user, "testpost") + main.CreateNewPost(repo, user, "testpost") + files, err := ioutil.ReadDir(path.Join(repo, user, "public")) + if err != nil { + t.Error("Error reading directory") + } + if len(files) < 3 { + t.Error(fmt.Sprintf("Only %d posts created", len(files))) + } } \ No newline at end of file