Skip to content

Commit

Permalink
Add tests to get team local endpoints (mattermost#15071)
Browse files Browse the repository at this point in the history
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
  • Loading branch information
mgdelacroix and mattermod committed Jul 23, 2020
1 parent d952918 commit b660e21
Showing 1 changed file with 85 additions and 69 deletions.
154 changes: 85 additions & 69 deletions api4/team_test.go
Expand Up @@ -24,51 +24,53 @@ import (
func TestCreateTeam(t *testing.T) {
th := Setup(t)
defer th.TearDown()
Client := th.Client

team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN}
rteam, resp := Client.CreateTeam(team)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN}
rteam, resp := client.CreateTeam(team)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)

require.Equal(t, rteam.Name, team.Name, "names did not match")
require.Equal(t, rteam.Name, team.Name, "names did not match")

require.Equal(t, rteam.DisplayName, team.DisplayName, "display names did not match")
require.Equal(t, rteam.DisplayName, team.DisplayName, "display names did not match")

require.Equal(t, rteam.Type, team.Type, "types did not match")
require.Equal(t, rteam.Type, team.Type, "types did not match")

_, resp = Client.CreateTeam(rteam)
CheckBadRequestStatus(t, resp)
_, resp = client.CreateTeam(rteam)
CheckBadRequestStatus(t, resp)

rteam.Id = ""
_, resp = Client.CreateTeam(rteam)
CheckErrorMessage(t, resp, "store.sql_team.save.domain_exists.app_error")
CheckBadRequestStatus(t, resp)
rteam.Id = ""
_, resp = client.CreateTeam(rteam)
CheckErrorMessage(t, resp, "store.sql_team.save.domain_exists.app_error")
CheckBadRequestStatus(t, resp)

rteam.Name = ""
_, resp = Client.CreateTeam(rteam)
CheckErrorMessage(t, resp, "model.team.is_valid.characters.app_error")
CheckBadRequestStatus(t, resp)
rteam.Name = ""
_, resp = client.CreateTeam(rteam)
CheckErrorMessage(t, resp, "model.team.is_valid.characters.app_error")
CheckBadRequestStatus(t, resp)

r, err := Client.DoApiPost("/teams", "garbage")
require.NotNil(t, err, "should have errored")
r, err := client.DoApiPost("/teams", "garbage")
require.NotNil(t, err, "should have errored")

require.Equalf(t, r.StatusCode, http.StatusBadRequest, "wrong status code, actual: %s, expected: %s", strconv.Itoa(r.StatusCode), strconv.Itoa(http.StatusBadRequest))
require.Equalf(t, r.StatusCode, http.StatusBadRequest, "wrong status code, actual: %s, expected: %s", strconv.Itoa(r.StatusCode), strconv.Itoa(http.StatusBadRequest))

Client.Logout()
// Test GroupConstrained flag
groupConstrainedTeam := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN, GroupConstrained: model.NewBool(true)}
rteam, resp = client.CreateTeam(groupConstrainedTeam)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)

_, resp = Client.CreateTeam(rteam)
CheckUnauthorizedStatus(t, resp)
assert.Equal(t, *rteam.GroupConstrained, *groupConstrainedTeam.GroupConstrained, "GroupConstrained flags do not match")
})

th.LoginBasic()
th.Client.Logout()

// Test GroupConstrained flag
groupConstrainedTeam := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN, GroupConstrained: model.NewBool(true)}
rteam, resp = Client.CreateTeam(groupConstrainedTeam)
CheckNoError(t, resp)
CheckCreatedStatus(t, resp)
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN}
_, resp := th.Client.CreateTeam(team)
CheckUnauthorizedStatus(t, resp)

assert.Equal(t, *rteam.GroupConstrained, *groupConstrainedTeam.GroupConstrained, "GroupConstrained flags do not match")
th.LoginBasic()

// Check the appropriate permissions are enforced.
defaultRolePermissions := th.SaveDefaultRolePermissions()
Expand All @@ -79,7 +81,7 @@ func TestCreateTeam(t *testing.T) {
th.RemovePermissionFromRole(model.PERMISSION_CREATE_TEAM.Id, model.SYSTEM_USER_ROLE_ID)
th.AddPermissionToRole(model.PERMISSION_CREATE_TEAM.Id, model.SYSTEM_ADMIN_ROLE_ID)

_, resp = Client.CreateTeam(team)
_, resp = th.Client.CreateTeam(team)
CheckForbiddenStatus(t, resp)
}

Expand All @@ -104,7 +106,7 @@ func TestCreateTeamSanitization(t *testing.T) {
require.NotEmpty(t, rteam.InviteId, "should not have sanitized inviteid")
})

t.Run("system admin", func(t *testing.T) {
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
team := &model.Team{
DisplayName: t.Name() + "_2",
Name: GenerateTestTeamName(),
Expand All @@ -113,11 +115,11 @@ func TestCreateTeamSanitization(t *testing.T) {
AllowedDomains: "simulator.amazonses.com,localhost",
}

rteam, resp := th.SystemAdminClient.CreateTeam(team)
rteam, resp := client.CreateTeam(team)
CheckNoError(t, resp)
require.NotEmpty(t, rteam.Email, "should not have sanitized email")
require.NotEmpty(t, rteam.InviteId, "should not have sanitized inviteid")
})
}, "system admin")
}

func TestGetTeam(t *testing.T) {
Expand All @@ -126,19 +128,21 @@ func TestGetTeam(t *testing.T) {
Client := th.Client
team := th.BasicTeam

rteam, resp := Client.GetTeam(team.Id, "")
CheckNoError(t, resp)
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
rteam, resp := client.GetTeam(team.Id, "")
CheckNoError(t, resp)

require.Equal(t, rteam.Id, team.Id, "wrong team")
require.Equal(t, rteam.Id, team.Id, "wrong team")

_, resp = Client.GetTeam("junk", "")
CheckBadRequestStatus(t, resp)
_, resp = client.GetTeam("junk", "")
CheckBadRequestStatus(t, resp)

_, resp = Client.GetTeam("", "")
CheckNotFoundStatus(t, resp)
_, resp = client.GetTeam("", "")
CheckNotFoundStatus(t, resp)

_, resp = Client.GetTeam(model.NewId(), "")
CheckNotFoundStatus(t, resp)
_, resp = client.GetTeam(model.NewId(), "")
CheckNotFoundStatus(t, resp)
})

th.LoginTeamAdmin()

Expand All @@ -150,7 +154,7 @@ func TestGetTeam(t *testing.T) {

th.LoginBasic()
// AllowInviteOpen is false and team is open, and user is not on team
_, resp = Client.GetTeam(rteam2.Id, "")
_, resp := Client.GetTeam(rteam2.Id, "")
CheckForbiddenStatus(t, resp)

// AllowInviteOpen is true and team is invite, and user is not on team
Expand All @@ -161,8 +165,10 @@ func TestGetTeam(t *testing.T) {
_, resp = Client.GetTeam(team.Id, "")
CheckUnauthorizedStatus(t, resp)

_, resp = th.SystemAdminClient.GetTeam(rteam2.Id, "")
CheckNoError(t, resp)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
_, resp = client.GetTeam(rteam2.Id, "")
CheckNoError(t, resp)
})
}

func TestGetTeamSanitization(t *testing.T) {
Expand Down Expand Up @@ -213,13 +219,13 @@ func TestGetTeamSanitization(t *testing.T) {
require.NotEmpty(t, rteam.InviteId, "should not have sanitized inviteid")
})

t.Run("system admin", func(t *testing.T) {
rteam, resp := th.SystemAdminClient.GetTeam(team.Id, "")
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
rteam, resp := client.GetTeam(team.Id, "")
CheckNoError(t, resp)

require.NotEmpty(t, rteam.Email, "should not have sanitized email")
require.NotEmpty(t, rteam.InviteId, "should not have sanitized inviteid")
})
}, "system admin")
}

func TestGetTeamUnread(t *testing.T) {
Expand Down Expand Up @@ -979,6 +985,12 @@ func TestGetAllTeams(t *testing.T) {
})
}

