191 lines
4.4 KiB
Go
191 lines
4.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.andreafazzi.eu/andrea/oef/config"
|
|
"git.andreafazzi.eu/andrea/oef/orm"
|
|
"git.andreafazzi.eu/andrea/oef/renderer"
|
|
"github.com/PuerkitoBio/goquery"
|
|
jwt "github.com/dgrijalva/jwt-go"
|
|
"github.com/remogatto/prettytest"
|
|
)
|
|
|
|
var (
|
|
token string
|
|
handlers *Handlers
|
|
conf *config.ConfigT
|
|
)
|
|
|
|
// Start of setup
|
|
|
|
type testSuite struct {
|
|
prettytest.Suite
|
|
}
|
|
|
|
func authenticate(request *http.Request, tokenString string, signingKey string) (*http.Request, error) {
|
|
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
|
return []byte(signingKey), nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx := request.Context()
|
|
ctx = context.WithValue(ctx, "user", token)
|
|
return request.WithContext(ctx), nil
|
|
|
|
}
|
|
|
|
func requestToken(db *orm.Database, handlers *Handlers) string {
|
|
req, err := http.NewRequest("GET", "/get_token", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
req.SetBasicAuth("admin", "admin")
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
handlers.GetToken(db, []byte(db.Config.Keys.JWTSigningKey)).ServeHTTP(rr, req)
|
|
|
|
var data struct {
|
|
Token string
|
|
}
|
|
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return data.Token
|
|
|
|
}
|
|
|
|
func TestRunner(t *testing.T) {
|
|
prettytest.Run(
|
|
t,
|
|
new(testSuite),
|
|
)
|
|
}
|
|
|
|
func (t *testSuite) BeforeAll() {
|
|
|
|
var db *orm.Database
|
|
|
|
conf = new(config.ConfigT)
|
|
|
|
// Load the configuration
|
|
|
|
err := config.ReadFile("testdata/config.yaml", conf)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// conf.LogLevel = config.LOG_LEVEL_OFF
|
|
|
|
// Initialize the ORM
|
|
|
|
connected := false
|
|
for !connected {
|
|
var err error
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
db, err = orm.NewDatabase(conf, orm.Models)
|
|
if err != nil {
|
|
log.Print(err)
|
|
continue
|
|
}
|
|
|
|
connected = true
|
|
}
|
|
|
|
db.AutoMigrate()
|
|
|
|
// Initialize the renderers
|
|
|
|
htmlRenderer, err := renderer.NewHTMLRenderer("../templates/")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
renderer := map[string]renderer.Renderer{
|
|
"html": htmlRenderer,
|
|
}
|
|
|
|
permissions := map[string]map[string][]int{
|
|
"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},
|
|
},
|
|
}
|
|
|
|
handlers = NewHandlers(conf, renderer, db, orm.Models, permissions)
|
|
token = requestToken(db, handlers)
|
|
}
|
|
|
|
func (t *testSuite) TestReadAllContests() {
|
|
req, err := RequestReadAll(&Contest{})
|
|
// req, err := http.NewRequest("GET", "/contests?format=html&tpl_layout=base&tpl_content=contests", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
pattern := PathPattern{
|
|
"/%s",
|
|
"/%s?format=html&tpl_layout=base&tpl_content=%s",
|
|
[]string{"GET"}, PermissionReadAll,
|
|
}
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req, err = authenticate(req, token, conf.Keys.JWTSigningKey)
|
|
t.Nil(err)
|
|
|
|
if err != nil {
|
|
handlers.modelHandler("contests", pattern).ServeHTTP(rr, req)
|
|
|
|
t.Equal(http.StatusOK, rr.Code)
|
|
|
|
if !t.Failed() {
|
|
doc, err := goquery.NewDocumentFromResponse(rr.Result())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Find the review items
|
|
doc.Find(".sidebar-reviews article .content-block").Each(func(i int, s *goquery.Selection) {
|
|
// For each item found, get the band and title
|
|
band := s.Find("a").Text()
|
|
title := s.Find("i").Text()
|
|
fmt.Printf("Review %d: %s - %s\n", i, band, title)
|
|
})
|
|
t.True(strings.Contains(rr.Body.String(), "JUNIOR Contest"))
|
|
}
|
|
}
|
|
|
|
}
|