Skip to content

Commit 2529921

Browse files
committed
additional tests
1 parent 09b2393 commit 2529921

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

tests/Messaging/Adapter/Chat/DiscordTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Utopia\Tests\Adapter\Chat;
44

5+
use InvalidArgumentException;
56
use Utopia\Messaging\Adapter\Chat\Discord;
67
use Utopia\Messaging\Messages\Discord as DiscordMessage;
78
use Utopia\Tests\Adapter\Base;
@@ -25,4 +26,62 @@ public function testSendMessage(): void
2526

2627
$this->assertResponse($result);
2728
}
29+
30+
/**
31+
* @return array<array<string>>
32+
*/
33+
public static function invalidURLProvider(): array
34+
{
35+
return [
36+
'invalid URL format' => ['not-a-url'],
37+
'invalid scheme (http)' => ['http://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz'],
38+
'invalid host' => ['https://example.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz'],
39+
'missing path' => ['https://discord.com'],
40+
'no webhooks segment' => ['https://discord.com/api/invalid/123456789012345678/token'],
41+
'missing webhook ID' => ['https://discord.com/api/webhooks//token'],
42+
];
43+
}
44+
45+
/**
46+
* @dataProvider invalidURLProvider
47+
*/
48+
public function testInvalidURLs(string $invalidURL): void
49+
{
50+
$this->expectException(InvalidArgumentException::class);
51+
new Discord($invalidURL);
52+
}
53+
54+
public function testValidURLVariations(): void
55+
{
56+
// Valid URL format variations
57+
$validURLs = [
58+
'with api path' => 'https://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz',
59+
'without api path' => 'https://discord.com/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz',
60+
'with trailing slash' => 'https://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz/',
61+
];
62+
63+
foreach ($validURLs as $label => $url) {
64+
try {
65+
$discord = new Discord($url);
66+
// If we get here, the URL was accepted
67+
$this->assertTrue(true, "Valid URL variant '{$label}' was accepted as expected");
68+
} catch (InvalidArgumentException $e) {
69+
$this->fail("Valid URL variant '{$label}' was rejected: " . $e->getMessage());
70+
}
71+
}
72+
}
73+
74+
public function testWebhookIDExtraction(): void
75+
{
76+
// Create a reflection of Discord to access protected properties
77+
$webhookId = '123456789012345678';
78+
$url = "https://discord.com/api/webhooks/{$webhookId}/abcdefghijklmnopqrstuvwxyz";
79+
80+
$discord = new Discord($url);
81+
$reflector = new \ReflectionClass($discord);
82+
$property = $reflector->getProperty('webhookId');
83+
$property->setAccessible(true);
84+
85+
$this->assertEquals($webhookId, $property->getValue($discord), 'Webhook ID was not correctly extracted');
86+
}
2887
}

0 commit comments

Comments
 (0)