-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDiscordTest.php
More file actions
87 lines (72 loc) · 2.93 KB
/
DiscordTest.php
File metadata and controls
87 lines (72 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Utopia\Tests\Adapter\Chat;
use InvalidArgumentException;
use Utopia\Messaging\Adapter\Chat\Discord;
use Utopia\Messaging\Messages\Discord as DiscordMessage;
use Utopia\Tests\Adapter\Base;
class DiscordTest extends Base
{
public function testSendMessage(): void
{
$url = \getenv('DISCORD_WEBHOOK_URL');
$sender = new Discord($url);
$content = 'Test Content';
$message = new DiscordMessage(
content: $content,
wait: true
);
$result = $sender->send($message);
$this->assertResponse($result);
}
/**
* @return array<array<string>>
*/
public static function invalidURLProvider(): array
{
return [
'invalid URL format' => ['not-a-url'],
'invalid scheme (http)' => ['http://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz'],
'invalid host' => ['https://example.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz'],
'missing path' => ['https://discord.com'],
'no webhooks segment' => ['https://discord.com/api/invalid/123456789012345678/token'],
'missing webhook ID' => ['https://discord.com/api/webhooks//token'],
];
}
/**
* @dataProvider invalidURLProvider
*/
public function testInvalidURLs(string $invalidURL): void
{
$this->expectException(InvalidArgumentException::class);
new Discord($invalidURL);
}
public function testValidURLVariations(): void
{
// Valid URL format variations
$validURLs = [
'with api path' => 'https://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz',
'without api path' => 'https://discord.com/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz',
'with trailing slash' => 'https://discord.com/api/webhooks/123456789012345678/abcdefghijklmnopqrstuvwxyz/',
];
foreach ($validURLs as $label => $url) {
try {
$discord = new Discord($url);
// If we get here, the URL was accepted
$this->assertTrue(true, "Valid URL variant '{$label}' was accepted as expected");
} catch (InvalidArgumentException $e) {
$this->fail("Valid URL variant '{$label}' was rejected: " . $e->getMessage());
}
}
}
public function testWebhookIDExtraction(): void
{
// Create a reflection of Discord to access protected properties
$webhookId = '123456789012345678';
$url = "https://discord.com/api/webhooks/{$webhookId}/abcdefghijklmnopqrstuvwxyz";
$discord = new Discord($url);
$reflector = new \ReflectionClass($discord);
$property = $reflector->getProperty('webhookId');
$property->setAccessible(true);
$this->assertSame($webhookId, $property->getValue($discord), 'Webhook ID was not correctly extracted');
}
}