testing single user mode

This commit is contained in:
Niko Abeler 2022-08-05 22:04:03 +02:00
parent ecd5a056c8
commit 3f48211ffb
6 changed files with 121 additions and 17 deletions

View File

@ -32,7 +32,7 @@ func getTestRepo() owl.Repository {
return repo
}
func TestRepoIndexHandler(t *testing.T) {
func TestMultiUserRepoIndexHandler(t *testing.T) {
repo := getTestRepo()
repo.CreateUser("user_1")
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()
user, _ := repo.CreateUser("test-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()
user, _ := repo.CreateUser("test-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()
user, _ := repo.CreateUser("test-1")
post, _ := user.CreateNewPost("post-1")
// Create test media file
path := path.Join(post.MediaDir(), "data.txt")
println("Creating test media file:", path)
err := os.WriteFile(path, []byte("test"), 0644)
if err != nil {
t.Fatal(err)
}
// Create Request and Response
println(post.UrlMediaPath("data.txt"))
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
if err != nil {
t.Fatal(err)

View File

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

View File

@ -12,7 +12,7 @@ import (
)
type Post struct {
user User
user *User
id string
title string
}

View File

@ -68,7 +68,7 @@ func RenderPost(post Post) (string, error) {
Content: template.HTML(postHtml.String()),
}
return renderIntoBaseTemplate(post.user, data)
return renderIntoBaseTemplate(*post.user, data)
}
func RenderIndexPage(user User) (string, error) {

View File

@ -67,11 +67,19 @@ func OpenSingleUserRepo(name string, user_name string) (Repository, error) {
if err != nil {
return Repository{}, err
}
repo.single_user_mode = true
repo.active_user = user_name
user, err := repo.GetUser(user_name)
if err != nil {
return Repository{}, err
}
repo.SetSingleUser(user)
return repo, nil
}
func (repo *Repository) SetSingleUser(user User) {
repo.single_user_mode = true
repo.active_user = user.name
}
func (repo Repository) SingleUserName() string {
return repo.active_user
}
@ -107,18 +115,18 @@ func (repo Repository) Template() (string, error) {
func (repo Repository) Users() ([]User, error) {
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())
users := make([]User, len(userNames))
for i, name := range userNames {
users[i] = User{repo: repo, name: name}
users[i] = User{repo: &repo, name: name}
}
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}
// check if user already exists
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) {
user := User{repo: repo, name: name}
user := User{repo: &repo, name: name}
if !dirExists(user.Dir()) {
return User{}, fmt.Errorf("User does not exist")
}

View File

@ -11,7 +11,7 @@ import (
)
type User struct {
repo Repository
repo *Repository
name string
}
@ -60,7 +60,7 @@ func (user User) Posts() ([]string, error) {
}
func (user User) GetPost(id string) (Post, error) {
post := Post{user: user, id: id}
post := Post{user: &user, id: id}
_, metaData := post.MarkdownData()
title := metaData["title"]
post.title = fmt.Sprint(title)
@ -84,7 +84,7 @@ func (user User) CreateNewPost(title string) (Post, error) {
break
}
}
post := Post{user: user, id: folder_name, title: title}
post := Post{user: &user, id: folder_name, title: title}
initial_content := ""
initial_content += "---\n"