Skip to content

Commit

Permalink
feat(storage): adds support for storing user regions.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhartstonge committed Feb 21, 2024
1 parent 7be766d commit 13dcbab
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 3 deletions.
17 changes: 17 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type User struct {
// Roles contains roles that a user has been granted.
Roles []string `bson:"roles" json:"roles" xml:"roles"`

// Regions contains a list of regions that the user has access to.
Regions []string `bson:"regions" json:"regions" xml:"regions"`

// PersonID is a uniquely assigned id that references a person within the
// system.
// This enables applications where an external person data store is present.
Expand Down Expand Up @@ -164,6 +167,16 @@ func (u *User) DisableRoles(roles ...string) {
u.Roles = utils.RemoveFromStringSet(u.Roles, roles...)
}

// EnableRegions adds one or many regions to a user.
func (u *User) EnableRegions(regions ...string) {
u.Regions = utils.AppendToStringSet(u.Regions, regions...)
}

// DisableRegions removes one or many regions from a user.
func (u *User) DisableRegions(regions ...string) {
u.Regions = utils.RemoveFromStringSet(u.Regions, regions...)
}

// Equal enables checking equality as having a byte array in a struct stops
// allowing direct equality checks.
func (u User) Equal(x User) bool {
Expand Down Expand Up @@ -195,6 +208,10 @@ func (u User) Equal(x User) bool {
return false
}

if !stringArrayEquals(u.Regions, x.Regions) {
return false
}

if u.PersonID != x.PersonID {
return false
}
Expand Down
171 changes: 168 additions & 3 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func expectedUser() User {
"user",
"printer",
},
Regions: []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
},
PersonID: "123",
Disabled: false,
Username: "[email protected]",
Expand Down Expand Up @@ -542,7 +547,7 @@ func TestUser_DisableRoles_One(t *testing.T) {
func TestUser_DisableRoles_Many(t *testing.T) {
u := expectedUser()

expectedPeopleIDs := []string{
expectedRoles := []string{
"user",
"printer",
}
Expand All @@ -560,14 +565,140 @@ func TestUser_DisableRoles_Many(t *testing.T) {
"finance",
"groups",
)
assert.EqualValues(t, expectedPeopleIDs, u.Roles)
assert.EqualValues(t, expectedRoles, u.Roles)

u.DisableRoles(
"administrator",
"finance",
"groups",
)
assert.EqualValues(t, expectedPeopleIDs, u.Roles)
assert.EqualValues(t, expectedRoles, u.Roles)
}

func TestUser_EnableRegions_None(t *testing.T) {
u := expectedUser()

expectedUserRegions := []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
}

u.EnableRegions("eu-west-2")
assert.EqualValues(t, expectedUserRegions, u.Regions)

u.EnableRegions("ap-southeast-2")
assert.EqualValues(t, expectedUserRegions, u.Regions)
}

func TestUser_EnableRegions_One(t *testing.T) {
u := expectedUser()

expectedRegions := []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
"eu-south-2",
}

u.EnableRegions("eu-south-2")
assert.EqualValues(t, expectedRegions, u.Regions)

u.EnableRegions("eu-south-2")
assert.EqualValues(t, expectedRegions, u.Regions)

u.EnableRegions("ap-southeast-2")
assert.EqualValues(t, expectedRegions, u.Regions)
}

func TestUser_EnableRegions_Many(t *testing.T) {
u := expectedUser()

expectedRegions := []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
"ap-south-1",
"eu-south-2",
"me-central-1",
}

u.EnableRegions("ap-south-1", "eu-south-2", "me-central-1")
assert.EqualValues(t, expectedRegions, u.Regions)

u.EnableRegions("ap-south-1", "eu-west-2", "me-central-1")
assert.EqualValues(t, expectedRegions, u.Regions)
}

func TestUser_DisableRegions_None(t *testing.T) {
u := expectedUser()

expectedRegions := []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
}