t.Run("Local mode", func(t *testing.T) {
teams, res := th.LocalClient.GetAllTeams("", 0, 10)
CheckNoError(t, res)
require.Len(t, teams, 5)
})

t.Run("Unauthorized", func(t *testing.T) {
Client.Logout()
_, resp = Client.GetAllTeams("", 1, 10)
Expand Down Expand Up @@ -1033,8 +1045,8 @@ func TestGetAllTeamsSanitization(t *testing.T) {
require.True(t, team2Found, "wasn't returned the expected teams so the test wasn't run correctly")
})

t.Run("system admin", func(t *testing.T) {
rteams, resp := th.SystemAdminClient.GetAllTeams("", 0, 1000)
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
rteams, resp := client.GetAllTeams("", 0, 1000)
CheckNoError(t, resp)
for _, rteam := range rteams {
if rteam.Id != team.Id && rteam.Id != team2.Id {
Expand All @@ -1044,31 +1056,35 @@ func TestGetAllTeamsSanitization(t *testing.T) {
require.NotEmpty(t, rteam.Email, "should not have sanitized email")
require.NotEmpty(t, rteam.InviteId, "should not have sanitized inviteid")
}
})
}, "system admin")
}

func TestGetTeamByName(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
Client := th.Client
team := th.BasicTeam

rteam, resp := Client.GetTeamByName(team.Name, "")
CheckNoError(t, resp)
th.TestForAllClients(t, func(t *testing.T, client *model.Client4) {
rteam, resp := client.GetTeamByName(team.Name, "")
CheckNoError(t, resp)

require.Equal(t, rteam.Name, team.Name, "wrong team")
require.Equal(t, rteam.Name, team.Name, "wrong team")

_, resp = Client.GetTeamByName("junk", "")
CheckNotFoundStatus(t, resp)
_, resp = client.GetTeamByName("junk", "")
CheckNotFoundStatus(t, resp)

_, resp = Client.GetTeamByName("", "")
CheckNotFoundStatus(t, resp)
_, resp = client.GetTeamByName("", "")
CheckNotFoundStatus(t, resp)

_, resp = th.SystemAdminClient.GetTeamByName(strings.ToUpper(team.Name), "")
CheckNoError(t, resp)
})

Client.Logout()
_, resp = Client.GetTeamByName(team.Name, "")
th.TestForSystemAdminAndLocal(t, func(t *testing.T, client *model.Client4) {
_, resp := client.GetTeamByName(strings.ToUpper(team.Name), "")
CheckNoError(t, resp)
})

th.Client.Logout()
_, resp := th.Client.GetTeamByName(team.Name, "")
CheckUnauthorizedStatus(t, resp)

_, resp = th.SystemAdminClient.GetTeamByName(team.Name, "")
Expand All @@ -1077,18 +1093,18 @@ func TestGetTeamByName(t *testing.T) {
th.LoginTeamAdmin()

team2 := &model.Team{DisplayName: "Name", Name: GenerateTestTeamName(), Email: th.GenerateTestEmail(), Type: model.TEAM_OPEN, AllowOpenInvite: false}
rteam2, _ := Client.CreateTeam(team2)
rteam2, _ := th.Client.CreateTeam(team2)

team3 := &model.Team{DisplayName: "Name", Name: GenerateTestTeamName(), Email: th.GenerateTestEmail(), Type: model.TEAM_INVITE, AllowOpenInvite: true}
rteam3, _ := Client.CreateTeam(team3)
rteam3, _ := th.Client.CreateTeam(team3)

th.LoginBasic()
// AllowInviteOpen is false and team is open, and user is not on team
_, resp = Client.GetTeamByName(rteam2.Name, "")
_, resp = th.Client.GetTeamByName(rteam2.Name, "")
CheckForbiddenStatus(t, resp)

// AllowInviteOpen is true and team is invite only, and user is not on team
_, resp = Client.GetTeamByName(rteam3.Name, "")
_, resp = th.Client.GetTeamByName(rteam3.Name, "")
CheckForbiddenStatus(t, resp)
}

Expand Down

0 comments on commit b660e21

Please sign in to comment.