Skip to content

Commit

Permalink
Merge 51decd1 into 86610bc
Browse files Browse the repository at this point in the history
  • Loading branch information
TruePack committed Aug 27, 2019
2 parents 86610bc + 51decd1 commit e38b9d0
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
8 changes: 8 additions & 0 deletions selvpcclient/resell/v2/users/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
Package users provides the ability to retrieve and manage users through the
Resell v2 API.
Example of getting a single user referenced by its id
user, _, err := users.Get(context, resellClient, userID)
if err != nil {
log.Fatal(err)
}
fmt.Println(user)
Example of getting all users
allUsers, _, err := users.List(ctx, resellClient)
Expand Down
23 changes: 23 additions & 0 deletions selvpcclient/resell/v2/users/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ import (

const resourceURL = "users"

// Get returns a single user by its id.
func Get(ctx context.Context, client *selvpcclient.ServiceClient, id string) (*User, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL, id}, "/")
responseResult, err := client.DoRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
if responseResult.Err != nil {
return nil, responseResult, responseResult.Err
}

// Extract an user from the response body.
var result struct {
User *User `json:"user"`
}
err = responseResult.ExtractResult(&result)
if err != nil {
return nil, responseResult, err
}

return result.User, responseResult, nil
}

// List gets a list of users in the current domain.
func List(ctx context.Context, client *selvpcclient.ServiceClient) ([]*User, *selvpcclient.ResponseResult, error) {
url := strings.Join([]string{client.Endpoint, resourceURL}, "/")
Expand Down
27 changes: 27 additions & 0 deletions selvpcclient/resell/v2/users/testing/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ package testing

import "github.com/selectel/go-selvpcclient/selvpcclient/resell/v2/users"

// TestGetUsersResponseRaw represents a raw response from the Get request.
const TestGetUsersResponseRaw = `
{
"user": {
"enabled": false,
"id": "c4b7e0581b964c52a1597fe0931eccdf",
"name": "User1"
}
}
`

// TestGetUsersResponse represents the unmarshalled TestGetUsersResponseRaw response.
var TestGetUsersResponse = &users.User{
ID: "c4b7e0581b964c52a1597fe0931eccdf",
Name: "User1",
Enabled: false,
}

// TestGetUserInvalidResponseRaw represents a raw invalid response with single user.
const TestGetUserInvalidResponseRaw = `
{
"user": {
"id": 999
}
}
`

// TestListUsersResponseRaw represents a raw response from the List request.
const TestListUsersResponseRaw = `
{
Expand Down
105 changes: 105 additions & 0 deletions selvpcclient/resell/v2/users/testing/requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,111 @@ import (
"github.com/selectel/go-selvpcclient/selvpcclient/testutils"
)

func TestGetUser(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f",
RawResponse: TestGetUsersResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
actualResponse, _, err := users.Get(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f")
if err != nil {
t.Fatal(err)
}
expectedResponse := TestGetUsersResponse

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if !reflect.DeepEqual(actualResponse, expectedResponse) {
t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse)
}
}

func TestGetUserHTTPError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f",
RawResponse: TestGetUsersResponseRaw,
Method: http.MethodGet,
Status: http.StatusBadGateway,
CallFlag: &endpointCalled,
})

ctx := context.Background()
user, httpResponse, err := users.Get(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if err == nil {
t.Fatal("expected error from the Delete method")
}
if httpResponse.StatusCode != http.StatusBadGateway {
t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadRequest, httpResponse.StatusCode)
}
if user != nil {
t.Fatal("expected no users from the List method")
}
}

func TestGetUserTimeoutError(t *testing.T) {
testEnv := testutils.SetupTestEnv()
testEnv.Server.Close()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()

ctx := context.Background()
_, _, err := users.Get(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f")

if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestGetUsersUnmarshalError(t *testing.T) {
endpointCalled := false

testEnv := testutils.SetupTestEnv()
defer testEnv.TearDownTestEnv()
testEnv.NewTestResellV2Client()
testutils.HandleReqWithoutBody(t, &testutils.HandleReqOpts{
Mux: testEnv.Mux,
URL: "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f",
RawResponse: TestGetUserInvalidResponseRaw,
Method: http.MethodGet,
Status: http.StatusOK,
CallFlag: &endpointCalled,
})

ctx := context.Background()
user, _, err := users.Get(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f")

if !endpointCalled {
t.Fatal("endpoint wasn't called")
}
if user != nil {
t.Fatal("expected no user from the Get method")
}
if err == nil {
t.Fatal("expected error from the Get method")
}
}

func TestListUsers(t *testing.T) {
endpointCalled := false

Expand Down

0 comments on commit e38b9d0

Please sign in to comment.