From acd294909122417da549c79089cc5932c7797a42 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 25 Jan 2020 12:19:31 +0800 Subject: [PATCH] Improve tests. Signed-off-by: Mior Muhammad Zaki --- tests/Feature/RpcRequestTest.php | 133 +++++++++++++++++++++++++++++ tests/Feature/UpStatusPageTest.php | 3 +- 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/RpcRequestTest.php diff --git a/tests/Feature/RpcRequestTest.php b/tests/Feature/RpcRequestTest.php new file mode 100644 index 0000000..29674e0 --- /dev/null +++ b/tests/Feature/RpcRequestTest.php @@ -0,0 +1,133 @@ +afterApplicationCreated(static function () { + Router::rpc('math.add', 'Minions\Polyfill\Tests\Stubs\MathAdd'); + }); + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * + * @return void + */ + protected function getEnvironmentSetUp($app) + { + parent::getEnvironmentSetUp($app); + + $config = $app->make('config'); + + $config->set([ + 'minions.id' => 'server-project-id', + 'minions.projects' => [ + 'client-project-id' => [ + 'token' => 'secret-token', + 'signature' => 'secret-signature', + ], + 'server-project-id' => [ + 'endpoint' => 'http://127.0.0.1:8000/rpc', + 'token' => 'secret-token', + 'signature' => 'secret-signature', + ], + ], + ]); + } + + /** @test */ + public function it_can_make_rpc_request_from_a_client() + { + $id = \time(); + $message = new Message('math.add', [1, 2, 3, 4], $id); + + $this->postJson('/rpc', json_decode($message->toJson(), true), [ + 'X-Request-ID' => 'client-project-id', + 'Authorization' => 'Token secret-token', + 'X-Signature' => $message->signature('secret-signature'), + 'Content-Type' => 'application/json', + ])->assertOk() + ->assertJson([ + 'jsonrpc' => '2.0', + 'id' => $id, + 'result' => 10, + ]); + } + + /** @test */ + public function it_cant_make_rpc_request_from_a_invalid_client() + { + $id = \time(); + $message = new Message('math.add', [1, 2, 3, 4], $id); + + $this->postJson('/rpc', json_decode($message->toJson(), true), [ + 'X-Request-ID' => 'not-client-project-id', + 'Authorization' => 'Token secret-token', + 'X-Signature' => $message->signature('secret-signature'), + 'Content-Type' => 'application/json', + ])->assertOk() + ->assertJson([ + 'jsonrpc' => '2.0', + 'id' => null, + 'error' => [ + 'code' => -32600, + 'message' => 'Unable to find project: not-client-project-id', + ], + ]); + } + + /** @test */ + public function it_cant_make_rpc_request_from_a_client_when_token_is_invalid() + { + $id = \time(); + $message = new Message('math.add', [1, 2, 3, 4], $id); + + $this->postJson('/rpc', json_decode($message->toJson(), true), [ + 'X-Request-ID' => 'client-project-id', + 'Authorization' => 'Token not-secret-token', + 'X-Signature' => $message->signature('secret-signature'), + 'Content-Type' => 'application/json', + ])->assertOk() + ->assertJson([ + 'jsonrpc' => '2.0', + 'id' => null, + 'error' => [ + 'code' => -32652, + 'message' => 'Invalid Token.', + ], + ]); + } + + /** @test */ + public function it_cant_make_rpc_request_from_a_client_when_signature_is_invalid() + { + $id = \time(); + $message = new Message('math.add', [1, 2, 3, 4], $id); + + $this->postJson('/rpc', json_decode($message->toJson(), true), [ + 'X-Request-ID' => 'client-project-id', + 'Authorization' => 'Token secret-token', + 'X-Signature' => $message->signature('not-secret-signature'), + 'Content-Type' => 'application/json', + ])->assertOk() + ->assertJson([ + 'jsonrpc' => '2.0', + 'id' => null, + 'error' => [ + 'code' => -32652, + 'message' => 'Invalid Signature.', + ], + ]); + } +} diff --git a/tests/Feature/UpStatusPageTest.php b/tests/Feature/UpStatusPageTest.php index a88521f..4a405d8 100644 --- a/tests/Feature/UpStatusPageTest.php +++ b/tests/Feature/UpStatusPageTest.php @@ -11,6 +11,7 @@ public function it_can_show_service_is_up() { $this->get('/rpc') ->assertOk() - ->assertSee('OK'); + ->assertSee('OK') + ->assertHeader('Content-Type', 'text/plain; charset=UTF-8'); } }