testing single user mode
This commit is contained in:
parent
ecd5a056c8
commit
3f48211ffb
|
@ -32,7 +32,7 @@ func getTestRepo() owl.Repository {
|
||||||
return repo
|
return repo
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepoIndexHandler(t *testing.T) {
|
func TestMultiUserRepoIndexHandler(t *testing.T) {
|
||||||
repo := getTestRepo()
|
repo := getTestRepo()
|
||||||
repo.CreateUser("user_1")
|
repo.CreateUser("user_1")
|
||||||
repo.CreateUser("user_2")
|
repo.CreateUser("user_2")
|
||||||
|
@ -63,7 +63,7 @@ func TestRepoIndexHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserIndexHandler(t *testing.T) {
|
func TestMultiUserUserIndexHandler(t *testing.T) {
|
||||||
repo := getTestRepo()
|
repo := getTestRepo()
|
||||||
user, _ := repo.CreateUser("test-1")
|
user, _ := repo.CreateUser("test-1")
|
||||||
user.CreateNewPost("post-1")
|
user.CreateNewPost("post-1")
|
||||||
|
@ -90,7 +90,7 @@ func TestUserIndexHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostHandler(t *testing.T) {
|
func TestMultiUserPostHandler(t *testing.T) {
|
||||||
repo := getTestRepo()
|
repo := getTestRepo()
|
||||||
user, _ := repo.CreateUser("test-1")
|
user, _ := repo.CreateUser("test-1")
|
||||||
post, _ := user.CreateNewPost("post-1")
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
@ -111,21 +111,19 @@ func TestPostHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPostMediaHandler(t *testing.T) {
|
func TestMultiUserPostMediaHandler(t *testing.T) {
|
||||||
repo := getTestRepo()
|
repo := getTestRepo()
|
||||||
user, _ := repo.CreateUser("test-1")
|
user, _ := repo.CreateUser("test-1")
|
||||||
post, _ := user.CreateNewPost("post-1")
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
// Create test media file
|
// Create test media file
|
||||||
path := path.Join(post.MediaDir(), "data.txt")
|
path := path.Join(post.MediaDir(), "data.txt")
|
||||||
println("Creating test media file:", path)
|
|
||||||
err := os.WriteFile(path, []byte("test"), 0644)
|
err := os.WriteFile(path, []byte("test"), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Request and Response
|
// Create Request and Response
|
||||||
println(post.UrlMediaPath("data.txt"))
|
|
||||||
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
|
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
|
@ -0,0 +1,98 @@
|
||||||
|
package main_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
owl "h4kor/owl-blogs"
|
||||||
|
main "h4kor/owl-blogs/cmd/owl-web"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getSingleUserTestRepo() (owl.Repository, owl.User) {
|
||||||
|
repo, _ := owl.CreateRepository(testRepoName())
|
||||||
|
user, _ := repo.CreateUser("test-1")
|
||||||
|
repo.SetSingleUser(user)
|
||||||
|
return repo, user
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleUserUserIndexHandler(t *testing.T) {
|
||||||
|
repo, user := getSingleUserTestRepo()
|
||||||
|
user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", user.UrlPath(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.SingleUserRouter(repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the response body contains names of users
|
||||||
|
if !strings.Contains(rr.Body.String(), "post-1") {
|
||||||
|
t.Error("post-1 not listed on index page. Got: ")
|
||||||
|
t.Error(rr.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleUserPostHandler(t *testing.T) {
|
||||||
|
repo, user := getSingleUserTestRepo()
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", post.UrlPath(), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.SingleUserRouter(repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSingleUserPostMediaHandler(t *testing.T) {
|
||||||
|
repo, user := getSingleUserTestRepo()
|
||||||
|
post, _ := user.CreateNewPost("post-1")
|
||||||
|
|
||||||
|
// Create test media file
|
||||||
|
path := path.Join(post.MediaDir(), "data.txt")
|
||||||
|
err := os.WriteFile(path, []byte("test"), 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Request and Response
|
||||||
|
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
router := main.SingleUserRouter(repo)
|
||||||
|
router.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
// Check the status code is what we expect.
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
||||||
|
status, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the response body contains data of media file
|
||||||
|
if !(rr.Body.String() == "test") {
|
||||||
|
t.Error("Got wrong media file content. Expected 'test' Got: ")
|
||||||
|
t.Error(rr.Body.String())
|
||||||
|
}
|
||||||
|
}
|
2
post.go
2
post.go
|
@ -12,7 +12,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
user User
|
user *User
|
||||||
id string
|
id string
|
||||||
title string
|
title string
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ func RenderPost(post Post) (string, error) {
|
||||||
Content: template.HTML(postHtml.String()),
|
Content: template.HTML(postHtml.String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderIntoBaseTemplate(post.user, data)
|
return renderIntoBaseTemplate(*post.user, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderIndexPage(user User) (string, error) {
|
func RenderIndexPage(user User) (string, error) {
|
||||||
|
|
|
@ -67,11 +67,19 @@ func OpenSingleUserRepo(name string, user_name string) (Repository, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Repository{}, err
|
return Repository{}, err
|
||||||
}
|
}
|
||||||
repo.single_user_mode = true
|
user, err := repo.GetUser(user_name)
|
||||||
repo.active_user = user_name
|
if err != nil {
|
||||||
|
return Repository{}, err
|
||||||
|
}
|
||||||
|
repo.SetSingleUser(user)
|
||||||
return repo, nil
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repo *Repository) SetSingleUser(user User) {
|
||||||
|
repo.single_user_mode = true
|
||||||
|
repo.active_user = user.name
|
||||||
|
}
|
||||||
|
|
||||||
func (repo Repository) SingleUserName() string {
|
func (repo Repository) SingleUserName() string {
|
||||||
return repo.active_user
|
return repo.active_user
|
||||||
}
|
}
|
||||||
|
@ -107,18 +115,18 @@ func (repo Repository) Template() (string, error) {
|
||||||
|
|
||||||
func (repo Repository) Users() ([]User, error) {
|
func (repo Repository) Users() ([]User, error) {
|
||||||
if repo.single_user_mode {
|
if repo.single_user_mode {
|
||||||
return []User{{repo: repo, name: repo.active_user}}, nil
|
return []User{{repo: &repo, name: repo.active_user}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
userNames := listDir(repo.UsersDir())
|
userNames := listDir(repo.UsersDir())
|
||||||
users := make([]User, len(userNames))
|
users := make([]User, len(userNames))
|
||||||
for i, name := range userNames {
|
for i, name := range userNames {
|
||||||
users[i] = User{repo: repo, name: name}
|
users[i] = User{repo: &repo, name: name}
|
||||||
}
|
}
|
||||||
return users, nil
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo Repository) CreateUser(name string) (User, error) {
|
func (repo *Repository) CreateUser(name string) (User, error) {
|
||||||
new_user := User{repo: repo, name: name}
|
new_user := User{repo: repo, name: name}
|
||||||
// check if user already exists
|
// check if user already exists
|
||||||
if dirExists(new_user.Dir()) {
|
if dirExists(new_user.Dir()) {
|
||||||
|
@ -147,7 +155,7 @@ func (repo Repository) CreateUser(name string) (User, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo Repository) GetUser(name string) (User, error) {
|
func (repo Repository) GetUser(name string) (User, error) {
|
||||||
user := User{repo: repo, name: name}
|
user := User{repo: &repo, name: name}
|
||||||
if !dirExists(user.Dir()) {
|
if !dirExists(user.Dir()) {
|
||||||
return User{}, fmt.Errorf("User does not exist")
|
return User{}, fmt.Errorf("User does not exist")
|
||||||
}
|
}
|
||||||
|
|
6
user.go
6
user.go
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
repo Repository
|
repo *Repository
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ func (user User) Posts() ([]string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (user User) GetPost(id string) (Post, error) {
|
func (user User) GetPost(id string) (Post, error) {
|
||||||
post := Post{user: user, id: id}
|
post := Post{user: &user, id: id}
|
||||||
_, metaData := post.MarkdownData()
|
_, metaData := post.MarkdownData()
|
||||||
title := metaData["title"]
|
title := metaData["title"]
|
||||||
post.title = fmt.Sprint(title)
|
post.title = fmt.Sprint(title)
|
||||||
|
@ -84,7 +84,7 @@ func (user User) CreateNewPost(title string) (Post, error) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
post := Post{user: user, id: folder_name, title: title}
|
post := Post{user: &user, id: folder_name, title: title}
|
||||||
|
|
||||||
initial_content := ""
|
initial_content := ""
|
||||||
initial_content += "---\n"
|
initial_content += "---\n"
|
||||||
|
|
Loading…
Reference in New Issue