Skip to content

Commit

Permalink
refactored and added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Saurabh Agarwal <[email protected]>
  • Loading branch information
saurabh-io committed Aug 3, 2023
1 parent be014b6 commit 29fe64c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func NewResourceActionRoles(resource, httpMethod string, roles []string) Resourc
return ResourceActionRoles{}
}

//res, _ := strings.CutPrefix(resource, "/")
return newResourceActionRoles(resource, httpMethod, roles)
}

Expand All @@ -34,7 +33,6 @@ func NewResourceActionUriRoles(resource, actionUri string, roles []string) Resou
return ResourceActionRoles{}
}

//res, _ := strings.CutPrefix(resource, "/")
return newResourceActionRoles(resource, httpMethod, roles)
}

Expand Down Expand Up @@ -109,6 +107,12 @@ func SanitizeMembers(members []string) []string {
}

func makeRarKey(action, resource, actionPrefix string) string {
// TODO - Check if resource itself is "/"
if strings.TrimSpace(resource) == "" || strings.TrimSpace(action) == "" {
log.Warn("makeRarKey empty resource or action", "resource", resource, "action", action)
return ""
}

resNoPrefix, _ := strings.CutPrefix(resource, "/")
httpMethod := getHttpMethod(action, actionPrefix)
if httpMethod == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,65 @@ import (

const actionUri = "http:GET"

func TestMakeRarKeyForPolicy(t *testing.T) {
aKey := providerscommon.MakeRarKeyForPolicy(actionUri, "/humanresources/us")
assert.Equal(t, "resrol-httpget-humanresources-us", aKey)
func TestNewResourceActionRoles_Invalid(t *testing.T) {
act := providerscommon.NewResourceActionRoles("/some", "INVALID", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRoles("/some", "http:GET", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRoles("/some", "httpget", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRoles(" ", http.MethodGet, []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)
}

func TestNameValue(t *testing.T) {
resRole := providerscommon.NewResourceActionUriRoles("/humanresources/us", actionUri, []string{"some-role"})
assert.Equal(t, "resrol-httpget-humanresources-us", resRole.Name())
assert.Equal(t, `["some-role"]`, resRole.Value())
func TestNewResourceActionRoles_Success(t *testing.T) {
act := providerscommon.NewResourceActionRoles("/some", http.MethodGet, []string{"mem1", "mem2"})
assert.Equal(t, providerscommon.ResourceActionRoles{
Action: http.MethodGet,
Resource: "/some",
Roles: []string{"mem1", "mem2"},
}, act)
}

func TestNewResourceActionUriRoles_InvalidMethod(t *testing.T) {
act := providerscommon.NewResourceActionUriRoles("/some", "INVALID", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionUriRoles("/some", http.MethodGet, []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionUriRoles("/some", "httpget", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)
}

func TestNewResourceActionUriRoles_Success(t *testing.T) {
act := providerscommon.NewResourceActionUriRoles("/some", "http:GET", []string{"mem1", "mem2"})
assert.Equal(t, providerscommon.ResourceActionRoles{
Action: http.MethodGet,
Resource: "/some",
Roles: []string{"mem1", "mem2"},
}, act)
}

func TestNewResourceActionRolesFromProviderValue_Invalid(t *testing.T) {
act := providerscommon.NewResourceActionRolesFromProviderValue("invalid", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRolesFromProviderValue("badprefix-httpget-some", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRolesFromProviderValue("resrol-httpbadmethod-some", []string{})
assert.Equal(t, providerscommon.ResourceActionRoles{}, act)

act = providerscommon.NewResourceActionRolesFromProviderValue("resrol-httpget-some", []string{"mem1", "mem2"})
assert.Equal(t, providerscommon.ResourceActionRoles{
Action: http.MethodGet,
Resource: "/some",
Roles: []string{"mem1", "mem2"},
}, act)
}

func TestNewResourceActionRolesFromProviderValue(t *testing.T) {
Expand All @@ -27,3 +77,25 @@ func TestNewResourceActionRolesFromProviderValue(t *testing.T) {
assert.Equal(t, "/humanresources/us", act.Resource)
assert.Equal(t, []string{"some-role"}, act.Roles)
}

func TestMakeRarKeyForPolicy_Invalid(t *testing.T) {
aKey := providerscommon.MakeRarKeyForPolicy(" ", "/humanresources/us")
assert.Equal(t, "", aKey)

aKey = providerscommon.MakeRarKeyForPolicy(actionUri, " ")
assert.Equal(t, "", aKey)

aKey = providerscommon.MakeRarKeyForPolicy(http.MethodGet, "/humanresources/us")
assert.Equal(t, "", aKey)
}

func TestMakeRarKeyForPolicy(t *testing.T) {
aKey := providerscommon.MakeRarKeyForPolicy(actionUri, "/humanresources/us")
assert.Equal(t, "resrol-httpget-humanresources-us", aKey)
}

func TestNameValue(t *testing.T) {
resRole := providerscommon.NewResourceActionUriRoles("/humanresources/us", actionUri, []string{"some-role"})
assert.Equal(t, "resrol-httpget-humanresources-us", resRole.Name())
assert.Equal(t, `["some-role"]`, resRole.Value())
}

0 comments on commit 29fe64c

Please sign in to comment.