Added public project attribute & worker access api endpoints

This commit is contained in:
simon987
2019-01-24 20:39:17 -05:00
parent 1d656099f5
commit f250a2180c
16 changed files with 432 additions and 70 deletions

View File

@@ -19,6 +19,7 @@ func TestCreateGetProject(t *testing.T) {
Version: "Test Version",
Priority: 123,
Motd: "motd",
Public: true,
})
id := resp.Id
@@ -56,6 +57,9 @@ func TestCreateGetProject(t *testing.T) {
if getResp.Project.Motd != "motd" {
t.Error()
}
if getResp.Project.Public != true {
t.Error()
}
}
func TestCreateProjectInvalid(t *testing.T) {

View File

@@ -129,6 +129,7 @@ func TestCreateGetTask(t *testing.T) {
CloneUrl: "http://github.com/test/test",
GitRepo: "myrepo",
Priority: 999,
Public: true,
})
createTask(api.CreateTaskRequest{
@@ -170,6 +171,9 @@ func TestCreateGetTask(t *testing.T) {
if taskResp.Task.Project.CloneUrl != "http://github.com/test/test" {
t.Error()
}
if taskResp.Task.Project.Public != true {
t.Error()
}
}
func createTasks(prefix string) (int64, int64) {
@@ -180,6 +184,7 @@ func createTasks(prefix string) (int64, int64) {
CloneUrl: "http://github.com/test/test",
GitRepo: prefix + "low1",
Priority: 1,
Public: true,
})
highP := createProject(api.CreateProjectRequest{
Name: prefix + "high",
@@ -187,6 +192,7 @@ func createTasks(prefix string) (int64, int64) {
CloneUrl: "http://github.com/test/test",
GitRepo: prefix + "high1",
Priority: 999,
Public: true,
})
createTask(api.CreateTaskRequest{
Project: lowP.Id,
@@ -266,6 +272,86 @@ func TestTaskPriority(t *testing.T) {
}
}
func TestTaskNoAccess(t *testing.T) {
wid := genWid()
pid := createProject(api.CreateProjectRequest{
Name: "This is a private proj",
Motd: "private",
Version: "private",
Priority: 1,
CloneUrl: "fjkslejf cesl",
GitRepo: "fffffffff",
Public: false,
}).Id
createResp := createTask(api.CreateTaskRequest{
Project: pid,
Priority: 1,
MaxAssignTime: 10,
MaxRetries: 2,
Recipe: "---",
})
if createResp.Ok != true {
t.Error()
}
grantAccess(wid, pid)
removeAccess(wid, pid)
tResp := getTaskFromProject(pid, wid)
if tResp.Ok != false {
t.Error()
}
if len(tResp.Message) <= 0 {
t.Error()
}
if tResp.Task != nil {
t.Error()
}
}
func TestTaskHasAccess(t *testing.T) {
wid := genWid()
pid := createProject(api.CreateProjectRequest{
Name: "This is a private proj1",
Motd: "private1",
Version: "private1",
Priority: 1,
CloneUrl: "josaeiuf cesl",
GitRepo: "wewwwwwwwwwwwwwwwwwwwwww",
Public: false,
}).Id
createResp := createTask(api.CreateTaskRequest{
Project: pid,
Priority: 1,
MaxAssignTime: 10,
MaxRetries: 2,
Recipe: "---",
})
if createResp.Ok != true {
t.Error()
}
grantAccess(wid, pid)
tResp := getTaskFromProject(pid, wid)
if tResp.Ok != true {
t.Error()
}
if tResp.Task == nil {
t.Error()
}
}
func TestNoMoreTasks(t *testing.T) {
wid := genWid()

View File

@@ -66,6 +66,78 @@ func TestGetWorkerInvalid(t *testing.T) {
}
}
func TestGrantAccessFailedProjectConstraint(t *testing.T) {
wid := genWid()
resp := grantAccess(wid, 38274593)
if resp.Ok != false {
t.Error()
}
if len(resp.Message) <= 0 {
t.Error()
}
}
func TestRemoveAccessFailedProjectConstraint(t *testing.T) {
wid := genWid()
resp := removeAccess(wid, 38274593)
if resp.Ok != false {
t.Error()
}
if len(resp.Message) <= 0 {
t.Error()
}
}
func TestRemoveAccessFailedWorkerConstraint(t *testing.T) {
pid := createProject(api.CreateProjectRequest{
Priority: 1,
GitRepo: "dfffffffffff",
CloneUrl: "fffffffffff23r",
Version: "f83w9rw",
Motd: "ddddddddd",
Name: "removeaccessfailedworkerconstraint",
Public: true,
}).Id
resp := removeAccess(&uuid.Nil, pid)
if resp.Ok != false {
t.Error()
}
if len(resp.Message) <= 0 {
t.Error()
}
}
func TestGrantAccessFailedWorkerConstraint(t *testing.T) {
pid := createProject(api.CreateProjectRequest{
Priority: 1,
GitRepo: "dfffffffffff1",
CloneUrl: "fffffffffff23r1",
Version: "f83w9rw1",
Motd: "ddddddddd1",
Name: "grantaccessfailedworkerconstraint",
Public: true,
}).Id
resp := removeAccess(&uuid.Nil, pid)
if resp.Ok != false {
t.Error()
}
if len(resp.Message) <= 0 {
t.Error()
}
}
func createWorker(req api.CreateWorkerRequest) (*api.CreateWorkerResponse, *http.Response) {
r := Post("/worker/create", req)
@@ -94,3 +166,33 @@ func genWid() *uuid.UUID {
resp, _ := createWorker(api.CreateWorkerRequest{})
return &resp.WorkerId
}
func grantAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
r := Post("/access/grant", api.WorkerAccessRequest{
WorkerId: wid,
ProjectId: project,
})
var resp *api.WorkerAccessResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
}
func removeAccess(wid *uuid.UUID, project int64) *api.WorkerAccessResponse {
r := Post("/access/remove", api.WorkerAccessRequest{
WorkerId: wid,
ProjectId: project,
})
var resp *api.WorkerAccessResponse
data, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(data, &resp)
handleErr(err)
return resp
}

45
test/schema.sql Normal file → Executable file
View File

@@ -1,18 +1,20 @@
DROP TABLE IF EXISTS workeridentity, Worker, Project, Task, log_entry;
DROP TABLE IF EXISTS worker_identity, worker, project, task, log_entry,
worker_has_access_to_project;
DROP TYPE IF EXISTS status;
DROP TYPE IF EXISTS loglevel;
DROP TYPE IF EXISTS log_level;
CREATE TYPE status as ENUM (
'new',
'failed',
'closed'
'closed',
'timeout'
);
CREATE TYPE loglevel as ENUM (
CREATE TYPE log_level as ENUM (
'fatal', 'panic', 'error', 'warning', 'info', 'debug', 'trace'
);
CREATE TABLE workerIdentity
CREATE TABLE worker_identity
(
id SERIAL PRIMARY KEY,
remote_addr TEXT,
@@ -24,6 +26,7 @@ CREATE TABLE workerIdentity
CREATE TABLE worker
(
id TEXT PRIMARY KEY,
alias TEXT DEFAULT NULL,
created INTEGER,
identity INTEGER REFERENCES workerIdentity (id)
);
@@ -32,28 +35,38 @@ CREATE TABLE project
(
id SERIAL PRIMARY KEY,
priority INTEGER DEFAULT 0,
motd TEXT DEFAULT '',
name TEXT UNIQUE,
clone_url TEXT,
git_repo TEXT UNIQUE,
version TEXT
version TEXT,
motd TEXT,
public boolean
);
CREATE TABLE worker_has_access_to_project
(
worker TEXT REFERENCES worker (id),
project INTEGER REFERENCES project (id),
primary key (worker, project)
);
CREATE TABLE task
(
id SERIAL PRIMARY KEY,
priority INTEGER DEFAULT 0,
project INTEGER REFERENCES project (id),
assignee TEXT REFERENCES worker (id),
retries INTEGER DEFAULT 0,
max_retries INTEGER,
status Status DEFAULT 'new',
recipe TEXT
id SERIAL PRIMARY KEY,
priority INTEGER DEFAULT 0,
project INTEGER REFERENCES project (id),
assignee TEXT REFERENCES worker (id),
retries INTEGER DEFAULT 0,
max_retries INTEGER,
status Status DEFAULT 'new',
recipe TEXT,
max_assign_time INTEGER DEFAULT 0,
assign_time INTEGER DEFAULT 0
);
CREATE TABLE log_entry
(
level loglevel,
level log_level,
message TEXT,
message_data TEXT,
timestamp INT