From 2ece2dcba4a6a79655b80f304dc3be854315100d Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 23 Jan 2020 17:54:41 +0100 Subject: [PATCH] Fix tests --- config/config.go | 8 +++++ handlers/compose/docker-compose.yml | 1 + handlers/handlers.go | 17 +++++++++-- handlers/handlers_test.go | 47 ++++++++++++++++++++++++++--- 4 files changed, 66 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 5b02e906..c727922f 100644 --- a/config/config.go +++ b/config/config.go @@ -142,6 +142,10 @@ func (c *ConfigT) CreatePath(model interface{}, path, format string) string { return path + "?" + c.query(model, CreateLabel, format).Encode() } +func (c *ConfigT) DeletePath(model interface{}, path, format string) string { + return path + "?format=html" +} + func (c *ConfigT) CreatePattern() PathPattern { return c.Handlers.PathPatterns[actions[CreateLabel]] } @@ -154,6 +158,10 @@ func (c *ConfigT) ReadPattern() PathPattern { return c.Handlers.PathPatterns[actions[ReadLabel]] } +func (c *ConfigT) DeletePattern() PathPattern { + return c.Handlers.PathPatterns[actions[DeleteLabel]] +} + func (c *ConfigT) query(model interface{}, action int, format string) url.Values { var tplContent string diff --git a/handlers/compose/docker-compose.yml b/handlers/compose/docker-compose.yml index e3a185ac..20925919 100644 --- a/handlers/compose/docker-compose.yml +++ b/handlers/compose/docker-compose.yml @@ -16,6 +16,7 @@ services: smtp: image: digiplant/fake-smtp + container_name: oef_test_smtp ports: - "1025:25" diff --git a/handlers/handlers.go b/handlers/handlers.go index b3501fbb..2451f7ad 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -221,6 +221,10 @@ func (h *Handlers) NewCreateRequest(model interface{}, path string, format strin return request, err } +func (h *Handlers) NewDeleteRequest(model interface{}, path string, format string) (*http.Request, error) { + return http.NewRequest("DELETE", h.Config.DeletePath(model, path, format), nil) +} + func (h *Handlers) onError(w http.ResponseWriter, r *http.Request, err string) { http.Redirect(w, r, "/login?tpl_layout=login&tpl_content=login", http.StatusTemporaryRedirect) } @@ -357,11 +361,12 @@ func (h *Handlers) delete(w http.ResponseWriter, r *http.Request, model string, } else { postFn, err := h.Database.GetFunc(pattern.Path(model)) if err != nil { - h.Renderer[r.URL.Query().Get("format")].Render(w, r, h.CookieStore, err) + return err + // h.Renderer[r.URL.Query().Get("format")].Render(w, r, h.CookieStore, err) } data, err = postFn(h.Database, mux.Vars(r), w, r) if err != nil { - h.Renderer["html"].Render(w, r, h.CookieStore, err) + return err } else if pattern.RedirectPattern != "" { var data struct { RedirectUrl string `json:"redirect_url"` @@ -413,6 +418,14 @@ func (h *Handlers) Read(model interface{}) http.Handler { return http.HandlerFunc(fn) } +func (h *Handlers) Delete(model interface{}) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + h.delete(w, r, reflect.ModelNameLowerPlural(model), h.Config.DeletePattern()) + } + + return http.HandlerFunc(fn) +} + func (h *Handlers) modelHandler(model string, pattern config.PathPattern) handlerFuncWithError { fn := func(w http.ResponseWriter, r *http.Request) error { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index c64ed7f6..a5463737 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -3,6 +3,7 @@ package handlers import ( "context" "encoding/json" + "fmt" "log" "net/http" "net/http/httptest" @@ -17,6 +18,7 @@ import ( "git.andreafazzi.eu/andrea/oef/renderer" "github.com/PuerkitoBio/goquery" jwt "github.com/dgrijalva/jwt-go" + "github.com/gorilla/mux" "github.com/remogatto/prettytest" ) @@ -70,6 +72,24 @@ func requestToken(handlers *Handlers, username string, password string) string { } +func deleteParticipant(id uint) { + req, err := handlers.NewDeleteRequest(&orm.Participant{}, fmt.Sprintf("/participants/%d/delete", id), "json") + if err != nil { + panic(err) + } + + req, err = login(req, handlers, "admin", "admin") + if err != nil { + panic(err) + } + + rr := httptest.NewRecorder() + + router := mux.NewRouter() + router.Handle("/participants/{id}/delete", handlers.Delete(&orm.Participant{})) + router.ServeHTTP(rr, req) +} + func TestRunner(t *testing.T) { prettytest.Run( t, @@ -162,7 +182,10 @@ func (t *testSuite) TestReadAllContests() { if !t.Failed() { rr := httptest.NewRecorder() - handlers.ReadAll(&orm.Contest{}).ServeHTTP(rr, req) + router := mux.NewRouter() + router.Handle("/contests", handlers.ReadAll(&orm.Contest{})) + router.ServeHTTP(rr, req) + t.Equal(http.StatusOK, rr.Code) if !t.Failed() { @@ -193,7 +216,10 @@ func (t *testSuite) TestReadContest() { if !t.Failed() { rr := httptest.NewRecorder() - handlers.Read(&orm.Contest{}).ServeHTTP(rr, req) + router := mux.NewRouter() + router.Handle("/contests/{id}", handlers.Read(&orm.Contest{})) + router.ServeHTTP(rr, req) + t.Equal(http.StatusOK, rr.Code) if !t.Failed() { @@ -223,7 +249,10 @@ func (t *testSuite) TestSchoolSubscriptionForm() { rr := httptest.NewRecorder() - handlers.Read(&orm.School{}).ServeHTTP(rr, req) + router := mux.NewRouter() + router.Handle("/create/", handlers.Read(&orm.School{})) + router.ServeHTTP(rr, req) + t.Equal(http.StatusOK, rr.Code) doc, err := goquery.NewDocumentFromReader(rr.Body) @@ -268,7 +297,10 @@ func (t *testSuite) TestSchoolSubscription() { if !t.Failed() { rr := httptest.NewRecorder() - handlers.Create(&orm.School{}).ServeHTTP(rr, req) + router := mux.NewRouter() + router.Handle("/create/", handlers.Create(&orm.School{})) + router.ServeHTTP(rr, req) + t.Equal(http.StatusSeeOther, rr.Code) if !t.Failed() { @@ -320,9 +352,14 @@ func (t *testSuite) TestSchoolSubscription() { rr := httptest.NewRecorder() - handlers.Create(&orm.Participant{}).ServeHTTP(rr, req) + router := mux.NewRouter() + router.Handle("/create/", handlers.Create(&orm.Participant{})) + router.ServeHTTP(rr, req) + t.Equal(http.StatusSeeOther, rr.Code) + deleteParticipant(1001) + } }