json and test refactory
This commit is contained in:
parent
d3b8a5c5ae
commit
bbf84b1232
|
@ -2,8 +2,10 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
log "github.com/Sirupsen/logrus" // TODO-Bad
|
||||||
"goji.io"
|
"goji.io"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
@ -59,3 +61,17 @@ func SessionHandler(h Handle) goji.HandlerFunc {
|
||||||
JSONOutput(ctx, w, r, data, err)
|
JSONOutput(ctx, w, r, data, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//JSONDecoder handle complete request of JSON
|
||||||
|
func JSONDecoder(r io.Reader, data interface{}, w http.ResponseWriter, logger *log.Entry) (returnerr *ErrorResult) {
|
||||||
|
err := json.NewDecoder(r).Decode(data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("fetch request")
|
||||||
|
returnerr = &ErrorResult{
|
||||||
|
Message: "Internal Request Error",
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -30,7 +30,9 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
router.HandleFuncC(pat.Delete(prefix+"/delete"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(delete))))
|
router.HandleFuncC(pat.Delete(prefix+"/delete"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(delete))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/profil"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(profil))))
|
router.HandleFuncC(pat.Get(prefix+"/profil"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(profil))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainList))))
|
router.HandleFuncC(pat.Get(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainList))))
|
||||||
|
router.HandleFuncC(pat.Post(prefix+"/domain"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(domainAdd))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList))))
|
router.HandleFuncC(pat.Get(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseList))))
|
||||||
|
router.HandleFuncC(pat.Post(prefix+"/database"), libapi.SessionHandler(system.LoginHandler(ProfilHandler(databaseAdd))))
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkSignup(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func checkSignup(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
|
|
@ -76,6 +76,10 @@ func TestAPI(t *testing.T) {
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
assertion.Equal(result.Data, true)
|
assertion.Equal(result.Data, true)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TEST profil
|
* TEST profil
|
||||||
*/
|
*/
|
||||||
|
@ -98,32 +102,4 @@ func TestAPI(t *testing.T) {
|
||||||
result, w = session.JSONRequest("GET", "/host/profil", nil)
|
result, w = session.JSONRequest("GET", "/host/profil", nil)
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
assertion.NotEqual(result.Data, false)
|
assertion.NotEqual(result.Data, false)
|
||||||
|
|
||||||
/*
|
|
||||||
* TEST domainList
|
|
||||||
*/
|
|
||||||
session.Clean()
|
|
||||||
result, w = session.JSONRequest("GET", "/host/domain", nil)
|
|
||||||
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
|
||||||
assertion.Equal(result.Data, false)
|
|
||||||
|
|
||||||
loginTest(session, assertion)
|
|
||||||
|
|
||||||
result, w = session.JSONRequest("GET", "/host/domain", nil)
|
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
||||||
assertion.NotEqual(result.Data, false)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* TEST databaseList
|
|
||||||
*/
|
|
||||||
session.Clean()
|
|
||||||
result, w = session.JSONRequest("GET", "/host/database", nil)
|
|
||||||
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
|
||||||
assertion.Equal(result.Data, false)
|
|
||||||
|
|
||||||
loginTest(session, assertion)
|
|
||||||
|
|
||||||
result, w = session.JSONRequest("GET", "/host/database", nil)
|
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
||||||
assertion.NotEqual(result.Data, false)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
profil := ctx.Value("profil").(*Profil)
|
||||||
|
returndata = false
|
||||||
|
logger := log.GetLog(r, "databaselist")
|
||||||
|
var database []*Database
|
||||||
|
dbconnection.Where("profil = ?", profil.ID).Find(&database)
|
||||||
|
logger.Info("done")
|
||||||
|
returndata = database
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func databaseAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
|
profil := ctx.Value("profil").(*Profil)
|
||||||
|
returndata = false
|
||||||
|
logger := log.GetLog(r, "databaseadd")
|
||||||
|
|
||||||
|
var databaseRequest Database
|
||||||
|
returnerr = libapi.JSONDecoder(r.Body, &databaseRequest, w, logger)
|
||||||
|
if returnerr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
database := &Database{
|
||||||
|
ProfilID: profil.ID,
|
||||||
|
Password: databaseRequest.Password,
|
||||||
|
Comment: databaseRequest.Comment,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dbconnection.Create(database).Error; err != nil {
|
||||||
|
logger.Error("database: during create host database: ", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returndata = true
|
||||||
|
logger.Info("done")
|
||||||
|
return
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"dev.sum7.eu/sum7/warehost/system"
|
||||||
|
"dev.sum7.eu/sum7/warehost/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAPIDatabase(t *testing.T) {
|
||||||
|
|
||||||
|
assertion, db, router := test.Init(t)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
//load system Models to database
|
||||||
|
system.SyncModels(db)
|
||||||
|
db.Unscoped().Delete(Profil{})
|
||||||
|
SyncModels(db)
|
||||||
|
|
||||||
|
// Bind API
|
||||||
|
system.BindAPI(db, router, "")
|
||||||
|
BindAPI(db, router, "/host")
|
||||||
|
session := test.NewSession(router)
|
||||||
|
|
||||||
|
loginTest(session, assertion)
|
||||||
|
|
||||||
|
result, w := session.JSONRequest("DELETE", "/host/delete", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
|
// Need a Profile for Next tests
|
||||||
|
result, w = session.JSONRequest("POST", "/host/signup", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.Equal(result.Data, true)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST databaseList
|
||||||
|
*/
|
||||||
|
session.Clean()
|
||||||
|
result, w = session.JSONRequest("GET", "/host/database", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
|
loginTest(session, assertion)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("GET", "/host/database", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.NotEqual(result.Data, false)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST databaseAdd
|
||||||
|
*/
|
||||||
|
result, w = session.JSONRequest("POST", "/host/database", Database{
|
||||||
|
Password: "example.de",
|
||||||
|
Comment: "test",
|
||||||
|
})
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.NotEqual(result.Data, false)
|
||||||
|
}
|
|
@ -19,13 +19,31 @@ func domainList(ctx context.Context, w http.ResponseWriter, r *http.Request) (re
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
func domainAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
||||||
profil := ctx.Value("profil").(*Profil)
|
profil := ctx.Value("profil").(*Profil)
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "databaselist")
|
logger := log.GetLog(r, "domainadd")
|
||||||
var database []*Database
|
|
||||||
dbconnection.Where("profil = ?", profil.ID).Find(&database)
|
var domainRequest Domain
|
||||||
|
returnerr = libapi.JSONDecoder(r.Body, &domainRequest, w, logger)
|
||||||
|
if returnerr != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
domain := &Domain{
|
||||||
|
ProfilID: profil.ID,
|
||||||
|
FQDN: domainRequest.FQDN,
|
||||||
|
Mail: domainRequest.Mail,
|
||||||
|
Web: domainRequest.Web,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dbconnection.Create(domain).Error; err != nil {
|
||||||
|
logger.Error("database: during create host domain: ", err)
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returndata = true
|
||||||
logger.Info("done")
|
logger.Info("done")
|
||||||
returndata = database
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package host
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"dev.sum7.eu/sum7/warehost/system"
|
||||||
|
"dev.sum7.eu/sum7/warehost/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAPIDomain(t *testing.T) {
|
||||||
|
|
||||||
|
assertion, db, router := test.Init(t)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
//load system Models to database
|
||||||
|
system.SyncModels(db)
|
||||||
|
db.Unscoped().Delete(Profil{})
|
||||||
|
SyncModels(db)
|
||||||
|
|
||||||
|
// Bind API
|
||||||
|
system.BindAPI(db, router, "")
|
||||||
|
BindAPI(db, router, "/host")
|
||||||
|
session := test.NewSession(router)
|
||||||
|
|
||||||
|
loginTest(session, assertion)
|
||||||
|
|
||||||
|
result, w := session.JSONRequest("DELETE", "/host/delete", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
|
||||||
|
// Need a Profile for Next tests
|
||||||
|
result, w = session.JSONRequest("POST", "/host/signup", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.Equal(result.Data, true)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST domainList
|
||||||
|
*/
|
||||||
|
session.Clean()
|
||||||
|
result, w = session.JSONRequest("GET", "/host/domain", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
|
loginTest(session, assertion)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("GET", "/host/domain", nil)
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.NotEqual(result.Data, false)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* TEST domainAdd
|
||||||
|
*/
|
||||||
|
result, w = session.JSONRequest("POST", "/host/domain", Domain{
|
||||||
|
FQDN: "example.de",
|
||||||
|
})
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
||||||
|
assertion.NotEqual(result.Data, false)
|
||||||
|
|
||||||
|
result, w = session.JSONRequest("POST", "/host/domain", []byte{2, 3})
|
||||||
|
assertion.Equal(w.StatusCode, http.StatusBadRequest)
|
||||||
|
assertion.Equal(result.Data, false)
|
||||||
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
@ -28,7 +27,7 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
|
|
||||||
router.HandleFuncC(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve)))
|
router.HandleFuncC(pat.Get(prefix+"/involve"), libapi.SessionHandler(libsystem.LoginHandler(involve)))
|
||||||
router.HandleFuncC(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd)))
|
router.HandleFuncC(pat.Post(prefix+"/website"), libapi.SessionHandler(libsystem.LoginHandler(websiteAdd)))
|
||||||
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
|
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteEdit))))
|
||||||
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete))))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(websiteDelete))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList))))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/permission"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionList))))
|
||||||
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd))))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/permission/:loginid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(permissionAdd))))
|
||||||
|
@ -39,11 +38,11 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree))))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuTree))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList))))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/menu/list"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuList))))
|
||||||
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd))))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/menu"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuAdd))))
|
||||||
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
|
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuEdit))))
|
||||||
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete))))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/menu/:menuid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(menuDelete))))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList))))
|
router.HandleFuncC(pat.Get(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageList))))
|
||||||
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd))))
|
router.HandleFuncC(pat.Post(prefix+"/website/:websiteid/page"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageAdd))))
|
||||||
router.HandleFuncC(pat.Put(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
|
router.HandleFuncC(pat.Patch(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageEdit))))
|
||||||
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
|
router.HandleFuncC(pat.Delete(prefix+"/website/:websiteid/page/:pageid"), libapi.SessionHandler(libsystem.LoginHandler(InvolveWebsiteHandler(pageDelete))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,12 +65,9 @@ func websiteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (re
|
||||||
logger := log.GetLog(r, "websiteadd")
|
logger := log.GetLog(r, "websiteadd")
|
||||||
tx := dbconnection.Begin()
|
tx := dbconnection.Begin()
|
||||||
var websiteRequest Website
|
var websiteRequest Website
|
||||||
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
returnerr = libapi.JSONDecoder(r.Body, &websiteRequest, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
website := &Website{Name: websiteRequest.Name}
|
website := &Website{Name: websiteRequest.Name}
|
||||||
|
@ -100,13 +96,11 @@ func websiteEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (r
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "websiteedit")
|
logger := log.GetLog(r, "websiteedit")
|
||||||
var websiteRequest Website
|
var websiteRequest Website
|
||||||
err := json.NewDecoder(r.Body).Decode(&websiteRequest)
|
returnerr = libapi.JSONDecoder(r.Body, &websiteRequest, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{Message: "Internal Request Error"}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
websiteRequest.ID = ctx.Value("websiteid").(int64)
|
websiteRequest.ID = ctx.Value("websiteid").(int64)
|
||||||
if err := dbconnection.Save(websiteRequest).Error; err != nil {
|
if err := dbconnection.Save(websiteRequest).Error; err != nil {
|
||||||
logger.Error("Database: during edit Website")
|
logger.Error("Database: during edit Website")
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -38,13 +37,8 @@ func menuAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (retur
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "menuadd")
|
logger := log.GetLog(r, "menuadd")
|
||||||
var menuEntry Menu
|
var menuEntry Menu
|
||||||
err := json.NewDecoder(r.Body).Decode(&menuEntry)
|
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{
|
|
||||||
Message: "Internal Request Error",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,13 +66,8 @@ func menuEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", menuid)
|
logger = logger.WithField("id", menuid)
|
||||||
err = json.NewDecoder(r.Body).Decode(&menuEntry)
|
returnerr = libapi.JSONDecoder(r.Body, &menuEntry, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{
|
|
||||||
Message: "Internal Request Error",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
menuEntry.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -27,13 +26,8 @@ func pageAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (retur
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "pageadd")
|
logger := log.GetLog(r, "pageadd")
|
||||||
var page Page
|
var page Page
|
||||||
err := json.NewDecoder(r.Body).Decode(&page)
|
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{
|
|
||||||
Message: "Internal Request Error",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,13 +59,8 @@ func pageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger = logger.WithField("id", pageid)
|
logger = logger.WithField("id", pageid)
|
||||||
err = json.NewDecoder(r.Body).Decode(&page)
|
returnerr = libapi.JSONDecoder(r.Body, &page, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
returnerr = &libapi.ErrorResult{
|
|
||||||
Message: "Internal Request Error",
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
page.WebsiteID = ctx.Value("websiteid").(int64)
|
page.WebsiteID = ctx.Value("websiteid").(int64)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package system
|
package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -38,10 +37,10 @@ func BindAPI(db *gorm.DB, router *goji.Mux, prefix string) {
|
||||||
router.HandleFuncC(pat.Post(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteAdd)))
|
router.HandleFuncC(pat.Post(prefix+"/invite"), libapi.SessionHandler(LoginHandler(inviteAdd)))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginList)))
|
router.HandleFuncC(pat.Get(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginList)))
|
||||||
router.HandleFuncC(pat.Post(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginAdd)))
|
router.HandleFuncC(pat.Post(prefix+"/user"), libapi.SessionHandler(LoginHandler(loginAdd)))
|
||||||
router.HandleFuncC(pat.Put(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginEdit)))
|
router.HandleFuncC(pat.Patch(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginEdit)))
|
||||||
router.HandleFuncC(pat.Delete(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginDelete)))
|
router.HandleFuncC(pat.Delete(prefix+"/user/:id"), libapi.SessionHandler(LoginHandler(loginDelete)))
|
||||||
router.HandleFuncC(pat.Get(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitor)))
|
router.HandleFuncC(pat.Get(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitor)))
|
||||||
router.HandleFuncC(pat.Put(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitorAdminToggle)))
|
router.HandleFuncC(pat.Patch(prefix+"/invitor"), libapi.SessionHandler(LoginHandler(invitorAdminToggle)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Status to get Login and Server status
|
// Status to get Login and Server status
|
||||||
|
@ -79,12 +78,11 @@ func login(ctx context.Context, w http.ResponseWriter, r *http.Request) (returnd
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "login")
|
logger := log.GetLog(r, "login")
|
||||||
var requestlogin RequestLogin
|
var requestlogin RequestLogin
|
||||||
err := json.NewDecoder(r.Body).Decode(&requestlogin)
|
returnerr = libapi.JSONDecoder(r.Body, &requestlogin, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = logger.WithField("user", requestlogin.Username)
|
logger = logger.WithField("user", requestlogin.Username)
|
||||||
var login = Login{Username: requestlogin.Username}
|
var login = Login{Username: requestlogin.Username}
|
||||||
dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
|
dbconnection.Where("mail = ?", requestlogin.Username).First(&login)
|
||||||
|
@ -119,12 +117,11 @@ func password(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
logger := log.GetLog(r, "password")
|
logger := log.GetLog(r, "password")
|
||||||
var changePasswordRequest ChangePasswordRequest
|
var changePasswordRequest ChangePasswordRequest
|
||||||
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&changePasswordRequest)
|
returnerr = libapi.JSONDecoder(r.Body, &changePasswordRequest, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output, _ := libpassword.Validate(login.Password, changePasswordRequest.CurrentPassword)
|
output, _ := libpassword.Validate(login.Password, changePasswordRequest.CurrentPassword)
|
||||||
if !output {
|
if !output {
|
||||||
logger.Warn("wrong current password")
|
logger.Warn("wrong current password")
|
||||||
|
@ -187,12 +184,11 @@ func inviteAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret
|
||||||
returndata = false
|
returndata = false
|
||||||
logger := log.GetLog(r, "inviteadd")
|
logger := log.GetLog(r, "inviteadd")
|
||||||
var newLogin RequestLogin
|
var newLogin RequestLogin
|
||||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
returnerr = libapi.JSONDecoder(r.Body, &newLogin, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
invite := &Invite{
|
invite := &Invite{
|
||||||
Login: *login,
|
Login: *login,
|
||||||
Invited: Login{
|
Invited: Login{
|
||||||
|
@ -242,17 +238,17 @@ func loginAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (retu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var newLogin RequestLogin
|
var newLogin RequestLogin
|
||||||
err := json.NewDecoder(r.Body).Decode(&newLogin)
|
returnerr = libapi.JSONDecoder(r.Body, &newLogin, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
loginObj := Login{
|
loginObj := Login{
|
||||||
Username: strings.ToLower(newLogin.Username),
|
Username: strings.ToLower(newLogin.Username),
|
||||||
Password: libpassword.NewHash(newLogin.Password),
|
Password: libpassword.NewHash(newLogin.Password),
|
||||||
Active: true,
|
Active: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := dbconnection.Create(loginObj).Error; err != nil {
|
if err := dbconnection.Create(loginObj).Error; err != nil {
|
||||||
logger.Warn("error create login")
|
logger.Warn("error create login")
|
||||||
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
returnerr = &libapi.ErrorResult{Message: "Username exists already"}
|
||||||
|
@ -277,10 +273,8 @@ func loginEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (ret
|
||||||
logger = logger.WithField("id", id)
|
logger = logger.WithField("id", id)
|
||||||
var invitedLogin = Login{ID: id}
|
var invitedLogin = Login{ID: id}
|
||||||
var changeLogin RequestLogin
|
var changeLogin RequestLogin
|
||||||
err = json.NewDecoder(r.Body).Decode(&changeLogin)
|
returnerr = libapi.JSONDecoder(r.Body, &changeLogin, w, logger)
|
||||||
if err != nil {
|
if returnerr != nil {
|
||||||
logger.Error("fetch request")
|
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,15 +63,15 @@ func TestAPI(t *testing.T) {
|
||||||
|
|
||||||
// Test if crash on if not login in
|
// Test if crash on if not login in
|
||||||
result, w = session.JSONRequest("GET", "/logout", nil)
|
result, w = session.JSONRequest("GET", "/logout", nil)
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
assertion.Equal(result.Data, false)
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* TEST password
|
* TEST password
|
||||||
*/
|
*/
|
||||||
|
session.Clean()
|
||||||
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"})
|
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"})
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
assertion.Equal(result.Data, false)
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
//login before
|
//login before
|
||||||
|
@ -103,7 +103,7 @@ func TestAPI(t *testing.T) {
|
||||||
*/
|
*/
|
||||||
session.Clean()
|
session.Clean()
|
||||||
result, w = session.JSONRequest("GET", "/invite", nil)
|
result, w = session.JSONRequest("GET", "/invite", nil)
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
assertion.Equal(result.Data, false)
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
//login before
|
//login before
|
||||||
|
@ -117,7 +117,7 @@ func TestAPI(t *testing.T) {
|
||||||
*/
|
*/
|
||||||
session.Clean()
|
session.Clean()
|
||||||
result, w = session.JSONRequest("GET", "/user", nil)
|
result, w = session.JSONRequest("GET", "/user", nil)
|
||||||
assertion.Equal(w.StatusCode, http.StatusOK)
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
||||||
assertion.Equal(result.Data, false)
|
assertion.Equal(result.Data, false)
|
||||||
|
|
||||||
//login before
|
//login before
|
||||||
|
|
Reference in New Issue