add some test
This commit is contained in:
parent
b59e8bc5c6
commit
0649adf82a
|
@ -31,6 +31,6 @@ func SessionStart(w http.ResponseWriter, r *http.Request) assession.Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionDestroy destroy a session on a request
|
// SessionDestroy destroy a session on a request
|
||||||
func SessionDestroy(w http.ResponseWriter, r *http.Request) assession.Session {
|
func SessionDestroy(w http.ResponseWriter, r *http.Request) {
|
||||||
return SessionDestroy(w, r)
|
data.SessionDestroy(w, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ func logout(ctx context.Context, w http.ResponseWriter, r *http.Request) (return
|
||||||
libsession.SessionDestroy(w, r)
|
libsession.SessionDestroy(w, r)
|
||||||
logger := log.GetLog(r, "logout")
|
logger := log.GetLog(r, "logout")
|
||||||
if login := sess.Get("login"); login != nil {
|
if login := sess.Get("login"); login != nil {
|
||||||
logger = logger.WithField("user", login.(Login).Username)
|
logger = logger.WithField("user", login.(*Login).Username)
|
||||||
}
|
}
|
||||||
sess.Delete("login")
|
sess.Delete("login")
|
||||||
sess.Delete("profil")
|
sess.Delete("profil")
|
||||||
|
@ -98,7 +98,7 @@ func login(ctx context.Context, w http.ResponseWriter, r *http.Request) (returnd
|
||||||
if output {
|
if output {
|
||||||
returndata = true
|
returndata = true
|
||||||
dbconnection.Model(&login).Update("LastLoginAt", time.Now())
|
dbconnection.Model(&login).Update("LastLoginAt", time.Now())
|
||||||
sess.Set("login", login)
|
sess.Set("login", &login)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
} else {
|
} else {
|
||||||
logger.Warn("wrong password")
|
logger.Warn("wrong password")
|
||||||
|
@ -122,8 +122,7 @@ func password(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
err := json.NewDecoder(r.Body).Decode(&changePasswordRequest)
|
err := json.NewDecoder(r.Body).Decode(&changePasswordRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("fetch request")
|
logger.Error("fetch request")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
output, _ := libpassword.Validate(login.Password, changePasswordRequest.CurrentPassword)
|
output, _ := libpassword.Validate(login.Password, changePasswordRequest.CurrentPassword)
|
||||||
|
@ -144,7 +143,7 @@ func password(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error save new password"}
|
returnerr = &libapi.ErrorResult{Message: "Error save new password"}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sess.Set("login", *login)
|
sess.Set("login", login)
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = true
|
returndata = true
|
||||||
return
|
return
|
||||||
|
@ -191,8 +190,7 @@ func inviteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret
|
||||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("fetch request")
|
logger.Error("fetch request")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
invite := &Invite{
|
invite := &Invite{
|
||||||
|
@ -224,8 +222,8 @@ func loginList(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret
|
||||||
selectfield = "ID, mail, superadmin, active"
|
selectfield = "ID, mail, superadmin, active"
|
||||||
}
|
}
|
||||||
if err := dbconnection.Select(selectfield).Find(&logins).Error; err != nil {
|
if err := dbconnection.Select(selectfield).Find(&logins).Error; err != nil {
|
||||||
logger.Warn("sql edit login")
|
logger.Warn("sql list login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Error during edit login"}
|
returnerr = &libapi.ErrorResult{Message: "Error during list login"}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
|
@ -247,8 +245,7 @@ func loginAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("fetch request")
|
logger.Error("fetch request")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
loginObj := Login{
|
loginObj := Login{
|
||||||
|
@ -283,8 +280,7 @@ func loginEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret
|
||||||
err = json.NewDecoder(r.Body).Decode(&changeLogin)
|
err = json.NewDecoder(r.Body).Decode(&changeLogin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("fetch request")
|
logger.Error("fetch request")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,20 +19,26 @@ func TestAPI(t *testing.T) {
|
||||||
BindAPI(db, router, "")
|
BindAPI(db, router, "")
|
||||||
session := test.NewSession(router)
|
session := test.NewSession(router)
|
||||||
|
|
||||||
// Test status
|
/*
|
||||||
|
* TEST status
|
||||||
|
*/
|
||||||
result, w := session.JSONRequest("GET", "/status", nil)
|
result, w := session.JSONRequest("GET", "/status", nil)
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Nil(result.Error)
|
assert.Nil(result.Error)
|
||||||
assert.Equal(result.Data, true)
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
// Test login
|
/*
|
||||||
|
* TEST login
|
||||||
|
*/
|
||||||
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"})
|
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"})
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Equal(result.Data, false)
|
assert.Equal(result.Data, false)
|
||||||
|
assert.Equal(result.Error.Fields[0], "password")
|
||||||
|
|
||||||
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
|
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Equal(result.Data, false)
|
assert.Equal(result.Data, false)
|
||||||
|
assert.Equal(result.Error.Fields[0], "username")
|
||||||
|
|
||||||
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
|
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
|
||||||
assert.Equal(w.StatusCode, http.StatusBadRequest)
|
assert.Equal(w.StatusCode, http.StatusBadRequest)
|
||||||
|
@ -41,7 +47,9 @@ func TestAPI(t *testing.T) {
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Equal(result.Data, true)
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
// Test logout
|
/*
|
||||||
|
* TEST logout
|
||||||
|
*/
|
||||||
result, w = session.JSONRequest("GET", "/logout", nil)
|
result, w = session.JSONRequest("GET", "/logout", nil)
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Equal(result.Data, true)
|
assert.Equal(result.Data, true)
|
||||||
|
@ -50,4 +58,70 @@ func TestAPI(t *testing.T) {
|
||||||
result, w = session.JSONRequest("GET", "/logout", nil)
|
result, w = session.JSONRequest("GET", "/logout", nil)
|
||||||
assert.Equal(w.StatusCode, http.StatusOK)
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
assert.Equal(result.Data, false)
|
assert.Equal(result.Data, false)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST password
|
||||||
|
*/
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, false)
|
||||||
|
|
||||||
|
//login before
|
||||||
|
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", []byte{2, 3})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusBadRequest)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-wrong", NewPassword: "root-bug"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, false)
|
||||||
|
assert.Equal(result.Error.Fields[0], "currentpassword")
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: ""})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, false)
|
||||||
|
assert.Equal(result.Error.Fields[0], "newpassword")
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-tmp"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-tmp", NewPassword: "root"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST inviteList
|
||||||
|
*/
|
||||||
|
session.Clean()
|
||||||
|
result, w = session.JSONRequest("GET", "/invite", nil)
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, false)
|
||||||
|
|
||||||
|
//login before
|
||||||
|
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("GET", "/invite", nil)
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST loginList
|
||||||
|
*/
|
||||||
|
session.Clean()
|
||||||
|
result, w = session.JSONRequest("GET", "/user", nil)
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, false)
|
||||||
|
|
||||||
|
//login before
|
||||||
|
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assert.Equal(result.Data, true)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("GET", "/user", nil)
|
||||||
|
assert.Equal(w.StatusCode, http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ func LoginHandler(h libapi.Handle) libapi.Handle {
|
||||||
returndata = false
|
returndata = false
|
||||||
|
|
||||||
if login := sess.Get("login"); login != nil {
|
if login := sess.Get("login"); login != nil {
|
||||||
if loginObj := login.(Login); loginObj.Active {
|
if loginObj := login.(*Login); loginObj.Active {
|
||||||
ctx = context.WithValue(ctx, "login", &loginObj)
|
ctx = context.WithValue(ctx, "login", loginObj)
|
||||||
returndata, returnerr = h(ctx, w, r)
|
returndata, returnerr = h(ctx, w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
21
test/main.go
21
test/main.go
|
@ -5,11 +5,9 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
|
||||||
"os"
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/astaxie/session"
|
|
||||||
_ "github.com/astaxie/session/providers/memory"
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -18,21 +16,21 @@ import (
|
||||||
libconfig "dev.sum7.eu/sum7/warehost/config"
|
libconfig "dev.sum7.eu/sum7/warehost/config"
|
||||||
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
log "dev.sum7.eu/sum7/warehost/lib/log"
|
log "dev.sum7.eu/sum7/warehost/lib/log"
|
||||||
|
libsession "dev.sum7.eu/sum7/warehost/lib/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Init to initialisieren a API
|
//Init to initialisieren a API
|
||||||
func Init(t *testing.T) (assertion *assert.Assertions, dbconnection *gorm.DB, router *goji.Mux) {
|
func Init(t *testing.T) (assertion *assert.Assertions, dbconnection *gorm.DB, router *goji.Mux) {
|
||||||
assertion = assert.New(t)
|
assertion = assert.New(t)
|
||||||
libconfig.ReadConfigFile(os.Getenv("GOPATH") +"/src/dev.sum7.eu/sum7/warehost/cmd/warehost/config.yml.example")
|
libconfig.ReadConfigFile(os.Getenv("GOPATH") + "/src/dev.sum7.eu/sum7/warehost/cmd/warehost/config.yml.example")
|
||||||
log.NewLogger(libconfig.Data.Log.Path)
|
log.NewLogger(libconfig.Data.Log.Path)
|
||||||
|
|
||||||
// Session mgmt
|
// Session mgmt
|
||||||
sessions, err := session.NewManager("memory", "session", 3600)
|
libsession.Init()
|
||||||
go sessions.GC()
|
go libsession.Stop()
|
||||||
assertion.NoError(err)
|
|
||||||
|
|
||||||
// Database
|
// Database
|
||||||
dbconnection, err = gorm.Open("postgres", libconfig.Data.Database)
|
dbconnection, err := gorm.Open("postgres", libconfig.Data.Database)
|
||||||
assertion.NoError(err)
|
assertion.NoError(err)
|
||||||
|
|
||||||
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
dbconnection.Callback().Create().Remove("gorm:update_time_stamp")
|
||||||
|
@ -67,12 +65,15 @@ func (r *Request) JSONRequest(method string, url string, body interface{}) (json
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r.router.ServeHTTP(w, req)
|
r.router.ServeHTTP(w, req)
|
||||||
res = w.Result()
|
res = w.Result()
|
||||||
r.cookies = res.Cookies()
|
cookies := res.Cookies()
|
||||||
|
if len(cookies) > 0 {
|
||||||
|
r.cookies = cookies
|
||||||
|
}
|
||||||
json.NewDecoder(w.Body).Decode(&jsonResult)
|
json.NewDecoder(w.Body).Decode(&jsonResult)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanSession to clean the current session
|
// CleanSession to clean the current session
|
||||||
func (r *Request) CleanSession() {
|
func (r *Request) Clean() {
|
||||||
r.cookies = nil
|
r.cookies = nil
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue