-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package privilege | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/client" | ||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" | ||
"github.com/datadrivers/go-nexus-client/nexus3/schema/security" | ||
) | ||
|
||
const ( | ||
securityWildcardAPIEndpoint = securityPrivilegesAPIEndpoint + "/wildcard" | ||
) | ||
|
||
type SecurityPrivilegeWildcardService struct { | ||
client *client.Client | ||
|
||
// Script *SecurityPrivilegeWildcardService | ||
} | ||
|
||
func NewSecurityPrivilegeWildcardService(c *client.Client) *SecurityPrivilegeWildcardService { | ||
return &SecurityPrivilegeWildcardService{ | ||
client: c, | ||
} | ||
} | ||
|
||
func (s *SecurityPrivilegeWildcardService) Create(p security.PrivilegeWildcard) error { | ||
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, resp, err := s.client.Post(securityWildcardAPIEndpoint, ioReader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusCreated { | ||
return fmt.Errorf("could not create wildcard privilege \"%s\": HTTP: %d, %s", p.Name, resp.StatusCode, string(body)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *SecurityPrivilegeWildcardService) Update(name string, p security.PrivilegeWildcard) error { | ||
ioReader, err := tools.JsonMarshalInterfaceToIOReader(p) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, resp, err := s.client.Put(fmt.Sprintf("%s/%s", securityWildcardAPIEndpoint, p.Name), ioReader) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != http.StatusNoContent { | ||
return fmt.Errorf("could not update wildcard privilege \"%s\": HTTP %d, %s", name, resp.StatusCode, string(body)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package privilege_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/security/privilege" | ||
"github.com/datadrivers/go-nexus-client/nexus3/pkg/tools" | ||
schemasecurity "github.com/datadrivers/go-nexus-client/nexus3/schema/security" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func getTestPrivilegeWildcard(name string, description string, pattern string) *schemasecurity.PrivilegeWildcard { | ||
return &schemasecurity.PrivilegeWildcard{ | ||
Name: name, | ||
Description: description, | ||
Pattern: pattern, | ||
} | ||
} | ||
|
||
func TestWildcardPrivilegeSecurity(t *testing.T) { | ||
privilegeWildcardName := fmt.Sprintf("wildcard-%d", tools.GetSeededRandomInteger(999)) | ||
testService := privilege.NewSecurityPrivilegeWildcardService(getTestClient()) | ||
privilegeService := privilege.NewSecurityPrivilegeService(getTestClient()) | ||
|
||
// Create wildcard-privilege object | ||
wildcardPrivilege := getTestPrivilegeWildcard(privilegeWildcardName, "demo descrp", "nexus:*") | ||
err := testService.Create(*wildcardPrivilege) | ||
assert.Nil(t, err) | ||
|
||
// Fetch recently created wildcard-privilege object and do some checks | ||
wildcardPrivilegeFetched, err := privilegeService.Get(privilegeWildcardName) | ||
assert.Nil(t, err) | ||
assert.Equal(t, privilegeWildcardName, wildcardPrivilegeFetched.Name) | ||
assert.Equal(t, "demo descrp", wildcardPrivilegeFetched.Description) | ||
assert.Equal(t, "nexus:*", wildcardPrivilegeFetched.Pattern) | ||
|
||
// Update wildcard-privilege object | ||
wildcardPrivilege = getTestPrivilegeWildcard(privilegeWildcardName, "demo descrp updated", "nexus:nexus") | ||
err = testService.Update(privilegeWildcardName, *wildcardPrivilege) | ||
assert.Nil(t, err) | ||
wildcardPrivilegeFetched, err = privilegeService.Get(privilegeWildcardName) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "demo descrp updated", wildcardPrivilegeFetched.Description) | ||
assert.Equal(t, "nexus:nexus", wildcardPrivilegeFetched.Pattern) | ||
|
||
// Delete wildcard-privilege-object | ||
err = privilegeService.Delete(privilegeWildcardName) | ||
assert.Nil(t, err) | ||
|
||
// Check for successful deletion | ||
wildcardPrivilegeFetched, err = privilegeService.Get(privilegeWildcardName) | ||
assert.Error(t, err) | ||
assert.Nil(t, wildcardPrivilegeFetched) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters