create posts

This commit is contained in:
Niko Abeler 2022-07-19 21:25:58 +02:00
parent 59a5992114
commit 760241d965
3 changed files with 107 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
users/

29
main.go
View File

@ -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("<html><body><{{content}}/body></html>"), 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")

View File

@ -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)))
}
}