Add test for create school as admin

This commit is contained in:
Andrea Fazzi 2020-01-17 12:53:35 +01:00
parent 7f20441abf
commit d1a7d1a0db
3 changed files with 75 additions and 15 deletions

View file

@ -130,12 +130,16 @@ func (pp PathPattern) Path(model string) string {
return fmt.Sprintf(pp.PathPattern, model) return fmt.Sprintf(pp.PathPattern, model)
} }
func (c *ConfigT) ReadAllPath(model interface{}, format string) string { func (c *ConfigT) ReadAllPath(model interface{}, path, format string) string {
return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadAllLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() return path + "?" + c.query(model, ReadAllLabel, format).Encode()
} }
func (c *ConfigT) ReadPath(model interface{}, format string) string { func (c *ConfigT) ReadPath(model interface{}, path, format string) string {
return fmt.Sprintf(c.Handlers.PathPatterns[actions[ReadLabel]].PathPattern, reflect.ModelNameLowerPlural(model)) + "?" + c.query(model, format).Encode() 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 { func (c *ConfigT) ReadAllPattern() PathPattern {
@ -146,15 +150,23 @@ func (c *ConfigT) ReadPattern() PathPattern {
return c.Handlers.PathPatterns[actions[ReadLabel]] return c.Handlers.PathPatterns[actions[ReadLabel]]
} }
func (c *ConfigT) PathWithQueryParams(model interface{}, path, format string) string { func (c *ConfigT) query(model interface{}, action int, format string) url.Values {
return path + "?" + c.query(model, format).Encode() var tplContent string
}
func (c *ConfigT) query(model interface{}, format string) url.Values {
values := make(url.Values) values := make(url.Values)
values.Add("format", format) 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 values.Add("tpl_layout", "base") // FIXME: use config value
return values return values

View file

@ -183,9 +183,26 @@ func NewHandlers(config *config.ConfigT, renderer map[string]renderer.Renderer,
return handlers return handlers
} }
func (h *Handlers) NewRequest(model interface{}, path string, format string) (*http.Request, error) { func (h *Handlers) NewReadAllRequest(model interface{}, path string, format string) (*http.Request, error) {
log.Print(h.Config.PathWithQueryParams(model, path, format)) return http.NewRequest("GET", h.Config.ReadAllPath(model, path, format), nil)
return http.NewRequest("GET", h.Config.PathWithQueryParams(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) { func (h *Handlers) onError(w http.ResponseWriter, r *http.Request, err string) {

View file

@ -152,7 +152,7 @@ func (t *testSuite) BeforeAll() {
} }
func (t *testSuite) TestReadAllContests() { func (t *testSuite) TestReadAllContests() {
req, err := handlers.NewRequest(&orm.Contest{}, "/contests", "html") req, err := handlers.NewReadAllRequest(&orm.Contest{}, "/contests", "html")
t.Nil(err) t.Nil(err)
req, err = login(req, handlers, "admin", "admin") req, err = login(req, handlers, "admin", "admin")
@ -183,7 +183,7 @@ func (t *testSuite) TestReadAllContests() {
} }
func (t *testSuite) TestReadContest() { 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) t.Nil(err)
req, err = login(req, handlers, "admin", "admin") req, err = login(req, handlers, "admin", "admin")
@ -202,7 +202,38 @@ func (t *testSuite) TestReadContest() {
} }
expected := "JUNIOR Contest" expected := "JUNIOR Contest"
ok := true 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) { if !strings.Contains(s.Text(), expected) {
ok = false ok = false
} }