22
33namespace Utopia \Tests \Adapter \Chat ;
44
5+ use InvalidArgumentException ;
56use Utopia \Messaging \Adapter \Chat \Discord ;
67use Utopia \Messaging \Messages \Discord as DiscordMessage ;
78use Utopia \Tests \Adapter \Base ;
@@ -10,13 +11,9 @@ class DiscordTest extends Base
1011{
1112 public function testSendMessage (): void
1213 {
13- $ id = \getenv ('DISCORD_WEBHOOK_ID ' );
14- $ token = \getenv ('DISCORD_WEBHOOK_TOKEN ' );
14+ $ url = \getenv ('DISCORD_WEBHOOK_URL ' );
1515
16- $ sender = new Discord (
17- webhookId: $ id ,
18- webhookToken: $ token
19- );
16+ $ sender = new Discord ($ url );
2017
2118 $ content = 'Test Content ' ;
2219
@@ -29,4 +26,62 @@ public function testSendMessage(): void
2926
3027 $ this ->assertResponse ($ result );
3128 }
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+ }
3287}
0 commit comments