8000 [Notifier] Updated the NTFY notifier to run without a user parameter by lostfocus · Pull Request #53594 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Updated the NTFY notifier to run without a user parameter #53594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ protected function doSend(MessageInterface $message): SentMessage

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

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

public function supports(MessageInterface $message): bool
{
return $message instanceof PushMessage &&
(null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
return $message instanceof PushMessage
&& (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
}

public function __toString(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public function create(Dsn $dsn): TransportInterface
$transport->setPort($port);
}

if (!empty($user = $dsn->getUser()) && !empty($password = $dsn->getPassword())) {
if (!empty($user = $dsn->getUser())) {
$transport->setUser($user);
}

if (!empty($password = $dsn->getPassword())) {
$transport->setPassword($password);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public static function createProvider(): iterable
'ntfy://ntfy.sh/test',
'ntfy://user:password@default/test',
];
yield [
'ntfy://ntfy.sh/test',
'ntfy://:password@default/test',
];
yield [
'ntfy://ntfy.sh:8888/test',
'ntfy://user:password@default:8888/test?secureHttp=off',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ public function testSend()
$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}

public function testSendWithPassword()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);
$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['id' => '2BYIwRmvBKcv', 'event' => 'message']));

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response): ResponseInterface {
$expectedBody = json_encode(['topic' => 'test', 'title' => 'Hello', 'message' => 'World']);
$expectedAuthorization = 'Authorization: Bearer testtokentesttoken';
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
$this->assertTrue(\in_array($expectedAuthorization, $options['headers'], true));

return $response;
});

$transport = $this->createTransport($client)->setPassword('testtokentesttoken');

$sentMessage = $transport->send(new PushMessage('Hello', 'World'));

$this->assertSame('2BYIwRmvBKcv', $sentMessage->getMessageId());
}

public function testSendWithUserAndPassword()
{
$response = $this->createMock(ResponseInterface::class);
Expand Down
0