Cleanup api responses

This commit is contained in:
simon987
2019-02-16 19:44:03 -05:00
parent 8784b536d3
commit 71e05ecdd6
21 changed files with 809 additions and 888 deletions

View File

@@ -6,7 +6,6 @@ import (
"github.com/simon987/task_tracker/api"
"github.com/simon987/task_tracker/config"
"golang.org/x/net/publicsuffix"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"testing"
@@ -14,63 +13,21 @@ import (
func TestLoginAndAccountInfo(t *testing.T) {
regResp := register(&api.RegisterRequest{
Username: "testusername",
Password: "testpassword",
})
if regResp.Ok != true {
t.Error()
}
loginResp, r := login(&api.LoginRequest{
Username: "testusername",
Password: "testpassword",
})
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()
}
//c := getSessionCtx("testusername", "testusername", false)
//
//r, _ := c.Get(config.Cfg.ServerAddr + "/account")
//
//details := &api.GetAccountDetailsResponse{}
//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()
//}
}
func TestInvalidUsernameRegister(t *testing.T) {
@@ -133,7 +90,7 @@ func TestInvalidCredentialsLogin(t *testing.T) {
Username: "testinvalidcreds",
})
r, _ := login(&api.LoginRequest{
r := login(&api.LoginRequest{
Username: "testinvalidcreds",
Password: "wrong",
})
@@ -152,10 +109,10 @@ func TestRequireManageAccessRole(t *testing.T) {
CloneUrl: "testRequireManageAccessRole",
Name: "testRequireManageAccessRole",
Version: "testRequireManageAccessRole",
}, user).Id
}, user).Content.Id
w := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Submit: true,
Assign: true,
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)
resp := &api.RegisterResponse{}
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}
func login(request *api.LoginRequest) (*api.LoginResponse, *http.Response) {
func login(request *api.LoginRequest) (ar api.JsonResponse) {
r := Post("/login", request, nil, nil)
resp := &api.LoginResponse{}
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, resp)
handleErr(err)
return resp, r
UnmarshalResponse(r, &ar)
return
}
func getSessionCtx(username string, password string, admin bool) *http.Client {

View File

@@ -39,7 +39,7 @@ func TestWebHookDontUpdateVersion(t *testing.T) {
Name: "My version should not be updated",
Version: "old",
GitRepo: "username/not_this_one",
})
}).Content
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
bodyReader := bytes.NewReader(body)
@@ -59,7 +59,7 @@ func TestWebHookDontUpdateVersion(t *testing.T) {
t.Error()
}
getResp, _ := getProjectAsAdmin(resp.Id)
getResp := getProjectAsAdmin(resp.Id).Content
if getResp.Project.Version != "old" {
t.Error()
@@ -71,7 +71,7 @@ func TestWebHookUpdateVersion(t *testing.T) {
Name: "My version should be updated",
Version: "old",
GitRepo: "username/repo_name",
})
}).Content
body := []byte(`{"ref": "refs/heads/master", "after": "new", "repository": {"full_name": "username/repo_name"}}`)
bodyReader := bytes.NewReader(body)
@@ -91,7 +91,7 @@ func TestWebHookUpdateVersion(t *testing.T) {
t.Error()
}
getResp, _ := getProjectAsAdmin(resp.Id)
getResp := getProjectAsAdmin(resp.Id).Content
if getResp.Project.Version != "new" {
t.Error()

View File

@@ -1,28 +1,19 @@
package test
import (
"encoding/json"
"github.com/simon987/task_tracker/api"
"io/ioutil"
"testing"
)
func TestIndex(t *testing.T) {
r := Get("/", nil, nil)
var info InfoAR
UnmarshalResponse(r, &info)
body, _ := ioutil.ReadAll(r.Body)
var info api.Info
err := json.Unmarshal(body, &info)
if err != nil {
t.Error(err.Error())
}
if len(info.Name) <= 0 {
if len(info.Info.Name) <= 0 {
t.Error()
}
if len(info.Version) <= 0 {
if len(info.Info.Version) <= 0 {
t.Error()
}
}

View File

@@ -1,12 +1,10 @@
package test
import (
"encoding/json"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/simon987/task_tracker/api"
"github.com/simon987/task_tracker/storage"
"io/ioutil"
"testing"
"time"
)
@@ -141,12 +139,12 @@ func TestGetLogs(t *testing.T) {
t.Error()
}
if len(*r.Logs) <= 0 {
if len(*r.Content.Logs) <= 0 {
t.Error()
}
debugFound := false
for _, log := range *r.Logs {
for _, log := range *r.Content.Logs {
if log.Message == "This one shouldn't be returned" {
t.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{
Since: since,
Level: level,
}, nil, nil)
resp := &api.GetLogResponse{}
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}

View File

@@ -22,7 +22,7 @@ func TestCreateGetProject(t *testing.T) {
Hidden: false,
})
id := resp.Id
id := resp.Content.Id
if id == 0 {
t.Fail()
@@ -31,7 +31,7 @@ func TestCreateGetProject(t *testing.T) {
t.Fail()
}
getResp, _ := getProjectAsAdmin(id)
getResp := getProjectAsAdmin(id).Content
if getResp.Project.Id != id {
t.Error()
@@ -111,7 +111,7 @@ func TestCreateDuplicateProjectRepo(t *testing.T) {
func TestGetProjectNotFound(t *testing.T) {
getResp, r := getProjectAsAdmin(12345)
getResp := getProjectAsAdmin(12345)
if getResp.Ok != false {
t.Fail()
@@ -120,10 +120,6 @@ func TestGetProjectNotFound(t *testing.T) {
if len(getResp.Message) <= 0 {
t.Fail()
}
if r.StatusCode != 404 {
t.Fail()
}
}
func TestUpdateProjectValid(t *testing.T) {
@@ -136,7 +132,7 @@ func TestUpdateProjectValid(t *testing.T) {
CloneUrl: "CloneUrlA",
GitRepo: "GitRepoA",
Priority: 1,
}).Id
}).Content.Id
updateResp := updateProject(api.UpdateProjectRequest{
Priority: 2,
@@ -152,7 +148,7 @@ func TestUpdateProjectValid(t *testing.T) {
t.Error()
}
proj, _ := getProjectAsAdmin(pid)
proj := getProjectAsAdmin(pid).Content
if proj.Project.Public != false {
t.Error()
@@ -184,7 +180,7 @@ func TestUpdateProjectInvalid(t *testing.T) {
CloneUrl: "333333333333333",
GitRepo: "llllllllllllllllllls",
Priority: 1,
}).Id
}).Content.Id
updateResp := updateProject(api.UpdateProjectRequest{
Priority: -1,
@@ -214,7 +210,7 @@ func TestUpdateProjectConstraintFail(t *testing.T) {
CloneUrl: "testUpdateProjectConstraintFail",
GitRepo: "testUpdateProjectConstraintFail",
Priority: 1,
}).Id
}).Content.Id
createProjectAsAdmin(api.CreateProjectRequest{
Public: true,
@@ -314,8 +310,8 @@ func TestHiddenProjectsNotShownInList(t *testing.T) {
list := getProjectList(nil)
for _, p := range *list.Projects {
if p.Id == r.Id {
for _, p := range *list.Content.Projects {
if p.Id == r.Content.Id {
t.Error()
}
}
@@ -356,10 +352,12 @@ func TestHiddenProjectNotAccessible(t *testing.T) {
t.Error()
}
pAdmin, _ := getProject(r.Id, testAdminCtx)
pUser, _ := getProject(r.Id, testUserCtx)
pOtherUser, _ := getProject(r.Id, otherUser)
pGuest, _ := getProject(r.Id, nil)
pid := r.Content.Id
pAdmin := getProject(pid, testAdminCtx)
pUser := getProject(pid, testUserCtx)
pOtherUser := getProject(pid, otherUser)
pGuest := getProject(pid, nil)
if pAdmin.Ok != true {
t.Error()
@@ -388,7 +386,7 @@ func TestUpdateProjectPermissions(t *testing.T) {
GitRepo: "newupdateprojectpermissions",
CloneUrl: "newupdateprojectpermissions",
Name: "newupdateprojectpermissions",
}, p.Id, nil)
}, p.Content.Id, nil)
if r.Ok != false {
t.Error()
@@ -411,8 +409,8 @@ func TestUserWithReadAccessShouldSeeHiddenProjectInList(t *testing.T) {
list := getProjectList(testUserCtx)
found := false
for _, p := range *list.Projects {
if p.Id == pHidden.Id {
for _, p := range *list.Content.Projects {
if p.Id == pHidden.Content.Id {
found = true
}
}
@@ -435,8 +433,8 @@ func TestAdminShouldSeeHiddenProjectInList(t *testing.T) {
list := getProjectList(testAdminCtx)
found := false
for _, p := range *list.Projects {
if p.Id == pHidden.Id {
for _, p := range *list.Content.Projects {
if p.Id == pHidden.Content.Id {
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)
}
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)
var resp api.CreateProjectResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
UnmarshalResponse(r, &ar)
return
}
func getProjectAsAdmin(id int64) (*api.GetProjectResponse, *http.Response) {
func getProjectAsAdmin(id int64) ProjectAR {
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)
var getResp api.GetProjectResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &getResp)
handleErr(err)
return &getResp, r
UnmarshalResponse(r, &ar)
return
}
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)
var resp api.UpdateProjectResponse
var resp api.JsonResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
@@ -489,13 +476,8 @@ func updateProject(request api.UpdateProjectRequest, pid int64, s *http.Client)
return &resp
}
func getProjectList(s *http.Client) *api.GetAllProjectsResponse {
func getProjectList(s *http.Client) (ar ProjectListAR) {
r := Get("/project/list", nil, s)
var resp api.GetAllProjectsResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
UnmarshalResponse(r, &ar)
return
}

View File

@@ -19,8 +19,8 @@ func BenchmarkCreateTaskRemote(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
createTask(api.CreateTaskRequest{
Project: resp.Id,
createTask(api.SubmitTaskRequest{
Project: resp.Content.Id,
Priority: 1,
Recipe: "{}",
MaxRetries: 1,

View File

@@ -1,11 +1,9 @@
package test
import (
"encoding/json"
"fmt"
"github.com/simon987/task_tracker/api"
"github.com/simon987/task_tracker/storage"
"io/ioutil"
"testing"
)
@@ -16,17 +14,17 @@ func TestCreateTaskValid(t *testing.T) {
Version: "Test Version",
CloneUrl: "http://github.com/test/test",
GitRepo: "Some git repo",
}).Id
}).Content.Id
worker := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Submit: true,
Assign: false,
}, worker)
acceptAccessRequest(pid, worker.Id, testAdminCtx)
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: pid,
Recipe: "{}",
MaxRetries: 3,
@@ -41,7 +39,7 @@ func TestCreateTaskInvalidProject(t *testing.T) {
worker := genWid()
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: 123456,
Recipe: "{}",
MaxRetries: 3,
@@ -103,7 +101,7 @@ func TestCreateTaskInvalidRetries(t *testing.T) {
worker := genWid()
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: 1,
MaxRetries: -1,
}, worker)
@@ -121,7 +119,7 @@ func TestCreateTaskInvalidRecipe(t *testing.T) {
worker := genWid()
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: 1,
Recipe: "",
MaxRetries: 3,
@@ -138,37 +136,33 @@ func TestCreateTaskInvalidRecipe(t *testing.T) {
func TestCreateGetTask(t *testing.T) {
//Make sure there is always a project for id:1
resp := createProjectAsAdmin(api.CreateProjectRequest{
pid := createProjectAsAdmin(api.CreateProjectRequest{
Name: "My project",
Version: "1.0",
CloneUrl: "http://github.com/test/test",
GitRepo: "myrepo",
Priority: 999,
Public: true,
})
}).Content.Id
worker := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Submit: true,
Assign: true,
Project: resp.Id,
Project: pid,
}, worker)
acceptAccessRequest(resp.Id, worker.Id, testAdminCtx)
acceptAccessRequest(pid, worker.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
Project: resp.Id,
createTask(api.SubmitTaskRequest{
Project: pid,
Recipe: "{\"url\":\"test\"}",
MaxRetries: 3,
Priority: 9999,
VerificationCount: 12,
}, worker)
taskResp := getTaskFromProject(resp.Id, worker)
taskResp := getTaskFromProject(pid, worker).Content
if taskResp.Ok != true {
t.Error()
}
if taskResp.Task.VerificationCount != 12 {
t.Error()
}
@@ -187,7 +181,7 @@ func TestCreateGetTask(t *testing.T) {
if taskResp.Task.MaxRetries != 3 {
t.Error()
}
if taskResp.Task.Project.Id != resp.Id {
if taskResp.Task.Project.Id != pid {
t.Error()
}
if taskResp.Task.Project.Priority != 999 {
@@ -213,7 +207,7 @@ func createTasks(prefix string) (int64, int64) {
GitRepo: prefix + "low1",
Priority: 1,
Public: true,
})
}).Content
highP := createProjectAsAdmin(api.CreateProjectRequest{
Name: prefix + "high",
Version: "1.0",
@@ -221,37 +215,37 @@ func createTasks(prefix string) (int64, int64) {
GitRepo: prefix + "high1",
Priority: 999,
Public: true,
})
}).Content
worker := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Submit: true,
Assign: false,
Project: highP.Id,
}, worker)
acceptAccessRequest(highP.Id, worker.Id, testAdminCtx)
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Submit: true,
Assign: false,
Project: lowP.Id,
}, worker)
acceptAccessRequest(lowP.Id, worker.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Project: lowP.Id,
Recipe: "low1",
Priority: 0,
}, worker)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Project: lowP.Id,
Recipe: "low2",
Priority: 1,
}, worker)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Project: highP.Id,
Recipe: "high1",
Priority: 100,
}, worker)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Project: highP.Id,
Recipe: "high2",
Priority: 101,
@@ -265,10 +259,10 @@ func TestTaskProjectPriority(t *testing.T) {
wid := genWid()
l, h := createTasks("withProject")
t1 := getTaskFromProject(l, wid)
t2 := getTaskFromProject(l, wid)
t3 := getTaskFromProject(h, wid)
t4 := getTaskFromProject(h, wid)
t1 := getTaskFromProject(l, wid).Content
t2 := getTaskFromProject(l, wid).Content
t3 := getTaskFromProject(h, wid).Content
t4 := getTaskFromProject(h, wid).Content
if t1.Task.Recipe != "low2" {
t.Error()
@@ -295,10 +289,10 @@ func TestTaskPriority(t *testing.T) {
createTasks("")
t1 := getTask(wid)
t2 := getTask(wid)
t3 := getTask(wid)
t4 := getTask(wid)
t1 := getTask(wid).Content
t2 := getTask(wid).Content
t3 := getTask(wid).Content
t4 := getTask(wid).Content
if t1.Task.Recipe != "high2" {
t.Error()
@@ -326,16 +320,16 @@ func TestTaskNoAccess(t *testing.T) {
CloneUrl: "fjkslejf cesl",
GitRepo: "fffffffff",
Public: false,
}).Id
}).Content.Id
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, worker)
acceptAccessRequest(worker.Id, pid, testAdminCtx)
createResp := createTask(api.CreateTaskRequest{
createResp := createTask(api.SubmitTaskRequest{
Project: pid,
Priority: 1,
MaxAssignTime: 10,
@@ -357,7 +351,7 @@ func TestTaskNoAccess(t *testing.T) {
if len(tResp.Message) <= 0 {
t.Error()
}
if tResp.Task != nil {
if tResp.Content.Task != nil {
t.Error()
}
}
@@ -374,16 +368,16 @@ func TestTaskHasAccess(t *testing.T) {
CloneUrl: "josaeiuf cesl",
GitRepo: "wewwwwwwwwwwwwwwwwwwwwww",
Public: false,
}).Id
}).Content.Id
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Submit: true,
Assign: true,
Project: pid,
}, worker)
acceptAccessRequest(worker.Id, pid, testAdminCtx)
createResp := createTask(api.CreateTaskRequest{
createResp := createTask(api.SubmitTaskRequest{
Project: pid,
Priority: 1,
MaxAssignTime: 10,
@@ -400,7 +394,7 @@ func TestTaskHasAccess(t *testing.T) {
if tResp.Ok != true {
t.Error()
}
if tResp.Task == nil {
if tResp.Content.Task == nil {
t.Error()
}
}
@@ -426,23 +420,23 @@ func TestReleaseTaskSuccess(t *testing.T) {
Name: "testreleasetask",
Motd: "",
Public: true,
}).Id
}).Content.Id
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, worker)
acceptAccessRequest(pid, worker.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Priority: 0,
Project: pid,
Recipe: "{}",
MaxRetries: 3,
}, worker)
task := getTaskFromProject(pid, worker).Task
task := getTaskFromProject(pid, worker).Content.Task
releaseResp := releaseTask(api.ReleaseTaskRequest{
TaskId: task.Id,
@@ -471,17 +465,17 @@ func TestCreateIntCollision(t *testing.T) {
Public: true,
Name: "testcreateintcollision",
Version: "testcreateintcollision",
}).Id
}).Content.Id
w := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w)
acceptAccessRequest(pid, w.Id, testAdminCtx)
if createTask(api.CreateTaskRequest{
if createTask(api.SubmitTaskRequest{
Project: pid,
Hash64: 123,
Priority: 1,
@@ -490,7 +484,7 @@ func TestCreateIntCollision(t *testing.T) {
t.Error()
}
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: pid,
Hash64: 123,
Priority: 1,
@@ -517,17 +511,17 @@ func TestCreateStringCollision(t *testing.T) {
Public: true,
Name: "testcreatestringcollision",
Version: "testcreatestringcollision",
}).Id
}).Content.Id
w := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w)
acceptAccessRequest(pid, w.Id, testAdminCtx)
if createTask(api.CreateTaskRequest{
if createTask(api.SubmitTaskRequest{
Project: pid,
UniqueString: "Hello, world",
Priority: 1,
@@ -536,14 +530,14 @@ func TestCreateStringCollision(t *testing.T) {
t.Error()
}
resp := createTask(api.CreateTaskRequest{
resp := createTask(api.SubmitTaskRequest{
Project: pid,
UniqueString: "Hello, world",
Priority: 1,
Recipe: "{}",
}, w)
if !createTask(api.CreateTaskRequest{
if !createTask(api.SubmitTaskRequest{
Project: pid,
UniqueString: "This one should work",
Priority: 1,
@@ -572,28 +566,28 @@ func TestCannotVerifySameTaskTwice(t *testing.T) {
Public: true,
Name: "verifysametasktwice",
Version: "verifysametasktwice",
}).Id
}).Content.Id
w := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w)
acceptAccessRequest(pid, w.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
VerificationCount: 2,
Project: pid,
Recipe: "verifysametasktwice",
}, w)
task := getTaskFromProject(pid, w).Task
task := getTaskFromProject(pid, w).Content.Task
rlr := releaseTask(api.ReleaseTaskRequest{
Result: storage.TR_OK,
TaskId: task.Id,
Verification: 123,
}, w)
}, w).Content
if rlr.Updated != false {
t.Error()
@@ -616,22 +610,22 @@ func TestVerification2(t *testing.T) {
Public: true,
Name: "verify2",
Version: "verify2",
}).Id
}).Content.Id
w := genWid()
w2 := genWid()
w3 := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w)
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w2)
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
@@ -640,40 +634,40 @@ func TestVerification2(t *testing.T) {
acceptAccessRequest(pid, w2.Id, testAdminCtx)
acceptAccessRequest(pid, w3.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
VerificationCount: 2,
Project: pid,
Recipe: "verify2",
}, w)
task := getTaskFromProject(pid, w).Task
task := getTaskFromProject(pid, w).Content.Task
rlr := releaseTask(api.ReleaseTaskRequest{
Result: storage.TR_OK,
TaskId: task.Id,
Verification: 123,
}, w)
}, w).Content
if rlr.Updated != false {
t.Error()
}
task2 := getTaskFromProject(pid, w2).Task
task2 := getTaskFromProject(pid, w2).Content.Task
rlr2 := releaseTask(api.ReleaseTaskRequest{
Result: storage.TR_OK,
Verification: 1,
TaskId: task2.Id,
}, w2)
}, w2).Content
if rlr2.Updated != false {
t.Error()
}
task3 := getTaskFromProject(pid, w3).Task
task3 := getTaskFromProject(pid, w3).Content.Task
rlr3 := releaseTask(api.ReleaseTaskRequest{
Result: storage.TR_OK,
Verification: 123,
TaskId: task3.Id,
}, w3)
}, w3).Content
if rlr3.Updated != true {
t.Error()
@@ -690,24 +684,24 @@ func TestReleaseTaskFail(t *testing.T) {
Public: true,
Name: "releasefail",
Version: "releasefail",
}).Id
}).Content.Id
w := genWid()
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: pid,
Assign: true,
Submit: true,
}, w)
acceptAccessRequest(pid, w.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
MaxRetries: 0,
Project: pid,
VerificationCount: 1,
Recipe: "releasefail",
}, w)
task := getTaskFromProject(pid, w).Task
task := getTaskFromProject(pid, w).Content.Task
resp := releaseTask(api.ReleaseTaskRequest{
Result: storage.TR_FAIL,
@@ -715,13 +709,12 @@ func TestReleaseTaskFail(t *testing.T) {
Verification: 1,
}, w)
if resp.Updated != true {
if resp.Content.Updated != true {
t.Error()
}
if resp.Ok != true {
t.Error()
}
}
func TestTaskChain(t *testing.T) {
@@ -733,7 +726,7 @@ func TestTaskChain(t *testing.T) {
Public: true,
GitRepo: "testtaskchain1",
CloneUrl: "testtaskchain1",
}).Id
}).Content.Id
p2 := createProjectAsAdmin(api.CreateProjectRequest{
Name: "testtaskchain2",
@@ -741,13 +734,13 @@ func TestTaskChain(t *testing.T) {
GitRepo: "testtaskchain2",
CloneUrl: "testtaskchain2",
Chain: p1,
}).Id
requestAccess(api.WorkerAccessRequest{
}).Content.Id
requestAccess(api.CreateWorkerAccessRequest{
Project: p1,
Assign: true,
Submit: true,
}, w)
requestAccess(api.WorkerAccessRequest{
requestAccess(api.CreateWorkerAccessRequest{
Project: p2,
Assign: true,
Submit: true,
@@ -755,20 +748,20 @@ func TestTaskChain(t *testing.T) {
acceptAccessRequest(p1, w.Id, testAdminCtx)
acceptAccessRequest(p2, w.Id, testAdminCtx)
createTask(api.CreateTaskRequest{
createTask(api.SubmitTaskRequest{
Project: p2,
Recipe: "###",
VerificationCount: 0,
}, w)
t1 := getTaskFromProject(p2, w).Task
t1 := getTaskFromProject(p2, w).Content.Task
releaseTask(api.ReleaseTaskRequest{
TaskId: t1.Id,
Result: storage.TR_OK,
}, w)
chained := getTaskFromProject(p1, w).Task
chained := getTaskFromProject(p1, w).Content.Task
if chained.VerificationCount != t1.VerificationCount {
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)
var resp api.CreateTaskResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
UnmarshalResponse(r, &ar)
return
}
func getTask(worker *storage.Worker) *api.GetTaskResponse {
r := Get(fmt.Sprintf("/task/get"), worker, nil)
var resp api.GetTaskResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
func getTask(worker *storage.Worker) (ar TaskAR) {
r := Get("/task/get", worker, nil)
UnmarshalResponse(r, &ar)
return
}
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)
var resp api.GetTaskResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
UnmarshalResponse(r, &ar)
return
}
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)
var resp api.ReleaseTaskResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return &resp
UnmarshalResponse(r, &ar)
return
}

View File

@@ -1,50 +1,39 @@
package test
import (
"encoding/json"
"fmt"
"github.com/simon987/task_tracker/api"
"github.com/simon987/task_tracker/storage"
"io/ioutil"
"net/http"
"testing"
)
func TestCreateGetWorker(t *testing.T) {
resp, r := createWorker(api.CreateWorkerRequest{
resp := createWorker(api.CreateWorkerRequest{
Alias: "my_worker_alias",
})
if r.StatusCode != 200 {
t.Error()
}
w := resp.Content.Worker
if resp.Ok != true {
t.Error()
}
getResp, r := getWorker(resp.Worker.Id)
getResp := getWorker(w.Id)
if r.StatusCode != 200 {
t.Error()
}
if resp.Worker.Id != getResp.Worker.Id {
if w.Id != getResp.Content.Worker.Id {
t.Error()
}
if resp.Worker.Alias != "my_worker_alias" {
if w.Alias != "my_worker_alias" {
t.Error()
}
}
func TestGetWorkerNotFound(t *testing.T) {
resp, r := getWorker(99999999)
resp := getWorker(99999999)
if r.StatusCode != 404 {
t.Error()
}
if resp.Ok != false {
t.Error()
}
@@ -52,11 +41,8 @@ func TestGetWorkerNotFound(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 {
t.Error()
}
@@ -76,16 +62,16 @@ func TestUpdateAliasValid(t *testing.T) {
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()
}
}
func TestCreateWorkerAliasInvalid(t *testing.T) {
resp, _ := createWorker(api.CreateWorkerRequest{
resp := createWorker(api.CreateWorkerRequest{
Alias: "unassigned", //reserved alias
})
@@ -105,9 +91,9 @@ func TestInvalidAccessRequest(t *testing.T) {
Name: "testinvalidaccessreq",
CloneUrl: "testinvalidaccessreq",
GitRepo: "testinvalidaccessreq",
}).Id
}).Content.Id
r := requestAccess(api.WorkerAccessRequest{
r := requestAccess(api.CreateWorkerAccessRequest{
Submit: false,
Assign: false,
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)
var resp *api.CreateWorkerResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp, r
UnmarshalResponse(r, &ar)
return
}
func getWorker(id int64) (*api.GetWorkerResponse, *http.Response) {
func getWorker(id int64) (ar WorkerAR) {
r := Get(fmt.Sprintf("/worker/get/%d", id), nil, nil)
var resp *api.GetWorkerResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp, r
UnmarshalResponse(r, &ar)
return
}
func genWid() *storage.Worker {
resp, _ := createWorker(api.CreateWorkerRequest{})
return resp.Worker
resp := createWorker(api.CreateWorkerRequest{})
return resp.Content.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)
var resp *api.WorkerAccessRequestResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}
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,
nil, s)
var resp *api.WorkerAccessRequestResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}
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,
nil, s)
var resp *api.WorkerAccessRequestResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}
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)
var resp *api.UpdateWorkerResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
UnmarshalResponse(r, &ar)
return
}

View File

@@ -6,6 +6,8 @@ import (
"crypto/hmac"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/simon987/task_tracker/api"
"github.com/simon987/task_tracker/config"
"github.com/simon987/task_tracker/storage"
"io"
@@ -19,6 +21,11 @@ type SessionContext struct {
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 {
if s == nil {
@@ -88,3 +95,80 @@ func GenericJson(body io.ReadCloser) map[string]interface{} {
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"`
}