Fix tests

This commit is contained in:
Andrea Fazzi 2020-01-23 17:54:41 +01:00
parent 819a3caad6
commit 2ece2dcba4
4 changed files with 66 additions and 7 deletions

View file

@ -142,6 +142,10 @@ func (c *ConfigT) CreatePath(model interface{}, path, format string) string {
return path + "?" + c.query(model, CreateLabel, format).Encode() 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 { func (c *ConfigT) CreatePattern() PathPattern {
return c.Handlers.PathPatterns[actions[CreateLabel]] return c.Handlers.PathPatterns[actions[CreateLabel]]
} }
@ -154,6 +158,10 @@ func (c *ConfigT) ReadPattern() PathPattern {
return c.Handlers.PathPatterns[actions[ReadLabel]] 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 { func (c *ConfigT) query(model interface{}, action int, format string) url.Values {
var tplContent string var tplContent string

View file

@ -16,6 +16,7 @@ services:
smtp: smtp:
image: digiplant/fake-smtp image: digiplant/fake-smtp
container_name: oef_test_smtp
ports: ports:
- "1025:25" - "1025:25"

View file

@ -221,6 +221,10 @@ func (h *Handlers) NewCreateRequest(model interface{}, path string, format strin
return request, err 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) { 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) 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 { } else {
postFn, err := h.Database.GetFunc(pattern.Path(model)) postFn, err := h.Database.GetFunc(pattern.Path(model))
if err != nil { 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) data, err = postFn(h.Database, mux.Vars(r), w, r)
if err != nil { if err != nil {
h.Renderer["html"].Render(w, r, h.CookieStore, err) return err
} else if pattern.RedirectPattern != "" { } else if pattern.RedirectPattern != "" {
var data struct { var data struct {
RedirectUrl string `json:"redirect_url"` RedirectUrl string `json:"redirect_url"`
@ -413,6 +418,14 @@ func (h *Handlers) Read(model interface{}) http.Handler {
return http.HandlerFunc(fn) 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 { func (h *Handlers) modelHandler(model string, pattern config.PathPattern) handlerFuncWithError {
fn := func(w http.ResponseWriter, r *http.Request) error { fn := func(w http.ResponseWriter, r *http.Request) error {

View file

@ -3,6 +3,7 @@ package handlers
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"log" "log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -17,6 +18,7 @@ import (
"git.andreafazzi.eu/andrea/oef/renderer" "git.andreafazzi.eu/andrea/oef/renderer"
"github.com/PuerkitoBio/goquery" "github.com/PuerkitoBio/goquery"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/remogatto/prettytest" "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) { func TestRunner(t *testing.T) {
prettytest.Run( prettytest.Run(
t, t,
@ -162,7 +182,10 @@ func (t *testSuite) TestReadAllContests() {
if !t.Failed() { if !t.Failed() {
rr := httptest.NewRecorder() 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) t.Equal(http.StatusOK, rr.Code)
if !t.Failed() { if !t.Failed() {
@ -193,7 +216,10 @@ func (t *testSuite) TestReadContest() {
if !t.Failed() { if !t.Failed() {
rr := httptest.NewRecorder() 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) t.Equal(http.StatusOK, rr.Code)
if !t.Failed() { if !t.Failed() {
@ -223,7 +249,10 @@ func (t *testSuite) TestSchoolSubscriptionForm() {
rr := httptest.NewRecorder() 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) t.Equal(http.StatusOK, rr.Code)
doc, err := goquery.NewDocumentFromReader(rr.Body) doc, err := goquery.NewDocumentFromReader(rr.Body)
@ -268,7 +297,10 @@ func (t *testSuite) TestSchoolSubscription() {
if !t.Failed() { if !t.Failed() {
rr := httptest.NewRecorder() 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) t.Equal(http.StatusSeeOther, rr.Code)
if !t.Failed() { if !t.Failed() {
@ -320,9 +352,14 @@ func (t *testSuite) TestSchoolSubscription() {
rr := httptest.NewRecorder() 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) t.Equal(http.StatusSeeOther, rr.Code)
deleteParticipant(1001)
} }
} }