Skip to content

Commit

Permalink
Added voucher security check
Browse files Browse the repository at this point in the history
  • Loading branch information
slischka committed Mar 31, 2021
1 parent 5346d77 commit c9d996c
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

- run: bin/cs
- run: bin/stan
- run: vendor/bin/tester tests -c tests/php-ci.ini --coverage coverage.xml --coverage-src src
- run: vendor/bin/tester tests -C --coverage coverage.xml --coverage-src src
- run: bash <(curl -s https://codecov.io/bash) -t df85f2a9-bd16-49f2-bfb3-64f092dcca7a

- store_artifacts:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/composer.lock
/tests/locks/*
/tests/php.ini
/vendor/*
.idea
/tests/**/output/*
Expand Down
4 changes: 1 addition & 3 deletions bin/tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ declare(strict_types = 1);
\escapeshellarg(
__DIR__ . '/../vendor/bin/tester'
)
. ' -c ' . \escapeshellarg(
'../tests/php.ini'
)
. ' -C '
. ' '
. '../tests',
$return
Expand Down
23 changes: 22 additions & 1 deletion src/Fapi/FapiClient/Tools/SecurityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ final class SecurityChecker

/**
* @param mixed[] $invoice
* @return bool
* @deprecated use isInvoiceSecurityValid instead
*/
public static function isValid(array $invoice, int $time, string $expectedSecurity): bool
{
return self::isInvoiceSecurityValid($invoice, $time, $expectedSecurity);
}

/**
* @param mixed[] $invoice
*/
public static function isInvoiceSecurityValid(array $invoice, int $time, string $expectedSecurity): bool
{
$id = $invoice['id'] ?? null;
$number = $invoice['number'] ?? null;
Expand All @@ -29,4 +37,17 @@ public static function isValid(array $invoice, int $time, string $expectedSecuri
return $expectedSecurity === \sha1($time . $id . $number . $itemsSecurityHash);
}

/**
* @param mixed[] $voucher
* @param mixed[] $itemTemplate
*/
public static function isVoucherSecurityValid(array $voucher, array $itemTemplate, int $time, string $expectedSecurity): bool
{
$voucherId = $voucher['id'] ?? '';
$voucherCode = $voucher['code'] ?? '';
$itemSecurityHash = \md5(($itemTemplate['id'] ?? '') . ($itemTemplate['code'] ?? ''));

return $expectedSecurity === \sha1($time . $voucherId . $voucherCode . $itemSecurityHash);
}

}
84 changes: 78 additions & 6 deletions tests/Fapi/ToolsTest/SecurityCheckerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,51 @@ final class SecurityCheckerTest extends TestCase
{

/**
* @dataProvider getIsValidData
* @dataProvider getIsInvoiceSecurityValid
* @param mixed[] $invoice
*/
public function testIsValid(array $invoice, int $time, string $expectedSecurity)
public function testIsInvoiceSecurityValid(array $invoice, int $time, string $expectedSecurity)
{
Assert::true(SecurityChecker::isValid($invoice, $time, $expectedSecurity));
}

/**
* @dataProvider getInvalidData
* @dataProvider getIsInvoiceSecurityInvalid
* @param mixed[] $invoice
*/
public function testInvalid(array $invoice, int $time, string $expectedSecurity)
public function testIsInvoiceSecurityInvalid(array $invoice, int $time, string $expectedSecurity)
{
Assert::false(SecurityChecker::isValid($invoice, $time, $expectedSecurity));
}

/**
* @dataProvider getIsVoucherSecurityValid
* @param mixed[] $voucher
* @param mixed[] $itemTemplate
*/
public function testIsVoucherSecurityValid(array $voucher, array $itemTemplate, int $time, string $expectedSecurity)
{
Assert::true(SecurityChecker::isVoucherSecurityValid($voucher, $itemTemplate, $time, $expectedSecurity));
}

/**
* @dataProvider getIsVoucherSecurityInvalid
* @param mixed[] $voucher
* @param mixed[] $itemTemplate
*/
public function testIsVoucherSecurityInvalid(
array $voucher,
array $itemTemplate,
int $time,
string $expectedSecurity
) {
Assert::false(SecurityChecker::isVoucherSecurityValid($voucher, $itemTemplate, $time, $expectedSecurity));
}

/**
* @return mixed[]
*/
public function getIsValidData(): array
public function getIsInvoiceSecurityValid(): array
{
return [
[
Expand All @@ -60,7 +84,7 @@ final class SecurityCheckerTest extends TestCase
/**
* @return mixed[]
*/
public function getInvalidData(): array
public function getIsInvoiceSecurityInvalid(): array
{
return [
[
Expand All @@ -85,6 +109,54 @@ final class SecurityCheckerTest extends TestCase
];
}

/**
* @return mixed[]
*/
public function getIsVoucherSecurityValid(): array
{
return [
[
'voucher' => [
'id' => 102,
'code' => 'ZQSDP3',
],
'itemTemplate' => [
'id' => 1,
'code' => 'STARTY',
],
'time' => 1617179013,
'expectedSecurity' => 'cf7550d28d2015944992225ae3a42752608060b7',
],
];
}

/**
* @return mixed[]
*/
public function getIsVoucherSecurityInvalid(): array
{
return [
[
'voucher' => [
'id' => 1,
'code' => "ABCD",
],
'itemTemplate' => [
'id' => 1,
'code' => 'test',
],
'time' => 1542298656,
'expectedSecurity' => '35221e0d0168d282edc3768ed4b4e878dec3c921',
],
[
'voucher' => [],
'itemTemplate' => [],
'time' => 1542298656,
'expectedSecurity' => '35221e0d0168d282edc3768ed4b4e878dec3c921',
],
];
}

}

(new SecurityCheckerTest())->run();
2 changes: 0 additions & 2 deletions tests/php-ci.ini

This file was deleted.

2 changes: 0 additions & 2 deletions tests/php-unix.ini

This file was deleted.

2 changes: 0 additions & 2 deletions tests/php-win.ini

This file was deleted.

0 comments on commit c9d996c

Please sign in to comment.