From 7ca88395e6073ada38f1467ab01d131d8acf72fd Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Thu, 23 Jan 2020 09:15:27 +0100 Subject: [PATCH] Fix test --- handlers/handlers_test.go | 50 ++++++++++++++++++++++++++++++++------- orm/participant.go | 17 ++++++------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index 6289c390..c64ed7f6 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strconv" "strings" "testing" "time" @@ -96,7 +97,7 @@ func (t *testSuite) BeforeAll() { for !connected { var err error - time.Sleep(2 * time.Second) + time.Sleep(5 * time.Second) db, err = orm.NewDatabase(conf, orm.Models) if err != nil { @@ -213,7 +214,7 @@ func (t *testSuite) TestReadContest() { } -func (t *testSuite) TestGetSchoolSubscription() { +func (t *testSuite) TestSchoolSubscriptionForm() { req, err := handlers.NewCreateRequest(&orm.School{}, "/create/", "html", "GET", nil) t.Nil(err) @@ -234,13 +235,11 @@ func (t *testSuite) TestGetSchoolSubscription() { "Codice meccanografico", "Indirizzo dell'istituto", "Regione", - "Cognome referente di sede", - "Nome referente di sede", - "Cognome responsabile di gara", - "Nome responsabile di gara", - "Cognome referente di sede", - "Nome referente di sede", "Indirizzo email", + "Nome del referente di sede", + "Cognome del referente di sede", + "Nome del responsabile di gara", + "Cognome del responsabile di gara", } ok := true doc.Find(".control-label").Each(func(i int, s *goquery.Selection) { @@ -253,7 +252,7 @@ func (t *testSuite) TestGetSchoolSubscription() { } -func (t *testSuite) TestPostCreateSchoolFormAsSubscriber() { +func (t *testSuite) TestSchoolSubscription() { form := url.Values{} form.Add("Name", "FooSchool") form.Add("Code", "123") @@ -293,4 +292,37 @@ func (t *testSuite) TestPostCreateSchoolFormAsSubscriber() { } } + var school orm.School + err = handlers.Database.DB().First(&school, 501).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{}, "/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() + + handlers.Create(&orm.Participant{}).ServeHTTP(rr, req) + t.Equal(http.StatusSeeOther, rr.Code) + + } + } diff --git a/orm/participant.go b/orm/participant.go index ea9d060e..2692ec1a 100644 --- a/orm/participant.go +++ b/orm/participant.go @@ -169,6 +169,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re return participant, nil } else { participant := new(Participant) + err := renderer.Decode(participant, r) if err != nil { return nil, err @@ -218,17 +219,17 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re } // FIXME: Should be managed by API - // var response Response + var response Response - // if err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil { - // return nil, err - // } + if err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil { + return nil, err + } - // response.UserModifierCreate = NewUserModifierCreate(db, r) + response.UserModifierCreate = NewUserModifierCreate(db, r) - // if err := db._db.Save(&response).Error; err != nil { - // return nil, err - // } + if err := db._db.Save(&response).Error; err != nil { + return nil, err + } return participant, nil }