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

Users testcases #2942

Merged
merged 3 commits into from
Sep 12, 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
27 changes: 25 additions & 2 deletions users/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,42 @@ func (self *UserManagerTestSuite) TestDeleteUser() {
"OrgAdmin", "UserO2", []string{"O1", "O2"}, reader_policy)
assert.NoError(self.T(), err)

// Lookup using ORG_ADMIN
user_record, err = users.GetUser(self.Ctx, "OrgAdmin", "UserO2")
assert.NoError(self.T(), err)
golden.Set("UserO2 is in O1 and O2", user_record)
golden.Set("OrgAdmin UserO2 is in O1 and O2", user_record)

// Lookup using O1's SERVER_ADMIN
user_record, err = users.GetUser(self.Ctx, "AdminO1", "UserO2")
assert.NoError(self.T(), err)
golden.Set("AdminO1 UserO2 is in O1", user_record)

// Lookup using O2's SERVER_ADMIN
user_record, err = users.GetUser(self.Ctx, "AdminO2", "UserO2")
assert.NoError(self.T(), err)
golden.Set("AdminO2 UserO2 is in O2", user_record)

// AdminO2 will remove the user from all orgs, but they remain in
// O1 because AdminO2 has no accesss to O1
err = users.DeleteUser(
self.Ctx, "AdminO2", "UserO2", users.LIST_ALL_ORGS)
assert.NoError(self.T(), err)

// GetUser returns PermissionDenied if the user requesting does
// not have OrgAdmin and does not belong to any of the same orgs
user_record, err = users.GetUser(self.Ctx, "AdminO2", "UserO2")
assert.ErrorContains(self.T(), err, "PermissionDenied")
golden.Set("AdminO2 UserO2 removed from O2", err.Error())

// If the user was added to O1 and removed from O2, it should
// still exist in O1
user_record, err = users.GetUser(self.Ctx, "AdminO1", "UserO2")
assert.NoError(self.T(), err)
golden.Set("AdminO1 UserO2 still in O1", user_record)

user_record, err = users.GetUser(self.Ctx, "OrgAdmin", "UserO2")
assert.NoError(self.T(), err)
golden.Set("UserO2 removed from O2", user_record)
golden.Set("OrgAdmin UserO2 removed from O2", user_record)

goldie.Assert(self.T(), "TestDeleteUser",
json.MustMarshalIndent(golden))
Expand Down
32 changes: 30 additions & 2 deletions users/fixtures/TestDeleteUser.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
]
},
"UserO2 is in O1 and O2": {
"OrgAdmin UserO2 is in O1 and O2": {
"name": "UserO2",
"orgs": [
{
Expand All @@ -21,7 +21,35 @@
}
]
},
"UserO2 removed from O2": {
"AdminO1 UserO2 is in O1": {
"name": "UserO2",
"orgs": [
{
"name": "O1",
"id": "O1"
}
]
},
"AdminO2 UserO2 is in O2": {
"name": "UserO2",
"orgs": [
{
"name": "O2",
"id": "O2"
}
]
},
"AdminO2 UserO2 removed from O2": "PermissionDenied",
"AdminO1 UserO2 still in O1": {
"name": "UserO2",
"orgs": [
{
"name": "O1",
"id": "O1"
}
]
},
"OrgAdmin UserO2 removed from O2": {
"name": "UserO2",
"orgs": [
{
Expand Down
8 changes: 8 additions & 0 deletions users/fixtures/TestMakeUsers.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
{
"name": "\u003croot\u003e",
"id": "root"
},
{
"name": "O1",
"id": "O1"
},
{
"name": "O2",
"id": "O2"
}
]
},
Expand Down
49 changes: 49 additions & 0 deletions users/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@ package users_test
import (
"testing"

"github.com/Velocidex/ordereddict"
"github.com/sebdah/goldie"
"github.com/stretchr/testify/suite"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/file_store/test_utils"
"www.velocidex.com/golang/velociraptor/json"
"www.velocidex.com/golang/velociraptor/services"
"www.velocidex.com/golang/velociraptor/services/orgs"
"www.velocidex.com/golang/velociraptor/users"
"www.velocidex.com/golang/velociraptor/vtesting/assert"
)

type UserManagerTestSuite struct {
test_utils.TestSuite
}

func (self *UserManagerTestSuite) SetupTest() {
self.TestSuite.SetupTest()

self.LoadArtifacts(`name: Server.Audit.Logs
type: SERVER_EVENT
`)
}

func (self *UserManagerTestSuite) makeUserWithRoles(username, org_id, role string) {
org_manager, err := services.GetOrgManager()
assert.NoError(self.T(), err)
Expand Down Expand Up @@ -53,6 +65,43 @@ func (self *UserManagerTestSuite) makeUsers() {
self.makeUserWithRoles("UserO2", "O2", "reader")
}

// The rest of the tests depend on this state being correct. Make sure it is.
func (self *UserManagerTestSuite) TestMakeUsers() {
self.makeUsers()

golden := ordereddict.NewDict()

user_record, err := users.GetUser(self.Ctx, "OrgAdmin", "OrgAdmin")
assert.NoError(self.T(), err)
golden.Set("OrgAdmin OrgAdmin", user_record)

user_record, err = users.GetUser(self.Ctx, "OrgAdmin", "UserO1")
assert.NoError(self.T(), err)
golden.Set("OrgAdmin UserO1", user_record)

user_record, err = users.GetUser(self.Ctx, "AdminO1", "UserO1")
assert.NoError(self.T(), err)
golden.Set("AdminO1 UserO1", user_record)

user_record, err = users.GetUser(self.Ctx, "AdminO2", "UserO1")
assert.ErrorContains(self.T(), err, "PermissionDenied")
golden.Set("AdminO2 UserO1", err.Error())

user_record, err = users.GetUser(self.Ctx, "OrgAdmin", "UserO2")
assert.NoError(self.T(), err)
golden.Set("OrgAdmin UserO2", user_record)

user_record, err = users.GetUser(self.Ctx, "AdminO2", "UserO2")
assert.NoError(self.T(), err)
golden.Set("AdminO2 UserO2", user_record)

user_record, err = users.GetUser(self.Ctx, "AdminO1", "UserO2")
assert.ErrorContains(self.T(), err, "PermissionDenied")
golden.Set("AdminO1 UserO2", err.Error())

goldie.Assert(self.T(), "TestMakeUsers", json.MustMarshalIndent(golden))
}

func TestUserManger(t *testing.T) {
orgs.NonceForTest = "Nonce"

Expand Down