Skip to content

Commit 1f38549

Browse files
authored
Merge pull request #14 from anfly0/development
Development
2 parents f58bf0f + 53e171a commit 1f38549

4 files changed

Lines changed: 114 additions & 52 deletions

File tree

src/Auth.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ class Auth implements MiddlewareInterface, LoggerAwareInterface
4949
*/
5050
private $secret;
5151

52+
/**
53+
* @var StreamFactoryInterface
54+
*/
5255
private $streamFactory;
5356

5457
/**
@@ -79,7 +82,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
7982
}
8083

8184
$request = $this->getRequestWithSeekableBody($request);
82-
$signature = $this->getSignature($this->secret, $request->getBody());
85+
$signature = $this->getSignature($this->secret, (string) $request->getBody());
8386

8487
if (!hash_equals($request->getHeader(self::SIGNATURE_NAME)[0], $signature)) {
8588
$this->logger->warning(self::LOG_MSG_SIGNATURE_NOT_MATCHING);

tests/AuthTest.php

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,39 @@ class AuthTest extends TestCase
1717
const SECRET = 'THIS_IS_A_SECRET';
1818
const LOGGER_CHANNEL_NAME = 'Unit-test-logger';
1919

20+
/**
21+
* @var Auth
22+
*/
2023
protected $authenticator;
24+
25+
/**
26+
* @var GithubRequestMockFactory
27+
*/
2128
protected $mockRequestFactory;
29+
30+
/**
31+
* @var RequestHandlerInterface&\PHPUnit\Framework\MockObject\MockObject
32+
*/
2233
protected $mockHandler;
34+
35+
/**
36+
* @var \Psr\Http\Message\ResponseFactoryInterface
37+
*/
2338
protected $responseFactory;
39+
40+
/**
41+
* @var \Psr\Http\Message\StreamFactoryInterface
42+
*/
2443
protected $streamFactory;
44+
45+
/**
46+
* @var Resource
47+
*/
2548
protected $logFile;
49+
50+
/**
51+
* @var Logger
52+
*/
2653
protected $logger;
2754

2855
protected function setUp(): void
@@ -33,18 +60,21 @@ protected function setUp(): void
3360
$this->authenticator = new Auth(self::SECRET, $this->responseFactory, $this->streamFactory);
3461
$this->logFile = fopen('php://memory', 'rw');
3562
$this->logger = new Logger(self::LOGGER_CHANNEL_NAME);
36-
63+
3764
$this->mockHandler = $this->createMock(RequestHandlerInterface::class);
3865
$this->mockRequestFactory = new GithubRequestMockFactory(self::SECRET);
3966
}
40-
67+
4168
protected function tearDown(): void
4269
{
4370
parent::tearDown();
4471
fclose($this->logFile);
4572
}
46-
47-
public static function serverRequestProvider()
73+
74+
/**
75+
* @return ServerRequestInterface[][]
76+
*/
77+
public static function serverRequestProvider(): array
4878
{
4979
$mockRequestFactory = new GithubRequestMockFactory(self::SECRET);
5080
return [
@@ -53,14 +83,14 @@ public static function serverRequestProvider()
5383
];
5484
}
5585

56-
public function testExtendsPsr15Interface()
86+
public function testExtendsPsr15Interface(): void
5787
{
5888
$this->assertInstanceOf(
5989
MiddlewareInterface::class,
6090
new Auth(self::SECRET, $this->responseFactory, $this->streamFactory)
6191
);
6292
}
63-
public function testResponseMissingSignature()
93+
public function testResponseMissingSignature(): void
6494
{
6595
$request = $this->mockRequestFactory->createMissingHeaderRequest();
6696
$result = $this->authenticator->process($request, $this->mockHandler);
@@ -69,7 +99,7 @@ public function testResponseMissingSignature()
6999
$this->assertEquals('Bad Request', $result->getReasonPhrase());
70100
}
71101

72-
public function testResponseIncorrectSignature()
102+
public function testResponseIncorrectSignature(): void
73103
{
74104
$request = $this->mockRequestFactory->createUnauthenticRequest();
75105
$result = $this->authenticator->process($request, $this->mockHandler);
@@ -78,67 +108,67 @@ public function testResponseIncorrectSignature()
78108
$this->assertEquals('Unauthorized', $result->getReasonPhrase());
79109
}
80110

