Skip to content

Commit

Permalink
feat: pass in optional role flag when using members invite (#90)
Browse files Browse the repository at this point in the history
* pass in optional role flag when using members invite

* default is already provided in help (by default!)

* remove test file

* use stringp for bding role flag
  • Loading branch information
k3llymariee authored Mar 28, 2024
1 parent 7518423 commit b51470d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/cliflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ const (
FlagFlag = "flag"
ProjectFlag = "project"
EmailsFlag = "emails"
RoleFlag = "role"
)
13 changes: 12 additions & 1 deletion cmd/members/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func NewInviteCmd(client members.Client) (*cobra.Command, error) {
return nil, err
}

cmd.Flags().StringP(
cliflags.RoleFlag,
"r",
"reader",
"Built-in role for the member - one of reader, writer, or admin",
)
err = viper.BindPFlag(cliflags.RoleFlag, cmd.Flags().Lookup(cliflags.RoleFlag))
if err != nil {
return nil, err
}

return cmd, nil
}

Expand All @@ -44,7 +55,7 @@ func runInvite(client members.Client) func(*cobra.Command, []string) error {
viper.GetString(cliflags.APITokenFlag),
viper.GetString(cliflags.BaseURIFlag),
viper.GetStringSlice(cliflags.EmailsFlag),
defaultRole,
viper.GetString(cliflags.RoleFlag),
)
if err != nil {
return err
Expand Down
56 changes: 56 additions & 0 deletions cmd/members/invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,59 @@ func TestInvite(t *testing.T) {
assert.EqualError(t, err, "base-uri is invalid"+errorHelp)
})
}

func TestInviteWithOptionalRole(t *testing.T) {
mockArgs := []interface{}{
"testAccessToken",
"http://test.com",
[]string{"testemail1@test.com", "testemail2@test.com"},
"writer",
}
t.Run("with valid optional long form flag 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`,
"--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 valid optional short form flag 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`,
"-r",
"writer",
}

output, err := cmd.CallCmd(t, nil, &client, nil, args)

require.NoError(t, err)
assert.JSONEq(t, `{"valid": true}`, string(output))
})
}

0 comments on commit b51470d

Please sign in to comment.