Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn user of updating idp managed teams #1273

Merged
merged 3 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions cloud/team/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ var (
teamPagnationLimit = 100
)

func confirmOperation() bool {
y, _ := input.Confirm("This is an IDP-managed team. Are you sure you want to continue the operation?")
return y
}

func CreateTeam(name, description string, out io.Writer, client astrocore.CoreClient) error {
if name == "" {
return ErrInvalidName
Expand Down Expand Up @@ -167,8 +172,13 @@ func UpdateTeam(id, name, description string, out io.Writer, client astrocore.Co
return ErrTeamNotFound
}
}
if team.IsIdpManaged {
y := confirmOperation()
if !y {
return nil
}
}
teamID := team.Id

teamUpdateRequest := astrocore.UpdateTeamJSONRequestBody{}

if name == "" {
Expand Down Expand Up @@ -444,7 +454,7 @@ func GetOrgTeams(client astrocore.CoreClient) ([]astrocore.Team, error) {
func ListOrgTeams(out io.Writer, client astrocore.CoreClient) error {
table := printutil.Table{
DynamicPadding: true,
Header: []string{"ID", "Name", "Description", "CREATE DATE"},
Header: []string{"ID", "NAME", "DESCRIPTION", "IDP MANAGED", "CREATE DATE"},
}
teams, err := GetOrgTeams(client)
if err != nil {
Expand All @@ -456,6 +466,7 @@ func ListOrgTeams(out io.Writer, client astrocore.CoreClient) error {
teams[i].Id,
teams[i].Name,
*teams[i].Description,
strconv.FormatBool(teams[i].IsIdpManaged),
teams[i].CreatedAt.Format(time.RFC3339),
}, false)
}
Expand Down Expand Up @@ -496,6 +507,12 @@ func Delete(id string, out io.Writer, client astrocore.CoreClient) error {
return ErrTeamNotFound
}
}
if team.IsIdpManaged {
y := confirmOperation()
if !y {
return nil
}
}
teamID := team.Id
resp, err := client.DeleteTeamWithResponse(httpContext.Background(), ctx.OrganizationShortName, teamID)
if err != nil {
Expand Down Expand Up @@ -541,6 +558,12 @@ func RemoveUser(teamID, teamMemberID string, out io.Writer, client astrocore.Cor
return ErrTeamNotFound
}
}
if team.IsIdpManaged {
y := confirmOperation()
if !y {
return nil
}
}
teamID = team.Id
if team.Members == nil {
return ErrNoTeamMembersFoundInTeam
Expand Down Expand Up @@ -610,6 +633,12 @@ func AddUser(teamID, userID string, out io.Writer, client astrocore.CoreClient)
return ErrTeamNotFound
}
}
if team.IsIdpManaged {
y := confirmOperation()
if !y {
return nil
}
}
teamID = team.Id

var userSelection astrocore.User
Expand Down
111 changes: 111 additions & 0 deletions cloud/team/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ var (
Id: "team1-id",
Members: &teamMembers,
}
team2 = astrocore.Team{
CreatedAt: time.Now(),
Name: "team 2",
Description: &description,
Id: "team2-id",
Members: &teamMembers,
IsIdpManaged: true,
}
teams = []astrocore.Team{
team1,
team2,
}
GetUserWithResponseOK = astrocore.GetUserResponse{
HTTPResponse: &http.Response{
Expand All @@ -60,6 +69,12 @@ var (
},
JSON200: &team1,
}
GetIDPManagedTeamWithResponseOK = astrocore.GetTeamResponse{
HTTPResponse: &http.Response{
StatusCode: 200,
},
JSON200: &team2,
}
errorBodyGet, _ = json.Marshal(astrocore.Error{
Message: "failed to get team",
})
Expand Down Expand Up @@ -688,6 +703,29 @@ func TestDelete(t *testing.T) {
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("happy path Delete with idp managed team", func(t *testing.T) {
expectedOutMessage := fmt.Sprintf("Astro Team %s was successfully deleted\n", team2.Name)
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("DeleteTeamWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&DeleteOrganizationTeamResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "y")()
err := Delete(team2.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("error path user reject delete", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("DeleteTeamWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&DeleteOrganizationTeamResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "n")()
err := Delete(team2.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, "", out.String())
})

t.Run("error path no org teams found", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
Expand Down Expand Up @@ -783,6 +821,29 @@ func TestUpdate(t *testing.T) {
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("happy path Update with idp managed team", func(t *testing.T) {
expectedOutMessage := fmt.Sprintf("Astro Team %s was successfully updated\n", team2.Name)
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("UpdateTeamWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&UpdateTeamResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "y")()
err := UpdateTeam(team2.Id, "name", "description", out, mockClient)
assert.NoError(t, err)
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("happy path user reject Update", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("UpdateTeamWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&UpdateTeamResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "n")()
err := UpdateTeam(team2.Id, "name", "description", out, mockClient)
assert.NoError(t, err)
assert.Equal(t, "", out.String())
})

t.Run("happy path Update no description passed in", func(t *testing.T) {
expectedOutMessage := fmt.Sprintf("Astro Team %s was successfully updated\n", team1.Name)
out := new(bytes.Buffer)
Expand Down Expand Up @@ -959,6 +1020,31 @@ func TestAddUser(t *testing.T) {
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("happy path AddUser with idp managed team", func(t *testing.T) {
expectedOutMessage := fmt.Sprintf("Astro User %s was successfully added to team %s \n", user1.Id, team2.Name)
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("GetUserWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetUserWithResponseOK, nil).Twice()
mockClient.On("AddTeamMembersWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&AddTeamMemberResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "y")()
err := AddUser(team2.Id, user1.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("user reject AddUser", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("GetUserWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetUserWithResponseOK, nil).Twice()
mockClient.On("AddTeamMembersWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&AddTeamMemberResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "n")()
err := AddUser(team2.Id, user1.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, "", out.String())
})

t.Run("error path no org teams found", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
Expand Down Expand Up @@ -1111,6 +1197,31 @@ func TestRemoveUser(t *testing.T) {
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("happy path RemoveUser with idp managed team", func(t *testing.T) {
expectedOutMessage := fmt.Sprintf("Astro User %s was successfully removed from team %s \n", user1.Id, team2.Name)
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("ListOrgUsersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&ListOrgUsersResponseOK, nil).Twice()
mockClient.On("RemoveTeamMemberWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&RemoveTeamMemberResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "y")()
err := RemoveUser(team2.Id, user1.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, expectedOutMessage, out.String())
})

t.Run("user reject RemoveUser with idp managed team", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
mockClient.On("GetTeamWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&GetIDPManagedTeamWithResponseOK, nil).Twice()
mockClient.On("ListOrgUsersWithResponse", mock.Anything, mock.Anything, mock.Anything).Return(&ListOrgUsersResponseOK, nil).Twice()
mockClient.On("RemoveTeamMemberWithResponse", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&RemoveTeamMemberResponseOK, nil).Once()
defer testUtil.MockUserInput(t, "n")()
err := RemoveUser(team2.Id, user1.Id, out, mockClient)
assert.NoError(t, err)
assert.Equal(t, "", out.String())
})

t.Run("error path no org teams found", func(t *testing.T) {
out := new(bytes.Buffer)
mockClient := new(astrocore_mocks.ClientWithResponsesInterface)
Expand Down