From a2595719550836e8ef32963666adc40e4014e14a Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Sat, 11 Jan 2020 09:11:49 +0100 Subject: [PATCH] Begin working on tests --- go.mod | 2 +- go.sum | 2 + handlers/handlers_test.go | 142 ++++++++++++++++++++++++++++++++++++++ scripts/import/main.go | 65 +---------------- 4 files changed, 148 insertions(+), 63 deletions(-) create mode 100644 handlers/handlers_test.go diff --git a/go.mod b/go.mod index d55d3b90..7b0c74ee 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/jinzhu/gorm v1.9.11 github.com/jinzhu/inflection v1.0.0 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/sethvargo/go-password v0.1.3 github.com/smartystreets/goconvey v1.6.4 // indirect diff --git a/go.sum b/go.sum index 5b92022a..bee6b44e 100644 --- a/go.sum +++ b/go.sum @@ -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/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-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/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= github.com/sethvargo/go-password v0.1.3 h1:18KkbGDkw8SuzeohAbWqBLNSfRQblVwEHOLbPa0PvWM= diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go new file mode 100644 index 00000000..f2bc9c41 --- /dev/null +++ b/handlers/handlers_test.go @@ -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")) + } + +} diff --git a/scripts/import/main.go b/scripts/import/main.go index bd48d532..b06e90bc 100644 --- a/scripts/import/main.go +++ b/scripts/import/main.go @@ -54,7 +54,7 @@ func importSchools(client *client.Client, filename string) error { if err != nil { panic(err) } - log.Println("Create school with ID", id) + log.Println("Created school with ID", id) } return nil @@ -76,7 +76,7 @@ func importParticipants(client *client.Client, filename string) error { if err != nil { panic(err) } - log.Println("Create participant with ID", id) + log.Println("Created participant with ID", id) } return nil @@ -90,70 +90,11 @@ func importContest(client *client.Client, filename string) error { 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) if err != nil { return err } - log.Println("Create 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) - // } - - // } + log.Println("Created contest with ID", contestID) return nil }