From d1a7d1a0db4576cf27dc68b51396ece5828f6349 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Fri, 17 Jan 2020 12:53:35 +0100 Subject: [PATCH] Add test for create school as admin --- config/config.go | 30 +++++++++++++++++++++--------- handlers/handlers.go | 23 ++++++++++++++++++++--- handlers/handlers_test.go | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index abc6841e..6f67570c 100644 --- a/config/config.go +++ b/config/config.go @@ -130,12 +130,16 @@ func (pp PathPattern) Path(model string) string { return fmt.Sprintf(pp.PathPattern, model) } -func (c *ConfigT) ReadAllPath(model interface{}, format string) string { - return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadAllLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() +func (c *ConfigT) ReadAllPath(model interface{}, path, format string) string { + return path + "?" + c.query(model, ReadAllLabel, format).Encode() } -func (c *ConfigT) ReadPath(model interface{}, format string) string { - return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() +func (c *ConfigT) ReadPath(model interface{}, path, format string) string { + return path + "?" + c.query(model, ReadLabel, format).Encode() +} + +func (c *ConfigT) CreatePath(model interface{}, path, format string) string { + return path + "?" + c.query(model, CreateLabel, format).Encode() } func (c *ConfigT) ReadAllPattern() PathPattern { @@ -146,15 +150,23 @@ func (c *ConfigT) ReadPattern() PathPattern { return c.Handlers.PathPatterns[actions[ReadLabel]] } -func (c *ConfigT) PathWithQueryParams(model interface{}, path, format string) string { - return path + "?" + c.query(model, format).Encode() -} +func (c *ConfigT) query(model interface{}, action int, format string) url.Values { + var tplContent string -func (c *ConfigT) query(model interface{}, format string) url.Values { values := make(url.Values) values.Add("format", format) - values.Add("tpl_content", reflect.ModelNameLowerPlural(model)) + + switch action { + case CreateLabel: + tplContent = reflect.ModelNameLowerPlural(model) + "_add_update" + case ReadAllLabel: + tplContent = reflect.ModelNameLowerPlural(model) + case ReadLabel: + tplContent = reflect.ModelNameLowerPlural(model) + "_show" + } + + values.Add("tpl_content", tplContent) values.Add("tpl_layout", "base") // FIXME: use config value return values diff --git a/handlers/handlers.go b/handlers/handlers.go index aa1ceab1..517b8223 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -183,9 +183,26 @@ func NewHandlers(config *config.ConfigT, renderer map[string]renderer.Renderer, return handlers } -func (h *Handlers) NewRequest(model interface{}, path string, format string) (*http.Request, error) { - log.Print(h.Config.PathWithQueryParams(model, path, format)) - return http.NewRequest("GET", h.Config.PathWithQueryParams(model, path, format), nil) +func (h *Handlers) NewReadAllRequest(model interface{}, path string, format string) (*http.Request, error) { + return http.NewRequest("GET", h.Config.ReadAllPath(model, path, format), nil) +} + +func (h *Handlers) NewReadRequest(model interface{}, path string, format string) (*http.Request, error) { + return http.NewRequest("GET", h.Config.ReadPath(model, path, format), nil) +} + +func (h *Handlers) NewCreateRequest(model interface{}, path string, format string, method string) (*http.Request, error) { + var ( + request *http.Request + err error + ) + switch method { + case "GET": + request, err = http.NewRequest("GET", h.Config.CreatePath(model, path, format), nil) + case "POST": + request, err = http.NewRequest("POST", h.Config.CreatePath(model, path, format), nil) + } + return request, err } func (h *Handlers) onError(w http.ResponseWriter, r *http.Request, err string) { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 3fcce387..5977beab 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -152,7 +152,7 @@ func (t *testSuite) BeforeAll() { } func (t *testSuite) TestReadAllContests() { - req, err := handlers.NewRequest(&orm.Contest{}, "/contests", "html") + req, err := handlers.NewReadAllRequest(&orm.Contest{}, "/contests", "html") t.Nil(err) req, err = login(req, handlers, "admin", "admin") @@ -183,7 +183,7 @@ func (t *testSuite) TestReadAllContests() { } func (t *testSuite) TestReadContest() { - req, err := handlers.NewRequest(&orm.Contest{}, "/contests/1", "html") + req, err := handlers.NewReadRequest(&orm.Contest{}, "/contests/1", "html") t.Nil(err) req, err = login(req, handlers, "admin", "admin") @@ -202,7 +202,38 @@ func (t *testSuite) TestReadContest() { } expected := "JUNIOR Contest" ok := true - doc.Find(".list-group-item").Each(func(i int, s *goquery.Selection) { + doc.Find("h1").Each(func(i int, s *goquery.Selection) { + if !strings.Contains(s.Text(), expected) { + ok = false + } + }) + t.True(ok) + } + } + +} + +func (t *testSuite) TestCreateSchoolAsSubscriber() { + req, err := handlers.NewCreateRequest(&orm.School{}, "/create/", "html", "GET") + t.Nil(err) + + req, err = login(req, handlers, "admin", "admin") + t.Nil(err) + + if !t.Failed() { + rr := httptest.NewRecorder() + + handlers.Read(&orm.Contest{}).ServeHTTP(rr, req) + t.Equal(http.StatusOK, rr.Code) + + if !t.Failed() { + doc, err := goquery.NewDocumentFromReader(rr.Body) + if err != nil { + log.Fatal(err) + } + expected := "Denominazione Istituto" + ok := true + doc.Find(".control-label").Each(func(i int, s *goquery.Selection) { if !strings.Contains(s.Text(), expected) { ok = false }