create posts
This commit is contained in:
parent
59a5992114
commit
760241d965
|
@ -0,0 +1 @@
|
||||||
|
users/
|
29
main.go
29
main.go
|
@ -3,6 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"time"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateNewUser(repo string, name string) {
|
func CreateNewUser(repo string, name string) {
|
||||||
|
@ -13,10 +15,37 @@ func CreateNewUser(repo string, name string) {
|
||||||
user_dir := path.Join(repo, name)
|
user_dir := path.Join(repo, name)
|
||||||
os.Mkdir(user_dir, 0755)
|
os.Mkdir(user_dir, 0755)
|
||||||
os.Mkdir(path.Join(user_dir, "meta"), 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", "VERSION"), []byte("0.0.1"), 0644)
|
||||||
os.WriteFile(path.Join(user_dir, "meta", "base.html"), []byte("<html><body><{{content}}/body></html>"), 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() {
|
func main() {
|
||||||
println("KISS Social")
|
println("KISS Social")
|
||||||
println("Commands")
|
println("Commands")
|
||||||
|
|
84
main_test.go
84
main_test.go
|
@ -1,31 +1,101 @@
|
||||||
package main_test
|
package main_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
"path"
|
||||||
|
"time"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"h4kor/kiss-social"
|
"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) {
|
func TestCanCreateANewUser(t *testing.T) {
|
||||||
// Create a new user
|
// Create a new user
|
||||||
main.CreateNewUser("/tmp/test", "testuser")
|
repo := testRepo()
|
||||||
if _, err := os.Stat("/tmp/test/testuser"); err != nil {
|
user := randomUserName()
|
||||||
|
main.CreateNewUser(repo, user)
|
||||||
|
if _, err := os.Stat(path.Join(repo, user, "")); err != nil {
|
||||||
t.Error("User directory not created")
|
t.Error("User directory not created")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateUserAddsVersionFile(t *testing.T) {
|
func TestCreateUserAddsVersionFile(t *testing.T) {
|
||||||
// Create a new user
|
// Create a new user
|
||||||
main.CreateNewUser("/tmp/test", "testuser")
|
repo := testRepo()
|
||||||
if _, err := os.Stat("/tmp/test/testuser/meta/VERSION"); err != nil {
|
user := randomUserName()
|
||||||
|
main.CreateNewUser(repo, user)
|
||||||
|
if _, err := os.Stat(path.Join(repo, user, "/meta/VERSION")); err != nil {
|
||||||
t.Error("Version file not created")
|
t.Error("Version file not created")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
|
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
|
||||||
// Create a new user
|
// Create a new user
|
||||||
main.CreateNewUser("/tmp/test", "testuser")
|
repo := testRepo()
|
||||||
if _, err := os.Stat("/tmp/test/testuser/meta/base.html"); err != nil {
|
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")
|
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)))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue