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"
|
2020-01-15 16:42:35 +01:00
|
|
|
"fmt"
|
2020-01-15 13:40:35 +01:00
|
|
|
"log"
|
2020-01-11 09:11:49 +01:00
|
|
|
"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"
|
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-11 09:11:49 +01:00
|
|
|
"github.com/remogatto/prettytest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-14 16:28:27 +01:00
|
|
|
token string
|
|
|
|
handlers *Handlers
|
2020-01-15 13:40:35 +01:00
|
|
|
conf *config.ConfigT
|
2020-01-11 09:11:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start of setup
|
|
|
|
|
|
|
|
type testSuite struct {
|
|
|
|
prettytest.Suite
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:28:27 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
func requestToken(db *orm.Database, handlers *Handlers) string {
|
2020-01-14 16:28:27 +01:00
|
|
|
req, err := http.NewRequest("GET", "/get_token", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth("admin", "admin")
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
handlers.GetToken(db, []byte(db.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-11 09:11:49 +01:00
|
|
|
func TestRunner(t *testing.T) {
|
|
|
|
prettytest.Run(
|
|
|
|
t,
|
|
|
|
new(testSuite),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testSuite) BeforeAll() {
|
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
var db *orm.Database
|
|
|
|
|
|
|
|
conf = new(config.ConfigT)
|
2020-01-11 09:11:49 +01:00
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
// Load the configuration
|
|
|
|
|
|
|
|
err := config.ReadFile("testdata/config.yaml", conf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// conf.LogLevel = config.LOG_LEVEL_OFF
|
|
|
|
|
2020-01-11 09:11:49 +01:00
|
|
|
// Initialize the ORM
|
|
|
|
|
|
|
|
connected := false
|
|
|
|
for !connected {
|
2020-01-15 13:40:35 +01:00
|
|
|
var err error
|
|
|
|
|
2020-01-13 16:33:20 +01:00
|
|
|
time.Sleep(5 * time.Second)
|
2020-01-15 13:40:35 +01:00
|
|
|
|
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 {
|
2020-01-15 13:40:35 +01:00
|
|
|
log.Print(err)
|
2020-01-11 09:11:49 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-01-15 13:40:35 +01:00
|
|
|
|
2020-01-11 09:11:49 +01:00
|
|
|
connected = true
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
db.AutoMigrate()
|
2020-01-13 08:14:58 +01:00
|
|
|
|
2020-01-11 09:11:49 +01:00
|
|
|
// Initialize the renderers
|
|
|
|
|
|
|
|
htmlRenderer, err := renderer.NewHTMLRenderer("./testdata/templates/")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
renderer := map[string]renderer.Renderer{
|
|
|
|
"html": htmlRenderer,
|
2020-01-13 16:33:20 +01:00
|
|
|
}
|
|
|
|
|
2020-01-15 16:42:35 +01:00
|
|
|
handlers = NewHandlers(conf, renderer, db, orm.Models)
|
2020-01-15 13:40:35 +01:00
|
|
|
token = requestToken(db, handlers)
|
2020-01-11 09:11:49 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 08:14:58 +01:00
|
|
|
func (t *testSuite) TestReadAllContests() {
|
|
|
|
req, err := http.NewRequest("GET", "/contests?format=html&tpl_layout=base&tpl_content=contests", nil)
|
2020-01-11 09:11:49 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern := PathPattern{
|
|
|
|
"/%s",
|
|
|
|
"/%s?format=html&tpl_layout=base&tpl_content=%s",
|
|
|
|
[]string{"GET"}, PermissionReadAll,
|
|
|
|
}
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
2020-01-13 08:14:58 +01:00
|
|
|
|
2020-01-15 13:40:35 +01:00
|
|
|
req, err = authenticate(req, token, conf.Keys.JWTSigningKey)
|
2020-01-14 16:28:27 +01:00
|
|
|
t.Nil(err)
|
2020-01-13 08:14:58 +01:00
|
|
|
|
2020-01-14 16:28:27 +01:00
|
|
|
if err != nil {
|
|
|
|
handlers.modelHandler("contests", pattern).ServeHTTP(rr, req)
|
2020-01-11 09:11:49 +01:00
|
|
|
|
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-15 16:42:35 +01:00
|
|
|
doc, err := goquery.NewDocumentFromResponse(rr)
|
|
|
|
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)
|
|
|
|
})
|
2020-01-14 16:28:27 +01:00
|
|
|
t.True(strings.Contains(rr.Body.String(), "JUNIOR Contest"))
|
|
|
|
}
|
2020-01-11 09:11:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|