u.DisableRegions("me-central-1")
assert.EqualValues(t, expectedRegions, u.Regions)
}

func TestUser_DisableRegions_One(t *testing.T) {
u := expectedUser()

expectedRegions := []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
}

u.DisableRegions("australia")
assert.EqualValues(t, expectedRegions, u.Regions)

u.DisableRegions("australia")
assert.EqualValues(t, expectedRegions, u.Regions)

u.DisableRegions("ap-southeast-2")
assert.EqualValues(t, expectedRegions[:len(expectedRegions)-1], u.Regions)

u.DisableRegions("new zealand")
assert.EqualValues(t, expectedRegions[:len(expectedRegions)-1], u.Regions)

u.DisableRegions("us-west-1")
assert.EqualValues(t, expectedRegions[1:len(expectedRegions)-1], u.Regions)
}

func TestUser_DisableRegions_Many(t *testing.T) {
u := expectedUser()

expectedRegions := []string{
"us-west-1",
"eu-west-2",
"ap-south-1",
}

u.Regions = []string{
"us-west-1",
"eu-west-2",
"ap-southeast-2",
"ap-south-1",
"eu-south-2",
"me-central-1",
}

u.DisableRegions(
"ap-southeast-2",
"eu-south-2",
"me-central-1",
)
assert.EqualValues(t, expectedRegions, u.Regions)

u.DisableRegions(
"aussie",
"nz",
"america",
)
assert.EqualValues(t, expectedRegions, u.Regions)
}

func TestUser_Equal(t *testing.T) {
Expand Down Expand Up @@ -751,6 +882,36 @@ func TestUser_Equal(t *testing.T) {
},
expected: false,
},
{
description: "regions should be equal",
x: User{
Regions: []string{"ap-southeast-2", "us-west-1"},
},
y: User{
Regions: []string{"ap-southeast-2", "us-west-1"},
},
expected: true,
},
{
description: "regions should not be equal",
x: User{
Regions: []string{"ap-southeast-2", "us-west-1"},
},
y: User{
Regions: []string{"ap-southeast-2", "us-east-1"},
},
expected: false,
},
{
description: "regions length should not be equal",
x: User{
Regions: []string{"ap-southeast-2"},
},
y: User{
Regions: []string{"ap-southeast-2", "us-west-1"},
},
expected: false,
},
{
description: "personid should be equal",
x: User{
Expand Down Expand Up @@ -929,6 +1090,7 @@ func TestUser_Equal(t *testing.T) {
AllowedPersonAccess: []string{"elvis"},
Scopes: []string{"10x", "2x"},
Roles: []string{"cheese"},
Regions: []string{"ap-southeast-2", "us-west-1"},
PersonID: "123",
Disabled: false,
Username: "[email protected]",
Expand All @@ -945,6 +1107,7 @@ func TestUser_Equal(t *testing.T) {
AllowedPersonAccess: []string{"elvis"},
Scopes: []string{"10x", "2x"},
Roles: []string{"cheese"},
Regions: []string{"ap-southeast-2", "us-west-1"},
PersonID: "123",
Disabled: false,
Username: "[email protected]",
Expand All @@ -965,6 +1128,7 @@ func TestUser_Equal(t *testing.T) {
AllowedPersonAccess: []string{"elvis"},
Scopes: []string{"10x", "2x"},
Roles: []string{"cheese"},
Regions: []string{"ap-southeast-2", "us-west-1"},
PersonID: "123",
Disabled: false,
Username: "[email protected]",
Expand All @@ -981,6 +1145,7 @@ func TestUser_Equal(t *testing.T) {
AllowedPersonAccess: []string{"elvis"},
Scopes: []string{"10x"},
Roles: []string{"cheese"},
Regions: []string{"ap-southeast-2", "us-west-1"},
PersonID: "123",
Disabled: false,
Username: "[email protected]",
Expand Down

0 comments on commit 13dcbab

Please sign in to comment.