-
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: alias command to invite members (#84)
* add top level environments cmd * refactor members create to accept list of emails * add invite member command * fix tests, imports * add tests * oops remove environments stuff * update for new flags
- Loading branch information
1 parent
708925b
commit 7002866
Showing
8 changed files
with
166 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ const ( | |
BaseURIFlag = "base-uri" | ||
FlagFlag = "flag" | ||
ProjectFlag = "project" | ||
EmailsFlag = "emails" | ||
) |
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,57 @@ | ||
package members | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"ldcli/cmd/cliflags" | ||
"ldcli/cmd/validators" | ||
"ldcli/internal/members" | ||
) | ||
|
||
const defaultRole = "reader" | ||
|
||
func NewInviteCmd(client members.Client) (*cobra.Command, error) { | ||
cmd := &cobra.Command{ | ||
Args: validators.Validate(), | ||
Long: "Invite new members", | ||
RunE: runInvite(client), | ||
Short: "Invite new members", | ||
Use: "invite", | ||
} | ||
|
||
cmd.Flags().StringSliceP("emails", "e", []string{}, "A comma separated list of emails") | ||
err := cmd.MarkFlagRequired("emails") | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = viper.BindPFlag("emails", cmd.Flags().Lookup("emails")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cmd, nil | ||
} | ||
|
||
func runInvite(client members.Client) func(*cobra.Command, []string) error { | ||
return func(cmd *cobra.Command, args []string) error { | ||
|
||
response, err := client.Create( | ||
context.Background(), | ||
viper.GetString(cliflags.APITokenFlag), | ||
viper.GetString(cliflags.BaseURIFlag), | ||
viper.GetStringSlice(cliflags.EmailsFlag), | ||
defaultRole, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(cmd.OutOrStdout(), string(response)+"\n") | ||
|
||
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,87 @@ | ||
package members_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"ldcli/cmd" | ||
"ldcli/internal/errors" | ||
"ldcli/internal/members" | ||
) | ||
|
||
func TestInvite(t *testing.T) { | ||
errorHelp := ". See `ldcli members invite --help` for supported flags and usage." | ||
mockArgs := []interface{}{ | ||
"testAccessToken", | ||
"http://test.com", | ||
[]string{"testemail1@test.com", "testemail2@test.com"}, | ||
"reader", | ||
} | ||
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", | ||
"invite", | ||
"--api-token", | ||
"testAccessToken", | ||
"--base-uri", | ||
"http://test.com", | ||
"-e", | ||
`testemail1@test.com,testemail2@test.com`, | ||
} | ||
|
||
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", | ||
"invite", | ||
"--api-token", | ||
"testAccessToken", | ||
"--base-uri", | ||
"http://test.com", | ||
"-e", | ||
`testemail1@test.com,testemail2@test.com`, | ||
} | ||
|
||
_, 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", | ||
"invite", | ||
} | ||
|
||
_, err := cmd.CallCmd(t, nil, &members.MockClient{}, nil, args) | ||
|
||
assert.EqualError(t, err, `required flag(s) "api-token", "emails" not set`+errorHelp) | ||
}) | ||
|
||
t.Run("with invalid base-uri is an error", func(t *testing.T) { | ||
args := []string{ | ||
"members", | ||
"invite", | ||
"--base-uri", "invalid", | ||
} | ||
|
||
_, err := cmd.CallCmd(t, nil, &members.MockClient{}, nil, args) | ||
|
||
assert.EqualError(t, err, "base-uri is invalid"+errorHelp) | ||
}) | ||
} |
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