2022-07-19 18:41:35 +00:00
|
|
|
package main_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-07-19 19:25:58 +00:00
|
|
|
"os"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2022-07-19 18:41:35 +00:00
|
|
|
"h4kor/kiss-social"
|
|
|
|
)
|
|
|
|
|
2022-07-19 19:25:58 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-19 18:41:35 +00:00
|
|
|
func TestCanCreateANewUser(t *testing.T) {
|
|
|
|
// Create a new user
|
2022-07-19 19:25:58 +00:00
|
|
|
repo := testRepo()
|
|
|
|
user := randomUserName()
|
|
|
|
main.CreateNewUser(repo, user)
|
|
|
|
if _, err := os.Stat(path.Join(repo, user, "")); err != nil {
|
2022-07-19 18:41:35 +00:00
|
|
|
t.Error("User directory not created")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUserAddsVersionFile(t *testing.T) {
|
|
|
|
// Create a new user
|
2022-07-19 19:25:58 +00:00
|
|
|
repo := testRepo()
|
|
|
|
user := randomUserName()
|
|
|
|
main.CreateNewUser(repo, user)
|
|
|
|
if _, err := os.Stat(path.Join(repo, user, "/meta/VERSION")); err != nil {
|
2022-07-19 18:41:35 +00:00
|
|
|
t.Error("Version file not created")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateUserAddsBaseHtmlFile(t *testing.T) {
|
|
|
|
// Create a new user
|
2022-07-19 19:25:58 +00:00
|
|
|
repo := testRepo()
|
|
|
|
user := randomUserName()
|
|
|
|
main.CreateNewUser(repo, user)
|
|
|
|
if _, err := os.Stat(path.Join(repo, user, "/meta/base.html")); err != nil {
|
2022-07-19 18:41:35 +00:00
|
|
|
t.Error("Base html file not created")
|
|
|
|
}
|
2022-07-19 19:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)))
|
|
|
|
}
|
2022-07-19 18:41:35 +00:00
|
|
|
}
|