-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create command to invite members (#68)
* add members command * add create member command and add client method * create members client * remove unused client * remove spacing * fix existing tests, add mock members client * add tests * remove run func * add member create cmd tests
- Loading branch information
1 parent
ab854d9
commit e1a2ca5
Showing
11 changed files
with
369 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package members | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"ldcli/internal/errors" | ||
"ldcli/internal/members" | ||
) | ||
|
||
func NewCreateCmd(client members.Client) (*cobra.Command, error) { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a new member", | ||
Long: "Create a new member", | ||
PreRunE: validate, | ||
RunE: runCreate(client), | ||
} | ||
|
||
cmd.Flags().StringP("data", "d", "", "Input data in JSON") | ||
err := cmd.MarkFlagRequired("data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = viper.BindPFlag("data", cmd.Flags().Lookup("data")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cmd, nil | ||
} | ||
|
||
type inputData struct { | ||
Email string `json:"email"` | ||
Role string `json:"role"` | ||
} | ||
|
||
func runCreate(client members.Client) func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
var data inputData | ||
// TODO: why does viper.GetString("data") not work? | ||
err := json.Unmarshal([]byte(cmd.Flags().Lookup("data").Value.String()), &data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
response, err := client.Create( | ||
context.Background(), | ||
viper.GetString("accessToken"), | ||
viper.GetString("baseUri"), | ||
data.Email, | ||
data.Role, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// validate ensures the flags are valid before using them. | ||
// TODO: refactor with flags & projects validate(). | ||
func validate(cmd *cobra.Command, args []string) error { | ||
_, err := url.ParseRequestURI(viper.GetString("baseUri")) | ||
if err != nil { | ||
return errors.ErrInvalidBaseURI | ||
} | ||
|
||
return nil | ||
} |
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,86 @@ | ||
package members_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"ldcli/cmd" | ||
"ldcli/internal/errors" | ||
"ldcli/internal/members" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
mockArgs := []interface{}{ | ||
"testAccessToken", | ||
"http://test.com", | ||
"testemail@test.com", | ||
"writer", | ||
} | ||
t.Run("with valid flags calls members API", func(t *testing.T) { | ||
client := members.MockClient{} | ||
client. | ||
On("Create", mockArgs...). | ||
Return([]byte(cmd.ValidResponse), nil) | ||
args := []string{ | ||
"members", | ||
"create", | ||
"-t", | ||
"testAccessToken", | ||
"-u", | ||
"http://test.com", | ||
"-d", | ||
`{"email": "testemail@test.com", "role": "writer"}`, | ||
} | ||
|
||
output, err := cmd.CallCmd(t, nil, &client, nil, args) | ||
|
||
require.NoError(t, err) | ||
assert.JSONEq(t, `{"valid": true}`, string(output)) | ||
}) | ||
|
||
t.Run("with an error response is an error", func(t *testing.T) { | ||
client := members.MockClient{} | ||
client. | ||
On("Create", mockArgs...). | ||
Return([]byte(`{}`), errors.NewError("An error")) | ||
args := []string{ | ||
"members", | ||
"create", | ||
"-t", | ||
"testAccessToken", | ||
"-u", | ||
"http://test.com", | ||
"-d", | ||
`{"email": "testemail@test.com", "role": "writer"}`, | ||
} | ||
|
||
_, err := cmd.CallCmd(t, nil, &client, nil, args) | ||
|
||
require.EqualError(t, err, "An error") | ||
}) | ||
|
||
t.Run("with missing required flags is an error", func(t *testing.T) { | ||
args := []string{ | ||
"members", | ||
"create", | ||
} | ||
|
||
_, err := cmd.CallCmd(t, nil, &members.MockClient{}, nil, args) | ||
|
||
assert.EqualError(t, err, `required flag(s) "accessToken", "data" not set`) | ||
}) | ||
|
||
t.Run("with invalid baseUri is an error", func(t *testing.T) { | ||
args := []string{ | ||
"projects", | ||
"create", | ||
"--baseUri", "invalid", | ||
} | ||
|
||
_, err := cmd.CallCmd(t, nil, &members.MockClient{}, nil, args) | ||
|
||
assert.EqualError(t, err, "baseUri is invalid") | ||
}) | ||
} |
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,25 @@ | ||
package members | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"ldcli/internal/members" | ||
) | ||
|
||
func NewMembersCmd(client members.Client) (*cobra.Command, error) { | ||
cmd := &cobra.Command{ | ||
Use: "members", | ||
Short: "Make requests (create/invite) on members", | ||
Long: "Make requests (create/invite) on members", | ||
} | ||
|
||
createCmd, err := NewCreateCmd(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cmd.AddCommand(createCmd) | ||
|
||
return cmd, nil | ||
|
||
} |
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
Oops, something went wrong.