Skip to content

Commit 30810dd

Browse files
bug #53594 [Notifier] Updated the NTFY notifier to run without a user parameter (lostfocus)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Notifier] Updated the NTFY notifier to run without a user parameter | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT The ntfy integration of the notifier component was missing the option to run without set username. Commits ------- fa3be01 [Notifier] Updated the NTFY notifier to run without a user parameter
2 parents a318801 + fa3be01 commit 30810dd

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ protected function doSend(MessageInterface $message): SentMessage
8484

8585
if (null !== $this->user && null !== $this->password) {
8686
$headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '=');
87+
} elseif (null !== $this->password) {
88+
$headers['Authorization'] = 'Bearer '.$this->password;
8789
}
8890

8991
$response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [
@@ -115,8 +117,8 @@ protected function doSend(MessageInterface $message): SentMessage
115117

116118
public function supports(MessageInterface $message): bool
117119
{
118-
return $message instanceof PushMessage &&
119-
(null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
120+
return $message instanceof PushMessage
121+
&& (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
120122
}
121123

122124
public function __toString(): string

src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransportFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ public function create(Dsn $dsn): TransportInterface
4141
$transport->setPort($port);
4242
}
4343

44-
if (!empty($user = $dsn->getUser()) && !empty($password = $dsn->getPassword())) {
44+
if (!empty($user = $dsn->getUser())) {
4545
$transport->setUser($user);
46+
}
47+
48+
if (!empty($password = $dsn->getPassword())) {
4649
$transport->setPassword($password);
4750
}
4851

src/Symfony/Component/Notifier/Bridge/Ntfy/Tests/NtfyTransportFactoryTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public static function createProvider(): iterable
3131
'ntfy://ntfy.sh/test',
3232
'ntfy://user:password@default/test',
3333
];
34+
yield [
35+
'ntfy://ntfy.sh/test',
36+
'ntfy://:password@default/test',
37+
];
3438
yield [
3539
'ntfy://ntfy.sh:8888/test',
3640
'ntfy://user:password@default:8888/test?secureHttp=off',

src/Symfony/Component/Notifier/Bridge/Ntfy/Tests/NtfyTransportTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,32 @@ public function testSend()
8585
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
8686
}
8787

88+
public function testSendWithPassword()
89+
{
90+
$response = $this->createMock(ResponseInterface::class);
91+
$response->expects($this->exactly(2))
92+
->method('getStatusCode')
93+
->willReturn(200);
94+
$response->expects($this->once())
95+
->method('getContent')
96+
->willReturn(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));
97+
98+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response): ResponseInterface {
99+
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
100+
$expectedAuthorization = 'Authorization: Bearer testtokentesttoken';
101+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
102+
$this->assertTrue(\in_array($expectedAuthorization, $options['headers'], true));
103+
104+
return $response;
105+
});
106+
107+
$transport = $this->createTransport($client)->setPassword('testtokentesttoken');
108+
109+
$sentMessage = $transport->send(new PushMessage('Hello', 'World'));
110+
111+
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
112+
}
113+
88114
public function testSendWithUserAndPassword()
89115
{
90116
$response = $this->createMock(ResponseInterface::class);

0 commit comments

Comments
 (0)