diff --git a/src/User/Sign.php b/src/User/Sign.php index edb07d1..9b228d5 100644 --- a/src/User/Sign.php +++ b/src/User/Sign.php @@ -26,6 +26,11 @@ class Sign private Logger $logger; + /** + * @var array + */ + private array $default_parameters = []; + public function __construct(Settings $settings, Connection $connection, Url $url, Configuration $configuration, Language $language, Logger $logger) { @@ -37,6 +42,14 @@ public function __construct(Settings $settings, Connection $connection, Url $url $this->logger = $logger; } + /** + * @param array $parameters + */ + public function setDefaultParameters(array $parameters): void + { + $this->default_parameters = $parameters; + } + /** * @param array $parameters @@ -51,9 +64,13 @@ public function authenticate(bool $reload = false, array $parameters = []): ?str 'application_product' => $this->configuration->product(), 'application_language' => $this->language->get(), 'application_version' => $this->configuration->version(), - 'application_parameters' => array_merge([ - 'guest' => $token === null, - ], $parameters), + 'application_parameters' => array_merge( + $this->default_parameters, + $parameters, + [ + 'guest' => $token === null, + ], + ), ], $token ?? ''); } diff --git a/tests/User/SignTestJwt.phpt b/tests/User/SignTestJwt.phpt new file mode 100644 index 0000000..66f214a --- /dev/null +++ b/tests/User/SignTestJwt.phpt @@ -0,0 +1,109 @@ +setDefaultParameters(['test1' => 'default_test1', 'test3' => 'default_test3']); + + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => false, + 'test1' => 'test1', + 'test2' => 'test2', + 'test3' => 'default_test3' + ] + ], 'test_application_token')->once()->andReturn('0.0.0'); + + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['test1' => 'test1', 'test2' => 'test2'])); + } + + public function testGuestParameterOverride(): void + { + $sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class)); + + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => false, + ] + ], 'test_application_token')->once()->andReturn('0.0.0'); + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['guest' => 'someValue'])); + } + + public function testGuestParameterNullToken(): void + { + $sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class)); + + $settings->shouldReceive('load')->with('static:application_token', false)->andReturn(null); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => true, + ] + ], '')->once()->andReturn('0.0.0'); + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate()); + } + + + public function tearDown(): void + { + Mockery::close(); + } +} + +(new SignTest())->run();