mirror of
https://github.com/simon987/task_tracker.git
synced 2025-04-19 18:16:45 +00:00
Cleanup api responses
This commit is contained in:
parent
8784b536d3
commit
71e05ecdd6
106
api/auth.go
106
api/auth.go
@ -7,55 +7,13 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const MinPasswordLength = 8
|
|
||||||
const MinUsernameLength = 3
|
|
||||||
const MaxUsernameLength = 16
|
|
||||||
|
|
||||||
type LoginRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LoginResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Manager *storage.Manager `json:"manager"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type RegisterRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AccountDetails struct {
|
|
||||||
LoggedIn bool `json:"logged_in"`
|
|
||||||
Manager *storage.Manager `json:"manager,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetAllManagersResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Managers *[]storage.Manager `json:"managers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *RegisterRequest) isValid() bool {
|
|
||||||
return MinUsernameLength <= len(r.Username) &&
|
|
||||||
len(r.Username) <= MaxUsernameLength &&
|
|
||||||
MinPasswordLength <= len(r.Password)
|
|
||||||
}
|
|
||||||
|
|
||||||
type RegisterResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) Login(r *Request) {
|
func (api *WebAPI) Login(r *Request) {
|
||||||
|
|
||||||
req := &LoginRequest{}
|
req := &LoginRequest{}
|
||||||
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LoginResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -68,7 +26,7 @@ func (api *WebAPI) Login(r *Request) {
|
|||||||
"username": req.Username,
|
"username": req.Username,
|
||||||
}).Warning("Login attempt")
|
}).Warning("Login attempt")
|
||||||
|
|
||||||
r.Json(LoginResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid username/password",
|
Message: "Invalid username/password",
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -78,9 +36,11 @@ func (api *WebAPI) Login(r *Request) {
|
|||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
sess.Set("manager", manager)
|
sess.Set("manager", manager)
|
||||||
|
|
||||||
r.OkJson(LoginResponse{
|
r.OkJson(JsonResponse{
|
||||||
Manager: manager,
|
Content: LoginResponse{
|
||||||
Ok: true,
|
Manager: manager,
|
||||||
|
},
|
||||||
|
Ok: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
@ -101,7 +61,7 @@ func (api *WebAPI) Register(r *Request) {
|
|||||||
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LoginResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -109,7 +69,7 @@ func (api *WebAPI) Register(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !req.isValid() {
|
if !req.isValid() {
|
||||||
r.Json(LoginResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid register request",
|
Message: "Invalid register request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -126,7 +86,7 @@ func (api *WebAPI) Register(r *Request) {
|
|||||||
"username": string(manager.Username),
|
"username": string(manager.Username),
|
||||||
}).Warning("Register attempt")
|
}).Warning("Register attempt")
|
||||||
|
|
||||||
r.Json(LoginResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -136,7 +96,7 @@ func (api *WebAPI) Register(r *Request) {
|
|||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
sess.Set("manager", manager)
|
sess.Set("manager", manager)
|
||||||
|
|
||||||
r.OkJson(RegisterResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -145,7 +105,7 @@ func (api *WebAPI) Register(r *Request) {
|
|||||||
}).Info("Registered")
|
}).Info("Registered")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) AccountDetails(r *Request) {
|
func (api *WebAPI) GetAccountDetails(r *Request) {
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
@ -155,35 +115,43 @@ func (api *WebAPI) AccountDetails(r *Request) {
|
|||||||
}).Trace("Account details request")
|
}).Trace("Account details request")
|
||||||
|
|
||||||
if manager == nil {
|
if manager == nil {
|
||||||
r.OkJson(AccountDetails{
|
r.OkJson(JsonResponse{
|
||||||
LoggedIn: false,
|
Ok: false,
|
||||||
|
Content: GetAccountDetailsResponse{
|
||||||
|
LoggedIn: false,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(AccountDetails{
|
r.OkJson(JsonResponse{
|
||||||
LoggedIn: true,
|
Ok: true,
|
||||||
Manager: manager.(*storage.Manager),
|
Content: GetAccountDetailsResponse{
|
||||||
|
LoggedIn: true,
|
||||||
|
Manager: manager.(*storage.Manager),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) GetAllManagers(r *Request) {
|
func (api *WebAPI) GetManagerList(r *Request) {
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
|
|
||||||
if manager == nil {
|
if manager == nil {
|
||||||
r.Json(GetAllManagersResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 401)
|
}, 401)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
managers := api.Database.GetAllManagers()
|
managers := api.Database.GetManagerList()
|
||||||
|
|
||||||
r.OkJson(GetAllManagersResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Managers: managers,
|
Content: GetManagerListResponse{
|
||||||
|
Managers: managers,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,7 +159,7 @@ func (api *WebAPI) PromoteManager(r *Request) {
|
|||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
if err != nil || id <= 0 {
|
if err != nil || id <= 0 {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid manager id",
|
Message: "Invalid manager id",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -202,7 +170,7 @@ func (api *WebAPI) PromoteManager(r *Request) {
|
|||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
|
|
||||||
if !manager.(*storage.Manager).WebsiteAdmin || manager.(*storage.Manager).Id == id {
|
if !manager.(*storage.Manager).WebsiteAdmin || manager.(*storage.Manager).Id == id {
|
||||||
r.Json(GetAllManagersResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 401)
|
}, 401)
|
||||||
@ -210,7 +178,7 @@ func (api *WebAPI) PromoteManager(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !manager.(*storage.Manager).WebsiteAdmin {
|
if !manager.(*storage.Manager).WebsiteAdmin {
|
||||||
r.Json(GetAllManagersResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -229,7 +197,7 @@ func (api *WebAPI) DemoteManager(r *Request) {
|
|||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
if err != nil || id <= 0 {
|
if err != nil || id <= 0 {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid manager id",
|
Message: "Invalid manager id",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -240,7 +208,7 @@ func (api *WebAPI) DemoteManager(r *Request) {
|
|||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
|
|
||||||
if manager == nil {
|
if manager == nil {
|
||||||
r.Json(GetAllManagersResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 401)
|
}, 401)
|
||||||
@ -248,7 +216,7 @@ func (api *WebAPI) DemoteManager(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !manager.(*storage.Manager).WebsiteAdmin || manager.(*storage.Manager).Id == id {
|
if !manager.(*storage.Manager).WebsiteAdmin || manager.(*storage.Manager).Id == id {
|
||||||
r.Json(GetAllManagersResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 403)
|
}, 403)
|
||||||
|
12
api/error.go
12
api/error.go
@ -5,21 +5,17 @@ import (
|
|||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorResponse struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
StackTrace string `json:"stack_trace"`
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleErr(err error, r *Request) {
|
func handleErr(err error, r *Request) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Error(err.Error())
|
logrus.Error(err.Error())
|
||||||
//debug.PrintStack()
|
//debug.PrintStack()
|
||||||
|
|
||||||
r.Json(ErrorResponse{
|
r.Json(JsonResponse{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
StackTrace: string(debug.Stack()),
|
Content: ErrorResponse{
|
||||||
|
StackTrace: string(debug.Stack()),
|
||||||
|
},
|
||||||
}, 500)
|
}, 500)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
api/git.go
30
api/git.go
@ -14,36 +14,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitPayload struct {
|
|
||||||
Ref string `json:"ref"`
|
|
||||||
Before string `json:"before"`
|
|
||||||
After string `json:"after"`
|
|
||||||
Repository struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Owner struct {
|
|
||||||
Id int64 `json:"id"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
Login string `json:"login"`
|
|
||||||
FullName string `json:"full_name"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
} `json:"owner"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
FullName string `json:"full_name"`
|
|
||||||
Private bool `json:"private"`
|
|
||||||
Fork bool `json:"fork"`
|
|
||||||
Size int64 `json:"size"`
|
|
||||||
HtmlUrl string `json:"html_url"`
|
|
||||||
SshUrl string `json:"ssh_url"`
|
|
||||||
CloneUrl string `json:"clone_url"`
|
|
||||||
DefaultBranch string `json:"default_branch"`
|
|
||||||
} `json:"repository"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g GitPayload) String() string {
|
|
||||||
jsonBytes, _ := json.Marshal(g)
|
|
||||||
return string(jsonBytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) ReceiveGitWebHook(r *Request) {
|
func (api *WebAPI) ReceiveGitWebHook(r *Request) {
|
||||||
|
|
||||||
if !signatureValid(r) {
|
if !signatureValid(r) {
|
||||||
|
@ -10,7 +10,7 @@ type Request struct {
|
|||||||
Ctx *fasthttp.RequestCtx
|
Ctx *fasthttp.RequestCtx
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) OkJson(object interface{}) {
|
func (r *Request) OkJson(object JsonResponse) {
|
||||||
|
|
||||||
resp, err := json.Marshal(object)
|
resp, err := json.Marshal(object)
|
||||||
handleErr(err, r)
|
handleErr(err, r)
|
||||||
@ -20,7 +20,7 @@ func (r *Request) OkJson(object interface{}) {
|
|||||||
handleErr(err, r)
|
handleErr(err, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Json(object interface{}, code int) {
|
func (r *Request) Json(object JsonResponse, code int) {
|
||||||
|
|
||||||
resp, err := json.Marshal(object)
|
resp, err := json.Marshal(object)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
55
api/log.go
55
api/log.go
@ -5,36 +5,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/simon987/task_tracker/config"
|
"github.com/simon987/task_tracker/config"
|
||||||
"github.com/simon987/task_tracker/storage"
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RequestHandler func(*Request)
|
|
||||||
|
|
||||||
type GetLogRequest struct {
|
|
||||||
Level storage.LogLevel `json:"level"`
|
|
||||||
Since int64 `json:"since"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogRequest struct {
|
|
||||||
Scope string `json:"scope"`
|
|
||||||
Message string `json:"Message"`
|
|
||||||
TimeStamp int64 `json:"timestamp"`
|
|
||||||
worker *storage.Worker
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetLogResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
Logs *[]storage.LogEntry `json:"logs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type LogResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *LogRequest) Time() time.Time {
|
func (e *LogRequest) Time() time.Time {
|
||||||
|
|
||||||
t := time.Unix(e.TimeStamp, 0)
|
t := time.Unix(e.TimeStamp, 0)
|
||||||
@ -94,7 +68,7 @@ func (api *WebAPI) LogTrace(r *Request) {
|
|||||||
|
|
||||||
entry, err := api.parseLogEntry(r)
|
entry, err := api.parseLogEntry(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -111,7 +85,7 @@ func (api *WebAPI) LogInfo(r *Request) {
|
|||||||
|
|
||||||
entry, err := api.parseLogEntry(r)
|
entry, err := api.parseLogEntry(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -128,7 +102,7 @@ func (api *WebAPI) LogWarn(r *Request) {
|
|||||||
|
|
||||||
entry, err := api.parseLogEntry(r)
|
entry, err := api.parseLogEntry(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -145,7 +119,7 @@ func (api *WebAPI) LogError(r *Request) {
|
|||||||
|
|
||||||
entry, err := api.parseLogEntry(r)
|
entry, err := api.parseLogEntry(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(LogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -163,7 +137,7 @@ func (api *WebAPI) GetLog(r *Request) {
|
|||||||
req := &GetLogRequest{}
|
req := &GetLogRequest{}
|
||||||
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetLogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -178,27 +152,20 @@ func (api *WebAPI) GetLog(r *Request) {
|
|||||||
"logCount": len(*logs),
|
"logCount": len(*logs),
|
||||||
}).Trace("Get log request")
|
}).Trace("Get log request")
|
||||||
|
|
||||||
r.OkJson(GetLogResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Logs: logs,
|
Content: GetLogResponse{
|
||||||
|
Logs: logs,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"getLogRequest": req,
|
"getLogRequest": req,
|
||||||
}).Warn("Invalid log request")
|
}).Warn("Invalid log request")
|
||||||
|
|
||||||
r.Json(GetLogResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid log request",
|
Message: "Invalid log request",
|
||||||
}, 400)
|
}, 400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r GetLogRequest) isValid() bool {
|
|
||||||
|
|
||||||
if r.Since <= 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
44
api/main.go
44
api/main.go
@ -20,10 +20,7 @@ type WebAPI struct {
|
|||||||
Cron *cron.Cron
|
Cron *cron.Cron
|
||||||
}
|
}
|
||||||
|
|
||||||
type Info struct {
|
type RequestHandler func(*Request)
|
||||||
Name string `json:"name"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var info = Info{
|
var info = Info{
|
||||||
Name: "task_tracker",
|
Name: "task_tracker",
|
||||||
@ -31,7 +28,10 @@ var info = Info{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Index(r *Request) {
|
func Index(r *Request) {
|
||||||
r.OkJson(info)
|
r.OkJson(JsonResponse{
|
||||||
|
Ok: true,
|
||||||
|
Content: info,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) setupMonitoring() {
|
func (api *WebAPI) setupMonitoring() {
|
||||||
@ -75,27 +75,27 @@ func New() *WebAPI {
|
|||||||
api.router.POST("/log/warn", LogRequestMiddleware(api.LogWarn))
|
api.router.POST("/log/warn", LogRequestMiddleware(api.LogWarn))
|
||||||
api.router.POST("/log/error", LogRequestMiddleware(api.LogError))
|
api.router.POST("/log/error", LogRequestMiddleware(api.LogError))
|
||||||
|
|
||||||
api.router.POST("/worker/create", LogRequestMiddleware(api.WorkerCreate))
|
api.router.POST("/worker/create", LogRequestMiddleware(api.CreateWorker))
|
||||||
api.router.POST("/worker/update", LogRequestMiddleware(api.WorkerUpdate))
|
api.router.POST("/worker/update", LogRequestMiddleware(api.UpdateWorker))
|
||||||
api.router.GET("/worker/get/:id", LogRequestMiddleware(api.WorkerGet))
|
api.router.GET("/worker/get/:id", LogRequestMiddleware(api.GetWorker))
|
||||||
api.router.GET("/worker/stats", LogRequestMiddleware(api.GetAllWorkerStats))
|
api.router.GET("/worker/stats", LogRequestMiddleware(api.GetAllWorkerStats))
|
||||||
|
|
||||||
api.router.POST("/project/create", LogRequestMiddleware(api.ProjectCreate))
|
api.router.POST("/project/create", LogRequestMiddleware(api.CreateProject))
|
||||||
api.router.GET("/project/get/:id", LogRequestMiddleware(api.ProjectGet))
|
api.router.GET("/project/get/:id", LogRequestMiddleware(api.GetProject))
|
||||||
api.router.POST("/project/update/:id", LogRequestMiddleware(api.ProjectUpdate))
|
api.router.POST("/project/update/:id", LogRequestMiddleware(api.UpdateProject))
|
||||||
api.router.GET("/project/list", LogRequestMiddleware(api.ProjectGetAllProjects))
|
api.router.GET("/project/list", LogRequestMiddleware(api.GetProjectList))
|
||||||
api.router.GET("/project/monitoring-between/:id", LogRequestMiddleware(api.GetSnapshotsBetween))
|
api.router.GET("/project/monitoring-between/:id", LogRequestMiddleware(api.GetSnapshotsWithinRange))
|
||||||
api.router.GET("/project/monitoring/:id", LogRequestMiddleware(api.GetNSnapshots))
|
api.router.GET("/project/monitoring/:id", LogRequestMiddleware(api.GetNSnapshots))
|
||||||
api.router.GET("/project/assignees/:id", LogRequestMiddleware(api.ProjectGetAssigneeStats))
|
api.router.GET("/project/assignees/:id", LogRequestMiddleware(api.GetAssigneeStatsForProject))
|
||||||
api.router.GET("/project/accesses/:id", LogRequestMiddleware(api.ProjectGetWorkerAccesses))
|
api.router.GET("/project/accesses/:id", LogRequestMiddleware(api.GetWorkerAccessListForProject))
|
||||||
api.router.POST("/project/request_access", LogRequestMiddleware(api.WorkerRequestAccess))
|
api.router.POST("/project/request_access", LogRequestMiddleware(api.CreateWorkerAccess))
|
||||||
api.router.POST("/project/accept_request/:id/:wid", LogRequestMiddleware(api.AcceptAccessRequest))
|
api.router.POST("/project/accept_request/:id/:wid", LogRequestMiddleware(api.AcceptAccessRequest))
|
||||||
api.router.POST("/project/reject_request/:id/:wid", LogRequestMiddleware(api.RejectAccessRequest))
|
api.router.POST("/project/reject_request/:id/:wid", LogRequestMiddleware(api.RejectAccessRequest))
|
||||||
|
|
||||||
api.router.POST("/task/create", LogRequestMiddleware(api.TaskCreate))
|
api.router.POST("/task/create", LogRequestMiddleware(api.SubmitTask))
|
||||||
api.router.GET("/task/get/:project", LogRequestMiddleware(api.TaskGetFromProject))
|
api.router.GET("/task/get/:project", LogRequestMiddleware(api.GetTaskFromProject))
|
||||||
api.router.GET("/task/get", LogRequestMiddleware(api.TaskGet))
|
api.router.GET("/task/get", LogRequestMiddleware(api.GetTask))
|
||||||
api.router.POST("/task/release", LogRequestMiddleware(api.TaskRelease))
|
api.router.POST("/task/release", LogRequestMiddleware(api.ReleaseTask))
|
||||||
|
|
||||||
api.router.POST("/git/receivehook", LogRequestMiddleware(api.ReceiveGitWebHook))
|
api.router.POST("/git/receivehook", LogRequestMiddleware(api.ReceiveGitWebHook))
|
||||||
|
|
||||||
@ -104,8 +104,8 @@ func New() *WebAPI {
|
|||||||
api.router.POST("/register", LogRequestMiddleware(api.Register))
|
api.router.POST("/register", LogRequestMiddleware(api.Register))
|
||||||
api.router.POST("/login", LogRequestMiddleware(api.Login))
|
api.router.POST("/login", LogRequestMiddleware(api.Login))
|
||||||
api.router.GET("/logout", LogRequestMiddleware(api.Logout))
|
api.router.GET("/logout", LogRequestMiddleware(api.Logout))
|
||||||
api.router.GET("/account", LogRequestMiddleware(api.AccountDetails))
|
api.router.GET("/account", LogRequestMiddleware(api.GetAccountDetails))
|
||||||
api.router.GET("/manager/list", LogRequestMiddleware(api.GetAllManagers))
|
api.router.GET("/manager/list", LogRequestMiddleware(api.GetManagerList))
|
||||||
api.router.GET("/manager/promote/:id", LogRequestMiddleware(api.PromoteManager))
|
api.router.GET("/manager/promote/:id", LogRequestMiddleware(api.PromoteManager))
|
||||||
api.router.GET("/manager/demote/:id", LogRequestMiddleware(api.DemoteManager))
|
api.router.GET("/manager/demote/:id", LogRequestMiddleware(api.DemoteManager))
|
||||||
|
|
||||||
|
269
api/models.go
Normal file
269
api/models.go
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/simon987/task_tracker/storage"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MinPasswordLength = 8
|
||||||
|
MinUsernameLength = 3
|
||||||
|
MaxUsernameLength = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
type JsonResponse struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
Content interface{} `json:"content,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GitPayload struct {
|
||||||
|
Ref string `json:"ref"`
|
||||||
|
Before string `json:"before"`
|
||||||
|
After string `json:"after"`
|
||||||
|
Repository struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Owner struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
Login string `json:"login"`
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
} `json:"owner"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
Private bool `json:"private"`
|
||||||
|
Fork bool `json:"fork"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
HtmlUrl string `json:"html_url"`
|
||||||
|
SshUrl string `json:"ssh_url"`
|
||||||
|
CloneUrl string `json:"clone_url"`
|
||||||
|
DefaultBranch string `json:"default_branch"`
|
||||||
|
} `json:"repository"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g GitPayload) String() string {
|
||||||
|
jsonBytes, _ := json.Marshal(g)
|
||||||
|
return string(jsonBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrorResponse struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
StackTrace string `json:"stack_trace"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginRequest struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginResponse struct {
|
||||||
|
Manager *storage.Manager `json:"manager"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterRequest struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RegisterRequest) isValid() bool {
|
||||||
|
return MinUsernameLength <= len(r.Username) &&
|
||||||
|
len(r.Username) <= MaxUsernameLength &&
|
||||||
|
MinPasswordLength <= len(r.Password)
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAccountDetailsResponse struct {
|
||||||
|
LoggedIn bool `json:"logged_in"`
|
||||||
|
Manager *storage.Manager `json:"manager,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetManagerListResponse struct {
|
||||||
|
Managers *[]storage.Manager `json:"managers"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetLogRequest struct {
|
||||||
|
Level storage.LogLevel `json:"level"`
|
||||||
|
Since int64 `json:"since"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r GetLogRequest) isValid() bool {
|
||||||
|
|
||||||
|
if r.Since <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogRequest struct {
|
||||||
|
Scope string `json:"scope"`
|
||||||
|
Message string `json:"Message"`
|
||||||
|
TimeStamp int64 `json:"timestamp"`
|
||||||
|
worker *storage.Worker
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetLogResponse struct {
|
||||||
|
Logs *[]storage.LogEntry `json:"logs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetSnapshotsResponse struct {
|
||||||
|
Snapshots *[]storage.ProjectMonitoringSnapshot `json:"snapshots,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateProjectRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
CloneUrl string `json:"clone_url"`
|
||||||
|
GitRepo string `json:"git_repo"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
Priority int64 `json:"priority"`
|
||||||
|
Motd string `json:"motd"`
|
||||||
|
Public bool `json:"public"`
|
||||||
|
Hidden bool `json:"hidden"`
|
||||||
|
Chain int64 `json:"chain"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *CreateProjectRequest) isValid() bool {
|
||||||
|
if len(req.Name) <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if req.Priority < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if req.Hidden && req.Public {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateProjectRequest struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
CloneUrl string `json:"clone_url"`
|
||||||
|
GitRepo string `json:"git_repo"`
|
||||||
|
Priority int64 `json:"priority"`
|
||||||
|
Motd string `json:"motd"`
|
||||||
|
Public bool `json:"public"`
|
||||||
|
Hidden bool `json:"hidden"`
|
||||||
|
Chain int64 `json:"chain"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *UpdateProjectRequest) isValid() bool {
|
||||||
|
if len(req.Name) <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if req.Priority < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if req.Hidden && req.Public {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateProjectResponse struct {
|
||||||
|
Id int64 `json:"id,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProjectResponse struct {
|
||||||
|
Project *storage.Project `json:"project,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProjectListResponse struct {
|
||||||
|
Projects *[]storage.Project `json:"projects,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAssigneeStatsForProjectResponse struct {
|
||||||
|
Assignees *[]storage.AssignedTasks `json:"assignees"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetWorkerAccessListForProjectResponse struct {
|
||||||
|
Accesses *[]storage.WorkerAccess `json:"accesses,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubmitTaskRequest struct {
|
||||||
|
Project int64 `json:"project"`
|
||||||
|
MaxRetries int64 `json:"max_retries"`
|
||||||
|
Recipe string `json:"recipe"`
|
||||||
|
Priority int64 `json:"priority"`
|
||||||
|
MaxAssignTime int64 `json:"max_assign_time"`
|
||||||
|
Hash64 int64 `json:"hash_u64"`
|
||||||
|
UniqueString string `json:"unique_string"`
|
||||||
|
VerificationCount int64 `json:"verification_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *SubmitTaskRequest) IsValid() bool {
|
||||||
|
if req.MaxRetries < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(req.Recipe) <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if req.Hash64 != 0 && req.UniqueString != "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReleaseTaskRequest struct {
|
||||||
|
TaskId int64 `json:"task_id"`
|
||||||
|
Result storage.TaskResult `json:"result"`
|
||||||
|
Verification int64 `json:"verification"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReleaseTaskResponse struct {
|
||||||
|
Updated bool `json:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateTaskResponse struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetTaskResponse struct {
|
||||||
|
Task *storage.Task `json:"task,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateWorkerRequest struct {
|
||||||
|
Alias string `json:"alias"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateWorkerRequest struct {
|
||||||
|
Alias string `json:"alias"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req *CreateWorkerRequest) isValid() bool {
|
||||||
|
if req.Alias == "unassigned" {
|
||||||
|
//Reserved alias
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateWorkerResponse struct {
|
||||||
|
Worker *storage.Worker `json:"worker,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetWorkerResponse struct {
|
||||||
|
Worker *storage.Worker `json:"worker,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAllWorkerStatsResponse struct {
|
||||||
|
Stats *[]storage.WorkerStats `json:"stats"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateWorkerAccessRequest struct {
|
||||||
|
Assign bool `json:"assign"`
|
||||||
|
Submit bool `json:"submit"`
|
||||||
|
Project int64 `json:"project"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *CreateWorkerAccessRequest) isValid() bool {
|
||||||
|
if !w.Assign && !w.Submit {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
type Info struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
}
|
@ -1,24 +1,17 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/simon987/task_tracker/storage"
|
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MonitoringSnapshotResponse struct {
|
func (api *WebAPI) GetSnapshotsWithinRange(r *Request) {
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Snapshots *[]storage.ProjectMonitoringSnapshot `json:"snapshots,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) GetSnapshotsBetween(r *Request) {
|
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
from := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("from")
|
from := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("from")
|
||||||
to := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("to")
|
to := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("to")
|
||||||
if err != nil || id <= 0 || from <= 0 || to <= 0 || from >= math.MaxInt32 || to >= math.MaxInt32 {
|
if err != nil || id <= 0 || from <= 0 || to <= 0 || from >= math.MaxInt32 || to >= math.MaxInt32 {
|
||||||
r.Json(MonitoringSnapshotResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid request",
|
Message: "Invalid request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -26,9 +19,11 @@ func (api *WebAPI) GetSnapshotsBetween(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
snapshots := api.Database.GetMonitoringSnapshotsBetween(id, from, to)
|
snapshots := api.Database.GetMonitoringSnapshotsBetween(id, from, to)
|
||||||
r.OkJson(MonitoringSnapshotResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Snapshots: snapshots,
|
Content: GetSnapshotsResponse{
|
||||||
|
Snapshots: snapshots,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +32,7 @@ func (api *WebAPI) GetNSnapshots(r *Request) {
|
|||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
count := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("count")
|
count := r.Ctx.Request.URI().QueryArgs().GetUintOrZero("count")
|
||||||
if err != nil || id <= 0 || count <= 0 || count >= 1000 {
|
if err != nil || id <= 0 || count <= 0 || count >= 1000 {
|
||||||
r.Json(MonitoringSnapshotResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid request",
|
Message: "Invalid request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -45,8 +40,10 @@ func (api *WebAPI) GetNSnapshots(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
snapshots := api.Database.GetNMonitoringSnapshots(id, count)
|
snapshots := api.Database.GetNMonitoringSnapshots(id, count)
|
||||||
r.OkJson(MonitoringSnapshotResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Snapshots: snapshots,
|
Content: GetSnapshotsResponse{
|
||||||
|
Snapshots: snapshots,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
256
api/project.go
256
api/project.go
@ -7,70 +7,41 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateProjectRequest struct {
|
func (api *WebAPI) GetProject(r *Request) {
|
||||||
Name string `json:"name"`
|
|
||||||
CloneUrl string `json:"clone_url"`
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
GitRepo string `json:"git_repo"`
|
handleErr(err, r) //todo handle invalid id
|
||||||
Version string `json:"version"`
|
|
||||||
Priority int64 `json:"priority"`
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
Motd string `json:"motd"`
|
manager := sess.Get("manager")
|
||||||
Public bool `json:"public"`
|
|
||||||
Hidden bool `json:"hidden"`
|
project := api.Database.GetProject(id)
|
||||||
Chain int64 `json:"chain"`
|
|
||||||
|
if project == nil {
|
||||||
|
r.Json(JsonResponse{
|
||||||
|
Ok: false,
|
||||||
|
Message: "Project not found",
|
||||||
|
}, 404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isProjectReadAuthorized(project, manager, api.Database) {
|
||||||
|
r.Json(JsonResponse{
|
||||||
|
Ok: false,
|
||||||
|
Message: "Unauthorized",
|
||||||
|
}, 403)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r.OkJson(JsonResponse{
|
||||||
|
Ok: true,
|
||||||
|
Content: GetProjectResponse{
|
||||||
|
Project: project,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateProjectRequest struct {
|
func (api *WebAPI) CreateProject(r *Request) {
|
||||||
Name string `json:"name"`
|
|
||||||
CloneUrl string `json:"clone_url"`
|
|
||||||
GitRepo string `json:"git_repo"`
|
|
||||||
Priority int64 `json:"priority"`
|
|
||||||
Motd string `json:"motd"`
|
|
||||||
Public bool `json:"public"`
|
|
||||||
Hidden bool `json:"hidden"`
|
|
||||||
Chain int64 `json:"chain"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateProjectResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateProjectResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Id int64 `json:"id,omitempty"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetProjectResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Project *storage.Project `json:"project,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetAllProjectsResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Projects *[]storage.Project `json:"projects,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetAssigneeStatsResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Assignees *[]storage.AssignedTasks `json:"assignees"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type WorkerAccessRequestResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProjectGetAccessRequestsResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Accesses *[]storage.WorkerAccess `json:"accesses,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) ProjectCreate(r *Request) {
|
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
@ -78,7 +49,7 @@ func (api *WebAPI) ProjectCreate(r *Request) {
|
|||||||
createReq := &CreateProjectRequest{}
|
createReq := &CreateProjectRequest{}
|
||||||
err := json.Unmarshal(r.Ctx.Request.Body(), createReq)
|
err := json.Unmarshal(r.Ctx.Request.Body(), createReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -96,12 +67,12 @@ func (api *WebAPI) ProjectCreate(r *Request) {
|
|||||||
Chain: createReq.Chain,
|
Chain: createReq.Chain,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidProject(project) {
|
if !createReq.isValid() {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"project": project,
|
"project": project,
|
||||||
}).Warn("Invalid project")
|
}).Warn("Invalid project")
|
||||||
|
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid project",
|
Message: "Invalid project",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -113,7 +84,7 @@ func (api *WebAPI) ProjectCreate(r *Request) {
|
|||||||
"project": project,
|
"project": project,
|
||||||
}).Warn("Unauthorized project creation")
|
}).Warn("Unauthorized project creation")
|
||||||
|
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "You are not permitted to create a project with this configuration",
|
Message: "You are not permitted to create a project with this configuration",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -122,7 +93,7 @@ func (api *WebAPI) ProjectCreate(r *Request) {
|
|||||||
|
|
||||||
id, err := api.Database.SaveProject(project)
|
id, err := api.Database.SaveProject(project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 500)
|
}, 500)
|
||||||
@ -131,20 +102,22 @@ func (api *WebAPI) ProjectCreate(r *Request) {
|
|||||||
|
|
||||||
api.Database.SetManagerRoleOn(manager.(*storage.Manager), id,
|
api.Database.SetManagerRoleOn(manager.(*storage.Manager), id,
|
||||||
storage.ROLE_MANAGE_ACCESS|storage.ROLE_READ|storage.ROLE_EDIT)
|
storage.ROLE_MANAGE_ACCESS|storage.ROLE_READ|storage.ROLE_EDIT)
|
||||||
r.OkJson(CreateProjectResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Id: id,
|
Content: CreateProjectResponse{
|
||||||
|
Id: id,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"project": project,
|
"project": project,
|
||||||
}).Debug("Created project")
|
}).Debug("Created project")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) ProjectUpdate(r *Request) {
|
func (api *WebAPI) UpdateProject(r *Request) {
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
if err != nil || id <= 0 {
|
if err != nil || id <= 0 {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid project id",
|
Message: "Invalid project id",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -154,12 +127,21 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
|
|||||||
updateReq := &UpdateProjectRequest{}
|
updateReq := &UpdateProjectRequest{}
|
||||||
err = json.Unmarshal(r.Ctx.Request.Body(), updateReq)
|
err = json.Unmarshal(r.Ctx.Request.Body(), updateReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !updateReq.isValid() {
|
||||||
|
r.Json(JsonResponse{
|
||||||
|
Ok: false,
|
||||||
|
Message: "Invalid request",
|
||||||
|
}, 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
project := &storage.Project{
|
project := &storage.Project{
|
||||||
Id: id,
|
Id: id,
|
||||||
Name: updateReq.Name,
|
Name: updateReq.Name,
|
||||||
@ -171,24 +153,11 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
|
|||||||
Hidden: updateReq.Hidden,
|
Hidden: updateReq.Hidden,
|
||||||
Chain: updateReq.Chain,
|
Chain: updateReq.Chain,
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidProject(project) {
|
|
||||||
logrus.WithFields(logrus.Fields{
|
|
||||||
"project": project,
|
|
||||||
}).Warn("Invalid project")
|
|
||||||
|
|
||||||
r.Json(CreateProjectResponse{
|
|
||||||
Ok: false,
|
|
||||||
Message: "Invalid project",
|
|
||||||
}, 400)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
|
|
||||||
if !isActionAuthorized(project.Id, manager, storage.ROLE_EDIT, api.Database) {
|
if !isActionOnProjectAuthorized(project.Id, manager, storage.ROLE_EDIT, api.Database) {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -200,7 +169,7 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
|
|||||||
|
|
||||||
err = api.Database.UpdateProject(project)
|
err = api.Database.UpdateProject(project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 500)
|
}, 500)
|
||||||
@ -209,7 +178,7 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
|
|||||||
"project": project,
|
"project": project,
|
||||||
}).Warn("Error during project update")
|
}).Warn("Error during project update")
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(UpdateProjectResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -219,20 +188,6 @@ func (api *WebAPI) ProjectUpdate(r *Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func isValidProject(project *storage.Project) bool {
|
|
||||||
if len(project.Name) <= 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if project.Priority < 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if project.Hidden && project.Public {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func isProjectCreationAuthorized(project *storage.Project, manager interface{}) bool {
|
func isProjectCreationAuthorized(project *storage.Project, manager interface{}) bool {
|
||||||
|
|
||||||
if manager == nil {
|
if manager == nil {
|
||||||
@ -245,7 +200,7 @@ func isProjectCreationAuthorized(project *storage.Project, manager interface{})
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func isActionAuthorized(project int64, manager interface{},
|
func isActionOnProjectAuthorized(project int64, manager interface{},
|
||||||
requiredRole storage.ManagerRole, db *storage.Database) bool {
|
requiredRole storage.ManagerRole, db *storage.Database) bool {
|
||||||
|
|
||||||
if manager == nil {
|
if manager == nil {
|
||||||
@ -283,39 +238,7 @@ func isProjectReadAuthorized(project *storage.Project, manager interface{}, db *
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) ProjectGet(r *Request) {
|
func (api *WebAPI) GetProjectList(r *Request) {
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
|
||||||
handleErr(err, r) //todo handle invalid id
|
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
|
||||||
manager := sess.Get("manager")
|
|
||||||
|
|
||||||
project := api.Database.GetProject(id)
|
|
||||||
|
|
||||||
if project == nil {
|
|
||||||
r.Json(GetProjectResponse{
|
|
||||||
Ok: false,
|
|
||||||
Message: "Project not found",
|
|
||||||
}, 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !isProjectReadAuthorized(project, manager, api.Database) {
|
|
||||||
r.Json(GetProjectResponse{
|
|
||||||
Ok: false,
|
|
||||||
Message: "Unauthorized",
|
|
||||||
}, 403)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
r.OkJson(GetProjectResponse{
|
|
||||||
Ok: true,
|
|
||||||
Project: project,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) ProjectGetAllProjects(r *Request) {
|
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
@ -329,26 +252,30 @@ func (api *WebAPI) ProjectGetAllProjects(r *Request) {
|
|||||||
|
|
||||||
projects := api.Database.GetAllProjects(id)
|
projects := api.Database.GetAllProjects(id)
|
||||||
|
|
||||||
r.OkJson(GetAllProjectsResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Projects: projects,
|
Content: GetProjectListResponse{
|
||||||
|
Projects: projects,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) ProjectGetAssigneeStats(r *Request) {
|
func (api *WebAPI) GetAssigneeStatsForProject(r *Request) {
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
handleErr(err, r) //todo handle invalid id
|
handleErr(err, r) //todo handle invalid id
|
||||||
|
|
||||||
stats := api.Database.GetAssigneeStats(id, 16)
|
stats := api.Database.GetAssigneeStats(id, 16)
|
||||||
|
|
||||||
r.OkJson(GetAssigneeStatsResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Assignees: stats,
|
Content: GetAssigneeStatsForProjectResponse{
|
||||||
|
Assignees: stats,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) ProjectGetWorkerAccesses(r *Request) {
|
func (api *WebAPI) GetWorkerAccessListForProject(r *Request) {
|
||||||
|
|
||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
@ -356,8 +283,8 @@ func (api *WebAPI) ProjectGetWorkerAccesses(r *Request) {
|
|||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
handleErr(err, r) //todo handle invalid id
|
handleErr(err, r) //todo handle invalid id
|
||||||
|
|
||||||
if !isActionAuthorized(id, manager, storage.ROLE_MANAGE_ACCESS, api.Database) {
|
if !isActionOnProjectAuthorized(id, manager, storage.ROLE_MANAGE_ACCESS, api.Database) {
|
||||||
r.Json(ProjectGetAccessRequestsResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -365,18 +292,20 @@ func (api *WebAPI) ProjectGetWorkerAccesses(r *Request) {
|
|||||||
}
|
}
|
||||||
accesses := api.Database.GetAllAccesses(id)
|
accesses := api.Database.GetAllAccesses(id)
|
||||||
|
|
||||||
r.OkJson(ProjectGetAccessRequestsResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Accesses: accesses,
|
Content: GetWorkerAccessListForProjectResponse{
|
||||||
|
Accesses: accesses,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) WorkerRequestAccess(r *Request) {
|
func (api *WebAPI) CreateWorkerAccess(r *Request) {
|
||||||
|
|
||||||
req := &WorkerAccessRequest{}
|
req := &CreateWorkerAccessRequest{}
|
||||||
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
err := json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(WorkerAccessRequestResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -384,7 +313,7 @@ func (api *WebAPI) WorkerRequestAccess(r *Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !req.isValid() {
|
if !req.isValid() {
|
||||||
r.Json(WorkerAccessRequestResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid request",
|
Message: "Invalid request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -393,10 +322,11 @@ func (api *WebAPI) WorkerRequestAccess(r *Request) {
|
|||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(WorkerAccessRequestResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 401)
|
}, 401)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
res := api.Database.SaveAccessRequest(&storage.WorkerAccess{
|
res := api.Database.SaveAccessRequest(&storage.WorkerAccess{
|
||||||
@ -407,11 +337,11 @@ func (api *WebAPI) WorkerRequestAccess(r *Request) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if res {
|
if res {
|
||||||
r.OkJson(WorkerAccessRequestResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.Json(WorkerAccessRequestResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Project is public, you already have " +
|
Message: "Project is public, you already have " +
|
||||||
"an active request or you already have access to this project",
|
"an active request or you already have access to this project",
|
||||||
@ -430,8 +360,8 @@ func (api *WebAPI) AcceptAccessRequest(r *Request) {
|
|||||||
sess := api.Session.StartFasthttp(r.Ctx)
|
sess := api.Session.StartFasthttp(r.Ctx)
|
||||||
manager := sess.Get("manager")
|
manager := sess.Get("manager")
|
||||||
|
|
||||||
if !isActionAuthorized(pid, manager, storage.ROLE_MANAGE_ACCESS, api.Database) {
|
if !isActionOnProjectAuthorized(pid, manager, storage.ROLE_MANAGE_ACCESS, api.Database) {
|
||||||
r.Json(WorkerAccessRequestResponse{
|
r.Json(JsonResponse{
|
||||||
Message: "Unauthorized",
|
Message: "Unauthorized",
|
||||||
Ok: false,
|
Ok: false,
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -441,11 +371,11 @@ func (api *WebAPI) AcceptAccessRequest(r *Request) {
|
|||||||
ok := api.Database.AcceptAccessRequest(wid, pid)
|
ok := api.Database.AcceptAccessRequest(wid, pid)
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
r.OkJson(WorkerAccessRequestResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(WorkerAccessRequestResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Worker did not have access to this project",
|
Message: "Worker did not have access to this project",
|
||||||
})
|
})
|
||||||
@ -463,11 +393,11 @@ func (api *WebAPI) RejectAccessRequest(r *Request) {
|
|||||||
ok := api.Database.RejectAccessRequest(wid, pid)
|
ok := api.Database.RejectAccessRequest(wid, pid)
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
r.OkJson(WorkerAccessRequestResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(WorkerAccessRequestResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Worker did not have access to this project",
|
Message: "Worker did not have access to this project",
|
||||||
})
|
})
|
||||||
|
141
api/task.go
141
api/task.go
@ -13,55 +13,21 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateTaskRequest struct {
|
func (api *WebAPI) SubmitTask(r *Request) {
|
||||||
Project int64 `json:"project"`
|
|
||||||
MaxRetries int64 `json:"max_retries"`
|
|
||||||
Recipe string `json:"recipe"`
|
|
||||||
Priority int64 `json:"priority"`
|
|
||||||
MaxAssignTime int64 `json:"max_assign_time"`
|
|
||||||
Hash64 int64 `json:"hash_u64"`
|
|
||||||
UniqueString string `json:"unique_string"`
|
|
||||||
VerificationCount int64 `json:"verification_count"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReleaseTaskRequest struct {
|
|
||||||
TaskId int64 `json:"task_id"`
|
|
||||||
Result storage.TaskResult `json:"result"`
|
|
||||||
Verification int64 `json:"verification"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ReleaseTaskResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Updated bool `json:"updated"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateTaskResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetTaskResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Task *storage.Task `json:"task,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) TaskCreate(r *Request) {
|
|
||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if worker == nil {
|
if worker == nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 401)
|
}, 401)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
createReq := &CreateTaskRequest{}
|
createReq := &SubmitTaskRequest{}
|
||||||
err = json.Unmarshal(r.Ctx.Request.Body(), createReq)
|
err = json.Unmarshal(r.Ctx.Request.Body(), createReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -76,56 +42,41 @@ func (api *WebAPI) TaskCreate(r *Request) {
|
|||||||
VerificationCount: createReq.VerificationCount,
|
VerificationCount: createReq.VerificationCount,
|
||||||
}
|
}
|
||||||
|
|
||||||
if createReq.IsValid() && isTaskValid(task) {
|
if !createReq.IsValid() {
|
||||||
|
|
||||||
if createReq.UniqueString != "" {
|
|
||||||
//TODO: Load key from config
|
|
||||||
createReq.Hash64 = int64(siphash.Hash(1, 2, []byte(createReq.UniqueString)))
|
|
||||||
}
|
|
||||||
|
|
||||||
err := api.Database.SaveTask(task, createReq.Project, createReq.Hash64, worker.Id)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
r.Json(CreateTaskResponse{
|
|
||||||
Ok: false,
|
|
||||||
Message: err.Error(),
|
|
||||||
}, 400)
|
|
||||||
} else {
|
|
||||||
r.OkJson(CreateTaskResponse{
|
|
||||||
Ok: true,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"task": task,
|
"task": task,
|
||||||
}).Warn("Invalid task")
|
}).Warn("Invalid task")
|
||||||
r.Json(CreateTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid task",
|
Message: "Invalid task",
|
||||||
}, 400)
|
}, 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if createReq.UniqueString != "" {
|
||||||
|
//TODO: Load key from config
|
||||||
|
createReq.Hash64 = int64(siphash.Hash(1, 2, []byte(createReq.UniqueString)))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = api.Database.SaveTask(task, createReq.Project, createReq.Hash64, worker.Id)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
r.Json(JsonResponse{
|
||||||
|
Ok: false,
|
||||||
|
Message: err.Error(),
|
||||||
|
}, 400)
|
||||||
|
} else {
|
||||||
|
r.OkJson(JsonResponse{
|
||||||
|
Ok: true,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (req *CreateTaskRequest) IsValid() bool {
|
func (api *WebAPI) GetTaskFromProject(r *Request) {
|
||||||
return req.Hash64 == 0 || req.UniqueString == ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func isTaskValid(task *storage.Task) bool {
|
|
||||||
if task.MaxRetries < 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if len(task.Recipe) <= 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) TaskGetFromProject(r *Request) {
|
|
||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -138,26 +89,28 @@ func (api *WebAPI) TaskGetFromProject(r *Request) {
|
|||||||
|
|
||||||
if task == nil {
|
if task == nil {
|
||||||
|
|
||||||
r.OkJson(GetTaskResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "No task available",
|
Message: "No task available",
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
r.OkJson(GetTaskResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Task: task,
|
Content: GetTaskResponse{
|
||||||
|
Task: task,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) TaskGet(r *Request) {
|
func (api *WebAPI) GetTask(r *Request) {
|
||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -167,16 +120,18 @@ func (api *WebAPI) TaskGet(r *Request) {
|
|||||||
task := api.Database.GetTask(worker)
|
task := api.Database.GetTask(worker)
|
||||||
if task == nil {
|
if task == nil {
|
||||||
|
|
||||||
r.OkJson(GetTaskResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "No task available",
|
Message: "No task available",
|
||||||
})
|
})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
r.OkJson(GetTaskResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Task: task,
|
Content: GetTaskResponse{
|
||||||
|
Task: task,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -236,11 +191,11 @@ func (api WebAPI) validateSignature(r *Request) (*storage.Worker, error) {
|
|||||||
return worker, nil
|
return worker, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) TaskRelease(r *Request) {
|
func (api *WebAPI) ReleaseTask(r *Request) {
|
||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -250,16 +205,18 @@ func (api *WebAPI) TaskRelease(r *Request) {
|
|||||||
req := &ReleaseTaskRequest{}
|
req := &ReleaseTaskRequest{}
|
||||||
err = json.Unmarshal(r.Ctx.Request.Body(), req)
|
err = json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(CreateProjectResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
}
|
}
|
||||||
res := api.Database.ReleaseTask(req.TaskId, worker.Id, req.Result, req.Verification)
|
res := api.Database.ReleaseTask(req.TaskId, worker.Id, req.Result, req.Verification)
|
||||||
|
|
||||||
response := ReleaseTaskResponse{
|
response := JsonResponse{
|
||||||
Updated: res,
|
Ok: true,
|
||||||
Ok: true,
|
Content: ReleaseTaskResponse{
|
||||||
|
Updated: res,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if !res {
|
if !res {
|
||||||
|
102
api/worker.go
102
api/worker.go
@ -9,51 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateWorkerRequest struct {
|
func (api *WebAPI) CreateWorker(r *Request) {
|
||||||
Alias string `json:"alias"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type UpdateWorkerResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateWorkerRequest struct {
|
|
||||||
Alias string `json:"alias"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateWorkerResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Worker *storage.Worker `json:"worker,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetWorkerResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Worker *storage.Worker `json:"worker,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetAllWorkerStatsResponse struct {
|
|
||||||
Ok bool `json:"ok"`
|
|
||||||
Message string `json:"message,omitempty"`
|
|
||||||
Stats *[]storage.WorkerStats `json:"stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type WorkerAccessRequest struct {
|
|
||||||
Assign bool `json:"assign"`
|
|
||||||
Submit bool `json:"submit"`
|
|
||||||
Project int64 `json:"project"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WorkerAccessRequest) isValid() bool {
|
|
||||||
if !w.Assign && !w.Submit {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (api *WebAPI) WorkerCreate(r *Request) {
|
|
||||||
|
|
||||||
workerReq := &CreateWorkerRequest{}
|
workerReq := &CreateWorkerRequest{}
|
||||||
err := json.Unmarshal(r.Ctx.Request.Body(), workerReq)
|
err := json.Unmarshal(r.Ctx.Request.Body(), workerReq)
|
||||||
@ -61,13 +17,13 @@ func (api *WebAPI) WorkerCreate(r *Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !canCreateWorker(r, workerReq) {
|
if !workerReq.isValid() {
|
||||||
|
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"createWorkerRequest": workerReq,
|
"createWorkerRequest": workerReq,
|
||||||
}).Warn("Failed CreateWorkerRequest")
|
}).Warn("Failed CreateWorkerRequest")
|
||||||
|
|
||||||
r.Json(CreateWorkerResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "You are now allowed to create a worker",
|
Message: "You are now allowed to create a worker",
|
||||||
}, 403)
|
}, 403)
|
||||||
@ -78,14 +34,16 @@ func (api *WebAPI) WorkerCreate(r *Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
handleErr(err, r)
|
handleErr(err, r)
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(CreateWorkerResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Worker: worker,
|
Content: CreateWorkerResponse{
|
||||||
|
Worker: worker,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) WorkerGet(r *Request) {
|
func (api *WebAPI) GetWorker(r *Request) {
|
||||||
|
|
||||||
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
id, err := strconv.ParseInt(r.Ctx.UserValue("id").(string), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -93,7 +51,7 @@ func (api *WebAPI) WorkerGet(r *Request) {
|
|||||||
"id": id,
|
"id": id,
|
||||||
}).Warn("Invalid worker id")
|
}).Warn("Invalid worker id")
|
||||||
|
|
||||||
r.Json(GetWorkerResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -103,7 +61,7 @@ func (api *WebAPI) WorkerGet(r *Request) {
|
|||||||
"id": id,
|
"id": id,
|
||||||
}).Warn("Invalid worker id")
|
}).Warn("Invalid worker id")
|
||||||
|
|
||||||
r.Json(GetWorkerResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Invalid worker id",
|
Message: "Invalid worker id",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -116,23 +74,25 @@ func (api *WebAPI) WorkerGet(r *Request) {
|
|||||||
|
|
||||||
worker.Secret = nil
|
worker.Secret = nil
|
||||||
|
|
||||||
r.OkJson(GetWorkerResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Worker: worker,
|
Content: GetWorkerResponse{
|
||||||
|
Worker: worker,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.Json(GetWorkerResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Worker not found",
|
Message: "Worker not found",
|
||||||
}, 404)
|
}, 404)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *WebAPI) WorkerUpdate(r *Request) {
|
func (api *WebAPI) UpdateWorker(r *Request) {
|
||||||
|
|
||||||
worker, err := api.validateSignature(r)
|
worker, err := api.validateSignature(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
}, 401)
|
}, 401)
|
||||||
@ -142,7 +102,7 @@ func (api *WebAPI) WorkerUpdate(r *Request) {
|
|||||||
req := &UpdateWorkerRequest{}
|
req := &UpdateWorkerRequest{}
|
||||||
err = json.Unmarshal(r.Ctx.Request.Body(), req)
|
err = json.Unmarshal(r.Ctx.Request.Body(), req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r.Json(GetTaskResponse{
|
r.Json(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not parse request",
|
Message: "Could not parse request",
|
||||||
}, 400)
|
}, 400)
|
||||||
@ -153,11 +113,11 @@ func (api *WebAPI) WorkerUpdate(r *Request) {
|
|||||||
ok := api.Database.UpdateWorker(worker)
|
ok := api.Database.UpdateWorker(worker)
|
||||||
|
|
||||||
if ok {
|
if ok {
|
||||||
r.OkJson(UpdateWorkerResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
r.OkJson(UpdateWorkerResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: false,
|
Ok: false,
|
||||||
Message: "Could not update worker",
|
Message: "Could not update worker",
|
||||||
})
|
})
|
||||||
@ -168,9 +128,11 @@ func (api *WebAPI) GetAllWorkerStats(r *Request) {
|
|||||||
|
|
||||||
stats := api.Database.GetAllWorkerStats()
|
stats := api.Database.GetAllWorkerStats()
|
||||||
|
|
||||||
r.OkJson(GetAllWorkerStatsResponse{
|
r.OkJson(JsonResponse{
|
||||||
Ok: true,
|
Ok: true,
|
||||||
Stats: stats,
|
Content: GetAllWorkerStatsResponse{
|
||||||
|
Stats: stats,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,16 +152,6 @@ func (api *WebAPI) workerCreate(request *CreateWorkerRequest) (*storage.Worker,
|
|||||||
return &worker, nil
|
return &worker, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func canCreateWorker(r *Request, cwr *CreateWorkerRequest) bool {
|
|
||||||
|
|
||||||
if cwr.Alias == "unassigned" {
|
|
||||||
//Reserved alias
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeSecret() []byte {
|
func makeSecret() []byte {
|
||||||
|
|
||||||
secret := make([]byte, 32)
|
secret := make([]byte, 32)
|
||||||
|
@ -161,7 +161,7 @@ func (database *Database) SetManagerRoleOn(manager *Manager, projectId int64, ro
|
|||||||
}).Info("Set manager role on project")
|
}).Info("Set manager role on project")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (database *Database) GetAllManagers() *[]Manager {
|
func (database *Database) GetManagerList() *[]Manager {
|
||||||
|
|
||||||
db := database.getDB()
|
db := database.getDB()
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/simon987/task_tracker/api"
|
"github.com/simon987/task_tracker/api"
|
||||||
"github.com/simon987/task_tracker/config"
|
"github.com/simon987/task_tracker/config"
|
||||||
"golang.org/x/net/publicsuffix"
|
"golang.org/x/net/publicsuffix"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
"testing"
|
"testing"
|
||||||
@ -14,63 +13,21 @@ import (
|
|||||||
|
|
||||||
func TestLoginAndAccountInfo(t *testing.T) {
|
func TestLoginAndAccountInfo(t *testing.T) {
|
||||||
|
|
||||||
regResp := register(&api.RegisterRequest{
|
//c := getSessionCtx("testusername", "testusername", false)
|
||||||
Username: "testusername",
|
//
|
||||||
Password: "testpassword",
|
//r, _ := c.Get(config.Cfg.ServerAddr + "/account")
|
||||||
})
|
//
|
||||||
|
//details := &api.GetAccountDetailsResponse{}
|
||||||
if regResp.Ok != true {
|
//data, _ := ioutil.ReadAll(r.Body)
|
||||||
t.Error()
|
//err := json.Unmarshal(data, details)
|
||||||
}
|
//handleErr(err)
|
||||||
|
//
|
||||||
loginResp, r := login(&api.LoginRequest{
|
//if details.LoggedIn != true {
|
||||||
Username: "testusername",
|
// t.Error()
|
||||||
Password: "testpassword",
|
//}
|
||||||
})
|
//if details.Manager.Username != "testusername" {
|
||||||
|
// t.Error()
|
||||||
if loginResp.Ok != true {
|
//}
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if loginResp.Manager.Username != "testusername" {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if loginResp.Manager.Id == 0 {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
ok := false
|
|
||||||
for _, c := range r.Cookies() {
|
|
||||||
if c.Name == config.Cfg.SessionCookieName {
|
|
||||||
ok = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ok != true {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
url := "http://" + config.Cfg.ServerAddr + "/account"
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
for _, c := range r.Cookies() {
|
|
||||||
req.AddCookie(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
client := http.Client{}
|
|
||||||
r, err = client.Do(req)
|
|
||||||
handleErr(err)
|
|
||||||
details := &api.AccountDetails{}
|
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err = json.Unmarshal(data, details)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
if details.LoggedIn != true {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if details.Manager.Username != "testusername" {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if details.Manager.Id != loginResp.Manager.Id {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInvalidUsernameRegister(t *testing.T) {
|
func TestInvalidUsernameRegister(t *testing.T) {
|
||||||
@ -133,7 +90,7 @@ func TestInvalidCredentialsLogin(t *testing.T) {
|
|||||||
Username: "testinvalidcreds",
|
Username: "testinvalidcreds",
|
||||||
})
|
})
|
||||||
|
|
||||||
r, _ := login(&api.LoginRequest{
|
r := login(&api.LoginRequest{
|
||||||
Username: "testinvalidcreds",
|
Username: "testinvalidcreds",
|
||||||
Password: "wrong",
|
Password: "wrong",
|
||||||
})
|
})
|
||||||
@ -152,10 +109,10 @@ func TestRequireManageAccessRole(t *testing.T) {
|
|||||||
CloneUrl: "testRequireManageAccessRole",
|
CloneUrl: "testRequireManageAccessRole",
|
||||||
Name: "testRequireManageAccessRole",
|
Name: "testRequireManageAccessRole",
|
||||||
Version: "testRequireManageAccessRole",
|
Version: "testRequireManageAccessRole",
|
||||||
}, user).Id
|
}, user).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
@ -177,28 +134,16 @@ func TestRequireManageAccessRole(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func register(request *api.RegisterRequest) *api.RegisterResponse {
|
func register(request *api.RegisterRequest) (ar RegisterAR) {
|
||||||
|
|
||||||
r := Post("/register", request, nil, nil)
|
r := Post("/register", request, nil, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
resp := &api.RegisterResponse{}
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func login(request *api.LoginRequest) (*api.LoginResponse, *http.Response) {
|
func login(request *api.LoginRequest) (ar api.JsonResponse) {
|
||||||
|
|
||||||
r := Post("/login", request, nil, nil)
|
r := Post("/login", request, nil, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
resp := &api.LoginResponse{}
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp, r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSessionCtx(username string, password string, admin bool) *http.Client {
|
func getSessionCtx(username string, password string, admin bool) *http.Client {
|
||||||
|
@ -39,7 +39,7 @@ func TestWebHookDontUpdateVersion(t *testing.T) {
|
|||||||
Name: "My version should not be updated",
|
Name: "My version should not be updated",
|
||||||
Version: "old",
|
Version: "old",
|
||||||
GitRepo: "username/not_this_one",
|
GitRepo: "username/not_this_one",
|
||||||
})
|
}).Content
|
||||||
|
|
||||||
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
|
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
|
||||||
bodyReader := bytes.NewReader(body)
|
bodyReader := bytes.NewReader(body)
|
||||||
@ -59,7 +59,7 @@ func TestWebHookDontUpdateVersion(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
getResp, _ := getProjectAsAdmin(resp.Id)
|
getResp := getProjectAsAdmin(resp.Id).Content
|
||||||
|
|
||||||
if getResp.Project.Version != "old" {
|
if getResp.Project.Version != "old" {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -71,7 +71,7 @@ func TestWebHookUpdateVersion(t *testing.T) {
|
|||||||
Name: "My version should be updated",
|
Name: "My version should be updated",
|
||||||
Version: "old",
|
Version: "old",
|
||||||
GitRepo: "username/repo_name",
|
GitRepo: "username/repo_name",
|
||||||
})
|
}).Content
|
||||||
|
|
||||||
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
|
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
|
||||||
bodyReader := bytes.NewReader(body)
|
bodyReader := bytes.NewReader(body)
|
||||||
@ -91,7 +91,7 @@ func TestWebHookUpdateVersion(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
getResp, _ := getProjectAsAdmin(resp.Id)
|
getResp := getProjectAsAdmin(resp.Id).Content
|
||||||
|
|
||||||
if getResp.Project.Version != "new" {
|
if getResp.Project.Version != "new" {
|
||||||
t.Error()
|
t.Error()
|
||||||
|
@ -1,28 +1,19 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/simon987/task_tracker/api"
|
|
||||||
"io/ioutil"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIndex(t *testing.T) {
|
func TestIndex(t *testing.T) {
|
||||||
|
|
||||||
r := Get("/", nil, nil)
|
r := Get("/", nil, nil)
|
||||||
|
var info InfoAR
|
||||||
|
UnmarshalResponse(r, &info)
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(r.Body)
|
if len(info.Info.Name) <= 0 {
|
||||||
var info api.Info
|
|
||||||
err := json.Unmarshal(body, &info)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(info.Name) <= 0 {
|
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if len(info.Version) <= 0 {
|
if len(info.Info.Version) <= 0 {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/simon987/task_tracker/api"
|
"github.com/simon987/task_tracker/api"
|
||||||
"github.com/simon987/task_tracker/storage"
|
"github.com/simon987/task_tracker/storage"
|
||||||
"io/ioutil"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -141,12 +139,12 @@ func TestGetLogs(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(*r.Logs) <= 0 {
|
if len(*r.Content.Logs) <= 0 {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
debugFound := false
|
debugFound := false
|
||||||
for _, log := range *r.Logs {
|
for _, log := range *r.Content.Logs {
|
||||||
if log.Message == "This one shouldn't be returned" {
|
if log.Message == "This one shouldn't be returned" {
|
||||||
t.Error()
|
t.Error()
|
||||||
} else if log.Message == "error" {
|
} else if log.Message == "error" {
|
||||||
@ -174,17 +172,12 @@ func TestGetLogsInvalid(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getLogs(since int64, level storage.LogLevel) *api.GetLogResponse {
|
func getLogs(since int64, level storage.LogLevel) (ar LogsAR) {
|
||||||
|
|
||||||
r := Post(fmt.Sprintf("/logs"), api.GetLogRequest{
|
r := Post(fmt.Sprintf("/logs"), api.GetLogRequest{
|
||||||
Since: since,
|
Since: since,
|
||||||
Level: level,
|
Level: level,
|
||||||
}, nil, nil)
|
}, nil, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
resp := &api.GetLogResponse{}
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ func TestCreateGetProject(t *testing.T) {
|
|||||||
Hidden: false,
|
Hidden: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
id := resp.Id
|
id := resp.Content.Id
|
||||||
|
|
||||||
if id == 0 {
|
if id == 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@ -31,7 +31,7 @@ func TestCreateGetProject(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
getResp, _ := getProjectAsAdmin(id)
|
getResp := getProjectAsAdmin(id).Content
|
||||||
|
|
||||||
if getResp.Project.Id != id {
|
if getResp.Project.Id != id {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -111,7 +111,7 @@ func TestCreateDuplicateProjectRepo(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetProjectNotFound(t *testing.T) {
|
func TestGetProjectNotFound(t *testing.T) {
|
||||||
|
|
||||||
getResp, r := getProjectAsAdmin(12345)
|
getResp := getProjectAsAdmin(12345)
|
||||||
|
|
||||||
if getResp.Ok != false {
|
if getResp.Ok != false {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
@ -120,10 +120,6 @@ func TestGetProjectNotFound(t *testing.T) {
|
|||||||
if len(getResp.Message) <= 0 {
|
if len(getResp.Message) <= 0 {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.StatusCode != 404 {
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateProjectValid(t *testing.T) {
|
func TestUpdateProjectValid(t *testing.T) {
|
||||||
@ -136,7 +132,7 @@ func TestUpdateProjectValid(t *testing.T) {
|
|||||||
CloneUrl: "CloneUrlA",
|
CloneUrl: "CloneUrlA",
|
||||||
GitRepo: "GitRepoA",
|
GitRepo: "GitRepoA",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
updateResp := updateProject(api.UpdateProjectRequest{
|
updateResp := updateProject(api.UpdateProjectRequest{
|
||||||
Priority: 2,
|
Priority: 2,
|
||||||
@ -152,7 +148,7 @@ func TestUpdateProjectValid(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
proj, _ := getProjectAsAdmin(pid)
|
proj := getProjectAsAdmin(pid).Content
|
||||||
|
|
||||||
if proj.Project.Public != false {
|
if proj.Project.Public != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -184,7 +180,7 @@ func TestUpdateProjectInvalid(t *testing.T) {
|
|||||||
CloneUrl: "333333333333333",
|
CloneUrl: "333333333333333",
|
||||||
GitRepo: "llllllllllllllllllls",
|
GitRepo: "llllllllllllllllllls",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
updateResp := updateProject(api.UpdateProjectRequest{
|
updateResp := updateProject(api.UpdateProjectRequest{
|
||||||
Priority: -1,
|
Priority: -1,
|
||||||
@ -214,7 +210,7 @@ func TestUpdateProjectConstraintFail(t *testing.T) {
|
|||||||
CloneUrl: "testUpdateProjectConstraintFail",
|
CloneUrl: "testUpdateProjectConstraintFail",
|
||||||
GitRepo: "testUpdateProjectConstraintFail",
|
GitRepo: "testUpdateProjectConstraintFail",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
createProjectAsAdmin(api.CreateProjectRequest{
|
createProjectAsAdmin(api.CreateProjectRequest{
|
||||||
Public: true,
|
Public: true,
|
||||||
@ -314,8 +310,8 @@ func TestHiddenProjectsNotShownInList(t *testing.T) {
|
|||||||
|
|
||||||
list := getProjectList(nil)
|
list := getProjectList(nil)
|
||||||
|
|
||||||
for _, p := range *list.Projects {
|
for _, p := range *list.Content.Projects {
|
||||||
if p.Id == r.Id {
|
if p.Id == r.Content.Id {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -356,10 +352,12 @@ func TestHiddenProjectNotAccessible(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
pAdmin, _ := getProject(r.Id, testAdminCtx)
|
pid := r.Content.Id
|
||||||
pUser, _ := getProject(r.Id, testUserCtx)
|
|
||||||
pOtherUser, _ := getProject(r.Id, otherUser)
|
pAdmin := getProject(pid, testAdminCtx)
|
||||||
pGuest, _ := getProject(r.Id, nil)
|
pUser := getProject(pid, testUserCtx)
|
||||||
|
pOtherUser := getProject(pid, otherUser)
|
||||||
|
pGuest := getProject(pid, nil)
|
||||||
|
|
||||||
if pAdmin.Ok != true {
|
if pAdmin.Ok != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -388,7 +386,7 @@ func TestUpdateProjectPermissions(t *testing.T) {
|
|||||||
GitRepo: "newupdateprojectpermissions",
|
GitRepo: "newupdateprojectpermissions",
|
||||||
CloneUrl: "newupdateprojectpermissions",
|
CloneUrl: "newupdateprojectpermissions",
|
||||||
Name: "newupdateprojectpermissions",
|
Name: "newupdateprojectpermissions",
|
||||||
}, p.Id, nil)
|
}, p.Content.Id, nil)
|
||||||
|
|
||||||
if r.Ok != false {
|
if r.Ok != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -411,8 +409,8 @@ func TestUserWithReadAccessShouldSeeHiddenProjectInList(t *testing.T) {
|
|||||||
list := getProjectList(testUserCtx)
|
list := getProjectList(testUserCtx)
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, p := range *list.Projects {
|
for _, p := range *list.Content.Projects {
|
||||||
if p.Id == pHidden.Id {
|
if p.Id == pHidden.Content.Id {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -435,8 +433,8 @@ func TestAdminShouldSeeHiddenProjectInList(t *testing.T) {
|
|||||||
list := getProjectList(testAdminCtx)
|
list := getProjectList(testAdminCtx)
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, p := range *list.Projects {
|
for _, p := range *list.Content.Projects {
|
||||||
if p.Id == pHidden.Id {
|
if p.Id == pHidden.Content.Id {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -446,42 +444,31 @@ func TestAdminShouldSeeHiddenProjectInList(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createProjectAsAdmin(req api.CreateProjectRequest) *api.CreateProjectResponse {
|
func createProjectAsAdmin(req api.CreateProjectRequest) CreateProjectAR {
|
||||||
return createProject(req, testAdminCtx)
|
return createProject(req, testAdminCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createProject(req api.CreateProjectRequest, s *http.Client) *api.CreateProjectResponse {
|
func createProject(req api.CreateProjectRequest, s *http.Client) (ar CreateProjectAR) {
|
||||||
r := Post("/project/create", req, nil, s)
|
r := Post("/project/create", req, nil, s)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp api.CreateProjectResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProjectAsAdmin(id int64) (*api.GetProjectResponse, *http.Response) {
|
func getProjectAsAdmin(id int64) ProjectAR {
|
||||||
return getProject(id, testAdminCtx)
|
return getProject(id, testAdminCtx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProject(id int64, s *http.Client) (*api.GetProjectResponse, *http.Response) {
|
func getProject(id int64, s *http.Client) (ar ProjectAR) {
|
||||||
|
|
||||||
r := Get(fmt.Sprintf("/project/get/%d", id), nil, s)
|
r := Get(fmt.Sprintf("/project/get/%d", id), nil, s)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var getResp api.GetProjectResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &getResp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &getResp, r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateProject(request api.UpdateProjectRequest, pid int64, s *http.Client) *api.UpdateProjectResponse {
|
func updateProject(request api.UpdateProjectRequest, pid int64, s *http.Client) *api.JsonResponse {
|
||||||
|
|
||||||
r := Post(fmt.Sprintf("/project/update/%d", pid), request, nil, s)
|
r := Post(fmt.Sprintf("/project/update/%d", pid), request, nil, s)
|
||||||
|
|
||||||
var resp api.UpdateProjectResponse
|
var resp api.JsonResponse
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
data, _ := ioutil.ReadAll(r.Body)
|
||||||
err := json.Unmarshal(data, &resp)
|
err := json.Unmarshal(data, &resp)
|
||||||
handleErr(err)
|
handleErr(err)
|
||||||
@ -489,13 +476,8 @@ func updateProject(request api.UpdateProjectRequest, pid int64, s *http.Client)
|
|||||||
return &resp
|
return &resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProjectList(s *http.Client) *api.GetAllProjectsResponse {
|
func getProjectList(s *http.Client) (ar ProjectListAR) {
|
||||||
r := Get("/project/list", nil, s)
|
r := Get("/project/list", nil, s)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp api.GetAllProjectsResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
@ -19,8 +19,8 @@ func BenchmarkCreateTaskRemote(b *testing.B) {
|
|||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: resp.Id,
|
Project: resp.Content.Id,
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
Recipe: "{}",
|
Recipe: "{}",
|
||||||
MaxRetries: 1,
|
MaxRetries: 1,
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/simon987/task_tracker/api"
|
"github.com/simon987/task_tracker/api"
|
||||||
"github.com/simon987/task_tracker/storage"
|
"github.com/simon987/task_tracker/storage"
|
||||||
"io/ioutil"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,17 +14,17 @@ func TestCreateTaskValid(t *testing.T) {
|
|||||||
Version: "Test Version",
|
Version: "Test Version",
|
||||||
CloneUrl: "http://github.com/test/test",
|
CloneUrl: "http://github.com/test/test",
|
||||||
GitRepo: "Some git repo",
|
GitRepo: "Some git repo",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: false,
|
Assign: false,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(pid, worker.Id, testAdminCtx)
|
acceptAccessRequest(pid, worker.Id, testAdminCtx)
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Recipe: "{}",
|
Recipe: "{}",
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
@ -41,7 +39,7 @@ func TestCreateTaskInvalidProject(t *testing.T) {
|
|||||||
|
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: 123456,
|
Project: 123456,
|
||||||
Recipe: "{}",
|
Recipe: "{}",
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
@ -103,7 +101,7 @@ func TestCreateTaskInvalidRetries(t *testing.T) {
|
|||||||
|
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: 1,
|
Project: 1,
|
||||||
MaxRetries: -1,
|
MaxRetries: -1,
|
||||||
}, worker)
|
}, worker)
|
||||||
@ -121,7 +119,7 @@ func TestCreateTaskInvalidRecipe(t *testing.T) {
|
|||||||
|
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: 1,
|
Project: 1,
|
||||||
Recipe: "",
|
Recipe: "",
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
@ -138,37 +136,33 @@ func TestCreateTaskInvalidRecipe(t *testing.T) {
|
|||||||
|
|
||||||
func TestCreateGetTask(t *testing.T) {
|
func TestCreateGetTask(t *testing.T) {
|
||||||
|
|
||||||
//Make sure there is always a project for id:1
|
pid := createProjectAsAdmin(api.CreateProjectRequest{
|
||||||
resp := createProjectAsAdmin(api.CreateProjectRequest{
|
|
||||||
Name: "My project",
|
Name: "My project",
|
||||||
Version: "1.0",
|
Version: "1.0",
|
||||||
CloneUrl: "http://github.com/test/test",
|
CloneUrl: "http://github.com/test/test",
|
||||||
GitRepo: "myrepo",
|
GitRepo: "myrepo",
|
||||||
Priority: 999,
|
Priority: 999,
|
||||||
Public: true,
|
Public: true,
|
||||||
})
|
}).Content.Id
|
||||||
|
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Project: resp.Id,
|
Project: pid,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(resp.Id, worker.Id, testAdminCtx)
|
acceptAccessRequest(pid, worker.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: resp.Id,
|
Project: pid,
|
||||||
Recipe: "{\"url\":\"test\"}",
|
Recipe: "{\"url\":\"test\"}",
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
Priority: 9999,
|
Priority: 9999,
|
||||||
VerificationCount: 12,
|
VerificationCount: 12,
|
||||||
}, worker)
|
}, worker)
|
||||||
|
|
||||||
taskResp := getTaskFromProject(resp.Id, worker)
|
taskResp := getTaskFromProject(pid, worker).Content
|
||||||
|
|
||||||
if taskResp.Ok != true {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if taskResp.Task.VerificationCount != 12 {
|
if taskResp.Task.VerificationCount != 12 {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
@ -187,7 +181,7 @@ func TestCreateGetTask(t *testing.T) {
|
|||||||
if taskResp.Task.MaxRetries != 3 {
|
if taskResp.Task.MaxRetries != 3 {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if taskResp.Task.Project.Id != resp.Id {
|
if taskResp.Task.Project.Id != pid {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if taskResp.Task.Project.Priority != 999 {
|
if taskResp.Task.Project.Priority != 999 {
|
||||||
@ -213,7 +207,7 @@ func createTasks(prefix string) (int64, int64) {
|
|||||||
GitRepo: prefix + "low1",
|
GitRepo: prefix + "low1",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
Public: true,
|
Public: true,
|
||||||
})
|
}).Content
|
||||||
highP := createProjectAsAdmin(api.CreateProjectRequest{
|
highP := createProjectAsAdmin(api.CreateProjectRequest{
|
||||||
Name: prefix + "high",
|
Name: prefix + "high",
|
||||||
Version: "1.0",
|
Version: "1.0",
|
||||||
@ -221,37 +215,37 @@ func createTasks(prefix string) (int64, int64) {
|
|||||||
GitRepo: prefix + "high1",
|
GitRepo: prefix + "high1",
|
||||||
Priority: 999,
|
Priority: 999,
|
||||||
Public: true,
|
Public: true,
|
||||||
})
|
}).Content
|
||||||
worker := genWid()
|
worker := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: false,
|
Assign: false,
|
||||||
Project: highP.Id,
|
Project: highP.Id,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(highP.Id, worker.Id, testAdminCtx)
|
acceptAccessRequest(highP.Id, worker.Id, testAdminCtx)
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: false,
|
Assign: false,
|
||||||
Project: lowP.Id,
|
Project: lowP.Id,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(lowP.Id, worker.Id, testAdminCtx)
|
acceptAccessRequest(lowP.Id, worker.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: lowP.Id,
|
Project: lowP.Id,
|
||||||
Recipe: "low1",
|
Recipe: "low1",
|
||||||
Priority: 0,
|
Priority: 0,
|
||||||
}, worker)
|
}, worker)
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: lowP.Id,
|
Project: lowP.Id,
|
||||||
Recipe: "low2",
|
Recipe: "low2",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
}, worker)
|
}, worker)
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: highP.Id,
|
Project: highP.Id,
|
||||||
Recipe: "high1",
|
Recipe: "high1",
|
||||||
Priority: 100,
|
Priority: 100,
|
||||||
}, worker)
|
}, worker)
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: highP.Id,
|
Project: highP.Id,
|
||||||
Recipe: "high2",
|
Recipe: "high2",
|
||||||
Priority: 101,
|
Priority: 101,
|
||||||
@ -265,10 +259,10 @@ func TestTaskProjectPriority(t *testing.T) {
|
|||||||
wid := genWid()
|
wid := genWid()
|
||||||
l, h := createTasks("withProject")
|
l, h := createTasks("withProject")
|
||||||
|
|
||||||
t1 := getTaskFromProject(l, wid)
|
t1 := getTaskFromProject(l, wid).Content
|
||||||
t2 := getTaskFromProject(l, wid)
|
t2 := getTaskFromProject(l, wid).Content
|
||||||
t3 := getTaskFromProject(h, wid)
|
t3 := getTaskFromProject(h, wid).Content
|
||||||
t4 := getTaskFromProject(h, wid)
|
t4 := getTaskFromProject(h, wid).Content
|
||||||
|
|
||||||
if t1.Task.Recipe != "low2" {
|
if t1.Task.Recipe != "low2" {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -295,10 +289,10 @@ func TestTaskPriority(t *testing.T) {
|
|||||||
|
|
||||||
createTasks("")
|
createTasks("")
|
||||||
|
|
||||||
t1 := getTask(wid)
|
t1 := getTask(wid).Content
|
||||||
t2 := getTask(wid)
|
t2 := getTask(wid).Content
|
||||||
t3 := getTask(wid)
|
t3 := getTask(wid).Content
|
||||||
t4 := getTask(wid)
|
t4 := getTask(wid).Content
|
||||||
|
|
||||||
if t1.Task.Recipe != "high2" {
|
if t1.Task.Recipe != "high2" {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -326,16 +320,16 @@ func TestTaskNoAccess(t *testing.T) {
|
|||||||
CloneUrl: "fjkslejf cesl",
|
CloneUrl: "fjkslejf cesl",
|
||||||
GitRepo: "fffffffff",
|
GitRepo: "fffffffff",
|
||||||
Public: false,
|
Public: false,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(worker.Id, pid, testAdminCtx)
|
acceptAccessRequest(worker.Id, pid, testAdminCtx)
|
||||||
|
|
||||||
createResp := createTask(api.CreateTaskRequest{
|
createResp := createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
MaxAssignTime: 10,
|
MaxAssignTime: 10,
|
||||||
@ -357,7 +351,7 @@ func TestTaskNoAccess(t *testing.T) {
|
|||||||
if len(tResp.Message) <= 0 {
|
if len(tResp.Message) <= 0 {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if tResp.Task != nil {
|
if tResp.Content.Task != nil {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -374,16 +368,16 @@ func TestTaskHasAccess(t *testing.T) {
|
|||||||
CloneUrl: "josaeiuf cesl",
|
CloneUrl: "josaeiuf cesl",
|
||||||
GitRepo: "wewwwwwwwwwwwwwwwwwwwwww",
|
GitRepo: "wewwwwwwwwwwwwwwwwwwwwww",
|
||||||
Public: false,
|
Public: false,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: true,
|
Submit: true,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(worker.Id, pid, testAdminCtx)
|
acceptAccessRequest(worker.Id, pid, testAdminCtx)
|
||||||
|
|
||||||
createResp := createTask(api.CreateTaskRequest{
|
createResp := createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
MaxAssignTime: 10,
|
MaxAssignTime: 10,
|
||||||
@ -400,7 +394,7 @@ func TestTaskHasAccess(t *testing.T) {
|
|||||||
if tResp.Ok != true {
|
if tResp.Ok != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if tResp.Task == nil {
|
if tResp.Content.Task == nil {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -426,23 +420,23 @@ func TestReleaseTaskSuccess(t *testing.T) {
|
|||||||
Name: "testreleasetask",
|
Name: "testreleasetask",
|
||||||
Motd: "",
|
Motd: "",
|
||||||
Public: true,
|
Public: true,
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, worker)
|
}, worker)
|
||||||
acceptAccessRequest(pid, worker.Id, testAdminCtx)
|
acceptAccessRequest(pid, worker.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Priority: 0,
|
Priority: 0,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Recipe: "{}",
|
Recipe: "{}",
|
||||||
MaxRetries: 3,
|
MaxRetries: 3,
|
||||||
}, worker)
|
}, worker)
|
||||||
|
|
||||||
task := getTaskFromProject(pid, worker).Task
|
task := getTaskFromProject(pid, worker).Content.Task
|
||||||
|
|
||||||
releaseResp := releaseTask(api.ReleaseTaskRequest{
|
releaseResp := releaseTask(api.ReleaseTaskRequest{
|
||||||
TaskId: task.Id,
|
TaskId: task.Id,
|
||||||
@ -471,17 +465,17 @@ func TestCreateIntCollision(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
Name: "testcreateintcollision",
|
Name: "testcreateintcollision",
|
||||||
Version: "testcreateintcollision",
|
Version: "testcreateintcollision",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
||||||
|
|
||||||
if createTask(api.CreateTaskRequest{
|
if createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Hash64: 123,
|
Hash64: 123,
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
@ -490,7 +484,7 @@ func TestCreateIntCollision(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Hash64: 123,
|
Hash64: 123,
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
@ -517,17 +511,17 @@ func TestCreateStringCollision(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
Name: "testcreatestringcollision",
|
Name: "testcreatestringcollision",
|
||||||
Version: "testcreatestringcollision",
|
Version: "testcreatestringcollision",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
||||||
|
|
||||||
if createTask(api.CreateTaskRequest{
|
if createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
UniqueString: "Hello, world",
|
UniqueString: "Hello, world",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
@ -536,14 +530,14 @@ func TestCreateStringCollision(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := createTask(api.CreateTaskRequest{
|
resp := createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
UniqueString: "Hello, world",
|
UniqueString: "Hello, world",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
Recipe: "{}",
|
Recipe: "{}",
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
if !createTask(api.CreateTaskRequest{
|
if !createTask(api.SubmitTaskRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
UniqueString: "This one should work",
|
UniqueString: "This one should work",
|
||||||
Priority: 1,
|
Priority: 1,
|
||||||
@ -572,28 +566,28 @@ func TestCannotVerifySameTaskTwice(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
Name: "verifysametasktwice",
|
Name: "verifysametasktwice",
|
||||||
Version: "verifysametasktwice",
|
Version: "verifysametasktwice",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
VerificationCount: 2,
|
VerificationCount: 2,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Recipe: "verifysametasktwice",
|
Recipe: "verifysametasktwice",
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
task := getTaskFromProject(pid, w).Task
|
task := getTaskFromProject(pid, w).Content.Task
|
||||||
rlr := releaseTask(api.ReleaseTaskRequest{
|
rlr := releaseTask(api.ReleaseTaskRequest{
|
||||||
Result: storage.TR_OK,
|
Result: storage.TR_OK,
|
||||||
TaskId: task.Id,
|
TaskId: task.Id,
|
||||||
Verification: 123,
|
Verification: 123,
|
||||||
}, w)
|
}, w).Content
|
||||||
|
|
||||||
if rlr.Updated != false {
|
if rlr.Updated != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -616,22 +610,22 @@ func TestVerification2(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
Name: "verify2",
|
Name: "verify2",
|
||||||
Version: "verify2",
|
Version: "verify2",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
w2 := genWid()
|
w2 := genWid()
|
||||||
w3 := genWid()
|
w3 := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w2)
|
}, w2)
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
@ -640,40 +634,40 @@ func TestVerification2(t *testing.T) {
|
|||||||
acceptAccessRequest(pid, w2.Id, testAdminCtx)
|
acceptAccessRequest(pid, w2.Id, testAdminCtx)
|
||||||
acceptAccessRequest(pid, w3.Id, testAdminCtx)
|
acceptAccessRequest(pid, w3.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
VerificationCount: 2,
|
VerificationCount: 2,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Recipe: "verify2",
|
Recipe: "verify2",
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
task := getTaskFromProject(pid, w).Task
|
task := getTaskFromProject(pid, w).Content.Task
|
||||||
rlr := releaseTask(api.ReleaseTaskRequest{
|
rlr := releaseTask(api.ReleaseTaskRequest{
|
||||||
Result: storage.TR_OK,
|
Result: storage.TR_OK,
|
||||||
TaskId: task.Id,
|
TaskId: task.Id,
|
||||||
Verification: 123,
|
Verification: 123,
|
||||||
}, w)
|
}, w).Content
|
||||||
|
|
||||||
if rlr.Updated != false {
|
if rlr.Updated != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
task2 := getTaskFromProject(pid, w2).Task
|
task2 := getTaskFromProject(pid, w2).Content.Task
|
||||||
rlr2 := releaseTask(api.ReleaseTaskRequest{
|
rlr2 := releaseTask(api.ReleaseTaskRequest{
|
||||||
Result: storage.TR_OK,
|
Result: storage.TR_OK,
|
||||||
Verification: 1,
|
Verification: 1,
|
||||||
TaskId: task2.Id,
|
TaskId: task2.Id,
|
||||||
}, w2)
|
}, w2).Content
|
||||||
|
|
||||||
if rlr2.Updated != false {
|
if rlr2.Updated != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
task3 := getTaskFromProject(pid, w3).Task
|
task3 := getTaskFromProject(pid, w3).Content.Task
|
||||||
rlr3 := releaseTask(api.ReleaseTaskRequest{
|
rlr3 := releaseTask(api.ReleaseTaskRequest{
|
||||||
Result: storage.TR_OK,
|
Result: storage.TR_OK,
|
||||||
Verification: 123,
|
Verification: 123,
|
||||||
TaskId: task3.Id,
|
TaskId: task3.Id,
|
||||||
}, w3)
|
}, w3).Content
|
||||||
|
|
||||||
if rlr3.Updated != true {
|
if rlr3.Updated != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -690,24 +684,24 @@ func TestReleaseTaskFail(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
Name: "releasefail",
|
Name: "releasefail",
|
||||||
Version: "releasefail",
|
Version: "releasefail",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
w := genWid()
|
w := genWid()
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: pid,
|
Project: pid,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
acceptAccessRequest(pid, w.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
MaxRetries: 0,
|
MaxRetries: 0,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
VerificationCount: 1,
|
VerificationCount: 1,
|
||||||
Recipe: "releasefail",
|
Recipe: "releasefail",
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
task := getTaskFromProject(pid, w).Task
|
task := getTaskFromProject(pid, w).Content.Task
|
||||||
|
|
||||||
resp := releaseTask(api.ReleaseTaskRequest{
|
resp := releaseTask(api.ReleaseTaskRequest{
|
||||||
Result: storage.TR_FAIL,
|
Result: storage.TR_FAIL,
|
||||||
@ -715,13 +709,12 @@ func TestReleaseTaskFail(t *testing.T) {
|
|||||||
Verification: 1,
|
Verification: 1,
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
if resp.Updated != true {
|
if resp.Content.Updated != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
if resp.Ok != true {
|
if resp.Ok != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTaskChain(t *testing.T) {
|
func TestTaskChain(t *testing.T) {
|
||||||
@ -733,7 +726,7 @@ func TestTaskChain(t *testing.T) {
|
|||||||
Public: true,
|
Public: true,
|
||||||
GitRepo: "testtaskchain1",
|
GitRepo: "testtaskchain1",
|
||||||
CloneUrl: "testtaskchain1",
|
CloneUrl: "testtaskchain1",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
p2 := createProjectAsAdmin(api.CreateProjectRequest{
|
p2 := createProjectAsAdmin(api.CreateProjectRequest{
|
||||||
Name: "testtaskchain2",
|
Name: "testtaskchain2",
|
||||||
@ -741,13 +734,13 @@ func TestTaskChain(t *testing.T) {
|
|||||||
GitRepo: "testtaskchain2",
|
GitRepo: "testtaskchain2",
|
||||||
CloneUrl: "testtaskchain2",
|
CloneUrl: "testtaskchain2",
|
||||||
Chain: p1,
|
Chain: p1,
|
||||||
}).Id
|
}).Content.Id
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: p1,
|
Project: p1,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
}, w)
|
}, w)
|
||||||
requestAccess(api.WorkerAccessRequest{
|
requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Project: p2,
|
Project: p2,
|
||||||
Assign: true,
|
Assign: true,
|
||||||
Submit: true,
|
Submit: true,
|
||||||
@ -755,20 +748,20 @@ func TestTaskChain(t *testing.T) {
|
|||||||
acceptAccessRequest(p1, w.Id, testAdminCtx)
|
acceptAccessRequest(p1, w.Id, testAdminCtx)
|
||||||
acceptAccessRequest(p2, w.Id, testAdminCtx)
|
acceptAccessRequest(p2, w.Id, testAdminCtx)
|
||||||
|
|
||||||
createTask(api.CreateTaskRequest{
|
createTask(api.SubmitTaskRequest{
|
||||||
Project: p2,
|
Project: p2,
|
||||||
Recipe: "###",
|
Recipe: "###",
|
||||||
VerificationCount: 0,
|
VerificationCount: 0,
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
t1 := getTaskFromProject(p2, w).Task
|
t1 := getTaskFromProject(p2, w).Content.Task
|
||||||
|
|
||||||
releaseTask(api.ReleaseTaskRequest{
|
releaseTask(api.ReleaseTaskRequest{
|
||||||
TaskId: t1.Id,
|
TaskId: t1.Id,
|
||||||
Result: storage.TR_OK,
|
Result: storage.TR_OK,
|
||||||
}, w)
|
}, w)
|
||||||
|
|
||||||
chained := getTaskFromProject(p1, w).Task
|
chained := getTaskFromProject(p1, w).Content.Task
|
||||||
|
|
||||||
if chained.VerificationCount != t1.VerificationCount {
|
if chained.VerificationCount != t1.VerificationCount {
|
||||||
t.Error()
|
t.Error()
|
||||||
@ -787,50 +780,26 @@ func TestTaskChain(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTask(request api.CreateTaskRequest, worker *storage.Worker) *api.CreateTaskResponse {
|
func createTask(request api.SubmitTaskRequest, worker *storage.Worker) (ar api.JsonResponse) {
|
||||||
|
|
||||||
r := Post("/task/create", request, worker, nil)
|
r := Post("/task/create", request, worker, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp api.CreateTaskResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTask(worker *storage.Worker) *api.GetTaskResponse {
|
func getTask(worker *storage.Worker) (ar TaskAR) {
|
||||||
|
r := Get("/task/get", worker, nil)
|
||||||
r := Get(fmt.Sprintf("/task/get"), worker, nil)
|
UnmarshalResponse(r, &ar)
|
||||||
|
return
|
||||||
var resp api.GetTaskResponse
|
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTaskFromProject(project int64, worker *storage.Worker) *api.GetTaskResponse {
|
func getTaskFromProject(project int64, worker *storage.Worker) (ar TaskAR) {
|
||||||
|
|
||||||
r := Get(fmt.Sprintf("/task/get/%d", project), worker, nil)
|
r := Get(fmt.Sprintf("/task/get/%d", project), worker, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp api.GetTaskResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func releaseTask(request api.ReleaseTaskRequest, worker *storage.Worker) *api.ReleaseTaskResponse {
|
func releaseTask(request api.ReleaseTaskRequest, worker *storage.Worker) (ar ReleaseAR) {
|
||||||
|
|
||||||
r := Post("/task/release", request, worker, nil)
|
r := Post("/task/release", request, worker, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp api.ReleaseTaskResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return &resp
|
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,39 @@
|
|||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/simon987/task_tracker/api"
|
"github.com/simon987/task_tracker/api"
|
||||||
"github.com/simon987/task_tracker/storage"
|
"github.com/simon987/task_tracker/storage"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCreateGetWorker(t *testing.T) {
|
func TestCreateGetWorker(t *testing.T) {
|
||||||
|
|
||||||
resp, r := createWorker(api.CreateWorkerRequest{
|
resp := createWorker(api.CreateWorkerRequest{
|
||||||
Alias: "my_worker_alias",
|
Alias: "my_worker_alias",
|
||||||
})
|
})
|
||||||
|
w := resp.Content.Worker
|
||||||
if r.StatusCode != 200 {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.Ok != true {
|
if resp.Ok != true {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
getResp, r := getWorker(resp.Worker.Id)
|
getResp := getWorker(w.Id)
|
||||||
|
|
||||||
if r.StatusCode != 200 {
|
if w.Id != getResp.Content.Worker.Id {
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if resp.Worker.Id != getResp.Worker.Id {
|
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Worker.Alias != "my_worker_alias" {
|
if w.Alias != "my_worker_alias" {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetWorkerNotFound(t *testing.T) {
|
func TestGetWorkerNotFound(t *testing.T) {
|
||||||
|
|
||||||
resp, r := getWorker(99999999)
|
resp := getWorker(99999999)
|
||||||
|
|
||||||
if r.StatusCode != 404 {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if resp.Ok != false {
|
if resp.Ok != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
@ -52,11 +41,8 @@ func TestGetWorkerNotFound(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetWorkerInvalid(t *testing.T) {
|
func TestGetWorkerInvalid(t *testing.T) {
|
||||||
|
|
||||||
resp, r := getWorker(-1)
|
resp := getWorker(-1)
|
||||||
|
|
||||||
if r.StatusCode != 400 {
|
|
||||||
t.Error()
|
|
||||||
}
|
|
||||||
if resp.Ok != false {
|
if resp.Ok != false {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
@ -76,16 +62,16 @@ func TestUpdateAliasValid(t *testing.T) {
|
|||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
w, _ := getWorker(wid.Id)
|
w := getWorker(wid.Id).Content.Worker
|
||||||
|
|
||||||
if w.Worker.Alias != "new alias" {
|
if w.Alias != "new alias" {
|
||||||
t.Error()
|
t.Error()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCreateWorkerAliasInvalid(t *testing.T) {
|
func TestCreateWorkerAliasInvalid(t *testing.T) {
|
||||||
|
|
||||||
resp, _ := createWorker(api.CreateWorkerRequest{
|
resp := createWorker(api.CreateWorkerRequest{
|
||||||
Alias: "unassigned", //reserved alias
|
Alias: "unassigned", //reserved alias
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -105,9 +91,9 @@ func TestInvalidAccessRequest(t *testing.T) {
|
|||||||
Name: "testinvalidaccessreq",
|
Name: "testinvalidaccessreq",
|
||||||
CloneUrl: "testinvalidaccessreq",
|
CloneUrl: "testinvalidaccessreq",
|
||||||
GitRepo: "testinvalidaccessreq",
|
GitRepo: "testinvalidaccessreq",
|
||||||
}).Id
|
}).Content.Id
|
||||||
|
|
||||||
r := requestAccess(api.WorkerAccessRequest{
|
r := requestAccess(api.CreateWorkerAccessRequest{
|
||||||
Submit: false,
|
Submit: false,
|
||||||
Assign: false,
|
Assign: false,
|
||||||
Project: pid,
|
Project: pid,
|
||||||
@ -122,81 +108,46 @@ func TestInvalidAccessRequest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWorker(req api.CreateWorkerRequest) (*api.CreateWorkerResponse, *http.Response) {
|
func createWorker(req api.CreateWorkerRequest) (ar WorkerAR) {
|
||||||
r := Post("/worker/create", req, nil, nil)
|
r := Post("/worker/create", req, nil, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.CreateWorkerResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp, r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWorker(id int64) (*api.GetWorkerResponse, *http.Response) {
|
func getWorker(id int64) (ar WorkerAR) {
|
||||||
|
|
||||||
r := Get(fmt.Sprintf("/worker/get/%d", id), nil, nil)
|
r := Get(fmt.Sprintf("/worker/get/%d", id), nil, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.GetWorkerResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp, r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genWid() *storage.Worker {
|
func genWid() *storage.Worker {
|
||||||
|
resp := createWorker(api.CreateWorkerRequest{})
|
||||||
resp, _ := createWorker(api.CreateWorkerRequest{})
|
return resp.Content.Worker
|
||||||
return resp.Worker
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestAccess(req api.WorkerAccessRequest, w *storage.Worker) *api.WorkerAccessRequestResponse {
|
func requestAccess(req api.CreateWorkerAccessRequest, w *storage.Worker) (ar WorkerAR) {
|
||||||
|
|
||||||
r := Post(fmt.Sprintf("/project/request_access"), req, w, nil)
|
r := Post(fmt.Sprintf("/project/request_access"), req, w, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.WorkerAccessRequestResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func acceptAccessRequest(pid int64, wid int64, s *http.Client) *api.WorkerAccessRequestResponse {
|
func acceptAccessRequest(pid int64, wid int64, s *http.Client) (ar api.JsonResponse) {
|
||||||
|
|
||||||
r := Post(fmt.Sprintf("/project/accept_request/%d/%d", pid, wid), nil,
|
r := Post(fmt.Sprintf("/project/accept_request/%d/%d", pid, wid), nil,
|
||||||
nil, s)
|
nil, s)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.WorkerAccessRequestResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func rejectAccessRequest(pid int64, wid int64, s *http.Client) *api.WorkerAccessRequestResponse {
|
func rejectAccessRequest(pid int64, wid int64, s *http.Client) (ar api.JsonResponse) {
|
||||||
|
|
||||||
r := Post(fmt.Sprintf("/project/reject_request/%d/%d", pid, wid), nil,
|
r := Post(fmt.Sprintf("/project/reject_request/%d/%d", pid, wid), nil,
|
||||||
nil, s)
|
nil, s)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.WorkerAccessRequestResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateWorker(request api.UpdateWorkerRequest, w *storage.Worker) *api.UpdateWorkerResponse {
|
func updateWorker(request api.UpdateWorkerRequest, w *storage.Worker) (ar api.JsonResponse) {
|
||||||
|
|
||||||
r := Post("/worker/update", request, w, nil)
|
r := Post("/worker/update", request, w, nil)
|
||||||
|
UnmarshalResponse(r, &ar)
|
||||||
var resp *api.UpdateWorkerResponse
|
return
|
||||||
data, _ := ioutil.ReadAll(r.Body)
|
|
||||||
err := json.Unmarshal(data, &resp)
|
|
||||||
handleErr(err)
|
|
||||||
|
|
||||||
return resp
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/simon987/task_tracker/api"
|
||||||
"github.com/simon987/task_tracker/config"
|
"github.com/simon987/task_tracker/config"
|
||||||
"github.com/simon987/task_tracker/storage"
|
"github.com/simon987/task_tracker/storage"
|
||||||
"io"
|
"io"
|
||||||
@ -19,6 +21,11 @@ type SessionContext struct {
|
|||||||
SessionCookie *http.Cookie
|
SessionCookie *http.Cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ResponseHeader struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
func Post(path string, x interface{}, worker *storage.Worker, s *http.Client) *http.Response {
|
func Post(path string, x interface{}, worker *storage.Worker, s *http.Client) *http.Response {
|
||||||
|
|
||||||
if s == nil {
|
if s == nil {
|
||||||
@ -88,3 +95,80 @@ func GenericJson(body io.ReadCloser) map[string]interface{} {
|
|||||||
|
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UnmarshalResponse(r *http.Response, result interface{}) {
|
||||||
|
data, err := ioutil.ReadAll(r.Body)
|
||||||
|
fmt.Println(string(data))
|
||||||
|
err = json.Unmarshal(data, result)
|
||||||
|
handleErr(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type WorkerAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Worker *storage.Worker `json:"worker"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Manager *storage.Manager `json:"manager"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProjectAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Project *storage.Project `json:"project"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateProjectAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Id int64 `json:"id"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type InfoAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
api.Info `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type LogsAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Logs *[]storage.LogEntry `json:"logs"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProjectListAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Projects *[]storage.Project `json:"projects"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TaskAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Task *storage.Task `json:"task"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ReleaseAR struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Content struct {
|
||||||
|
Updated bool `json:"updated"`
|
||||||
|
} `json:"content"`
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user