Skip to content

Commit e688811

Browse files
committed
Fixed the credentials on the SNS Client
1 parent 988f79b commit e688811

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/SnsServiceProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NotificationChannels\AwsSns;
44

55
use Aws\Sns\SnsClient as SnsService;
6+
use Illuminate\Support\Arr;
67
use Illuminate\Support\ServiceProvider;
78

89
class SnsServiceProvider extends ServiceProvider
@@ -19,7 +20,18 @@ public function boot()
1920
});
2021

2122
$this->app->bind(SnsService::class, function () {
22-
return new SnsService($this->app['config']['services.sns']);
23+
$config = array_merge(['version' => 'latest'], $this->app['config']['services.sns']);
24+
25+
return new SnsService($this->addSnsCredentials($config));
2326
});
2427
}
28+
29+
protected function addSnsCredentials($config): array
30+
{
31+
if (! empty($config['key']) && ! empty($config['secret'])) {
32+
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
33+
}
34+
35+
return $config;
36+
}
2537
}

tests/SnsServiceProviderTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace NotificationChannels\AwsSns\Test;
44

5+
use Aws\Sns\SnsClient;
6+
use Aws\Credentials\Credentials;
57
use Aws\Sns\SnsClient as SnsService;
68
use Illuminate\Contracts\Foundation\Application;
79
use Mockery;
@@ -77,6 +79,61 @@ public function it_gives_an_instantiated_sns_object_when_the_channel_asks_for_it
7779

7880
$this->provider->boot();
7981
}
82+
83+
/** @test */
84+
public function it_creates_the_aws_credentials_from_the_key_and_secret_options()
85+
{
86+
$this->app->shouldReceive('when')
87+
->with(SnsChannel::class)
88+
->once()
89+
->andReturn($this->app);
90+
91+
$this->app->shouldReceive('needs')
92+
->with(Sns::class)
93+
->once()
94+
->andReturn($this->app);
95+
96+
$this->app->shouldReceive('give')
97+
->with(Mockery::on(function ($sns) {
98+
return $sns() instanceof Sns;
99+
}))
100+
->once();
101+
102+
$this->app->shouldReceive('make')
103+
->with(SnsService::class)
104+
->andReturn(Mockery::mock(SnsService::class));
105+
106+
$this->app->shouldReceive('bind')
107+
->with(SnsService::class, Mockery::on(function ($sns) {
108+
/** @var SnsClient $snsClient */
109+
$snsClient = $sns();
110+
$credentials = $snsClient->getCredentials()->wait();
111+
$this->assertSame([
112+
'key' => 'aws-key-123',
113+
'secret' => 'aws-secret-ashd1i26312873asw',
114+
'token' => null,
115+
'expires' => null,
116+
], $credentials->toArray());
117+
118+
return true;
119+
}))
120+
->once()
121+
->andReturn($this->app);
122+
123+
$configArray = [
124+
'key' => 'aws-key-123',
125+
'secret' => 'aws-secret-ashd1i26312873asw',
126+
'region' => 'us-east-1',
127+
];
128+
129+
$this->app->shouldReceive('offsetGet')
130+
->with('config')
131+
->andReturn([
132+
'services.sns' => $configArray,
133+
]);
134+
135+
$this->provider->boot();
136+
}
80137
}
81138

82139
interface App extends Application, \ArrayAccess

0 commit comments

Comments
 (0)