Skip to content

Commit dc943ec

Browse files
author
Brian Flores
committed
added command and unit tests
1 parent 71ad0cc commit dc943ec

6 files changed

Lines changed: 229 additions & 0 deletions

File tree

plugin/commands/user/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command {
2828
cobraCmd.AddCommand(NewDeviceAccessCommand(sl).Command)
2929
cobraCmd.AddCommand(NewVpnSubnetCommand(sl).Command)
3030
cobraCmd.AddCommand(NewVpnManualCommand(sl).Command)
31+
cobraCmd.AddCommand(NewVpnPasswordCommand(sl).Command)
3132
return cobraCmd
3233
}
3334

plugin/commands/user/user_suite_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var availableCommands = []string{
3232
"permissions",
3333
"remove-access",
3434
"vpn-manual",
35+
"vpn-password",
3536
"vpn-subnet",
3637
}
3738

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package user
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/spf13/cobra"
7+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/errors"
8+
. "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
9+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
10+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
11+
)
12+
13+
type VpnPasswordCommand struct {
14+
*metadata.SoftlayerCommand
15+
UserManager managers.UserManager
16+
Command *cobra.Command
17+
Password string
18+
}
19+
20+
func NewVpnPasswordCommand(sl *metadata.SoftlayerCommand) (cmd *VpnPasswordCommand) {
21+
thisCmd := &VpnPasswordCommand{
22+
SoftlayerCommand: sl,
23+
UserManager: managers.NewUserManager(sl.Session),
24+
}
25+
26+
cobraCmd := &cobra.Command{
27+
Use: "vpn-password " + T("IDENTIFIER"),
28+
Short: T("Set the user VPN password."),
29+
Args: metadata.OneArgs,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
return thisCmd.Run(args)
32+
},
33+
}
34+
35+
cobraCmd.Flags().StringVar(&thisCmd.Password, "password", "", T("Your new VPN password [required]"))
36+
37+
//#nosec G104 -- This is a false positive
38+
cobraCmd.MarkFlagRequired("password")
39+
40+
thisCmd.Command = cobraCmd
41+
return thisCmd
42+
}
43+
44+
func (cmd *VpnPasswordCommand) Run(args []string) error {
45+
userID, err := strconv.Atoi(args[0])
46+
if err != nil {
47+
return errors.NewInvalidSoftlayerIdInputError("User ID")
48+
}
49+
50+
success, err := cmd.UserManager.UpdateVpnPassword(userID, cmd.Password)
51+
if err != nil {
52+
return errors.NewAPIError(T("Failed to update user vpn password."), err.Error(), 2)
53+
}
54+
if success {
55+
cmd.UI.Ok()
56+
cmd.UI.Print(T("Successfully updated user vpn."))
57+
}
58+
return nil
59+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package user_test
2+
3+
import (
4+
"errors"
5+
6+
"github.com/IBM-Cloud/ibm-cloud-cli-sdk/testhelpers/terminal"
7+
. "github.com/onsi/ginkgo"
8+
. "github.com/onsi/gomega"
9+
"github.com/softlayer/softlayer-go/session"
10+
11+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/commands/user"
12+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
"github.ibm.com/SoftLayer/softlayer-cli/plugin/testhelpers"
14+
)
15+
16+
var _ = Describe("user vpn-password", func() {
17+
var (
18+
fakeUI *terminal.FakeUI
19+
cliCommand *user.VpnPasswordCommand
20+
fakeSession *session.Session
21+
slCommand *metadata.SoftlayerCommand
22+
fakeUserManager *testhelpers.FakeUserManager
23+
)
24+
BeforeEach(func() {
25+
fakeUI = terminal.NewFakeUI()
26+
fakeSession = testhelpers.NewFakeSoftlayerSession([]string{})
27+
fakeUserManager = new(testhelpers.FakeUserManager)
28+
slCommand = metadata.NewSoftlayerCommand(fakeUI, fakeSession)
29+
cliCommand = user.NewVpnPasswordCommand(slCommand)
30+
cliCommand.Command.PersistentFlags().Var(cliCommand.OutputFlag, "output", "--output=JSON for json output.")
31+
cliCommand.UserManager = fakeUserManager
32+
})
33+
34+
Describe("user vpn-password", func() {
35+
36+
Context("Return error", func() {
37+
It("Set command without Argument", func() {
38+
err := testhelpers.RunCobraCommand(cliCommand.Command)
39+
Expect(err).To(HaveOccurred())
40+
Expect(err.Error()).To(ContainSubstring("Incorrect Usage: This command requires one argument"))
41+
})
42+
43+
It("Set command with an invalid user Id", func() {
44+
err := testhelpers.RunCobraCommand(cliCommand.Command, "abcde", "--password=Mypassword1.")
45+
Expect(err).To(HaveOccurred())
46+
Expect(err.Error()).To(ContainSubstring("Invalid input for 'User ID'. It must be a positive integer."))
47+
})
48+
49+
It("Set without any option", func() {
50+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111")
51+
Expect(err).To(HaveOccurred())
52+
Expect(err.Error()).To(ContainSubstring(`required flag(s) "password" not set`))
53+
})
54+
})
55+
56+
Context("Return error", func() {
57+
BeforeEach(func() {
58+
fakeUserManager.UpdateVpnPasswordReturns(false, errors.New("Failed to update user vpn password."))
59+
})
60+
It("Failed edit user", func() {
61+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111", "--password=Mypassword1.")
62+
Expect(err).To(HaveOccurred())
63+
Expect(err.Error()).To(ContainSubstring("Failed to update user vpn password."))
64+
})
65+
})
66+
67+
Context("Return no error", func() {
68+
BeforeEach(func() {
69+
fakeUserManager.UpdateVpnPasswordReturns(true, nil)
70+
})
71+
It("enable", func() {
72+
err := testhelpers.RunCobraCommand(cliCommand.Command, "111111", "--password=Mypassword1.")
73+
Expect(err).NotTo(HaveOccurred())
74+
Expect(fakeUI.Outputs()).To(ContainSubstring("OK"))
75+
Expect(fakeUI.Outputs()).To(ContainSubstring("Successfully updated user vpn"))
76+
})
77+
})
78+
})
79+
})

plugin/managers/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type UserManager interface {
5858
UpdateVpnUser(userId int) (bool, error)
5959
GetOverrides(userId int) ([]datatypes.Network_Service_Vpn_Overrides, error)
6060
DeleteUserVpnOverride(overrideId int) (bool, error)
61+
UpdateVpnPassword(userID int, password string) (bool, error)
6162
}
6263

6364
type userManager struct {
@@ -362,3 +363,10 @@ func (u userManager) DeleteUserVpnOverride(overrideId int) (bool, error) {
362363
networkServiceVpnOverridesService := services.GetNetworkServiceVpnOverridesService(u.Session)
363364
return networkServiceVpnOverridesService.Id(overrideId).DeleteObject()
364365
}
366+
367+
// Update a user’s VPN password.
368+
// int userID: The user customer identifier.
369+
// string password: New password
370+
func (u userManager) UpdateVpnPassword(userID int, password string) (bool, error) {
371+
return u.UserCustomerService.Id(userID).UpdateVpnPassword(&password)
372+
}

plugin/testhelpers/fake_user_manager.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)