Skip to content

Commit b305dc5

Browse files
committed
Fixed test by making channel names unique.
1 parent ec3ee0e commit b305dc5

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

examples/AccessManager.php

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@
1212
$subscribeKey = getenv('SUBSCRIBE_PAM_KEY') ?? 'demo';
1313
$secretKey = getenv('SECRET_PAM_KEY') ?? 'demo';
1414

15+
// Generate unique channel prefix for test isolation (prevents PAM propagation issues in CI/CD)
16+
$testId = uniqid('test-', true);
17+
$channelPrefix = getenv('TEST_CHANNEL_PREFIX') ?? $testId;
18+
19+
// Define channel names with unique prefix
20+
$publicChannel = "{$channelPrefix}-public-channel";
21+
$readOnlyChannel = "{$channelPrefix}-read-only-channel";
22+
$privateChannel = "{$channelPrefix}-private-channel";
23+
$adminOnlyChannel = "{$channelPrefix}-admin-only-channel";
24+
25+
// Define channel group with unique prefix
26+
$userGroup = "{$channelPrefix}-user-group";
27+
28+
echo "🔑 Test Configuration\n";
29+
echo "-------------------\n";
30+
echo "Test ID: {$testId}\n";
31+
echo "Channel Prefix: {$channelPrefix}\n";
32+
echo "Channels:\n";
33+
echo " - {$publicChannel}\n";
34+
echo " - {$readOnlyChannel}\n";
35+
echo " - {$privateChannel}\n";
36+
echo " - {$adminOnlyChannel}\n\n";
37+
1538
// Admin instance with full access (includes secret key)
1639
$adminConfig = new PNConfiguration();
1740
$adminConfig->setPublishKey($publishKey);
@@ -44,15 +67,15 @@ function printResult($testName, $success, $message = '')
4467

