-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user resource methods (#327)
* init commit * add testing
- Loading branch information
Showing
6 changed files
with
327 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package vela | ||
|
||
import ( | ||
"fmt" | ||
|
||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// UserService handles retrieving users from | ||
// the server methods of the Vela API. | ||
type UserService service | ||
|
||
// Get returns the provided user by name. | ||
func (svc *UserService) Get(name string) (*api.User, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := fmt.Sprintf("/api/v1/users/%s", name) | ||
|
||
// api user type we want to return | ||
v := new(api.User) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("GET", u, nil, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// GetCurrent returns the current user. | ||
func (svc *UserService) GetCurrent() (*api.User, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := "/api/v1/user" | ||
|
||
// api user type we want to return | ||
v := new(api.User) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("GET", u, nil, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// Update modifies a user with the provided details. | ||
func (svc *UserService) Update(name string, user *api.User) (*api.User, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := fmt.Sprintf("/api/v1/users/%s", name) | ||
|
||
// api User type we want to return | ||
v := new(api.User) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("PUT", u, user, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// Update modifies the current user with the provided details. | ||
func (svc *UserService) UpdateCurrent(user *api.User) (*api.User, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := "/api/v1/user" | ||
|
||
// api User type we want to return | ||
v := new(api.User) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("PUT", u, user, v) | ||
|
||
return v, resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package vela | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
api "github.com/go-vela/server/api/types" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestUser_Get_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.UserResp) | ||
|
||
var want api.User | ||
_ = json.Unmarshal(data, &want) | ||
|
||
// run test | ||
got, resp, err := c.User.Get("octocat") | ||
|
||
if err != nil { | ||
t.Errorf("User Get returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("User Get returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User Get is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_Get_404(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
want := api.User{} | ||
|
||
// run test | ||
got, resp, err := c.User.Get("not-found") | ||
|
||
if err == nil { | ||
t.Errorf("User Get should have returned err") | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
t.Errorf("User Get returned %v, want %v", resp.StatusCode, http.StatusNotFound) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User Get is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_Update_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.UserResp) | ||
|
||
var want api.User | ||
_ = json.Unmarshal(data, &want) | ||
|
||
req := api.User{ | ||
Admin: Bool(true), | ||
} | ||
|
||
// run test | ||
got, resp, err := c.User.Update("octocat", &req) | ||
|
||
if err != nil { | ||
t.Errorf("User Update returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("User Update returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User Update is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_Update_404(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
want := api.User{} | ||
|
||
req := api.User{ | ||
Admin: Bool(true), | ||
} | ||
|
||
// run test | ||
got, resp, err := c.User.Update("not-found", &req) | ||
|
||
if err == nil { | ||
t.Errorf("User Update should have returned err") | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
t.Errorf("User Update returned %v, want %v", resp.StatusCode, http.StatusNotFound) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User Update is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestCurrentUser_Get_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.UserResp) | ||
|
||
var want api.User | ||
_ = json.Unmarshal(data, &want) | ||
|
||
// run test | ||
got, resp, err := c.User.GetCurrent() | ||
|
||
if err != nil { | ||
t.Errorf("User GetCurrent returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("User GetCurrent returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User GetCurrent is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_GetCurrent_401(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
c.Authentication.SetTokenAuth("invalid") | ||
|
||
want := api.User{} | ||
|
||
// run test | ||
got, resp, err := c.User.GetCurrent() | ||
|
||
if err == nil { | ||
t.Errorf("User GetCurrent should have returned err") | ||
} | ||
|
||
if resp.StatusCode != http.StatusUnauthorized { | ||
t.Errorf("User GetCurrent returned %v, want %v", resp.StatusCode, http.StatusUnauthorized) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User GetCurrent is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_UpdateCurrent_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.UserResp) | ||
|
||
var want api.User | ||
_ = json.Unmarshal(data, &want) | ||
|
||
favorites := []string{"github/octocat"} | ||
|
||
req := api.User{ | ||
Favorites: &favorites, | ||
} | ||
|
||
// run test | ||
got, resp, err := c.User.UpdateCurrent(&req) | ||
|
||
if err != nil { | ||
t.Errorf("User UpdateCurrent returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("User UpdateCurrent returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User UpdateCurrent is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestUser_UpdateCurrent_401(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
c.Authentication.SetTokenAuth("invalid") | ||
|
||
want := api.User{} | ||
|
||
favorites := []string{"github/octocat"} | ||
|
||
req := api.User{ | ||
Favorites: &favorites, | ||
} | ||
|
||
// run test | ||
got, resp, err := c.User.UpdateCurrent(&req) | ||
|
||
if err == nil { | ||
t.Errorf("User UpdateCurrent should have returned err") | ||
} | ||
|
||
if resp.StatusCode != http.StatusUnauthorized { | ||
t.Errorf("User UpdateCurrent returned %v, want %v", resp.StatusCode, http.StatusUnauthorized) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("User UpdateCurrent is %v, want %v", got, want) | ||
} | ||
} |