package main import ( "fmt" "io/ioutil" "gitea.inhome.blapointe.com/local/notes-server/config" "gitea.inhome.blapointe.com/local/notes-server/server" "log" "net/http" "net/http/httptest" "os" "path" "regexp" "strings" "testing" ) func TestAll(t *testing.T) { for _, basedir := range []string{os.TempDir(), "./tempDir"} { os.MkdirAll(basedir, os.ModePerm) makeFiles(t, basedir) if basedir[0] == '.' && config.Root[0] != '.' { config.Root = "./" + config.Root } defer os.RemoveAll(config.Root) log.Println(config.Root) t.Log("trying with root", config.Root) s := makeServer(t) defer s.Close() testServer(t, s.URL) if basedir[0] == '.' { os.RemoveAll(basedir) } } } func makeFiles(t *testing.T, basedir string) { d, err := ioutil.TempDir(basedir, "pattern*") if err != nil { t.Fatal(err) } config.Root = d for _, dir := range []string{"dirA", "dirB", "."} { if err := os.MkdirAll(path.Join(d, dir), os.ModePerm); err != nil { t.Fatal(err) } for _, file := range []string{"fileA", "fileB"} { content := fmt.Sprintf("hello from %s/%s/%s", d, dir, file) err := ioutil.WriteFile( path.Join(d, dir, file), []byte(content), os.ModePerm, ) if err != nil { t.Fatal(err) } } } } func makeServer(t *testing.T) *httptest.Server { s := server.New() if err := s.Routes(); err != nil { t.Fatal(err) } return httptest.NewServer(s) } func testServer(t *testing.T, url string) { testCreate(t, url) testEdit(t, url) testDir(t, url) testFile(t, url) testNavRootDir(t, url) testNavRootFile(t, url) testNavDirFile(t, url) } func testCreate(t *testing.T, url string) { for _, path := range []string{"dirX/fileX", "fileX"} { resp, err := http.Get(url + "/create/" + path) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatal(resp.StatusCode) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasMultilink(s, path); !ok { t.Error(ok) } if ok := assertHasForm(s, "/submit/"+path); !ok { t.Error(ok) } if ok := assertHasTextArea(s); !ok { t.Error(ok) } if ok := assertHasSubmit(s); !ok { t.Error(ok) } } } func testEdit(t *testing.T, url string) { for _, path := range []string{"dirX/fileX", "fileX"} { resp, err := http.Get(url + "/edit/" + path) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatal(resp.StatusCode) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasMultilink(s, path); !ok { t.Error(ok) } if ok := assertHasForm(s); !ok { t.Error(ok) } if ok := assertHasTextArea(s); !ok { t.Error(ok) } if ok := assertHasSubmit(s); !ok { t.Error(ok) } } } func testDir(t *testing.T, url string) { path := url + "/notes/dirA" resp, err := http.Get(path) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatal(resp.StatusCode) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasMultilink(s, "/notes/dirA"); !ok { t.Error(ok) } if ok := assertHasForm(s, "/create/dirA"); !ok { t.Error(ok) } if ok := assertHasSubmit(s); !ok { t.Error(ok) } } func testFile(t *testing.T, url string) { path := url + "/notes/dirA/fileA" resp, err := http.Get(path) if err != nil { t.Fatal(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatal(resp.StatusCode) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasMultilink(s, "/notes/dirA", "/notes/dirA/fileA"); !ok { t.Error(ok) } if ok := assertHasHref(s, "/edit/dirA/fileA"); !ok { t.Error(ok) } } func testNavRootDir(t *testing.T, url string) { resp, err := http.Get(url + "/notes") if err != nil { t.Fatal(err) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasHref(s, "/notes/dirA"); !ok { t.Fatal(s) } } func testNavRootFile(t *testing.T, url string) { resp, err := http.Get(url + "/notes") if err != nil { t.Fatal(err) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasHref(s, "/notes/fileA"); !ok { t.Fatal(s) } } func testNavDirFile(t *testing.T, url string) { resp, err := http.Get(url + "/notes/dirA") if err != nil { t.Fatal(err) } b, _ := ioutil.ReadAll(resp.Body) s := string(b) if ok := assertHasHref(s, "/notes/dirA/fileA"); !ok { t.Fatal(s) } } func assertHasMultilink(body string, segments ...string) bool { if !strings.Contains(body, `/notes/`) { return false } for i := range segments { segments[i] = "/notes/" + strings.TrimPrefix(segments[i], "/notes/") } return assertHasHref(body, segments...) } func assertHasForm(body string, action ...string) bool { return strings.Contains(body, `