4568
// snippet.prepare_metadata
4669
// Create channel metadata for demo channels
47-
$channels = ['public-channel', 'read-only-channel', 'private-channel', 'admin-only-channel'];
70+
$channels = [$publicChannel, $readOnlyChannel, $privateChannel, $adminOnlyChannel];
4871
foreach ($channels as $channel) {
4972
try {
5073
$admin->setChannelMetadata()
5174
->channel($channel)
5275
->setName(ucwords(str_replace('-', ' ', $channel)))
5376
->setDescription("Demo channel for access manager testing - " . $channel)
5477
->setCustom([
55-
'type' => $channel === 'admin-only-channel' ? 'admin' : 'user',
78+
'type' => $channel === $adminOnlyChannel ? 'admin' : 'user',
5679
'created' => date('Y-m-d H:i:s'),
5780
'demo' => true
5881
])
@@ -119,12 +142,12 @@ function printResult($testName, $success, $message = '')
119142
->ttl(60) // 60 minutes
120143
->authorizedUuid('regular-user') // Restrict to specific user
121144
->addChannelResources([
122-
'public-channel' => ['read' => true, 'write' => true], // Full access
123-
'read-only-channel' => ['read' => true], // Read only - no write
124-
'private-channel' => ['read' => true, 'write' => true, 'manage' => true] // Full access including manage
145+
$publicChannel => ['read' => true, 'write' => true], // Full access
146+
$readOnlyChannel => ['read' => true], // Read only - no write
147+
$privateChannel => ['read' => true, 'write' => true, 'manage' => true] // Full access including manage
125148
])
126149
->addChannelGroupResources([
127-
'user-group' => ['read' => true] // Read only for channel groups
150+
$userGroup => ['read' => true] // Read only for channel groups
128151
])
129152
->addUuidResources([
130153
'regular-user' => ['get' => true, 'update' => true], // Self metadata access
@@ -162,7 +185,7 @@ function printResult($testName, $success, $message = '')
162185

163186
// Show channel permissions
164187
echo "\nChannel Permissions:\n";
165-
foreach (['public-channel', 'read-only-channel', 'private-channel'] as $channel) {
188+
foreach ([$publicChannel, $readOnlyChannel, $privateChannel] as $channel) {
166189
$permissions = $parsedToken->getChannelResource($channel);
167190
if ($permissions) {
168191
echo "- $channel: ";
@@ -203,7 +226,7 @@ function printResult($testName, $success, $message = '')
203226
// snippet.access_denied_without_token
204227
try {
205228
$user->publish()
206-
->channel('public-channel')
229+
->channel($publicChannel)
207230
->message(['text' => 'Hello without token!'])
208231
->sync();
209232
printResult("User publish to public-channel WITHOUT token", false, "Should have failed but succeeded");
@@ -213,7 +236,7 @@ function printResult($testName, $success, $message = '')
213236

214237
try {
215238
$user->history()
216-
->channel('public-channel')
239+
->channel($publicChannel)
217240
->count(1)
218241
->sync();
219242
printResult("User read from public-channel WITHOUT token", false, "Should have failed but succeeded");
@@ -236,7 +259,7 @@ function printResult($testName, $success, $message = '')
236259
// Test allowed operations
237260
try {
238261
$result = $user->publish()
239-
->channel('public-channel')
262+
->channel($publicChannel)
240263
->message(['text' => 'Hello with token!', 'timestamp' => time()])
241264
->sync();
242265
printResult("User publish to public-channel WITH token", true, "Message published successfully");
@@ -246,7 +269,7 @@ function printResult($testName, $success, $message = '')
246269

247270
try {
248271
$result = $user->history()
249-
->channel('public-channel')
272+
->channel($publicChannel)
250273
->count(5)
251274
->sync();
252275
printResult("User read from public-channel WITH token", true, "History retrieved successfully");
@@ -256,7 +279,7 @@ function printResult($testName, $success, $message = '')
256279

257280
try {
258281
$result = $user->history()
259-
->channel('private-channel')
282+
->channel($privateChannel)
260283
->count(5)
261284
->sync();
262285
printResult("User read from private-channel WITH token", true, "History retrieved successfully");
@@ -275,7 +298,7 @@ function printResult($testName, $success, $message = '')
275298
// Test read-only channel (can read but not write)
276299
try {
277300
$user->history()
278-
->channel('read-only-channel')
301+
->channel($readOnlyChannel)
279302
->count(1)
280303
->sync();
281304
printResult("User read from read-only-channel WITH token", true, "Read access granted");
@@ -285,7 +308,7 @@ function printResult($testName, $success, $message = '')
285308

286309
try {
287310
$user->publish()
288-
->channel('read-only-channel')
311+
->channel($readOnlyChannel)
289312
->message(['text' => 'Trying to write to read-only channel'])
290313
->sync();
291314
printResult("User publish to read-only-channel WITH token", false, "Should have failed but succeeded");
@@ -296,7 +319,7 @@ function printResult($testName, $success, $message = '')
296319
// Test channel not in token (should fail)
297320
try {
298321
$user->publish()
299-
->channel('admin-only-channel')
322+
->channel($adminOnlyChannel)
300323
->message(['text' => 'Trying to access admin channel'])
301324
->sync();
302325
printResult("User publish to admin-only-channel WITH token", false, "Should have failed but succeeded");
@@ -306,7 +329,7 @@ function printResult($testName, $success, $message = '')
306329

307330
try {
308331
$user->history()
309-
->channel('admin-only-channel')
332+
->channel($adminOnlyChannel)
310333
->count(1)
311334
->sync();
312335
printResult("User read from admin-only-channel WITH token", false, "Should have failed but succeeded");
@@ -371,7 +394,7 @@ function printResult($testName, $success, $message = '')
371394
// snippet.admin_unrestricted_access
372395
try {
373396
$result = $admin->publish()
374-
->channel('admin-only-channel')
397+
->channel($adminOnlyChannel)
375398
->message(['text' => 'Admin message', 'timestamp' => time()])
376399
->sync();
377400
printResult("Admin publish to admin-only-channel", true, "Admin has unrestricted access");
@@ -381,7 +404,7 @@ function printResult($testName, $success, $message = '')
381404

382405
try {
383406
$result = $admin->history()
384-
->channel('admin-only-channel')
407+
->channel($adminOnlyChannel)
385408
->count(5)
386409
->sync();
387410
printResult("Admin read from admin-only-channel", true, "Admin has unrestricted access");
@@ -418,7 +441,7 @@ function printResult($testName, $success, $message = '')
418441
// Test user access after revocation (should fail)
419442
try {
420443
$publishResult = $user->publish()
421-
->channel('public-channel')
444+
->channel($publicChannel)
422445
->message(['text' => 'Hello after revocation!'])
423446
->sync();
424447
// print_r($publishResult);

0 commit comments

Comments
 (0)