|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace BulkGate\Plugin\User\Test; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Lukáš Piják 2023 TOPefekt s.r.o. |
| 7 | + * @link https://www.bulkgate.com/ |
| 8 | + */ |
| 9 | + |
| 10 | +use Mockery; |
| 11 | +use Tester\{Assert, TestCase}; |
| 12 | +use BulkGate\Plugin\{Debug\Logger, |
| 13 | + Eshop\ConfigurationDefault, |
| 14 | + IO\Connection, |
| 15 | + IO\Url, |
| 16 | + Localization\Language, |
| 17 | + Settings\Settings, |
| 18 | + User\Sign, |
| 19 | + Utils\Jwt}; |
| 20 | + |
| 21 | +require_once __DIR__ . '/../bootstrap.php'; |
| 22 | + |
| 23 | +/** |
| 24 | + * @testCase |
| 25 | + */ |
| 26 | +class SignTest extends TestCase |
| 27 | +{ |
| 28 | + public function testDefaultParameters(): void |
| 29 | + { |
| 30 | + $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)); |
| 31 | + $sign->setDefaultParameters(['test1' => 'default_test1', 'test3' => 'default_test3']); |
| 32 | + |
| 33 | + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); |
| 34 | + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); |
| 35 | + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); |
| 36 | + |
| 37 | + $jwt = Mockery::mock('overload:' . Jwt::class); |
| 38 | + $jwt->shouldReceive('encode')->with([ |
| 39 | + 'application_id' => 12345, |
| 40 | + 'application_installation' => 'url', |
| 41 | + 'application_product' => 'eshop', |
| 42 | + 'application_language' => 'cs', |
| 43 | + 'application_version' => '1.0', |
| 44 | + 'application_parameters' => [ |
| 45 | + 'guest' => false, |
| 46 | + 'test1' => 'test1', |
| 47 | + 'test2' => 'test2', |
| 48 | + 'test3' => 'default_test3' |
| 49 | + ] |
| 50 | + ], 'test_application_token')->once()->andReturn('0.0.0'); |
| 51 | + |
| 52 | + |
| 53 | + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['test1' => 'test1', 'test2' => 'test2'])); |
| 54 | + } |
| 55 | + |
| 56 | + public function testGuestParameterOverride(): void |
| 57 | + { |
| 58 | + $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)); |
| 59 | + |
| 60 | + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); |
| 61 | + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); |
| 62 | + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); |
| 63 | + |
| 64 | + $jwt = Mockery::mock('overload:' . Jwt::class); |
| 65 | + $jwt->shouldReceive('encode')->with([ |
| 66 | + 'application_id' => 12345, |
| 67 | + 'application_installation' => 'url', |
| 68 | + 'application_product' => 'eshop', |
| 69 | + 'application_language' => 'cs', |
| 70 | + 'application_version' => '1.0', |
| 71 | + 'application_parameters' => [ |
| 72 | + 'guest' => false, |
| 73 | + ] |
| 74 | + ], 'test_application_token')->once()->andReturn('0.0.0'); |
| 75 | + |
| 76 | + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['guest' => 'someValue'])); |
| 77 | + } |
| 78 | + |
| 79 | + public function testGuestParameterNullToken(): void |
| 80 | + { |
| 81 | + $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)); |
| 82 | + |
| 83 | + $settings->shouldReceive('load')->with('static:application_token', false)->andReturn(null); |
| 84 | + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); |
| 85 | + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); |
| 86 | + |
| 87 | + $jwt = Mockery::mock('overload:' . Jwt::class); |
| 88 | + $jwt->shouldReceive('encode')->with([ |
| 89 | + 'application_id' => 12345, |
| 90 | + 'application_installation' => 'url', |
| 91 | + 'application_product' => 'eshop', |
| 92 | + 'application_language' => 'cs', |
| 93 | + 'application_version' => '1.0', |
| 94 | + 'application_parameters' => [ |
| 95 | + 'guest' => true, |
| 96 | + ] |
| 97 | + ], '')->once()->andReturn('0.0.0'); |
| 98 | + |
| 99 | + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate()); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public function tearDown(): void |
| 104 | + { |
| 105 | + Mockery::close(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +(new SignTest())->run(); |
0 commit comments