Skip to content
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
2 changes: 1 addition & 1 deletion pkg/enforcement/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type CheckRequest struct {
User User `json:"user"`
Action Action `json:"action"`
Resource Resource `json:"resource"`
Context map[string]string `json:"context"`
Context map[string]string `json:"context,omitempty"`
}

func NewCheckRequest(user User, action Action, resource Resource, context map[string]string) *CheckRequest {
Expand Down
2 changes: 1 addition & 1 deletion pkg/models/model_role_assignment_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/models/model_role_assignment_detailed_read.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/models/model_role_assignment_read.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/models/model_role_assignment_remove.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/models/model_role_assignment_resource_instance.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/models/model_user_role_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 63 additions & 39 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,21 @@ package tests
import (
"context"
"fmt"
"math/rand"
"os"
"reflect"
"testing"
"time"

"github.com/permitio/permit-golang/pkg/config"
"github.com/permitio/permit-golang/pkg/enforcement"
PermitErrors "github.com/permitio/permit-golang/pkg/errors"
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/permit"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"math/rand"
"os"
"reflect"
"testing"
"time"
)

var runId = randId()

func init() {
rand.Seed(time.Now().UnixNano())
println("Run ID: ", runId)
}

type MyResource struct {
UniqueID string
Type string
Expand Down Expand Up @@ -61,25 +55,27 @@ var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

func randId() string {
const n = 10
seed := rand.NewSource(time.Now().UnixNano())
r := rand.New(seed)
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
b[i] = letterRunes[r.Intn(len(letterRunes))]
}
return string(b)
}

func randKey(postfix string) string {
func randKey(runId, postfix string) string {
return runId + "-" + postfix
}

func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, roleKey, tenantKey, resourceKey, actionKey string) {
func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, roleKey, tenantKey, resourceKey, actionKey string, runId string) {
// Bulk (un)assignments
var users []*models.UserCreate
var bulkAssignments []models.RoleAssignmentCreate
var bulkUnAssignments []models.RoleAssignmentRemove

for i := 0; i < 3; i++ {
bulkUserKey := randKey(fmt.Sprintf("bulkuser-%d", i))
bulkUserKey := randKey(runId, fmt.Sprintf("bulkuser-%d", i))
bulkUserCreate := models.NewUserCreate(bulkUserKey)
users = append(users, models.NewUserCreate(bulkUserKey))
bulkAssignments = append(bulkAssignments, *models.NewRoleAssignmentCreate(roleKey, tenantKey, bulkUserKey))
Expand All @@ -101,7 +97,7 @@ func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, r
assert.Equal(t, roleKey, assigned[0].Role)
}

time.Sleep(6 * time.Second)
time.Sleep(15 * time.Second)
requests := make([]enforcement.CheckRequest, len(bulkAssignments))
for i, assignment := range bulkAssignments {
var tenant string
Expand Down Expand Up @@ -133,7 +129,7 @@ func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, r
assert.EqualValues(t, 3, *unassignReport.AssignmentsRemoved)
}

func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitContext, logger *zap.Logger, token string) {
func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitContext, logger *zap.Logger, token string, runId string) {
permitClient := permit.New(config.NewConfigBuilder(token).
WithPdpUrl(os.Getenv("PDP_URL")).
WithApiUrl(os.Getenv("API_URL")).
Expand All @@ -143,21 +139,21 @@ func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitCon
WithFactsSyncTimeout(10 * time.Second).
Build())

resourceKey := randKey("resource")
resourceKey := randKey(runId, "resource")
resourceCreate := *models.NewResourceCreate(resourceKey, resourceKey,
map[string]models.ActionBlockEditable{
"read": {Attributes: map[string]interface{}{"marker": "marker"}},
})
_, err := permitClient.Api.Resources.Create(ctx, resourceCreate)
assert.NoError(t, err)

roleKey := randKey("role")
roleKey := randKey(runId, "role")
roleCreate := models.NewRoleCreate(roleKey, roleKey)
roleCreate.SetPermissions([]string{fmt.Sprintf("%s:read", resourceKey)})
_, err = permitClient.Api.Roles.Create(ctx, *roleCreate)
assert.NoError(t, err)

userKey := randKey("user")
userKey := randKey(runId, "user")
userCreate := *models.NewUserCreate(userKey)
userCreate.SetFirstName("John")
userCreate.SetLastName("Doe")
Expand All @@ -172,21 +168,49 @@ func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitCon
assert.NoError(t, err)
assert.True(t, allowed)
}
func TestIntegration(t *testing.T) {

func TestFactsIntegration(t *testing.T) {
logger := zap.NewExample()
ctx := context.Background()
runId := randId()
t.Log("Run ID: ", runId)
project := os.Getenv("PROJECT")

userKey := randKey("user")
resourceKey := randKey("resource")
roleKey := randKey("role")
marker := randKey("marker")
actionKey := randKey("action")
actionGroupKey := randKey("actiongroup")
tenantKey := randKey("tenant-1")
secondTenantKey := randKey("tenant-2")
resourceSetKey := randKey("resourceset")
userSetKey := randKey("userset")
proxyConfigKey := randKey("proxyconfig")
if project == "" {
t.Fatal("PROJECT is not set")
}

env := os.Getenv("ENV")

if env == "" {
t.Fatal("ENV is not set")
}

token := os.Getenv("PDP_API_KEY")
if token == "" {
t.Fatal("PDP_API_KEY is not set")
}
permitContext := config.NewPermitContext(config.EnvironmentAPIKeyLevel, project, env)

// Test Facts API
factsApi(ctx, t, permitContext, logger, token, runId)
}
func TestIntegration(t *testing.T) {
logger := zap.NewExample()
ctx := context.Background()
runId := randId()
t.Log("Run ID: ", runId)
userKey := randKey(runId, "user")
resourceKey := randKey(runId, "resource")
roleKey := randKey(runId, "role")
marker := randKey(runId, "marker")
actionKey := randKey(runId, "action")
actionGroupKey := randKey(runId, "actiongroup")
tenantKey := randKey(runId, "tenant-1")
secondTenantKey := randKey(runId, "tenant-2")
resourceSetKey := randKey(runId, "resourceset")
userSetKey := randKey(runId, "userset")
proxyConfigKey := randKey(runId, "proxyconfig")

project := os.Getenv("PROJECT")

Expand All @@ -212,9 +236,6 @@ func TestIntegration(t *testing.T) {
WithLogger(logger).
Build())

// Test Facts API
factsApi(ctx, t, permitContext, logger, token)

// Create a user
userCreate := *models.NewUserCreate(userKey)
userCreate.SetFirstName("John")
Expand Down Expand Up @@ -344,7 +365,7 @@ func TestIntegration(t *testing.T) {
// Assign role to user
_, err = permitClient.Api.Users.AssignRole(ctx, userKey, roleKey, tenantKey)
assert.NoError(t, err)
time.Sleep(30 * time.Second)
time.Sleep(15 * time.Second)

// Testing List Tenants Users
// Note - Dependent on the user creation above -- consider decoupling this test from the user creation
Expand All @@ -366,7 +387,7 @@ func TestIntegration(t *testing.T) {
detailedRAs, err := permitClient.Api.RoleAssignments.ListDetailed(ctx, 1, 100, userKey, roleKey, tenantKey)
assert.NoError(t, err)
assert.Equal(t, 1, len(*detailedRAs))
checkBulk(ctx, t, permitClient, roleKey, tenantKey, resourceKey, "read")
checkBulk(ctx, t, permitClient, roleKey, tenantKey, resourceKey, "read", runId)

userSetCreate := *models.NewConditionSetCreate(userSetKey, userSetKey)
userSetCreate.SetType(models.USERSET)
Expand Down Expand Up @@ -407,6 +428,7 @@ func TestIntegration(t *testing.T) {
csUpdate.SetDescription("Top Secrets")
cs, err := permitClient.Api.ConditionSets.Update(ctx, resourceSetKey, csUpdate)
assert.NoError(t, err)
assert.NotNil(t, cs)
assert.Equal(t, "Top Secrets", *cs.Description)

_, err = permitClient.Api.ConditionSets.AssignSetPermissions(ctx, userSetKey, resourceKey+":"+actionKey, resourceSetKey)
Expand Down Expand Up @@ -446,6 +468,7 @@ func TestIntegration(t *testing.T) {
"read",
resourceCheck.WithTenant("").Build(),
)
assert.NoError(t, err)
assert.Len(t, allowedTenants, 1)
assert.Equal(t, tenantKey, allowedTenants[0].Key)
assert.True(t, assert.ObjectsAreEqualValues(allowedTenants[0].Attributes, tenantCreate.Attributes))
Expand Down Expand Up @@ -475,5 +498,6 @@ func TestIntegration(t *testing.T) {
proxyConfigUpdate.SetAuthMechanism(authMechanism)
proxyConfigUpdate.SetSecret(secret)
proxyConfigUpdate.SetMappingRules(mappingRules)
_, err = permitClient.Api.ProxyConfigs.Update(ctx, "pxcf", *proxyConfigUpdate)
_, err = permitClient.Api.ProxyConfigs.Update(ctx, proxyConfigKey, *proxyConfigUpdate)
assert.NoError(t, err)
}
Loading