142 lines
2.7 KiB
Go
142 lines
2.7 KiB
Go
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"))
|
|
}
|
|
|
|
}
|