oef/handlers/handlers_test.go

538 lines
12 KiB
Go
Raw Normal View History

2020-01-11 09:11:49 +01:00
package handlers
import (
2020-01-13 08:14:58 +01:00
"context"
2020-01-11 09:11:49 +01:00
"encoding/json"
"errors"
2020-01-23 17:54:41 +01:00
"fmt"
"log"
2020-01-11 09:11:49 +01:00
"net/http"
"net/http/httptest"
"net/url"
"regexp"
2020-01-23 09:15:27 +01:00
"strconv"
2020-01-17 12:11:13 +01:00
"strings"
2020-01-11 09:11:49 +01:00
"testing"
"time"
"git.andreafazzi.eu/andrea/oef/config"
"git.andreafazzi.eu/andrea/oef/orm"
"git.andreafazzi.eu/andrea/oef/renderer"
2020-01-15 16:42:35 +01:00
"github.com/PuerkitoBio/goquery"
2020-01-13 08:14:58 +01:00
jwt "github.com/dgrijalva/jwt-go"
2020-01-23 17:54:41 +01:00
"github.com/gorilla/mux"
2020-01-11 09:11:49 +01:00
"github.com/remogatto/prettytest"
)
var (
2020-01-14 16:28:27 +01:00
token string
handlers *Handlers
conf *config.ConfigT
2020-01-11 09:11:49 +01:00
)
// Start of setup
type testSuite struct {
prettytest.Suite
}
2020-01-17 11:06:28 +01:00
func login(request *http.Request, handlers *Handlers, username string, password string) (*http.Request, error) {
tokenString := requestToken(handlers, username, password)
2020-01-14 16:28:27 +01:00
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
2020-01-17 11:06:28 +01:00
return []byte(handlers.Config.Keys.JWTSigningKey), nil
2020-01-14 16:28:27 +01:00
})
if err != nil {
return nil, err
}
ctx := request.Context()
ctx = context.WithValue(ctx, "user", token)
return request.WithContext(ctx), nil
}
2020-01-17 11:06:28 +01:00
func requestToken(handlers *Handlers, username string, password string) string {
2020-01-14 16:28:27 +01:00
req, err := http.NewRequest("GET", "/get_token", nil)
if err != nil {
panic(err)
}
2020-01-17 11:06:28 +01:00
req.SetBasicAuth(username, password)
2020-01-14 16:28:27 +01:00
rr := httptest.NewRecorder()
2020-01-17 11:06:28 +01:00
handlers.GetToken(handlers.Database, []byte(handlers.Config.Keys.JWTSigningKey)).ServeHTTP(rr, req)
2020-01-14 16:28:27 +01:00
var data struct {
Token string
}
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
panic(err)
}
return data.Token
}
2020-01-24 10:55:01 +01:00
func deleteParticipant(id uint) int {
req, err := handlers.NewDeleteRequest(&orm.Participant{}, fmt.Sprintf("/participants/%d/delete", id), "html")
2020-01-23 17:54:41 +01:00
if err != nil {
panic(err)
}
req, err = login(req, handlers, "admin", "admin")
if err != nil {
panic(err)
}
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/participants/{id}/delete", handlers.Delete(&orm.Participant{}))
router.ServeHTTP(rr, req)
2020-01-24 10:55:01 +01:00
return rr.Code
}
func deleteSchool(id uint) int {
req, err := handlers.NewDeleteRequest(&orm.School{}, fmt.Sprintf("/schools/%d/delete", id), "html")
if err != nil {
panic(err)
}
req, err = login(req, handlers, "admin", "admin")
if err != nil {
panic(err)
}
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/schools/{id}/delete", handlers.Delete(&orm.School{}))
router.ServeHTTP(rr, req)
return rr.Code
2020-01-23 17:54:41 +01:00
}
func subscribeSchoolAsSchool() (uint, error) {
form := url.Values{}
form.Add("Name", "Foo School")
form.Add("Code", "123456789")
form.Add("Email", "foo@school.org")
req, err := handlers.NewCreateRequest(&orm.School{}, "/schools/create/", "html", "POST", form)
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req, err = login(req, handlers, "subscriber", "subscribe")
if err != nil {
panic(err)
}
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/schools/create/", handlers.Create(&orm.School{}))
router.ServeHTTP(rr, req)
if rr.Code != http.StatusSeeOther {
return 0, errors.New("Unexpected response code")
}
re := regexp.MustCompile(`/schools/([0-9]+)\?`)
matches := re.FindStringSubmatch(rr.Header()["Location"][0])
id, err := strconv.Atoi(matches[1])
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
}
2020-01-11 09:11:49 +01:00
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) BeforeAll() {
var db *orm.Database
conf = new(config.ConfigT)
2020-01-11 09:11:49 +01:00
// Load the configuration
err := config.ReadFile("testdata/config.yaml", conf)
if err != nil {
panic(err)
}
2020-01-27 13:15:51 +01:00
2020-02-05 10:10:27 +01:00
conf.LogLevel = config.LOG_LEVEL_DEBUG
2020-01-11 09:11:49 +01:00
// Initialize the ORM
connected := false
for !connected {
var err error
2020-01-23 09:15:27 +01:00
time.Sleep(5 * time.Second)
2020-01-15 16:42:35 +01:00
db, err = orm.NewDatabase(conf, orm.Models)
2020-01-11 09:11:49 +01:00
if err != nil {
log.Print(err)
2020-01-11 09:11:49 +01:00
continue
}
2020-01-11 09:11:49 +01:00
connected = true
}
db.AutoMigrate()
2020-01-13 08:14:58 +01:00
2020-01-11 09:11:49 +01:00
// Initialize the renderers
2020-01-16 12:47:35 +01:00
htmlRenderer, err := renderer.NewHTMLRenderer("../templates/")
2020-01-11 09:11:49 +01:00
if err != nil {
panic(err)
}
renderer := map[string]renderer.Renderer{
"html": htmlRenderer,
2020-01-13 16:33:20 +01:00
}
2020-01-17 07:59:57 +01:00
conf.Handlers.Permissions = map[string]map[string][]int{
2020-01-16 12:47:35 +01:00
"administrator": map[string][]int{
"Contest": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"Participant": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"School": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"Question": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"Answer": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"Response": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
},
"school": map[string][]int{
"Participant": []int{PermissionCreate, PermissionRead, PermissionReadAll, PermissionUpdate, PermissionDelete},
"School": []int{PermissionRead, PermissionUpdate},
},
"participant": map[string][]int{
"Participant": []int{PermissionRead},
"Response": []int{PermissionUpdate, PermissionRead},
},
"subscriber": map[string][]int{
"School": []int{PermissionCreate, PermissionRead},
},
}
2020-01-17 07:59:57 +01:00
conf.Handlers.PathPatterns = DefaultPathPatterns
conf.Handlers.APIPathPatterns = DefaultAPIPathPatterns
handlers = NewHandlers(conf, renderer, db, orm.Models)
2020-01-11 09:11:49 +01:00
}
2020-01-13 08:14:58 +01:00
func (t *testSuite) TestReadAllContests() {
2020-01-17 12:53:35 +01:00
req, err := handlers.NewReadAllRequest(&orm.Contest{}, "/contests", "html")
2020-01-17 11:06:28 +01:00
t.Nil(err)
2020-01-13 08:14:58 +01:00
2020-01-17 11:06:28 +01:00
req, err = login(req, handlers, "admin", "admin")
2020-01-14 16:28:27 +01:00
t.Nil(err)
2020-01-13 08:14:58 +01:00
2020-01-17 11:06:28 +01:00
if !t.Failed() {
rr := httptest.NewRecorder()
2020-01-11 09:11:49 +01:00
2020-01-23 17:54:41 +01:00
router := mux.NewRouter()
router.Handle("/contests", handlers.ReadAll(&orm.Contest{}))
router.ServeHTTP(rr, req)
2020-01-14 16:28:27 +01:00
t.Equal(http.StatusOK, rr.Code)
2020-01-11 09:11:49 +01:00
2020-01-14 16:28:27 +01:00
if !t.Failed() {
2020-01-17 11:06:28 +01:00
doc, err := goquery.NewDocumentFromReader(rr.Body)
2020-01-15 16:42:35 +01:00
if err != nil {
log.Fatal(err)
}
2020-01-17 12:11:13 +01:00
expected := []string{"JUNIOR Contest", "SENIOR Contest"}
ok := true
doc.Find(".list-group-item").Each(func(i int, s *goquery.Selection) {
if !strings.Contains(s.Text(), expected[i]) {
ok = false
}
})
t.True(ok)
}
}
}
func (t *testSuite) TestReadContest() {
2020-01-17 12:53:35 +01:00
req, err := handlers.NewReadRequest(&orm.Contest{}, "/contests/1", "html")
2020-01-17 12:11:13 +01:00
t.Nil(err)
req, err = login(req, handlers, "admin", "admin")
t.Nil(err)
if !t.Failed() {
rr := httptest.NewRecorder()
2020-01-23 17:54:41 +01:00
router := mux.NewRouter()
router.Handle("/contests/{id}", handlers.Read(&orm.Contest{}))
router.ServeHTTP(rr, req)
2020-01-17 12:11:13 +01:00
t.Equal(http.StatusOK, rr.Code)
if !t.Failed() {
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
expected := "JUNIOR Contest"
ok := true
2020-01-17 12:53:35 +01:00
doc.Find("h1").Each(func(i int, s *goquery.Selection) {
if !strings.Contains(s.Text(), expected) {
ok = false
}
})
t.True(ok)
}
}
}
2020-01-23 09:15:27 +01:00
func (t *testSuite) TestSchoolSubscriptionForm() {
2020-01-24 12:04:12 +01:00
req, err := handlers.NewCreateRequest(&orm.School{}, "/schools/create/", "html", "GET", nil)
2020-01-17 12:53:35 +01:00
t.Nil(err)
req, err = login(req, handlers, "subscriber", "subscribe")
t.Nil(err)
rr := httptest.NewRecorder()
2020-01-23 17:54:41 +01:00
router := mux.NewRouter()
2020-01-24 12:04:12 +01:00
router.Handle("/schools/create/", handlers.Create(&orm.School{}))
2020-01-23 17:54:41 +01:00
router.ServeHTTP(rr, req)
t.Equal(http.StatusOK, rr.Code)
2020-01-24 10:55:01 +01:00
if !t.Failed() {
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
2020-01-24 10:55:01 +01:00
expected := []string{
"Denominazione dell'istituto",
"Codice meccanografico",
"Indirizzo dell'istituto",
"Regione",
"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) {
t.Equal(expected[i], s.Text())
if t.Failed() {
ok = false
}
})
t.True(ok)
}
}
2020-01-23 09:15:27 +01:00
func (t *testSuite) TestSchoolSubscription() {
form := url.Values{}
form.Add("Name", "FooSchool")
form.Add("Code", "123")
form.Add("Email", "foo@school.org")
2020-01-24 12:04:12 +01:00
req, err := handlers.NewCreateRequest(&orm.School{}, "/schools/create/", "html", "POST", form)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
t.Nil(err)
req, err = login(req, handlers, "subscriber", "subscribe")
2020-01-17 12:53:35 +01:00
t.Nil(err)
if !t.Failed() {
rr := httptest.NewRecorder()
2020-01-23 17:54:41 +01:00
router := mux.NewRouter()
2020-01-24 10:55:01 +01:00
router.Handle("/schools/create/", handlers.Create(&orm.School{}))
2020-01-23 17:54:41 +01:00
router.ServeHTTP(rr, req)
t.Equal(http.StatusSeeOther, rr.Code)
2020-01-17 12:53:35 +01:00
schoolId, err := getIdFromPath(rr.Header()["Location"][0])
2020-01-27 13:15:51 +01:00
if err != nil {
panic(err)
}
2020-01-17 12:53:35 +01:00
if !t.Failed() {
doc, err := goquery.NewDocumentFromReader(rr.Body)
if err != nil {
log.Fatal(err)
}
expected := []string{
"FooSchool",
"123",
}
2020-01-17 12:53:35 +01:00
ok := true
doc.Find("dd").Each(func(i int, s *goquery.Selection) {
t.Equal(expected[i], s.Text())
if t.Failed() {
2020-01-17 12:11:13 +01:00
ok = false
}
})
t.True(ok)
2020-01-14 16:28:27 +01:00
}
2020-01-11 09:11:49 +01:00
var school orm.School
err = handlers.Database.DB().First(&school, schoolId).Error
t.Nil(err)
2020-01-23 09:15:27 +01:00
if !t.Failed() {
var user orm.User
if err := handlers.Database.DB().First(&user, school.UserID).Error; err != nil {
panic(err)
}
2020-01-23 09:15:27 +01:00
t.Equal("123", user.Username)
2020-01-23 09:15:27 +01:00
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)))
2020-01-23 09:15:27 +01:00
req, err := handlers.NewCreateRequest(&orm.Participant{}, "/participants/create/", "html", "POST", form)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
t.Nil(err)
2020-01-23 09:15:27 +01:00
req, err = login(req, handlers, user.Username, user.Password)
t.Nil(err)
2020-01-23 09:15:27 +01:00
rr := httptest.NewRecorder()
2020-01-23 09:15:27 +01:00
router := mux.NewRouter()
router.Handle("/participants/create/", handlers.Create(&orm.Participant{}))
router.ServeHTTP(rr, req)
2020-01-23 17:54:41 +01:00
t.Equal(http.StatusSeeOther, rr.Code)
2020-01-27 13:15:51 +01:00
if !t.Failed() {
participantId, err := getIdFromPath(rr.Header()["Location"][0])
if err != nil {
panic(err)
}
t.Equal(http.StatusOK, deleteParticipant(participantId))
}
2020-01-23 17:54:41 +01:00
}
2020-01-23 09:15:27 +01:00
t.Equal(http.StatusOK, deleteSchool(schoolId))
}
2020-01-24 10:55:01 +01:00
2020-01-11 09:11:49 +01:00
}
2020-02-05 10:10:27 +01:00
func (t *testSuite) TestParticipantResponse() {
form := url.Values{}
form.Set("Singleresponses.0", "9")
req, err := handlers.NewUpdateRequest(&orm.Response{}, "/responses/5/update", "html", "POST", form)
2020-02-05 10:10:27 +01:00
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
t.Nil(err)
req, err = login(req, handlers, "RHOMAT9HZ", "9HzXOfJPje")
t.Nil(err)
if !t.Failed() {
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.Handle("/responses/{id}/update", handlers.Update(&orm.Response{}))
router.ServeHTTP(rr, req)
t.Equal(http.StatusSeeOther, rr.Code)
if !t.Failed() {
req, err := handlers.NewReadRequest(&orm.Response{}, "/responses/5", "html")
2020-02-05 10:10:27 +01:00
t.Nil(err)
req, err = login(req, handlers, "admin", "admin")
t.Nil(err)
rr = httptest.NewRecorder()
router = mux.NewRouter()
router.Handle("/responses/{id}", handlers.Read(&orm.Response{}))
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 := "1"
ok := false
doc.Find("dd").Each(func(i int, s *goquery.Selection) {
if s.Text() == expected {
ok = true
2020-02-05 10:10:27 +01:00
}
})
t.True(ok)
}
}
}
}
func (t *testSuite) TestUserModifier() {
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)
}
2020-01-27 13:15:51 +01:00
expected := "subscriber[subscriber]"
ok := false
2020-01-27 11:43:56 +01:00
doc.Find("dd").Each(func(i int, s *goquery.Selection) {
if strings.Contains(s.Text(), expected) {
ok = true
}
})
t.True(ok)
}
}
t.Equal(http.StatusOK, deleteSchool(id))
}