2021-06-01 10:51:35 +02:00
|
|
|
package webtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/database"
|
2021-06-25 12:22:19 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/mailer"
|
2021-06-01 10:51:35 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/web"
|
|
|
|
)
|
|
|
|
|
2021-06-01 15:30:05 +02:00
|
|
|
var (
|
2021-06-01 17:47:37 +02:00
|
|
|
// DBConnection - url to database on setting up default WebService for webtest
|
2021-06-01 15:30:05 +02:00
|
|
|
DBConnection = "user=root password=root dbname=defaultdb host=localhost port=26257 sslmode=disable"
|
|
|
|
)
|
|
|
|
|
2021-07-14 13:57:23 +02:00
|
|
|
// Option to configure TestServer
|
|
|
|
type Option struct {
|
|
|
|
ReRun bool
|
|
|
|
DBSetup func(db *database.Database)
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:51:35 +02:00
|
|
|
type testServer struct {
|
2021-06-22 20:44:24 +02:00
|
|
|
DB *database.Database
|
2021-06-25 12:22:19 +02:00
|
|
|
Mails chan *mailer.TestingMail
|
|
|
|
Close func()
|
2021-06-01 10:51:35 +02:00
|
|
|
gin *gin.Engine
|
|
|
|
ws *web.Service
|
|
|
|
lastCookies []*http.Cookie
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:47:37 +02:00
|
|
|
// Login Request format (maybe just internal usage)
|
2021-06-01 10:51:35 +02:00
|
|
|
type Login struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:47:37 +02:00
|
|
|
// New starts WebService for testing
|
2021-06-28 13:59:27 +02:00
|
|
|
func New() (*testServer, error) {
|
2021-07-14 13:57:23 +02:00
|
|
|
return NewWithOption(Option{})
|
2021-07-14 12:29:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithDBSetup allows to reconfigure before ReRun the database - e.g. for adding Migration-Steps
|
|
|
|
func NewWithDBSetup(dbCall func(db *database.Database)) (*testServer, error) {
|
2021-07-14 13:57:23 +02:00
|
|
|
return NewWithOption(Option{ReRun: true, DBSetup: dbCall})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithOption allows to configure WebService for testing
|
|
|
|
func NewWithOption(option Option) (*testServer, error) {
|
2021-06-01 10:51:35 +02:00
|
|
|
// db setup
|
|
|
|
dbConfig := database.Database{
|
2021-06-01 15:30:05 +02:00
|
|
|
Connection: DBConnection,
|
2021-06-01 10:51:35 +02:00
|
|
|
Testdata: true,
|
|
|
|
Debug: false,
|
|
|
|
LogLevel: 0,
|
|
|
|
}
|
2021-07-14 13:57:23 +02:00
|
|
|
if option.DBSetup != nil {
|
|
|
|
option.DBSetup(&dbConfig)
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
if option.ReRun {
|
|
|
|
err = dbConfig.ReRun()
|
|
|
|
} else {
|
|
|
|
err = dbConfig.Run()
|
2021-07-14 12:29:58 +02:00
|
|
|
}
|
2021-06-01 10:51:35 +02:00
|
|
|
if err != nil && err != database.ErrNothingToMigrate {
|
2021-06-28 13:59:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if dbConfig.DB == nil {
|
|
|
|
return nil, database.ErrNotConnected
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// api setup
|
|
|
|
gin.EnableJsonDecoderDisallowUnknownFields()
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
2021-06-25 12:22:19 +02:00
|
|
|
mock, mail := mailer.NewFakeServer()
|
|
|
|
|
|
|
|
err = mail.Setup()
|
2021-06-28 13:59:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-25 12:22:19 +02:00
|
|
|
|
2021-06-01 10:51:35 +02:00
|
|
|
ws := &web.Service{
|
2021-06-25 12:22:19 +02:00
|
|
|
DB: dbConfig.DB,
|
|
|
|
Mailer: mail,
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
ws.Session.Name = "mysession"
|
|
|
|
ws.Session.Secret = "hidden"
|
|
|
|
|
|
|
|
r := gin.Default()
|
|
|
|
ws.LoadSession(r)
|
|
|
|
ws.Bind(r)
|
|
|
|
return &testServer{
|
2021-06-28 13:59:27 +02:00
|
|
|
DB: &dbConfig,
|
|
|
|
Mails: mock.Mails,
|
|
|
|
Close: mock.Close,
|
|
|
|
gin: r,
|
|
|
|
ws: ws,
|
|
|
|
}, nil
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
2021-06-01 17:47:37 +02:00
|
|
|
|
2021-06-15 17:58:02 +02:00
|
|
|
// DatabaseForget, to run a test without a database
|
|
|
|
func (s *testServer) DatabaseForget() {
|
|
|
|
s.ws.DB = nil
|
2021-06-22 20:44:24 +02:00
|
|
|
s.DB = nil
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
2021-06-01 17:47:37 +02:00
|
|
|
|
|
|
|
// Request sends a request to webtest WebService
|
2021-06-28 12:14:20 +02:00
|
|
|
func (s *testServer) Request(method, url string, body interface{}, expectCode int, jsonObj interface{}) error {
|
2021-06-01 10:51:35 +02:00
|
|
|
var jsonBody io.Reader
|
|
|
|
if body != nil {
|
|
|
|
if strBody, ok := body.(string); ok {
|
|
|
|
jsonBody = strings.NewReader(strBody)
|
|
|
|
} else {
|
|
|
|
jsonBodyArray, err := json.Marshal(body)
|
2021-06-28 12:14:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-01 10:51:35 +02:00
|
|
|
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest(method, url, jsonBody)
|
2021-06-28 12:14:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-01 17:47:37 +02:00
|
|
|
if len(s.lastCookies) > 0 {
|
|
|
|
for _, c := range s.lastCookies {
|
2021-06-01 10:51:35 +02:00
|
|
|
req.AddCookie(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
2021-06-01 17:47:37 +02:00
|
|
|
s.gin.ServeHTTP(w, req)
|
2021-06-01 10:51:35 +02:00
|
|
|
|
|
|
|
// valid statusCode
|
|
|
|
if expectCode != w.Code {
|
2021-06-28 12:14:20 +02:00
|
|
|
return fmt.Errorf("wrong status code, body: %v", w.Body)
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if jsonObj != nil {
|
|
|
|
// fetch JSON
|
|
|
|
err = json.NewDecoder(w.Body).Decode(jsonObj)
|
2021-06-28 12:14:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := w.Result()
|
|
|
|
if result != nil {
|
|
|
|
cookies := result.Cookies()
|
|
|
|
if len(cookies) > 0 {
|
2021-06-01 17:47:37 +02:00
|
|
|
s.lastCookies = cookies
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-28 12:14:20 +02:00
|
|
|
return nil
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 17:49:13 +02:00
|
|
|
// Login to API by send request
|
2021-06-28 12:14:20 +02:00
|
|
|
func (s *testServer) Login(login Login) error {
|
2021-06-01 10:51:35 +02:00
|
|
|
// POST: correct login
|
2021-06-28 12:14:20 +02:00
|
|
|
return s.Request(http.MethodPost, "/api/v1/auth/login", &login, http.StatusOK, nil)
|
2021-06-01 10:51:35 +02:00
|
|
|
}
|
|
|
|
|
2021-06-01 17:49:13 +02:00
|
|
|
// TestLogin to API by default login data
|
2021-06-28 12:14:20 +02:00
|
|
|
func (s *testServer) TestLogin() error {
|
|
|
|
return s.Login(Login{
|
2021-06-01 10:51:35 +02:00
|
|
|
Username: "admin",
|
|
|
|
Password: "CHANGEME",
|
|
|
|
})
|
|
|
|
}
|