This commit is contained in:
Andrea Fazzi 2020-01-23 09:15:27 +01:00
parent 39520a9acc
commit 7ca88395e6
2 changed files with 50 additions and 17 deletions

View file

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -96,7 +97,7 @@ func (t *testSuite) BeforeAll() {
for !connected { for !connected {
var err error var err error
time.Sleep(2 * time.Second) time.Sleep(5 * time.Second)
db, err = orm.NewDatabase(conf, orm.Models) db, err = orm.NewDatabase(conf, orm.Models)
if err != nil { 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) req, err := handlers.NewCreateRequest(&orm.School{}, "/create/", "html", "GET", nil)
t.Nil(err) t.Nil(err)
@ -234,13 +235,11 @@ func (t *testSuite) TestGetSchoolSubscription() {
"Codice meccanografico", "Codice meccanografico",
"Indirizzo dell'istituto", "Indirizzo dell'istituto",
"Regione", "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", "Indirizzo email",
"Nome del referente di sede",
"Cognome del referente di sede",
"Nome del responsabile di gara",
"Cognome del responsabile di gara",
} }
ok := true ok := true
doc.Find(".control-label").Each(func(i int, s *goquery.Selection) { 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 := url.Values{}
form.Add("Name", "FooSchool") form.Add("Name", "FooSchool")
form.Add("Code", "123") 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)
}
} }

View file

@ -169,6 +169,7 @@ func (model *Participant) Create(db *Database, args map[string]string, w http.Re
return participant, nil return participant, nil
} else { } else {
participant := new(Participant) participant := new(Participant)
err := renderer.Decode(participant, r) err := renderer.Decode(participant, r)
if err != nil { if err != nil {
return nil, err 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 // 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 { if err := db._db.First(&response, &Response{ParticipantID: participant.ID}).Error; err != nil {
// return nil, err return nil, err
// } }
// response.UserModifierCreate = NewUserModifierCreate(db, r) response.UserModifierCreate = NewUserModifierCreate(db, r)
// if err := db._db.Save(&response).Error; err != nil { if err := db._db.Save(&response).Error; err != nil {
// return nil, err return nil, err
// } }
return participant, nil return participant, nil
} }