sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

[system] add testing

This commit is contained in:
Martin Geno 2016-10-07 15:13:31 +02:00
parent 8230bd91d9
commit 8eb3aedc62
3 changed files with 132 additions and 5 deletions

View File

@ -75,6 +75,7 @@ func (api *API) Logout(w http.ResponseWriter, r *http.Request, _ httprouter.Para
sess.Delete("profil")
logger.Info("done")
returndata = true
return
}
@ -86,10 +87,7 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
err := json.NewDecoder(r.Body).Decode(&requestlogin)
if err != nil {
logger.Error("fetch request")
http.Error(w, err.Error(), http.StatusInternalServerError)
returnerr = &libapi.ErrorResult{
Message: "Internal Request Error",
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
logger = logger.WithField("user", requestlogin.Username)
@ -115,7 +113,6 @@ func (api *API) Login(w http.ResponseWriter, r *http.Request, _ httprouter.Param
logger.Warn("not active")
returnerr = &libapi.ErrorResult{Fields: []string{"active"}, Message: "Not a active User"}
}
return
}

53
system/api_test.go Normal file
View File

@ -0,0 +1,53 @@
package system
import (
"net/http"
"testing"
"dev.sum7.de/sum7/warehost/test"
)
func TestAPI(t *testing.T) {
assert, config, sessions, db, router := test.Init(t)
defer db.Close()
//load system Models to database
SyncModels(db)
// Bind API
NewAPI(config, sessions, db, router, "")
session := test.NewSession(router)
// Test status
result, w := session.JSONRequest("GET", "/status", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Nil(result.Error)
assert.Equal(result.Data, true)
// Test login
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
assert.Equal(w.StatusCode, http.StatusBadRequest)
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, true)
// Test logout
result, w = session.JSONRequest("GET", "/logout", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, true)
// Test if crash on if not login in
result, w = session.JSONRequest("GET", "/logout", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
}

77
test/main.go Normal file
View File

@ -0,0 +1,77 @@
package test
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/astaxie/session"
_ "github.com/astaxie/session/providers/memory"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
libconfig "dev.sum7.de/sum7/warehost/config"
libapi "dev.sum7.de/sum7/warehost/lib/api"
log "dev.sum7.de/sum7/warehost/lib/log"
)
//Init to initialisieren a API
func Init(t *testing.T) (assertion *assert.Assertions, config *libconfig.Config, sessions *session.Manager, dbconnection *gorm.DB, router *httprouter.Router) {
assertion = assert.New(t)
config = libconfig.ReadConfigFile("../cmd/warehost/config.yml.example")
log.NewLogger(config.Log.Path)
// Session mgmt
sessions, err := session.NewManager("memory", "session", 3600)
go sessions.GC()
assertion.NoError(err)
// Database
dbconnection, err = gorm.Open("postgres", config.Database)
assertion.NoError(err)
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
dbconnection.Callback().Update().Remove("gorm:update_time_stamp")
dbconnection.SingularTable(true)
dbconnection.LogMode(config.DatabaseDebug)
router = httprouter.New()
return
}
// Request a easy manager to test REST-API
type Request struct {
req *http.Request
cookies []*http.Cookie
router *httprouter.Router
}
// NewSession to get a new easy manager
func NewSession(router *httprouter.Router) *Request {
return &Request{router: router}
}
// JSONRequest send request to router
func (r *Request) JSONRequest(method string, url string, body interface{}) (jsonResult libapi.JSONResult, res *http.Response) {
jsonObj, _ := json.Marshal(body)
req, _ := http.NewRequest(method, url, bytes.NewReader(jsonObj))
for _, c := range r.cookies {
req.AddCookie(c)
}
w := httptest.NewRecorder()
r.router.ServeHTTP(w, req)
res = w.Result()
r.cookies = res.Cookies()
json.NewDecoder(w.Body).Decode(&jsonResult)
return
}
// CleanSession to clean the current session
func (r *Request) CleanSession() {
r.cookies = nil
}