Skip to content

Commit

Permalink
OSD-8633: select teams tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supreeth Basabattini committed Oct 12, 2021
1 parent 9cdc46c commit 07827d1
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/pdcli/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func loginHandler(cmd *cobra.Command, args []string) error {

// Check if user has selected a team
if cfg.TeamID == "" {
teamdID, err := teams.SelectTeam(pdClient)
teamdID, err := teams.SelectTeam(pdClient, os.Stdin)

if err != nil {
return err
Expand Down
11 changes: 7 additions & 4 deletions cmd/pdcli/teams/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package teams
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -37,7 +38,7 @@ func teamsHandler(cmd *cobra.Command, args []string) error {
}

// Fetch the user selected team ID
teamID, err := SelectTeam(pdClient)
teamID, err := SelectTeam(pdClient, os.Stdin)

if err != nil {
return err
Expand All @@ -58,7 +59,7 @@ func teamsHandler(cmd *cobra.Command, args []string) error {
}

// SelectTeam prompts the user to select a team and returns the selected team ID.
func SelectTeam(c client.PagerDutyClient) (string, error) {
func SelectTeam(c client.PagerDutyClient, stdin io.Reader) (string, error) {
var selectedTeam string
var userOptions pdApi.GetCurrentUserOptions

Expand All @@ -74,7 +75,7 @@ func SelectTeam(c client.PagerDutyClient) (string, error) {
// Check if the user belongs to any team
if len(user.Teams) == 0 {
fmt.Println("Your user account is currently not a part of any team")
return "", nil
os.Exit(0)
}

for i, team := range user.Teams {
Expand All @@ -85,7 +86,7 @@ func SelectTeam(c client.PagerDutyClient) (string, error) {

fmt.Print("Select Team: ")

reader := bufio.NewReader(os.Stdin)
reader := bufio.NewReader(stdin)

input, err := reader.ReadString('\n')

Expand All @@ -97,6 +98,8 @@ func SelectTeam(c client.PagerDutyClient) (string, error) {

if val, ok := userTeams[input]; ok {
selectedTeam = val
} else {
return "", fmt.Errorf("please select a valid option")
}

return selectedTeam, nil
Expand Down
82 changes: 82 additions & 0 deletions tests/teams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package tests

import (
"bytes"

pdApi "github.com/PagerDuty/go-pagerduty"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/openshift/pagerduty-short-circuiter/cmd/pdcli/teams"
mockpd "github.com/openshift/pagerduty-short-circuiter/pkg/client/mock"
)

var _ = Describe("select team", func() {
var (
mockCtrl *gomock.Controller
mockClient *mockpd.MockPagerDutyClient
)

BeforeEach(func() {
mockCtrl = gomock.NewController(GinkgoT())
mockClient = mockpd.NewMockPagerDutyClient(mockCtrl)
})

AfterEach(func() {
mockCtrl.Finish()
})

When("pdcli teams is run", func() {
It("prompts the user to select a team and retuns the team ID", func() {

userResponse := &pdApi.User{
Name: "my-user",
Teams: []pdApi.Team{
{Name: "my-team-a", APIObject: pdApi.APIObject{ID: "ABCD123"}},
{Name: "my-team-b", APIObject: pdApi.APIObject{ID: "EFGH456"}},
{Name: "my-team-c", APIObject: pdApi.APIObject{ID: "IJKL789"}},
},
}

expectedResult := "EFGH456"

mockClient.EXPECT().GetCurrentUser(gomock.Any()).Return(userResponse, nil).Times(1)

var stdin bytes.Buffer

stdin.Write([]byte("2\n"))

result, err := teams.SelectTeam(mockClient, &stdin)

Expect(err).ToNot(HaveOccurred())

Expect(result).To(Equal(expectedResult))
})
})

When("a user enters an option which is not valid (or) is out of bounds", func() {
It("throws an error", func() {

userResponse := &pdApi.User{
Name: "my-user",
Teams: []pdApi.Team{
{Name: "my-team-a", APIObject: pdApi.APIObject{ID: "ABCD123"}},
{Name: "my-team-b", APIObject: pdApi.APIObject{ID: "EFGH456"}},
{Name: "my-team-c", APIObject: pdApi.APIObject{ID: "IJKL789"}},
},
}

mockClient.EXPECT().GetCurrentUser(gomock.Any()).Return(userResponse, nil).Times(1)

var stdin bytes.Buffer

stdin.Write([]byte("X\n"))

result, err := teams.SelectTeam(mockClient, &stdin)

Expect(err).To(HaveOccurred())

Expect(result).To(BeEmpty())
})
})
})

0 comments on commit 07827d1

Please sign in to comment.