81-
public function test202ResponseFromHandlerCorrectSignature()
111+
public function test202ResponseFromHandlerCorrectSignature(): void
82112
{
83113
$request = $this->mockRequestFactory->createAuthenticRequest();
84-
114+
85115
$this->mockHandler->method('handle')
86116
->willReturn($this->responseFactory->createResponse(202, 'Accepted'));
87-
117+
88118
$result = $this->authenticator->process($request, $this->mockHandler);
89119

90120
$this->assertEquals(202, $result->getStatusCode());
91121
$this->assertEquals('Accepted', $result->getReasonPhrase());
92122
}
93123

94-
public function test201ResponseFromHandlerCorrectSignature()
124+
public function test201ResponseFromHandlerCorrectSignature(): void
95125
{
96126
$request = $this->mockRequestFactory->createAuthenticRequest();
97127

98128
$this->mockHandler->method('handle')
99129
->willReturn($this->responseFactory->createResponse(201, 'Created'));
100-
130+
101131
$result = $this->authenticator->process($request, $this->mockHandler);
102132

103133
$this->assertEquals(201, $result->getStatusCode());
104134
$this->assertEquals('Created', $result->getReasonPhrase());
105135
}
106136

107-
public function testLoggingOfMissingHeader()
137+
public function testLoggingOfMissingHeader(): void
108138
{
109139
$this->logger->pushHandler(new StreamHandler($this->logFile, Logger::DEBUG));
110140
$this->authenticator->setLogger($this->logger);
111141

112142
$request = $this->mockRequestFactory->createMissingHeaderRequest();
113-
$result = $this->authenticator->process($request, $this->mockHandler);
143+
$this->authenticator->process($request, $this->mockHandler);
114144

115145
rewind($this->logFile);
116146
$logLine = stream_get_line($this->logFile, 4096);
117147
$expected = self::LOGGER_CHANNEL_NAME . '.WARNING: ' . Auth::LOG_MSG_MISSING_HEADER;
118148
$this->assertStringContainsString($expected, $logLine);
119149
}
120150

121-
public function testLoggingOfFailedAuthentication()
151+
public function testLoggingOfFailedAuthentication(): void
122152
{
123153
$this->logger->pushHandler(new StreamHandler($this->logFile, Logger::DEBUG));
124154
$this->authenticator->setLogger($this->logger);
125155

126156
$request = $this->mockRequestFactory->createUnauthenticRequest();
127-
$result = $this->authenticator->process($request, $this->mockHandler);
157+
$this->authenticator->process($request, $this->mockHandler);
128158

129159
rewind($this->logFile);
130160
$logLine = stream_get_line($this->logFile, 4096);
131161
$expected = self::LOGGER_CHANNEL_NAME . '.WARNING: ' . Auth::LOG_MSG_SIGNATURE_NOT_MATCHING;
132162
$this->assertStringContainsString($expected, $logLine);
133163
}
134164

135-
public function testLoggingOfSuccessfulAuthentication()
165+
public function testLoggingOfSuccessfulAuthentication(): void
136166
{
137167
$this->logger->pushHandler(new StreamHandler($this->logFile, Logger::DEBUG));
138168
$this->authenticator->setLogger($this->logger);
139169

140170
$request = $this->mockRequestFactory->createAuthenticRequest();
141-
$result = $this->authenticator->process($request, $this->mockHandler);
171+
$this->authenticator->process($request, $this->mockHandler);
142172

143173
rewind($this->logFile);
144174
$logLine = stream_get_line($this->logFile, 4096);
@@ -149,42 +179,42 @@ public function testLoggingOfSuccessfulAuthentication()
149179
/**
150180
* @dataProvider serverRequestProvider
151181
*/
152-
public function testRequestBodyIsPassedToHandlerIsSeekable($request)
182+
public function testRequestBodyIsPassedToHandlerIsSeekable(ServerRequestInterface $request): void
153183
{
154184
$this->mockHandler->expects($this->atLeastOnce())
155-
->method('handle')
156-
->with($this->callback(function ($handlerRequest) {
157-
return $handlerRequest->getBody()->isSeekable();
158-
}));
159-
185+
->method('handle')
186+
->with($this->callback(function (ServerRequestInterface $handlerRequest) {
187+
return $handlerRequest->getBody()->isSeekable();
188+
}));
189+
160190
$this->authenticator->process($request, $this->mockHandler);
161191
}
162192

163193
/**
164194
* @dataProvider serverRequestProvider
165195
*/
166-
public function testRequestBodyIsNotAltered($request)
196+
public function testRequestBodyIsNotAltered(ServerRequestInterface $request): void
167197
{
168198
$this->mockHandler->expects($this->atLeastOnce())
169-
->method('handle')
170-
->with($this->callback(function ($handlerRequest) use ($request) {
171-
return (string) $handlerRequest->getBody() == (string) $request->getBody();
172-
}));
173-
199+
->method('handle')
200+
->with($this->callback(function (ServerRequestInterface $handlerRequest) use ($request) {
201+
return (string) $handlerRequest->getBody() == (string) $request->getBody();
202+
}));
203+
174204
$this->authenticator->process($request, $this->mockHandler);
175205
}
176-
206+
177207
/**
178208
* @dataProvider serverRequestProvider
179209
*/
180-
public function testRequestBodyIsRewound($request)
210+
public function testRequestBodyIsRewound(ServerRequestInterface $request): void
181211
{
182212
$this->mockHandler->expects($this->atLeastOnce())
183-
->method('handle')
184-
->with($this->callback(function ($handlerRequest) use ($request) {
185-
return $handlerRequest->getBody()->tell() === 0;
186-
}));
187-
213+
->method('handle')
214+
->with($this->callback(function (ServerRequestInterface $handlerRequest) {
215+
return $handlerRequest->getBody()->tell() === 0;
216+
}));
217+
188218
$this->authenticator->process($request, $this->mockHandler);
189219
}
190220
}

