Skip to content

Commit 3ffee82

Browse files
authored
Add additional test coverage for jwt-auth (#127)
1 parent 800c629 commit 3ffee82

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

plugins/jwt-auth/tests/ControllerTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Firebase\JWT\Key;
99
use Firebase\JWT\JWK as FirebaseJWK;
1010
use MixerApi\JwtAuth\Configuration\Configuration;
11+
use MixerApi\JwtAuth\Exception\JwtAuthException;
1112
use MixerApi\JwtAuth\Jwk\JwkSet;
1213

1314
class ControllerTest extends TestCase
@@ -23,10 +24,13 @@ class ControllerTest extends TestCase
2324

2425
public function setUp(): void
2526
{
26-
parent::setUp(); // TODO: Change the autogenerated stub
27+
parent::setUp();
2728
static::setAppNamespace('MixerApi\JwtAuth\Test\App');
2829
}
2930

31+
/**
32+
* When requesting the JWKS endpoint, the server responds with the keyset.
33+
*/
3034
public function test_jwks(): void
3135
{
3236
TestHelper::createRs256Config();
@@ -38,9 +42,12 @@ public function test_jwks(): void
3842
}
3943

4044
/**
45+
* When a valid login request is made with the given algorithm, the server responds with a JWT.
46+
*
4147
* @dataProvider dataProviderForAlg
4248
* @param string $alg
4349
* @return void
50+
* @throws JwtAuthException
4451
*/
4552
public function test_login(string $alg): void
4653
{
@@ -60,6 +67,8 @@ public function test_login(string $alg): void
6067
}
6168

6269
/**
70+
* When a login request is made with invalid credentials for the given algorithm the server responds with a 401.
71+
*
6372
* @dataProvider dataProviderForAlg
6473
* @param string $alg
6574
* @return void
@@ -72,6 +81,29 @@ public function test_login_fails(string $alg): void
7281
}
7382

7483
/**
84+
* When an authenticated request is made to an endpoint requiring authentication, the server responds with a 200.
85+
*
86+
* @dataProvider dataProviderForAlg
87+
* @param string $alg
88+
* @return void
89+
*/
90+
public function test_auth_works(string $alg): void
91+
{
92+
$alg === 'RS' ? TestHelper::createRs256Config() : TestHelper::createHs256Config();
93+
$this->post('/test/login.json', ['email' => '[email protected]', 'password' => 'password']);
94+
$this->assertResponseCode(200);
95+
$body = (string)$this->_response->getBody();
96+
$this->configRequest([
97+
'headers' => ['Authorization' => 'Bearer ' . $body],
98+
]);
99+
100+
$this->get('/test/index.json');
101+
$this->assertResponseOk();
102+
}
103+
104+
/**
105+
* When an unauthenticated request is made to an endpoint requiring authentication, the server responds with a 401.
106+
*
75107
* @dataProvider dataProviderForAlg
76108
* @param string $alg
77109
* @return void

tests/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
Configure::write('App.fullBaseUrl', 'http://localhost');
3636
putenv('DB=sqlite');
3737

38+
ini_set('error_reporting', 'E_ALL ^ E_DEPRECATED');
39+
3840
// Fixate sessionid early on, as php7.2+
3941
// does not allow the sessionid to be set after stdout
4042
// has been written to.

0 commit comments

Comments
 (0)