Skip to content

Commit

Permalink
Add support for dynamic application tokens
Browse files Browse the repository at this point in the history
* allow a notifiable model to overrule the globally set token

* Reflect app token design change in README

* Change toArray of Pushover receiver to be a single statement
  • Loading branch information
hxnk authored and casperboone committed Jan 31, 2018
1 parent 66cf5bc commit 1c28e4e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public function routeNotificationForPushover() {
->toDevice(['iphone', 'desktop']);
}
```

If you want to (dynamically) overrule the application token from the services config, e.g. because each user holds their own application token, return a `PushoverReceiver` object like this:
```php
...
public function routeNotificationForPushover() {
return PushoverReceiver::withUserKey('pushover-key')
->withApplicationToken('app-token');
}
```

You can also send a message to a Pushover group:
```php
...
Expand Down
6 changes: 3 additions & 3 deletions src/Pushover.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public function send($params)
}

/**
* Merge token into parameters array.
* Merge token into parameters array, unless it has been set on the PushoverReceiver.
*
* @param array $params
* @return array
*/
protected function paramsWithToken($params)
{
return array_merge($params, [
return array_merge([
'token' => $this->token,
]);
], $params);
}
}
18 changes: 16 additions & 2 deletions src/PushoverReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class PushoverReceiver
{
protected $key;
protected $token;
protected $devices = [];

/**
Expand Down Expand Up @@ -58,16 +59,29 @@ public function toDevice($device)
return $this;
}

/**
* Set the application token.
*
* @param $token
* @return PushoverReceiver
*/
public function withApplicationToken($token)
{
$this->token = $token;

return $this;
}

/**
* Get array representation of Pushover receiver.
*
* @return array
*/
public function toArray()
{
return [
return array_merge([
'user' => $this->key,
'device' => implode(',', $this->devices),
];
], $this->token ? ['token' => $this->token] : []);
}
}
40 changes: 37 additions & 3 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function setUp()
}

/** @test */
public function it_can_send_a_pushover_notification()
public function it_can_send_a_pushover_notification_with_a_global_token()
{
$message = (new PushoverMessage('Message text'))
->title('Message title')
Expand All @@ -44,7 +44,7 @@ public function it_can_send_a_pushover_notification()
->url('http://example.com', 'Example Website');

$this->requestWillBeSentToPushoverWith([
'token' => 'application-token',
'token' => 'global-application-token',
'user' => 'pushover-key',
'device' => 'iphone,desktop',
'message' => 'Message text',
Expand All @@ -58,7 +58,7 @@ public function it_can_send_a_pushover_notification()
'url_title' => 'Example Website',
]);

$pushover = new Pushover($this->guzzleClient, 'application-token');
$pushover = new Pushover($this->guzzleClient, 'global-application-token');

$channel = new PushoverChannel($pushover, $this->events);

Expand All @@ -67,6 +67,40 @@ public function it_can_send_a_pushover_notification()
$channel->send(new NotifiableWithPushoverReceiver, $this->notification);
}

/** @test */
public function it_can_send_a_pushover_notification_with_an_overridden_token()
{
$message = (new PushoverMessage('Message text'))
->title('Message title')
->emergencyPriority(60, 600)
->time(123456789)
->sound('boing')
->url('http://example.com', 'Example Website');

$this->requestWillBeSentToPushoverWith([
'token' => 'overridden-application-token',
'user' => 'pushover-key',
'device' => 'iphone,desktop',
'message' => 'Message text',
'title' => 'Message title',
'priority' => 2,
'retry' => 60,
'expire' => 600,
'timestamp' => 123456789,
'sound' => 'boing',
'url' => 'http://example.com',
'url_title' => 'Example Website',
]);

$pushover = new Pushover($this->guzzleClient, 'global-application-token');

$channel = new PushoverChannel($pushover, $this->events);

$this->notification->shouldReceive('toPushover')->andReturn($message);

$channel->send(new NotifiableWithPushoverReceiverWithToken(), $this->notification);
}

protected function requestWillBeSentToPushoverWith($params)
{
$this->guzzleClient->shouldReceive('post')
Expand Down
16 changes: 16 additions & 0 deletions tests/NotifiableWithPushoverReceiverWithToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace NotificationChannels\Pushover\Test;

use NotificationChannels\Pushover\PushoverReceiver;

class NotifiableWithPushoverReceiverWithToken extends Notifiable
{
public function routeNotificationFor($channel)
{
return PushoverReceiver::withUserKey('pushover-key')
->withApplicationToken('overridden-application-token')
->toDevice('iphone')
->toDevice('desktop');
}
}
18 changes: 18 additions & 0 deletions tests/PushoverReceiverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ public function it_can_set_up_a_receiver_with_a_group_key()
$this->assertArraySubset(['user' => 'pushover-key'], $pushoverReceiver->toArray());
}

/** @test */
public function it_can_set_up_a_receiver_with_an_application_token()
{
$pushoverReceiver = PushoverReceiver::withUserKey('pushover-key')->withApplicationToken('pushover-token');

$this->assertArraySubset(['user' => 'pushover-key', 'token' => 'pushover-token'], $pushoverReceiver->toArray());
}

/** @test */
public function it_only_exposes_app_token_when_set()
{
$pushoverReceiverUserKey = PushoverReceiver::withUserKey('pushover-key');
$pushoverReceiverGroupKey = PushoverReceiver::withGroupKey('pushover-key');

$this->assertArrayNotHasKey('token', $pushoverReceiverUserKey->toArray());
$this->assertArrayNotHasKey('token', $pushoverReceiverGroupKey->toArray());
}

/** @test */
public function it_can_add_a_single_device_to_the_receiver()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/PushoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ public function it_can_send_a_request_to_pushover()
$this->pushover->send([]);
}

/** @test */
public function it_can_send_a_request_with_an_overridden_token()
{
$this->guzzleClient->shouldReceive('post')
->with('https://api.pushover.net/1/messages.json', [
'form_params' => [
'token' => 'dynamic-application-token',
],
]);

$this->pushover->send(['token' => 'dynamic-application-token']);
}

/** @test */
public function it_can_accept_parameters_for_a_send_request()
{
Expand Down

0 comments on commit 1c28e4e

Please sign in to comment.