2022-09-05 18:34:24 +00:00
|
|
|
package web_test
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"h4kor/owl-blogs"
|
2022-09-05 18:34:24 +00:00
|
|
|
main "h4kor/owl-blogs/cmd/owl/web"
|
2022-11-02 21:02:49 +00:00
|
|
|
"h4kor/owl-blogs/priv/assertions"
|
2022-08-03 17:41:13 +00:00
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func randomName() 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 testRepoName() string {
|
|
|
|
return "/tmp/" + randomName()
|
|
|
|
}
|
|
|
|
|
2022-09-05 18:50:46 +00:00
|
|
|
func getTestRepo(config owl.RepoConfig) owl.Repository {
|
|
|
|
repo, _ := owl.CreateRepository(testRepoName(), config)
|
2022-08-03 17:41:13 +00:00
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
2022-08-05 20:04:03 +00:00
|
|
|
func TestMultiUserRepoIndexHandler(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-03 17:41:13 +00:00
|
|
|
repo.CreateUser("user_1")
|
|
|
|
repo.CreateUser("user_2")
|
|
|
|
|
|
|
|
// Create Request and Response
|
|
|
|
req, err := http.NewRequest("GET", "/", nil)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error creating request")
|
2022-08-03 17:41:13 +00:00
|
|
|
rr := httptest.NewRecorder()
|
2022-08-06 18:22:09 +00:00
|
|
|
router := main.Router(&repo)
|
2022-08-03 17:41:13 +00:00
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusOK)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Check the response body contains names of users
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertContains(t, rr.Body.String(), "user_1")
|
|
|
|
assertions.AssertContains(t, rr.Body.String(), "user_2")
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 20:04:03 +00:00
|
|
|
func TestMultiUserUserIndexHandler(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-03 17:41:13 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
user.CreateNewPost("post-1", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Create Request and Response
|
|
|
|
req, err := http.NewRequest("GET", user.UrlPath(), nil)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error creating request")
|
2022-08-03 17:41:13 +00:00
|
|
|
rr := httptest.NewRecorder()
|
2022-08-06 18:22:09 +00:00
|
|
|
router := main.Router(&repo)
|
2022-08-03 17:41:13 +00:00
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusOK)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Check the response body contains names of users
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertContains(t, rr.Body.String(), "post-1")
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 20:04:03 +00:00
|
|
|
func TestMultiUserPostHandler(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-03 17:41:13 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Create Request and Response
|
|
|
|
req, err := http.NewRequest("GET", post.UrlPath(), nil)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error creating request")
|
2022-08-03 17:41:13 +00:00
|
|
|
rr := httptest.NewRecorder()
|
2022-08-06 18:22:09 +00:00
|
|
|
router := main.Router(&repo)
|
2022-08-03 17:41:13 +00:00
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusOK)
|
2022-08-03 17:41:13 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 20:04:03 +00:00
|
|
|
func TestMultiUserPostMediaHandler(t *testing.T) {
|
2022-09-05 18:50:46 +00:00
|
|
|
repo := getTestRepo(owl.RepoConfig{})
|
2022-08-03 17:41:13 +00:00
|
|
|
user, _ := repo.CreateUser("test-1")
|
2022-10-13 18:33:00 +00:00
|
|
|
post, _ := user.CreateNewPost("post-1", false)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Create test media file
|
|
|
|
path := path.Join(post.MediaDir(), "data.txt")
|
|
|
|
err := os.WriteFile(path, []byte("test"), 0644)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error creating request")
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// Create Request and Response
|
|
|
|
req, err := http.NewRequest("GET", post.UrlMediaPath("data.txt"), nil)
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertNoError(t, err, "Error creating request")
|
2022-08-03 17:41:13 +00:00
|
|
|
rr := httptest.NewRecorder()
|
2022-08-06 18:22:09 +00:00
|
|
|
router := main.Router(&repo)
|
2022-08-03 17:41:13 +00:00
|
|
|
router.ServeHTTP(rr, req)
|
|
|
|
|
2022-11-02 21:02:49 +00:00
|
|
|
assertions.AssertStatus(t, rr, http.StatusOK)
|
2022-08-03 17:41:13 +00:00
|
|
|
|
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
}
|