Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: worker refresh auth endpoint
  • Loading branch information
plyr4 committed Mar 23, 2023
commit 6aa276aa994d658b6b8febd568881fd5529d673a
14 changes: 14 additions & 0 deletions vela/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ func (svc *WorkerService) Add(w *library.Worker) (*library.Token, *Response, err
return v, resp, err
}

// RefreshAuth exchanges a worker token for a new one.
func (svc *WorkerService) RefreshAuth(worker string) (*library.Token, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/workers/%s/refresh", worker)

// library Token type we want to return
v := new(library.Token)

// send request using client
resp, err := svc.client.Call("POST", u, nil, v)

return v, resp, err
}

// Update modifies a worker with the provided details.
func (svc *WorkerService) Update(worker string, w *library.Worker) (*library.Worker, *Response, error) {
// set the API endpoint path we send the request to
Expand Down
77 changes: 73 additions & 4 deletions vela/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestWorker_Get_404(t *testing.T) {
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker get returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker get returned %v, want %v", resp.StatusCode, http.StatusNotFound)
}

if !reflect.DeepEqual(got, &want) {
Expand Down Expand Up @@ -132,14 +132,65 @@ func TestWorker_Add_201(t *testing.T) {
}

if resp.StatusCode != http.StatusCreated {
t.Errorf("Worker add returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker add returned %v, want %v", resp.StatusCode, http.StatusCreated)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Worker add is %v, want %v", got, want)
}
}

func TestWorker_RefreshAuth_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.AddWorkerResp)

var want library.Token
_ = json.Unmarshal(data, &want)

worker := "worker_1"

// run test
got, resp, err := c.Worker.RefreshAuth(worker)

if err != nil {
t.Errorf("Worker RefreshAuth returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Worker RefreshAuth returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
t.Errorf("Worker RefreshAuth is %v, want %v", got, want)
}
}

func TestWorker_RefreshAuth_404(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

worker := "0"

// run test
_, resp, err := c.Worker.RefreshAuth(worker)

if err == nil {
t.Error("Worker RefreshAuth should have returned err")
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker RefreshAuth returned %v, want %v", resp.StatusCode, http.StatusNotFound)
}
}

func TestWorker_Update_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)
Expand Down Expand Up @@ -193,7 +244,7 @@ func TestWorker_Update_404(t *testing.T) {
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusNotFound)
}

if !reflect.DeepEqual(got, &want) {
Expand Down Expand Up @@ -235,7 +286,7 @@ func TestWorker_Remove_404(t *testing.T) {
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker remove returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker remove returned %v, want %v", resp.StatusCode, http.StatusNotFound)
}
}

Expand Down Expand Up @@ -300,6 +351,24 @@ func ExampleWorkerService_Add() {
fmt.Printf("Received response code %d, for worker %+v", resp.StatusCode, worker)
}

func ExampleWorkerService_RefreshAuth() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this used? Same goes for the rest of the ExampleWorkerService_* funcs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly im not sure. i added it for consistency

Copy link
Contributor Author

@plyr4 plyr4 Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seemed like leftover boiler plate tests from the initial worker router commits. we can probably remove them in favor the more detailed tests above them

Copy link
Member

@wass3rw3rk wass3rw3rk Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is used for documentation, see https://pkg.go.dev/github.com/go-vela/[email protected]/vela#example-WorkerService.Update as an example (no pun intended) which is extracted from ExampleWorkerService_Update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh!!! 😮‍💨 thats awesome. i didnt know go doc used test naming to populate that. thanks @wass3rw3rk

// Create a new vela client for interacting with server
c, _ := NewClient("http://localhost:8080", "", nil)

// Set new token in existing client
c.Authentication.SetPersonalAccessTokenAuth("token")

worker := "worker_1"

// Refresh a worker token with the server
_, resp, err := c.Worker.RefreshAuth(worker)
if err != nil {
fmt.Println(err)
}

fmt.Printf("Received response code %d, for worker %+v", resp.StatusCode, worker)
}

func ExampleWorkerService_Update() {
// Create a new vela client for interacting with server
c, _ := NewClient("http://localhost:8080", "", nil)
Expand Down