Fix tests
This commit is contained in:
parent
819a3caad6
commit
2ece2dcba4
4 changed files with 66 additions and 7 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ services:
|
|||
|
||||
smtp:
|
||||
image: digiplant/fake-smtp
|
||||
container_name: oef_test_smtp
|
||||
ports:
|
||||
- "1025:25"
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue