fix unittests for FroxlorRPC-class (maybe)

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann 2022-02-25 15:32:05 +01:00
parent d933549646
commit bbc3644e23
No known key found for this signature in database
GPG Key ID: 08A83830520FCECB
2 changed files with 25 additions and 65 deletions

View File

@ -92,7 +92,7 @@ class FroxlorRPC
} }
} }
} }
return false; throw new Exception('Invalid authorization credentials', 403);
} }
/** /**

View File

@ -11,32 +11,20 @@ use Froxlor\Api\FroxlorRPC;
class FroxlorRpcTest extends TestCase class FroxlorRpcTest extends TestCase
{ {
public function testInvalidRequestHeader()
{
$this->expectExceptionCode(400);
$this->expectExceptionMessage("Invalid request header");
FroxlorRPC::validateRequest(array());
}
public function testNoCredentialsGiven() public function testNoCredentialsGiven()
{ {
$this->expectExceptionCode(400); $this->expectExceptionCode(401);
$this->expectExceptionMessage("No authorization credentials given"); $this->expectExceptionMessage("Unauthenticated. Please provide api user credentials.");
FroxlorRPC::validateRequest(array( FroxlorRPC::validateRequest([]);
'header' => 'asd'
));
} }
public function testValidateAuthInvalid() public function testValidateAuthInvalid()
{ {
$this->expectExceptionCode(403); $this->expectExceptionCode(403);
$this->expectExceptionMessage("Invalid authorization credentials"); $this->expectExceptionMessage("Invalid authorization credentials");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'asd';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'asd';
'apikey' => 'asd', FroxlorRPC::validateRequest([]);
'secret' => 'asd'
]
));
} }
public function testValidateAuthAllowFromInvalid() public function testValidateAuthAllowFromInvalid()
@ -45,12 +33,9 @@ class FroxlorRpcTest extends TestCase
Database::query("UPDATE `api_keys` SET `allowed_from` = '123.123.123.123';"); Database::query("UPDATE `api_keys` SET `allowed_from` = '123.123.123.123';");
$this->expectExceptionCode(403); $this->expectExceptionCode(403);
$this->expectExceptionMessage("Invalid authorization credentials"); $this->expectExceptionMessage("Invalid authorization credentials");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', FroxlorRPC::validateRequest([]);
'secret' => 'test'
]
));
} }
public function testInvalidRequestBody() public function testInvalidRequestBody()
@ -58,68 +43,43 @@ class FroxlorRpcTest extends TestCase
Database::query("UPDATE `api_keys` SET `allowed_from` = '';"); Database::query("UPDATE `api_keys` SET `allowed_from` = '';");
$this->expectExceptionCode(400); $this->expectExceptionCode(400);
$this->expectExceptionMessage("Invalid request body"); $this->expectExceptionMessage("Invalid request body");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', FroxlorRPC::validateRequest([]);
'secret' => 'test'
]
));
} }
public function testNoCommandGiven() public function testNoCommandGiven()
{ {
$this->expectExceptionCode(400); $this->expectExceptionCode(400);
$this->expectExceptionMessage("No command given"); $this->expectExceptionMessage("No command given");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', FroxlorRPC::validateRequest(['cmd' => 'test']);
'secret' => 'test'
],
'body' => 'asd'
));
} }
public function testInvalidCommandGiven() public function testInvalidCommandGiven()
{ {
$this->expectExceptionCode(400); $this->expectExceptionCode(400);
$this->expectExceptionMessage("Invalid command"); $this->expectExceptionMessage("Invalid command");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', FroxlorRPC::validateRequest(['command' => 'Froxlor']);
'secret' => 'test'
],
'body' => [
'command' => 'Froxlor'
]
));
} }
public function testUnknownCommandGiven() public function testUnknownCommandGiven()
{ {
$this->expectExceptionCode(400); $this->expectExceptionCode(400);
$this->expectExceptionMessage("Unknown command"); $this->expectExceptionMessage("Unknown command");
FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', FroxlorRPC::validateRequest(['command' => 'SomeModule.cmd']);
'secret' => 'test'
],
'body' => [
'command' => 'SomeModule.cmd'
]
));
} }
public function testCommandOk() public function testCommandOk()
{ {
$result = FroxlorRPC::validateRequest(array( $_SERVER['PHP_AUTH_USER'] = 'test';
'header' => [ $_SERVER['PHP_AUTH_PW'] = 'test';
'apikey' => 'test', $result = FroxlorRPC::validateRequest(['command' => 'Froxlor.listFunctions']);
'secret' => 'test'
],
'body' => [
'command' => 'Froxlor.listFunctions'
]
));
$this->assertEquals('Froxlor', $result['command']['class']); $this->assertEquals('Froxlor', $result['command']['class']);
$this->assertEquals('listFunctions', $result['command']['method']); $this->assertEquals('listFunctions', $result['command']['method']);
$this->assertNull($result['params']); $this->assertNull($result['params']);