@@ -3,27 +3,21 @@ package tests
33import (
44 "context"
55 "fmt"
6+ "math/rand"
7+ "os"
8+ "reflect"
9+ "testing"
10+ "time"
11+
612 "github.com/permitio/permit-golang/pkg/config"
713 "github.com/permitio/permit-golang/pkg/enforcement"
814 PermitErrors "github.com/permitio/permit-golang/pkg/errors"
915 "github.com/permitio/permit-golang/pkg/models"
1016 "github.com/permitio/permit-golang/pkg/permit"
1117 "github.com/stretchr/testify/assert"
1218 "go.uber.org/zap"
13- "math/rand"
14- "os"
15- "reflect"
16- "testing"
17- "time"
1819)
1920
20- var runId = randId ()
21-
22- func init () {
23- rand .Seed (time .Now ().UnixNano ())
24- println ("Run ID: " , runId )
25- }
26-
2721type MyResource struct {
2822 UniqueID string
2923 Type string
@@ -61,25 +55,27 @@ var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
6155
6256func randId () string {
6357 const n = 10
58+ seed := rand .NewSource (time .Now ().UnixNano ())
59+ r := rand .New (seed )
6460 b := make ([]rune , n )
6561 for i := range b {
66- b [i ] = letterRunes [rand .Intn (len (letterRunes ))]
62+ b [i ] = letterRunes [r .Intn (len (letterRunes ))]
6763 }
6864 return string (b )
6965}
7066
71- func randKey (postfix string ) string {
67+ func randKey (runId , postfix string ) string {
7268 return runId + "-" + postfix
7369}
7470
75- func checkBulk (ctx context.Context , t * testing.T , permitClient * permit.Client , roleKey , tenantKey , resourceKey , actionKey string ) {
71+ func checkBulk (ctx context.Context , t * testing.T , permitClient * permit.Client , roleKey , tenantKey , resourceKey , actionKey string , runId string ) {
7672 // Bulk (un)assignments
7773 var users []* models.UserCreate
7874 var bulkAssignments []models.RoleAssignmentCreate
7975 var bulkUnAssignments []models.RoleAssignmentRemove
8076
8177 for i := 0 ; i < 3 ; i ++ {
82- bulkUserKey := randKey (fmt .Sprintf ("bulkuser-%d" , i ))
78+ bulkUserKey := randKey (runId , fmt .Sprintf ("bulkuser-%d" , i ))
8379 bulkUserCreate := models .NewUserCreate (bulkUserKey )
8480 users = append (users , models .NewUserCreate (bulkUserKey ))
8581 bulkAssignments = append (bulkAssignments , * models .NewRoleAssignmentCreate (roleKey , tenantKey , bulkUserKey ))
@@ -101,7 +97,7 @@ func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, r
10197 assert .Equal (t , roleKey , assigned [0 ].Role )
10298 }
10399
104- time .Sleep (6 * time .Second )
100+ time .Sleep (15 * time .Second )
105101 requests := make ([]enforcement.CheckRequest , len (bulkAssignments ))
106102 for i , assignment := range bulkAssignments {
107103 var tenant string
@@ -133,7 +129,7 @@ func checkBulk(ctx context.Context, t *testing.T, permitClient *permit.Client, r
133129 assert .EqualValues (t , 3 , * unassignReport .AssignmentsRemoved )
134130}
135131
136- func factsApi (ctx context.Context , t * testing.T , permitContext * config.PermitContext , logger * zap.Logger , token string ) {
132+ func factsApi (ctx context.Context , t * testing.T , permitContext * config.PermitContext , logger * zap.Logger , token string , runId string ) {
137133 permitClient := permit .New (config .NewConfigBuilder (token ).
138134 WithPdpUrl (os .Getenv ("PDP_URL" )).
139135 WithApiUrl (os .Getenv ("API_URL" )).
@@ -143,21 +139,21 @@ func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitCon
143139 WithFactsSyncTimeout (10 * time .Second ).
144140 Build ())
145141
146- resourceKey := randKey ("resource" )
142+ resourceKey := randKey (runId , "resource" )
147143 resourceCreate := * models .NewResourceCreate (resourceKey , resourceKey ,
148144 map [string ]models.ActionBlockEditable {
149145 "read" : {Attributes : map [string ]interface {}{"marker" : "marker" }},
150146 })
151147 _ , err := permitClient .Api .Resources .Create (ctx , resourceCreate )
152148 assert .NoError (t , err )
153149
154- roleKey := randKey ("role" )
150+ roleKey := randKey (runId , "role" )
155151 roleCreate := models .NewRoleCreate (roleKey , roleKey )
156152 roleCreate .SetPermissions ([]string {fmt .Sprintf ("%s:read" , resourceKey )})
157153 _ , err = permitClient .Api .Roles .Create (ctx , * roleCreate )
158154 assert .NoError (t , err )
159155
160- userKey := randKey ("user" )
156+ userKey := randKey (runId , "user" )
161157 userCreate := * models .NewUserCreate (userKey )
162158 userCreate .SetFirstName ("John" )
163159 userCreate .SetLastName ("Doe" )
@@ -172,21 +168,49 @@ func factsApi(ctx context.Context, t *testing.T, permitContext *config.PermitCon
172168 assert .NoError (t , err )
173169 assert .True (t , allowed )
174170}
175- func TestIntegration (t * testing.T ) {
171+
172+ func TestFactsIntegration (t * testing.T ) {
176173 logger := zap .NewExample ()
177174 ctx := context .Background ()
175+ runId := randId ()
176+ t .Log ("Run ID: " , runId )
177+ project := os .Getenv ("PROJECT" )
178178
179- userKey := randKey ("user" )
180- resourceKey := randKey ("resource" )
181- roleKey := randKey ("role" )
182- marker := randKey ("marker" )
183- actionKey := randKey ("action" )
184- actionGroupKey := randKey ("actiongroup" )
185- tenantKey := randKey ("tenant-1" )
186- secondTenantKey := randKey ("tenant-2" )
187- resourceSetKey := randKey ("resourceset" )
188- userSetKey := randKey ("userset" )
189- proxyConfigKey := randKey ("proxyconfig" )
179+ if project == "" {
180+ t .Fatal ("PROJECT is not set" )
181+ }
182+
183+ env := os .Getenv ("ENV" )
184+
185+ if env == "" {
186+ t .Fatal ("ENV is not set" )
187+ }
188+
189+ token := os .Getenv ("PDP_API_KEY" )
190+ if token == "" {
191+ t .Fatal ("PDP_API_KEY is not set" )
192+ }
193+ permitContext := config .NewPermitContext (config .EnvironmentAPIKeyLevel , project , env )
194+
195+ // Test Facts API
196+ factsApi (ctx , t , permitContext , logger , token , runId )
197+ }
198+ func TestIntegration (t * testing.T ) {
199+ logger := zap .NewExample ()
200+ ctx := context .Background ()
201+ runId := randId ()
202+ t .Log ("Run ID: " , runId )
203+ userKey := randKey (runId , "user" )
204+ resourceKey := randKey (runId , "resource" )
205+ roleKey := randKey (runId , "role" )
206+ marker := randKey (runId , "marker" )
207+ actionKey := randKey (runId , "action" )
208+ actionGroupKey := randKey (runId , "actiongroup" )
209+ tenantKey := randKey (runId , "tenant-1" )
210+ secondTenantKey := randKey (runId , "tenant-2" )
211+ resourceSetKey := randKey (runId , "resourceset" )
212+ userSetKey := randKey (runId , "userset" )
213+ proxyConfigKey := randKey (runId , "proxyconfig" )
190214
191215 project := os .Getenv ("PROJECT" )
192216
@@ -212,9 +236,6 @@ func TestIntegration(t *testing.T) {
212236 WithLogger (logger ).
213237 Build ())
214238
215- // Test Facts API
216- factsApi (ctx , t , permitContext , logger , token )
217-
218239 // Create a user
219240 userCreate := * models .NewUserCreate (userKey )
220241 userCreate .SetFirstName ("John" )
@@ -344,7 +365,7 @@ func TestIntegration(t *testing.T) {
344365 // Assign role to user
345366 _ , err = permitClient .Api .Users .AssignRole (ctx , userKey , roleKey , tenantKey )
346367 assert .NoError (t , err )
347- time .Sleep (30 * time .Second )
368+ time .Sleep (15 * time .Second )
348369
349370 // Testing List Tenants Users
350371 // Note - Dependent on the user creation above -- consider decoupling this test from the user creation
@@ -366,7 +387,7 @@ func TestIntegration(t *testing.T) {
366387 detailedRAs , err := permitClient .Api .RoleAssignments .ListDetailed (ctx , 1 , 100 , userKey , roleKey , tenantKey )
367388 assert .NoError (t , err )
368389 assert .Equal (t , 1 , len (* detailedRAs ))
369- checkBulk (ctx , t , permitClient , roleKey , tenantKey , resourceKey , "read" )
390+ checkBulk (ctx , t , permitClient , roleKey , tenantKey , resourceKey , "read" , runId )
370391
371392 userSetCreate := * models .NewConditionSetCreate (userSetKey , userSetKey )
372393 userSetCreate .SetType (models .USERSET )
@@ -407,6 +428,7 @@ func TestIntegration(t *testing.T) {
407428 csUpdate .SetDescription ("Top Secrets" )
408429 cs , err := permitClient .Api .ConditionSets .Update (ctx , resourceSetKey , csUpdate )
409430 assert .NoError (t , err )
431+ assert .NotNil (t , cs )
410432 assert .Equal (t , "Top Secrets" , * cs .Description )
411433
412434 _ , err = permitClient .Api .ConditionSets .AssignSetPermissions (ctx , userSetKey , resourceKey + ":" + actionKey , resourceSetKey )
@@ -446,6 +468,7 @@ func TestIntegration(t *testing.T) {
446468 "read" ,
447469 resourceCheck .WithTenant ("" ).Build (),
448470 )
471+ assert .NoError (t , err )
449472 assert .Len (t , allowedTenants , 1 )
450473 assert .Equal (t , tenantKey , allowedTenants [0 ].Key )
451474 assert .True (t , assert .ObjectsAreEqualValues (allowedTenants [0 ].Attributes , tenantCreate .Attributes ))
@@ -475,5 +498,6 @@ func TestIntegration(t *testing.T) {
475498 proxyConfigUpdate .SetAuthMechanism (authMechanism )
476499 proxyConfigUpdate .SetSecret (secret )
477500 proxyConfigUpdate .SetMappingRules (mappingRules )
478- _ , err = permitClient .Api .ProxyConfigs .Update (ctx , "pxcf" , * proxyConfigUpdate )
501+ _ , err = permitClient .Api .ProxyConfigs .Update (ctx , proxyConfigKey , * proxyConfigUpdate )
502+ assert .NoError (t , err )
479503}
0 commit comments