diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 33d4474c..8fa02ca1 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "regexp" "strconv" "strings" "testing" @@ -113,7 +114,7 @@ func deleteSchool(id uint) int { return rr.Code } -func subscribeSchoolAsSchool() error { +func subscribeSchoolAsSchool() (uint, error) { form := url.Values{} form.Add("Name", "Foo School") form.Add("Code", "123456789") @@ -138,12 +139,21 @@ func subscribeSchoolAsSchool() error { router.ServeHTTP(rr, req) if rr.Code != http.StatusSeeOther { - return errors.New("Unexpected response code") + return 0, errors.New("Unexpected response code") } - log.Println(rr.Header()["Location"]) + re := regexp.MustCompile(`/schools/([0-9]+)\?`) + matches := re.FindStringSubmatch(rr.Header()["Location"][0]) + id, err := strconv.Atoi(matches[1]) - return nil + return uint(id), nil +} + +func getIdFromPath(path string) (uint, error) { + re := regexp.MustCompile(`/[a-z]+/([0-9]+)\?`) + matches := re.FindStringSubmatch(path) + id, err := strconv.Atoi(matches[1]) + return uint(id), err } func TestRunner(t *testing.T) { @@ -361,13 +371,14 @@ func (t *testSuite) TestSchoolSubscription() { t.Equal(http.StatusSeeOther, rr.Code) + schoolId, err := getIdFromPath(rr.Header()["Location"][0]) if !t.Failed() { doc, err := goquery.NewDocumentFromReader(rr.Body) if err != nil { log.Fatal(err) } expected := []string{ - "FooSchooll", + "FooSchool", "123", } ok := true @@ -379,51 +390,85 @@ func (t *testSuite) TestSchoolSubscription() { }) t.True(ok) } - } - var school orm.School - err = handlers.Database.DB().First(&school, 501).Error - t.Nil(err) + var school orm.School + err = handlers.Database.DB().First(&school, schoolId).Error + t.Nil(err) + + if !t.Failed() { + var user orm.User + if err := handlers.Database.DB().First(&user, school.UserID).Error; err != nil { + panic(err) + } + + t.Equal("123", user.Username) + + form := url.Values{} + form.Add("Firstname", "Mario") + form.Add("Lastname", "BROS") + form.Add("Fiscalcode", "BRSMRE815ZL16") + form.Add("category_id", "1") + form.Add("school_id", strconv.Itoa(int(school.ID))) + + req, err := handlers.NewCreateRequest(&orm.Participant{}, "/participants/create/", "html", "POST", form) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + t.Nil(err) + + req, err = login(req, handlers, user.Username, user.Password) + t.Nil(err) + + rr := httptest.NewRecorder() + + router := mux.NewRouter() + router.Handle("/participants/create/", handlers.Create(&orm.Participant{})) + router.ServeHTTP(rr, req) + + t.Equal(http.StatusSeeOther, rr.Code) + + t.Equal(http.StatusOK, deleteParticipant(1001)) - if !t.Failed() { - var user orm.User - if err := handlers.Database.DB().First(&user, school.UserID).Error; err != nil { - panic(err) } - t.Equal("123", user.Username) - - form := url.Values{} - form.Add("Firstname", "Mario") - form.Add("Lastname", "BROS") - form.Add("Fiscalcode", "BRSMRE815ZL16") - form.Add("category_id", "1") - form.Add("school_id", strconv.Itoa(int(school.ID))) - - req, err := handlers.NewCreateRequest(&orm.Participant{}, "/participants/create/", "html", "POST", form) - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - t.Nil(err) - - req, err = login(req, handlers, user.Username, user.Password) - t.Nil(err) - - rr := httptest.NewRecorder() - - router := mux.NewRouter() - router.Handle("/participants/create/", handlers.Create(&orm.Participant{})) - router.ServeHTTP(rr, req) - - t.Equal(http.StatusSeeOther, rr.Code) - - t.Equal(http.StatusOK, deleteParticipant(1001)) - + t.Equal(http.StatusOK, deleteSchool(schoolId)) } - t.Equal(http.StatusOK, deleteSchool(501)) - } func (t *testSuite) TestUserModifier() { - err := subscribeSchoolAsSchool() + id, err := subscribeSchoolAsSchool() t.Nil(err) + + req, err := handlers.NewReadRequest(&orm.School{}, fmt.Sprintf("/schools/%d", id), "html") + t.Nil(err) + + req, err = login(req, handlers, "admin", "admin") + t.Nil(err) + + if !t.Failed() { + rr := httptest.NewRecorder() + + router := mux.NewRouter() + router.Handle("/schools/{id}", handlers.Read(&orm.School{})) + router.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 := "Creato da" + ok := false + doc.Find("dt").Each(func(i int, s *goquery.Selection) { + if strings.Contains(s.Text(), expected) { + ok = true + } + }) + t.True(ok) + } + } + + t.Equal(http.StatusOK, deleteSchool(id)) + } diff --git a/orm/school.go b/orm/school.go index 2c895c66..cf30caf4 100644 --- a/orm/school.go +++ b/orm/school.go @@ -195,6 +195,11 @@ func (model *School) Read(db *Database, args map[string]string, w http.ResponseW return nil, err } + if isAdministrator(r) { + school.UserModifierCreate.get(db) + school.UserModifierUpdate.get(db) + } + return &school, nil }