Add List Actions (#1861)

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
This commit is contained in:
b1ackd0t 2023-07-18 17:31:31 +03:00 committed by GitHub
parent aee0081864
commit 41be96a6e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 0 deletions

View File

@ -5,6 +5,7 @@ package policies
import (
"context"
"strings"
"time"
"github.com/mainflux/mainflux/internal/apiutil"
@ -160,3 +161,50 @@ func ValidateAction(act string) bool {
return false
}
// addListAction adds list actions to the actions slice if c_ or g_ actions are present.
//
// 1. If c_<anything> actions are present, add c_list and g_list actions to the actions slice.
//
// 2. If g_<anything> actions are present, add g_list action to the actions slice.
func addListAction(actions []string) []string {
hasCAction := false
hasGAction := false
for _, action := range actions {
if strings.HasPrefix(action, "c_") {
hasCAction = true
}
if strings.HasPrefix(action, "g_") {
hasGAction = true
}
}
updatedActions := make([]string, 0)
updatedActions = append(updatedActions, actions...)
if hasCAction {
updatedActions = append(updatedActions, "c_list", "g_list")
}
if hasGAction {
updatedActions = append(updatedActions, "g_list")
}
return removeDuplicates(updatedActions)
}
func removeDuplicates(slice []string) []string {
unique := make(map[string]bool)
result := make([]string, 0, len(slice))
for _, item := range slice {
if !unique[item] {
unique[item] = true
result = append(result, item)
}
}
return result
}

View File

@ -64,6 +64,7 @@ func (svc service) AddPolicy(ctx context.Context, token string, p Policy) error
if err := p.Validate(); err != nil {
return err
}
p.Actions = addListAction(p.Actions)
p.OwnerID = id
p.CreatedAt = time.Now()

View File

@ -84,6 +84,50 @@ func TestAddPolicy(t *testing.T) {
err: nil,
token: testsutil.GenerateValidToken(t, testsutil.GenerateUUID(t, idProvider), csvc, cRepo, phasher),
},
{
desc: "add a new policy with c_update action",
page: policies.PolicyPage{},
policy: policies.Policy{
Subject: testsutil.GenerateUUID(t, idProvider),
Object: testsutil.GenerateUUID(t, idProvider),
Actions: []string{"c_update"},
},
err: nil,
token: testsutil.GenerateValidToken(t, testsutil.GenerateUUID(t, idProvider), csvc, cRepo, phasher),
},
{
desc: "add a new policy with c_update and c_list action",
page: policies.PolicyPage{},
policy: policies.Policy{
Subject: testsutil.GenerateUUID(t, idProvider),
Object: testsutil.GenerateUUID(t, idProvider),
Actions: []string{"c_update", "c_list"},
},
err: nil,
token: testsutil.GenerateValidToken(t, testsutil.GenerateUUID(t, idProvider), csvc, cRepo, phasher),
},
{
desc: "add a new policy with g_update action",
page: policies.PolicyPage{},
policy: policies.Policy{
Subject: testsutil.GenerateUUID(t, idProvider),
Object: testsutil.GenerateUUID(t, idProvider),
Actions: []string{"g_update"},
},
err: nil,
token: testsutil.GenerateValidToken(t, testsutil.GenerateUUID(t, idProvider), csvc, cRepo, phasher),
},
{
desc: "add a new policy with g_update and g_list action",
page: policies.PolicyPage{},
policy: policies.Policy{
Subject: testsutil.GenerateUUID(t, idProvider),
Object: testsutil.GenerateUUID(t, idProvider),
Actions: []string{"g_update", "g_list"},
},
err: nil,
token: testsutil.GenerateValidToken(t, testsutil.GenerateUUID(t, idProvider), csvc, cRepo, phasher),
},
{
desc: "add a new policy with more actions",
page: policies.PolicyPage{},