tests/GithubRequestMockFactory.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,37 @@
22

33
namespace Anfly0\Middleware\GitHub\tests;
44

5+
use \Psr\Http\Message\ServerRequestInterface;
6+
57
class GithubRequestMockFactory
68
{
79
const BODY_DATA = '{"array":[1,2,3],"boolean":true,"color":"#82b92c","null":null,"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Hello World"}';
810
const ALG = 'sha1';
911
const HEADER_NAME = 'X-Hub-Signature';
1012

13+
/**
14+
* @var string
15+
*/
1116
protected $correctHash;
17+
18+
/**
19+
* @var string
20+
*/
1221
protected $wrongHash = '90502e8291a3e67eb88d722f3590aee599f73a27';
22+
23+
/**
24+
* @var ServerRequestInterface
25+
*/
1326
protected $baseMock;
27+
28+
/**
29+
* @var string
30+
*/
1431
protected $secret;
32+
33+
/**
34+
* @var StreamMockFactory
35+
*/
1536
protected $streamFactory;
1637

1738
public function __construct(string $secret)
@@ -23,36 +44,36 @@ public function __construct(string $secret)
2344
$this->streamFactory = new StreamMockFactory();
2445
}
2546

26-
public function createBaseRequestMock()
47+
public function createBaseRequestMock(): ServerRequestInterface
2748
{
2849
return clone $this->baseMock;
2950
}
3051

31-
public function createAuthenticRequest()
52+
public function createAuthenticRequest(): ServerRequestInterface
3253
{
3354
$mock = clone $this->baseMock;
3455
$mock = $mock->withHeader(self::HEADER_NAME, 'sha1=' . $this->correctHash);
3556
$mock = $mock->withBody($this->streamFactory->createSeekableStream());
3657
return $mock;
3758
}
3859

39-
public function createAuthenticRequestNotSeekableBody()
60+
public function createAuthenticRequestNotSeekableBody(): ServerRequestInterface
4061
{
4162
$mock = clone $this->baseMock;
4263
$mock = $mock->withHeader(self::HEADER_NAME, 'sha1=' . $this->correctHash);
4364
$mock = $mock->withBody($this->streamFactory->createNotSeekableStream());
4465
return $mock;
4566
}
4667

47-
public function createUnauthenticRequest()
68+
public function createUnauthenticRequest(): ServerRequestInterface
4869
{
4970
$mock = clone $this->baseMock;
5071
$mock = $mock->withHeader(self::HEADER_NAME, 'sha1=' . $this->wrongHash);
5172
$mock = $mock->withBody($this->streamFactory->createSeekableStream());
5273
return $mock;
5374
}
5475

55-
public function createMissingHeaderRequest()
76+
public function createMissingHeaderRequest(): ServerRequestInterface
5677
{
5778
$mock = clone $this->baseMock;
5879
$mock = $mock->withBody($this->streamFactory->createSeekableStream());

0 commit comments

Comments
 (0)