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
Next Next commit
feat: validate+register tests
  • Loading branch information
plyr4 committed Mar 21, 2023
commit 89a4d848bf7c6d165581120a282c7f1b5cd5fb88
13 changes: 10 additions & 3 deletions vela/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
package vela

import (
"errors"
"fmt"
"strings"

"github.com/go-vela/types/library"
)
Expand Down Expand Up @@ -204,10 +206,15 @@ func (svc *AdminUserService) Update(u *library.User) (*library.User, *Response,
return v, resp, err
}

// RegistrationToken generates a worker registration token with the provided details.
func (svc *AdminUserService) RegistrationToken(w *library.Worker) (*library.Token, *Response, error) {
// RegisterToken generates a worker registration token with the provided details.
func (svc *AdminWorkerService) RegisterToken(hostname string) (*library.Token, *Response, error) {
// validate input
if strings.EqualFold(hostname, "") {
return nil, nil, errors.New("bad request, no hostname provided")
}

// set the API endpoint path we send the request to
url := fmt.Sprintf("/api/v1/workers/%s/register-token", w.GetHostname())
url := fmt.Sprintf("/api/v1/workers/%s/register-token", hostname)

// library Token type we want to return
t := new(library.Token)
Expand Down
52 changes: 52 additions & 0 deletions vela/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,55 @@ func TestAdmin_Build_Queue_200(t *testing.T) {
t.Errorf("GetQueue() mismatch (-want +got):\n%s", diff)
}
}

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

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

// needs mocks from server feature
// data := []byte(server.RegisterTokenResp)
data := []byte{}

var want *library.Token

err := json.Unmarshal(data, &want)
if err != nil {
t.Error(err)
}

hostname := "foo"

// run test
got, resp, err := c.Admin.Worker.RegisterToken(hostname)
if err != nil {
t.Errorf("RegisterToken returned err: %v", err)
}

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

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("RegisterToken() mismatch (-want +got):\n%s", diff)
}
}

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

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

// bad hostname
hostname := ""

// run test
_, _, err := c.Admin.Worker.RegisterToken(hostname)
if err == nil {
t.Error("RegisterToken should have returned err")
}
}
1 change: 1 addition & 0 deletions vela/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,6 @@ func (svc *AuthenticationService) ValidateToken() (*Response, error) {

// attempt to validate a server token
resp, err := svc.client.Call("GET", u, nil, nil)

return resp, err
}
46 changes: 46 additions & 0 deletions vela/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,49 @@ func TestVela_Authentication_ExchangeTokens_BadInput(t *testing.T) {
t.Errorf("ExchangeTokens should not set Refresh Token")
}
}

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

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

c.Authentication.SetTokenAuth("foobar")

// run test
resp, err := c.Authentication.ValidateToken()

if err != nil {
t.Errorf("ValidateToken returned error %v", err)
}

if resp == nil {
t.Errorf("ValidateToken response should not be nil")
}

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

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

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

c.Authentication.SetTokenAuth("")

// run test
resp, err := c.Authentication.ValidateToken()

if err == nil {
t.Error("ValidateToken should have returned error")
}

if resp != nil {
t.Errorf("ValidateToken response should be nil")
}
}