124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/stretchr/testify/assert"
|
|
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
)
|
|
|
|
func doGet(t *testing.T, app *fiber.App, rawURL string) (*http.Response, error) {
|
|
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
t.Fatalf("Invalid url: %s", rawURL)
|
|
}
|
|
|
|
req := httptest.NewRequest("GET", u.RequestURI(), nil)
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Host = u.Host
|
|
|
|
return app.Test(req)
|
|
}
|
|
|
|
func doPost(t *testing.T, app *fiber.App, rawURL string, jsonBody interface{}) (*http.Response, error) {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
t.Fatalf("Invalid url: %s", rawURL)
|
|
}
|
|
|
|
buf, err := json.Marshal(jsonBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := httptest.NewRequest("POST", u.RequestURI(), bytes.NewReader(buf))
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Host = u.Host
|
|
return app.Test(req)
|
|
}
|
|
|
|
func doPut(t *testing.T, app *fiber.App, rawURL string, jsonBody interface{}) (*http.Response, error) {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
t.Fatalf("Invalid url: %s", rawURL)
|
|
}
|
|
|
|
buf, err := json.Marshal(jsonBody)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req := httptest.NewRequest("PUT", u.RequestURI(), bytes.NewReader(buf))
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
req.Host = u.Host
|
|
return app.Test(req)
|
|
}
|
|
|
|
func doDelete(t *testing.T, app *fiber.App, rawURL string) (*http.Response, error) {
|
|
u, err := url.Parse(rawURL)
|
|
if err != nil {
|
|
t.Fatalf("Invalid url: %s", rawURL)
|
|
}
|
|
|
|
req := httptest.NewRequest("DELETE", u.RequestURI(), nil)
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Host = u.Host
|
|
return app.Test(req)
|
|
}
|
|
|
|
func TestServer(t *testing.T) {
|
|
//var err error
|
|
trainingRepo := training.NewInMemoryTrainingRepository()
|
|
trainingDateRepo := training.NewInMemoryTrainingDateRepository()
|
|
trainingDateAttendeeRepo := training.NewInMemoryTrainingDateAttendeeRepository()
|
|
|
|
handlers := NewAPIHandlers(trainingRepo, trainingDateRepo, trainingDateAttendeeRepo)
|
|
app := NewTestingServer(handlers)
|
|
|
|
t.Run("Add training", func(t *testing.T) {
|
|
newTraining := NewTraining{
|
|
Name: "Testing Training",
|
|
Description: "This is a test training",
|
|
Days: 1,
|
|
Pricing: []TrainingPrice{
|
|
//{
|
|
// Amount: decimal.NewFromInt(200),
|
|
// Currency: "EUR",
|
|
// Type: OPEN,
|
|
//},
|
|
//{
|
|
// Amount: decimal.NewFromInt(1000),
|
|
// Currency: "EUR",
|
|
// Type: CORPORATE,
|
|
//},
|
|
//{
|
|
// Amount: decimal.NewFromInt(4900),
|
|
// Currency: "CZK",
|
|
// Type: OPEN,
|
|
//},
|
|
//{
|
|
// Amount: decimal.NewFromInt(24000),
|
|
// Currency: "CZK",
|
|
// Type: CORPORATE,
|
|
//},
|
|
},
|
|
}
|
|
|
|
rr, _ := doPost(t, app, "/v1/trainings", newTraining)
|
|
assert.Equal(t, http.StatusCreated, rr.StatusCode)
|
|
|
|
var resultTraining Training
|
|
err := json.NewDecoder(rr.Body).Decode(&resultTraining)
|
|
assert.NoError(t, err, "error unmarshalling response")
|
|
assert.Equal(t, newTraining.Name, resultTraining.Name)
|
|
assert.Equal(t, newTraining.Description, resultTraining.Description)
|
|
assert.Equal(t, newTraining.Days, resultTraining.Days)
|
|
//assert.Equal(t, newTraining.Pricing, resultTraining.Pricing)
|
|
})
|
|
}
|