diff --git a/app_test.go b/app_test.go index b2f4766..c6edb76 100644 --- a/app_test.go +++ b/app_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "net/http" "net/http/httptest" "testing" @@ -12,9 +13,14 @@ import ( const Cookie = "Set-Cookie" func client(method, path, cookie string) *httptest.ResponseRecorder { + return request(method, path, cookie, "") +} + +func request(method, path, cookie string, body string) *httptest.ResponseRecorder { gin.SetMode("test") app := NewApp() - req, _ := http.NewRequest(method, path, nil) + payload := bytes.NewBufferString(body) + req, _ := http.NewRequest(method, path, payload) if len(cookie) != 0 { req.Header.Set("Cookie", cookie) } @@ -41,7 +47,7 @@ func Test(t *testing.T) { }) g.It("Should return 200 on PUT /slides.md ", func() { - w := client("PUT", "/slides.md", cookie) + w := request("PUT", "/slides.md", cookie, "whatever") g.Assert(w.Code).Equal(200) }) @@ -51,7 +57,7 @@ func Test(t *testing.T) { }) g.It("Should return specific slide in preview", func() { - w := client("GET", "/published/shy-cell.md", cookie) + w := client("GET", "/published/slides/shy-cell.md", cookie) g.Assert(w.Code).Equal(200) }) @@ -60,6 +66,16 @@ func Test(t *testing.T) { g.Assert(w.Code).Equal(200) }) + g.It("Should return specific slide in preview without session", func() { + w := client("GET", "/published/slides/shy-cell.md", "") + g.Assert(w.Code).Equal(200) + }) + + g.It("Should return specific slide in edit mode without session", func() { + w := client("GET", "/stash/edit/shy-cell.md", "") + g.Assert(w.Code).Equal(200) + }) + g.It("Should works") }) diff --git a/main.go b/main.go index 4c37334..87052bf 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "io/ioutil" - "net/http" "os" "strings" @@ -22,7 +21,7 @@ func NewApp() *gin.Engine { store := sessions.NewCookieStore([]byte("secret")) r.Use(sessions.Sessions(SessionHeader, store)) - r.LoadHTMLGlob("templates/index.tmpl") + r.LoadHTMLGlob("templates/*.tmpl") r.Static("/static", "./static") r.GET("/", func(c *gin.Context) { @@ -43,7 +42,7 @@ func NewApp() *gin.Engine { session.Set("name", path) session.Save() - c.HTML(200, "users/index.tmpl", gin.H{ + c.HTML(200, "index.tmpl", gin.H{ "pubTo": path, }) }) @@ -133,12 +132,12 @@ func NewApp() *gin.Engine { session.Set("name", path) session.Save() - c.HTML(200, "users/index.tmpl", gin.H{ + c.HTML(200, "index.tmpl", gin.H{ "pubTo": path, }) }) - r.GET("/published/:name", func(c *gin.Context) { + r.GET("/published/slides/:name", func(c *gin.Context) { name := c.Param("name") log.WithFields(log.Fields{ @@ -152,7 +151,9 @@ func NewApp() *gin.Engine { session := sessions.Default(c) session.Set("name", path) session.Save() - c.Redirect(http.StatusMovedPermanently, "static/preview") + c.HTML(200, "slides.tmpl", gin.H{ + "pubTo": path, + }) }) return r diff --git a/templates/index.tmpl b/templates/index.tmpl index f3dd8a4..496411f 100644 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -1,4 +1,4 @@ -{{ define "users/index.tmpl" }} +{{ define "index.tmpl" }} @@ -14,7 +14,7 @@
diff --git a/static/slides.html b/templates/slides.tmpl similarity index 98% rename from static/slides.html rename to templates/slides.tmpl index 07fabf8..719ab66 100644 --- a/static/slides.html +++ b/templates/slides.tmpl @@ -1,3 +1,4 @@ +{{ define "slides.tmpl" }} @@ -47,3 +48,4 @@ +{{ end }}