Skip to content

Commit

Permalink
Merge pull request #33 from d-strobel/refactor/function-parameters
Browse files Browse the repository at this point in the history
Refactor/function parameters
  • Loading branch information
d-strobel authored Jan 5, 2024
2 parents 59e9946 + 1d34fe5 commit 566c4d2
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 263 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {
defer cancel()

// Run the GroupRead function to retrieve a local Windows group
group, err := c.GroupRead(ctx, local.GroupParams{Name: "Users"})
group, err := c.GroupRead(ctx, local.GroupReadParams{Name: "Users"})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func main() {
defer cancel()

// Run the GroupRead function to retrieve a local Windows group
group, err := c.Local.GroupRead(ctx, local.GroupParams{Name: "Users"})
group, err := c.Local.GroupRead(ctx, local.GroupReadParams{Name: "Users"})
if err != nil {
panic(err)
}
Expand Down
68 changes: 39 additions & 29 deletions windows/local/local_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,17 @@ type Group struct {
SID SID `json:"SID"`
}

// GroupParams represents parameters for interacting with local groups, including creation, updating, and deletion.
type GroupParams struct {
// Specifies a name for the group.
// The maximum length is 256 characters.
// GroupReadParams represents parameters for the GroupRead function.
type GroupReadParams struct {
// Specifies the name of the group.
Name string

// Specifies a comment for the group.
// The maximum length is 48 characters.
Description string

// Specifies the security ID (SID) of the security group.
SID string
}

// GroupRead gets a local group by SID or Name and returns a Group object.
//
// Accepted GroupParams:
// - Name
// - SID
func (c *LocalClient) GroupRead(ctx context.Context, params GroupParams) (Group, error) {
func (c *LocalClient) GroupRead(ctx context.Context, params GroupReadParams) (Group, error) {

// Declare Group object
var g Group
Expand Down Expand Up @@ -86,12 +77,19 @@ func (c *LocalClient) GroupList(ctx context.Context) ([]Group, error) {
return g, nil
}

// GroupCreateParams represents parameters for the GroupCreate function.
type GroupCreateParams struct {
// Specifies a name for the group.
// The maximum length is 256 characters.
Name string

// Specifies a comment for the group.
// The maximum length is 48 characters.
Description string
}

// GroupCreate creates a new local group and returns the Group object.
//
// Accepted GroupParams:
// - Name
// - Description
func (c *LocalClient) GroupCreate(ctx context.Context, params GroupParams) (Group, error) {
func (c *LocalClient) GroupCreate(ctx context.Context, params GroupCreateParams) (Group, error) {

// Declare Group object
var g Group
Expand Down Expand Up @@ -122,13 +120,20 @@ func (c *LocalClient) GroupCreate(ctx context.Context, params GroupParams) (Grou
return g, nil
}

// GroupUpdateParams represents parameters for the GroupUpdate function.
type GroupUpdateParams struct {
// Specifies the name of the group.
Name string

// Specifies a comment for the group.
Description string

// Specifies the security ID (SID) of the security group.
SID string
}

// GroupUpdate updates a local group.
//
// Accepted GroupParams:
// - Name
// - SID
// - Description
func (c *LocalClient) GroupUpdate(ctx context.Context, params GroupParams) error {
func (c *LocalClient) GroupUpdate(ctx context.Context, params GroupUpdateParams) error {

// Satisfy groupType interface
var g Group
Expand Down Expand Up @@ -160,12 +165,17 @@ func (c *LocalClient) GroupUpdate(ctx context.Context, params GroupParams) error
return nil
}

// GroupDeleteParams represents parameters for the GroupDelete function.
type GroupDeleteParams struct {
// Specifies the name of the group.
Name string

// Specifies the security ID (SID) of the security group.
SID string
}

// GroupDelete removes a local group by SID or Name.
//
// Accepted GroupParams:
// - Name
// - SID
func (c *LocalClient) GroupDelete(ctx context.Context, params GroupParams) error {
func (c *LocalClient) GroupDelete(ctx context.Context, params GroupDeleteParams) error {

// Satisfy groupType interface
var g Group
Expand Down
8 changes: 4 additions & 4 deletions windows/local/local_group_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (suite *LocalAccTestSuite) TestGroup1Read() {
defer cancel()

for _, c := range suite.clients {
params := local.GroupParams{
params := local.GroupReadParams{
Name: "Users",
}
g, err := c.GroupRead(ctx, params)
Expand Down Expand Up @@ -58,7 +58,7 @@ func (suite *LocalAccTestSuite) TestGroup3Create() {
defer cancel()

for i, c := range suite.clients {
params := local.GroupParams{
params := local.GroupCreateParams{
Name: fmt.Sprintf("Test-Group-%d", i),
Description: "This is a test group",
}
Expand All @@ -74,7 +74,7 @@ func (suite *LocalAccTestSuite) TestGroup4Update() {
defer cancel()

for i, c := range suite.clients {
params := local.GroupParams{
params := local.GroupUpdateParams{
Name: fmt.Sprintf("Test-Group-%d", i),
Description: "This is a test group updated",
}
Expand All @@ -88,7 +88,7 @@ func (suite *LocalAccTestSuite) TestGroup5Delete() {
defer cancel()

for i, c := range suite.clients {
params := local.GroupParams{
params := local.GroupDeleteParams{
Name: fmt.Sprintf("Test-Group-%d", i),
}
err := c.GroupDelete(ctx, params)
Expand Down
70 changes: 30 additions & 40 deletions windows/local/local_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (suite *LocalUnitTestSuite) TestGroupRead() {
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{
StdOut: usersGroup,
}, nil)
actualUsersGroup, err := c.GroupRead(ctx, GroupParams{Name: "Users"})
actualUsersGroup, err := c.GroupRead(ctx, GroupReadParams{Name: "Users"})
suite.Require().NoError(err)
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand All @@ -75,22 +75,22 @@ func (suite *LocalUnitTestSuite) TestGroupRead() {
suite.Run("should run the correct command", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupReadParams
expectedCMD string
}{
{
"assert users group by name",
GroupParams{Name: "Users"},
GroupReadParams{Name: "Users"},
"Get-LocalGroup -Name 'Users' | ConvertTo-Json -Compress",
},
{
"assert users group by sid",
GroupParams{SID: "123456789"},
GroupReadParams{SID: "123456789"},
"Get-LocalGroup -SID 123456789 | ConvertTo-Json -Compress",
},
{
"assert users group by name and sid",
GroupParams{Name: "Users", SID: "123456789"},
GroupReadParams{Name: "Users", SID: "123456789"},
"Get-LocalGroup -SID 123456789 | ConvertTo-Json -Compress",
},
}
Expand All @@ -116,22 +116,17 @@ func (suite *LocalUnitTestSuite) TestGroupRead() {
suite.Run("should return specific errors", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupReadParams
expectedErr string
}{
{
"assert error with empty parameters",
GroupParams{},
"windows.local.GroupRead: group parameter 'Name' or 'SID' must be set",
},
{
"assert error with just the description parameter",
GroupParams{Description: "test"},
GroupReadParams{},
"windows.local.GroupRead: group parameter 'Name' or 'SID' must be set",
},
{
"assert error when name contains wildcard",
GroupParams{Name: "Remote*"},
GroupReadParams{Name: "Remote*"},
"windows.local.GroupRead: group parameter 'Name' does not allow wildcards",
},
}
Expand Down Expand Up @@ -164,7 +159,7 @@ func (suite *LocalUnitTestSuite) TestGroupRead() {
}
expectedCMD := "Get-LocalGroup -Name 'Users' | ConvertTo-Json -Compress"
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{}, errors.New("test-error"))
_, err := c.GroupRead(ctx, GroupParams{Name: "Users"})
_, err := c.GroupRead(ctx, GroupReadParams{Name: "Users"})
suite.EqualError(err, "windows.local.GroupRead: test-error")
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand Down Expand Up @@ -226,7 +221,7 @@ func (suite *LocalUnitTestSuite) TestGroupCreate() {
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{
StdOut: testGroup,
}, nil)
actualTestGroup, err := c.GroupCreate(ctx, GroupParams{Name: "Test", Description: "Test group"})
actualTestGroup, err := c.GroupCreate(ctx, GroupCreateParams{Name: "Test", Description: "Test group"})
suite.Require().NoError(err)
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand All @@ -236,17 +231,17 @@ func (suite *LocalUnitTestSuite) TestGroupCreate() {
suite.Run("should run the correct command", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupCreateParams
expectedCMD string
}{
{
"assert without description parameter",
GroupParams{Name: "Test"},
GroupCreateParams{Name: "Test"},
"New-LocalGroup -Name 'Test' | ConvertTo-Json -Compress",
},
{
"assert with name and description parameter",
GroupParams{Name: "Test", Description: "Test group"},
GroupCreateParams{Name: "Test", Description: "Test group"},
"New-LocalGroup -Name 'Test' -Description 'Test group' | ConvertTo-Json -Compress",
},
}
Expand Down Expand Up @@ -280,7 +275,7 @@ func (suite *LocalUnitTestSuite) TestGroupCreate() {
}
expectedCMD := "New-LocalGroup -Name 'Test' | ConvertTo-Json -Compress"
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{}, errors.New("test-error"))
_, err := c.GroupCreate(ctx, GroupParams{Name: "Test"})
_, err := c.GroupCreate(ctx, GroupCreateParams{Name: "Test"})
suite.EqualError(err, "windows.local.GroupCreate: test-error")
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand All @@ -292,27 +287,27 @@ func (suite *LocalUnitTestSuite) TestGroupUpdate() {
suite.Run("should run the correct command", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupUpdateParams
expectedCMD string
}{
{
"assert with Name and Desctiption parameter",
GroupParams{Name: "Test", Description: "Testing"},
GroupUpdateParams{Name: "Test", Description: "Testing"},
"Set-LocalGroup -Name 'Test' -Description 'Testing'",
},
{
"assert with SID and Desctiption parameter",
GroupParams{SID: "S-12345", Description: "Testing"},
GroupUpdateParams{SID: "S-12345", Description: "Testing"},
"Set-LocalGroup -SID S-12345 -Description 'Testing'",
},
{
"assert with Name, SID and Desctiption parameter",
GroupParams{Name: "Test", SID: "S-12345", Description: "Testing"},
GroupUpdateParams{Name: "Test", SID: "S-12345", Description: "Testing"},
"Set-LocalGroup -SID S-12345 -Description 'Testing'",
},
{
"assert with Name parameter",
GroupParams{Name: "Test"},
GroupUpdateParams{Name: "Test"},
"Set-LocalGroup -Name 'Test' -Description ''",
},
}
Expand All @@ -338,17 +333,17 @@ func (suite *LocalUnitTestSuite) TestGroupUpdate() {
suite.Run("should return specific errors", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupUpdateParams
expectedErr string
}{
{
"assert error with empty parameters",
GroupParams{},
GroupUpdateParams{},
"windows.local.GroupUpdate: group parameter 'Name' or 'SID' must be set",
},
{
"assert error with just the description parameter",
GroupParams{Description: "test"},
GroupUpdateParams{Description: "test"},
"windows.local.GroupUpdate: group parameter 'Name' or 'SID' must be set",
},
}
Expand Down Expand Up @@ -381,7 +376,7 @@ func (suite *LocalUnitTestSuite) TestGroupUpdate() {
}
expectedCMD := "Set-LocalGroup -Name 'Test' -Description 'Test'"
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{}, errors.New("test-error"))
err := c.GroupUpdate(ctx, GroupParams{Name: "Test", Description: "Test"})
err := c.GroupUpdate(ctx, GroupUpdateParams{Name: "Test", Description: "Test"})
suite.EqualError(err, "windows.local.GroupUpdate: test-error")
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand All @@ -393,22 +388,22 @@ func (suite *LocalUnitTestSuite) TestGroupDelete() {
suite.Run("should run the correct command", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupDeleteParams
expectedCMD string
}{
{
"assert with Name parameter",
GroupParams{Name: "Test"},
GroupDeleteParams{Name: "Test"},
"Remove-LocalGroup -Name 'Test'",
},
{
"assert with SID parameter",
GroupParams{SID: "S-12345"},
GroupDeleteParams{SID: "S-12345"},
"Remove-LocalGroup -SID S-12345",
},
{
"assert with Name and SID parameter",
GroupParams{Name: "Test", SID: "S-12345"},
GroupDeleteParams{Name: "Test", SID: "S-12345"},
"Remove-LocalGroup -SID S-12345",
},
}
Expand All @@ -434,17 +429,12 @@ func (suite *LocalUnitTestSuite) TestGroupDelete() {
suite.Run("should return specific errors", func() {
tcs := []struct {
description string
inputParameters GroupParams
inputParameters GroupDeleteParams
expectedErr string
}{
{
"assert error with empty parameters",
GroupParams{},
"windows.local.GroupDelete: group parameter 'Name' or 'SID' must be set",
},
{
"assert error with just the description parameter",
GroupParams{Description: "test"},
GroupDeleteParams{},
"windows.local.GroupDelete: group parameter 'Name' or 'SID' must be set",
},
}
Expand Down Expand Up @@ -477,7 +467,7 @@ func (suite *LocalUnitTestSuite) TestGroupDelete() {
}
expectedCMD := "Remove-LocalGroup -Name 'Test'"
mockConn.On("Run", ctx, expectedCMD).Return(connection.CMDResult{}, errors.New("test-error"))
err := c.GroupDelete(ctx, GroupParams{Name: "Test"})
err := c.GroupDelete(ctx, GroupDeleteParams{Name: "Test"})
suite.EqualError(err, "windows.local.GroupDelete: test-error")
mockConn.AssertCalled(suite.T(), "Run", ctx, expectedCMD)
mockParser.AssertNotCalled(suite.T(), "DecodeCLIXML")
Expand Down
Loading

0 comments on commit 566c4d2

Please sign in to comment.