fleet/ee/server/service/users_test.go
Lucas Manuel Rodriguez 9864048ee9
Allow setting user roles during JIT provisioning (#10193)
#8411

PS: I've opened #10209 to solve the issue with Golang Code Coverage CI
checks.

- [X] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- ~[] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [x] Manual QA for all new/changed functionality
  - ~For Orbit and Fleet Desktop changes:~
- ~[ ] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.~
- ~[ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
2023-03-01 20:18:40 -03:00

171 lines
3.2 KiB
Go

package service
import (
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/require"
)
func TestRolesChanged(t *testing.T) {
for _, tc := range []struct {
name string
oldGlobal *string
oldTeams []fleet.UserTeam
newGlobal *string
newTeams []fleet.UserTeam
expectedRolesChanged bool
}{
{
name: "no roles",
expectedRolesChanged: false,
},
{
name: "no-role-to-global-role",
newGlobal: ptr.String("admin"),
expectedRolesChanged: true,
},
{
name: "global-role-to-no-role",
oldGlobal: ptr.String("admin"),
expectedRolesChanged: true,
},
{
name: "global-role-unchanged",
oldGlobal: ptr.String("admin"),
newGlobal: ptr.String("admin"),
expectedRolesChanged: false,
},
{
name: "global-role-to-other-role",
oldGlobal: ptr.String("admin"),
newGlobal: ptr.String("maintainer"),
expectedRolesChanged: true,
},
{
name: "global-role-to-team-role",
oldGlobal: ptr.String("admin"),
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
},
expectedRolesChanged: true,
},
{
name: "change-role-in-teams",
oldTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "maintainer",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
expectedRolesChanged: true,
},
{
name: "remove-from-team",
oldTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "maintainer",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
expectedRolesChanged: true,
},
{
name: "no-change-teams",
oldTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
expectedRolesChanged: false,
},
{
name: "added-to-teams",
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
expectedRolesChanged: true,
},
{
name: "removed-from-a-team-and-added-to-another",
oldTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 3},
Role: "observer",
},
},
newTeams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 1},
Role: "admin",
},
{
Team: fleet.Team{ID: 2},
Role: "maintainer",
},
},
expectedRolesChanged: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.expectedRolesChanged, rolesChanged(tc.oldGlobal, tc.oldTeams, tc.newGlobal, tc.newTeams))
})
}
}