Begin working on tests

This commit is contained in:
Andrea Fazzi 2020-01-11 09:11:49 +01:00
parent 013f9717b4
commit a259571955
4 changed files with 148 additions and 63 deletions

2
go.mod
View file

@ -15,7 +15,7 @@ require (
github.com/jinzhu/gorm v1.9.11 github.com/jinzhu/gorm v1.9.11
github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/inflection v1.0.0
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/remogatto/prettytest v0.0.0-20161014102941-8b5d7bfe964e github.com/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1
github.com/robfig/cron v1.2.0 github.com/robfig/cron v1.2.0
github.com/sethvargo/go-password v0.1.3 github.com/sethvargo/go-password v0.1.3
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect

2
go.sum
View file

@ -101,6 +101,8 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/remogatto/prettytest v0.0.0-20161014102941-8b5d7bfe964e h1:uQy6ob81XHCWTGzVTbVHnMDjc4jRKaIXuaNSsX5thCU= github.com/remogatto/prettytest v0.0.0-20161014102941-8b5d7bfe964e h1:uQy6ob81XHCWTGzVTbVHnMDjc4jRKaIXuaNSsX5thCU=
github.com/remogatto/prettytest v0.0.0-20161014102941-8b5d7bfe964e/go.mod h1:jOEnp79oIHy5cvQSHeLcgVJk1GHOOHJHQWps/d1N5Yo= github.com/remogatto/prettytest v0.0.0-20161014102941-8b5d7bfe964e/go.mod h1:jOEnp79oIHy5cvQSHeLcgVJk1GHOOHJHQWps/d1N5Yo=
github.com/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1 h1:qU1a7/zsCi5hydvlheSu2q0lCT0VCfbV27bEIpIuUoU=
github.com/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1/go.mod h1:jOEnp79oIHy5cvQSHeLcgVJk1GHOOHJHQWps/d1N5Yo=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/sethvargo/go-password v0.1.3 h1:18KkbGDkw8SuzeohAbWqBLNSfRQblVwEHOLbPa0PvWM= github.com/sethvargo/go-password v0.1.3 h1:18KkbGDkw8SuzeohAbWqBLNSfRQblVwEHOLbPa0PvWM=

142
handlers/handlers_test.go Normal file
View file

@ -0,0 +1,142 @@
package handlers
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"git.andreafazzi.eu/andrea/oef/config"
"git.andreafazzi.eu/andrea/oef/orm"
"git.andreafazzi.eu/andrea/oef/renderer"
"github.com/jinzhu/gorm"
"github.com/remogatto/prettytest"
)
var (
token string
)
// Start of setup
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) BeforeAll() {
var (
db *gorm.DB
err error
)
// Initialize the ORM
connected := false
for !connected {
time.Sleep(10 * time.Second)
db, err = orm.New("oef:oef@/oef_test?charset=utf8&parseTime=True&loc=Local")
if err != nil {
time.Sleep(5 * time.Second)
continue
}
connected = true
}
orm.Use(db)
orm.AutoMigrate()
// Initialize the renderers
htmlRenderer, err := renderer.NewHTMLRenderer("./testdata/templates/")
if err != nil {
panic(err)
}
jsonRenderer, err := renderer.NewJSONRenderer()
if err != nil {
panic(err)
}
csvRenderer, err := renderer.NewCSVRenderer()
if err != nil {
panic(err)
}
renderer.Render = make(map[string]func(http.ResponseWriter, *http.Request, interface{}, ...url.Values))
renderer.Render["html"] = func(w http.ResponseWriter, r *http.Request, data interface{}, options ...url.Values) {
htmlRenderer.Render(w, r, data, options...)
}
renderer.Render["json"] = func(w http.ResponseWriter, r *http.Request, data interface{}, options ...url.Values) {
jsonRenderer.Render(w, r, data, options...)
}
renderer.Render["csv"] = func(w http.ResponseWriter, r *http.Request, data interface{}, options ...url.Values) {
csvRenderer.Render(w, r, data, options...)
}
// Load the configuration
err = config.ReadFile("testdata/config.yaml", config.Config)
if err != nil {
panic(err)
}
config.Config.LogLevel = config.LOG_LEVEL_OFF
req, err := http.NewRequest("GET", "/get_token", nil)
if err != nil {
panic(err)
}
req.SetBasicAuth("admin", "admin")
rr := httptest.NewRecorder()
tokenHandler().ServeHTTP(rr, req)
var data struct {
Token string
UserID string
}
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
panic(err)
}
token = data.Token
}
func (t *testSuite) TestGetTeachersHTML() {
req, err := http.NewRequest("GET", "/teachers?format=html&tpl_layout=base&tpl_content=teachers", nil)
if err != nil {
panic(err)
}
pattern := PathPattern{
"/%s",
"/%s?format=html&tpl_layout=base&tpl_content=%s",
[]string{"GET"}, PermissionReadAll,
}
rr := httptest.NewRecorder()
modelHandler("teachers", pattern).ServeHTTP(rr, req)
t.Equal(http.StatusOK, rr.Code)
if !t.Failed() {
t.True(strings.Contains(rr.Body.String(), "DELLE ROSE"))
}
}

View file

@ -54,7 +54,7 @@ func importSchools(client *client.Client, filename string) error {
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Println("Create school with ID", id) log.Println("Created school with ID", id)
} }
return nil return nil
@ -76,7 +76,7 @@ func importParticipants(client *client.Client, filename string) error {
if err != nil { if err != nil {
panic(err) panic(err)
} }
log.Println("Create participant with ID", id) log.Println("Created participant with ID", id)
} }
return nil return nil
@ -90,70 +90,11 @@ func importContest(client *client.Client, filename string) error {
return err return err
} }
// var contests []*orm.Contest
// err := client.ReadAll(&contests)
// if err != nil {
// return err
// }
// // Remove all contest with the same name
// for _, c := range contests {
// if c.Name == contest.Name {
// log.Println("Remove contest with ID", c.ID)
// _, err := client.Delete(c)
// if err != nil {
// return err
// }
// }
// }
contestID, err := client.Create(contest) contestID, err := client.Create(contest)
if err != nil { if err != nil {
return err return err
} }
log.Println("Create contest with ID", contestID) log.Println("Created contest with ID", contestID)
// log.Println("Creating questions...")
// for _, question := range contest.Questions {
// // err := client.DeleteAllFunc(&[]*orm.Question{}, func(model interface{}) bool {
// // return model.(*orm.Question).Text == question.Text && model.(*orm.Question).ContestID == contestID
// // })
// // if err != nil {
// // return err
// // }
// question.ContestID = contestID
// questionID, err := client.Create(question)
// if err != nil {
// return err
// }
// log.Println("Create question with ID", questionID)
// for pos, answer := range question.Answers {
// // err := client.DeleteAllFunc(&[]*orm.Answer{}, func(model interface{}) bool {
// // return model.(*orm.Answer).Text == answer.Text && model.(*orm.Answer).Question != nil && model.(*orm.Answer).Question.Text == question.Text
// // })
// // if err != nil {
// // return err
// // }
// answer.QuestionID = questionID
// if pos == 0 {
// answer.Correct = true
// }
// id, err := client.Create(answer)
// if err != nil {
// return err
// }
// log.Println("Create answer with ID", id)
// }
// }
return nil return nil
} }