diff --git a/builder/PostCreateScript.php b/builder/PostCreateScript.php index 11c0d23..1e08225 100755 --- a/builder/PostCreateScript.php +++ b/builder/PostCreateScript.php @@ -121,13 +121,15 @@ public static function run(Event $event) $workdir = realpath(__DIR__ . '/..'); $stdIo = $event->getIO(); + $currentPhpVersion = PHP_MAJOR_VERSION . "." .PHP_MINOR_VERSION; + $stdIo->write("========================================================"); $stdIo->write(" Setup Project"); $stdIo->write(" Answer the questions below"); $stdIo->write("========================================================"); $stdIo->write(""); $stdIo->write("Project Directory: " . $workdir); - $phpVersion = $stdIo->ask('PHP Version [7.4]: ', '7.4'); + $phpVersion = $stdIo->ask("PHP Version [$currentPhpVersion]: ", $currentPhpVersion); $namespace = $stdIo->ask('Project namespace [MyRest]: ', 'MyRest'); $composerName = $stdIo->ask('Composer name [me/myrest]: ', 'me/myrest'); $mysqlConnection = $stdIo->ask('MySQL connection DEV [mysql://root:mysqlp455w0rd@mysql-container/mydb]: ', 'mysql://root:mysqlp455w0rd@mysql-container/mydb'); diff --git a/builder/Scripts.php b/builder/Scripts.php index 064bd1c..971b26e 100755 --- a/builder/Scripts.php +++ b/builder/Scripts.php @@ -280,7 +280,7 @@ public function runCodeGenerator(array $arguments) $nullableFields = []; foreach ($tableDefinition as $field) { if ($field['null'] == 'YES') { - $nullableFields[] = $field["field"]; + $nullableFields[] = $field["property"]; } } @@ -288,18 +288,25 @@ public function runCodeGenerator(array $arguments) $primaryKeys = []; foreach ($tableDefinition as $field) { if ($field['key'] == 'PRI') { - $primaryKeys[] = $field["field"]; + $primaryKeys[] = $field["property"]; } } - // Create an array with non-nullable fields but primary keys + // Create an array with non nullable fields but primary keys $nonNullableFields = []; foreach ($tableDefinition as $field) { if ($field['null'] == 'NO' && $field['key'] != 'PRI') { - $nonNullableFields[] = $field["field"]; + $nonNullableFields[] = $field["property"]; } } + // Create an array with non nullable fields but primary keys + foreach ($tableIndexes as $key => $field) { + $tableIndexes[$key]['camelColumnName'] = preg_replace_callback('/_(.?)/', function($match) { + return strtoupper($match[1]); + }, $field['column_name']); + } + $data = [ 'namespace' => 'RestTemplate', 'restTag' => ucwords(explode('_', strtolower($table))[0]), diff --git a/db/base.sql b/db/base.sql index 81b219e..95b3c6a 100644 --- a/db/base.sql +++ b/db/base.sql @@ -8,6 +8,8 @@ create table dummy ( field varchar(10) not null ); +create index ix_field on dummy(field); + insert into dummy (field) values ('fld value'); insert into dummy (field) values ('Test 1'); insert into dummy (field) values ('Test 2'); diff --git a/docker/fpm/php/custom.ini b/docker/fpm/php/custom.ini index 8a3ed35..2f645c1 100644 --- a/docker/fpm/php/custom.ini +++ b/docker/fpm/php/custom.ini @@ -6,4 +6,4 @@ upload_max_filesize = 40M ; Must be greater than or equal to upload_max_filesize post_max_size = 40M -memory_limit = 128M +memory_limit = 192M diff --git a/src/Model/User.php b/src/Model/User.php index ea1b06d..8ba38e7 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -11,17 +11,72 @@ #[OA\Schema(required: ["email"], type: "object", xml: new \OpenApi\Attributes\Xml(name: "User"))] class User extends UserModel { + // Property Fields + const PROP_RESETTOKENEXPIRE = 'resettokenexpire'; + const PROP_RESETTOKEN = 'resettoken'; + const PROP_RESETCODE = 'resetcode'; + const PROP_RESETALLOWED = 'resetallowed'; + + // Property Values + const VALUE_YES = 'yes'; + const VALUE_NO = 'no'; + + // Roles + const ROLE_ADMIN = 'admin'; + const ROLE_USER = 'user'; + + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $userid; + + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $name; + + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $email; + /** * @var ?string */ #[OA\Property(type: "string", format: "string")] - protected ?string $updated = null; + protected $username; + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $password; + + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $created; /** * @var ?string */ #[OA\Property(type: "string", format: "string")] - protected ?string $uuid = null; + protected $updated; + + /** + * @var ?string + */ + #[OA\Property(type: "string", format: "string")] + protected $admin = "no"; + + /** + * @OA\Property() + * @var ?string + */ + protected $uuid; /** * User constructor. diff --git a/src/Repository/BaseRepository.php b/src/Repository/BaseRepository.php index 80326c9..9cb6585 100644 --- a/src/Repository/BaseRepository.php +++ b/src/Repository/BaseRepository.php @@ -142,13 +142,13 @@ public static function getUuid() /** * @param Mapper $mapper - * @param string $pkFieldName - * @param string $modelField - * @return void + * @param string $binPropertyName + * @param string $uuidStrPropertyName + * @return FieldMapping */ - protected function setClosureFixBinaryUUID(Mapper $mapper, string $pkFieldName = 'id', string $modelField = 'uuid') + protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = 'id', $uuidStrPropertyName = 'uuid') { - $mapper->addFieldMapping(FieldMapping::create($pkFieldName) + $fieldMapping = FieldMapping::create($binPropertyName) ->withUpdateFunction(function ($value, $instance) { if (empty($value)) { return null; @@ -158,10 +158,23 @@ protected function setClosureFixBinaryUUID(Mapper $mapper, string $pkFieldName = } return $value; }) - ->withSelectFunction(function ($value, $instance) use ($modelField) { - return str_replace('-', '', $instance->{'get' . $modelField}()); - }) - ); + ->withSelectFunction(function ($value, $instance) use ($binPropertyName, $uuidStrPropertyName) { + if (!empty($uuidStrPropertyName)) { + $fieldValue = $instance->{'get' . $uuidStrPropertyName}(); + } else { + $fieldValue = HexUuidLiteral::getFormattedUuid($instance->{'get' . $binPropertyName}(), false); + } + if (is_null($fieldValue)) { + return null; + } + return $fieldValue; + }); + + if (!empty($mapper)) { + $mapper->addFieldMapping($fieldMapping); + } + + return $fieldMapping; } /** diff --git a/src/Repository/DummyRepository.php b/src/Repository/DummyRepository.php index 253aeb4..def217c 100644 --- a/src/Repository/DummyRepository.php +++ b/src/Repository/DummyRepository.php @@ -2,11 +2,12 @@ namespace RestTemplate\Repository; +use RestTemplate\Psr11; use ByJG\AnyDataset\Db\DbDriverInterface; +use ByJG\MicroOrm\FieldMapping; use ByJG\MicroOrm\Mapper; use ByJG\MicroOrm\Query; use ByJG\MicroOrm\Repository; -use ByJG\Serializer\Exception\InvalidArgumentException; use RestTemplate\Model\Dummy; class DummyRepository extends BaseRepository @@ -36,22 +37,17 @@ public function __construct(DbDriverInterface $dbDriver) $this->repository = new Repository($dbDriver, $mapper); } + /** - * @param $field string + * @param mixed $field * @return null|Dummy[] - * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException - * @throws InvalidArgumentException */ - public function getByField(string $field): ?array + public function getByField($field) { $query = Query::getInstance() ->table('dummy') - ->where('dummy.field like :field', ['field' => "%$field%"]); - + ->where('dummy.field = :value', ['value' => $field]); $result = $this->repository->getByQuery($query); - if (is_null($result)) { - return null; - } return $result; } diff --git a/src/Rest/DummyHexRest.php b/src/Rest/DummyHexRest.php index 8ee36b4..ec8f2f4 100644 --- a/src/Rest/DummyHexRest.php +++ b/src/Rest/DummyHexRest.php @@ -22,6 +22,7 @@ use RestTemplate\Model\DummyHex; use RestTemplate\Psr11; use RestTemplate\Repository\DummyHexRepository; +use RestTemplate\Model\User; class DummyHexRest extends ServiceAbstractBase { @@ -63,7 +64,7 @@ class DummyHexRest extends ServiceAbstractBase description: "The object DummyHex", content: new OA\JsonContent(ref: "#/components/schemas/DummyHex") )] - public function getDummyHex(HttpResponse $response, HttpRequest $request) + public function getDummyHex(HttpResponse $response, HttpRequest $request): void { $data = $this->requireAuthenticated(); @@ -149,7 +150,7 @@ public function getDummyHex(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function listDummyHex(HttpResponse $response, HttpRequest $request) + public function listDummyHex(HttpResponse $response, HttpRequest $request): void { $data = $this->requireAuthenticated(); @@ -222,9 +223,9 @@ public function listDummyHex(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function postDummyHex(HttpResponse $response, HttpRequest $request) + public function postDummyHex(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); @@ -281,9 +282,9 @@ public function postDummyHex(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function putDummyHex(HttpResponse $response, HttpRequest $request) + public function putDummyHex(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); diff --git a/src/Rest/DummyRest.php b/src/Rest/DummyRest.php index 8f446cf..48be47f 100644 --- a/src/Rest/DummyRest.php +++ b/src/Rest/DummyRest.php @@ -22,6 +22,7 @@ use RestTemplate\Model\Dummy; use RestTemplate\Psr11; use RestTemplate\Repository\DummyRepository; +use RestTemplate\Model\User; class DummyRest extends ServiceAbstractBase { @@ -63,7 +64,7 @@ class DummyRest extends ServiceAbstractBase description: "The object Dummy", content: new OA\JsonContent(ref: "#/components/schemas/Dummy") )] - public function getDummy(HttpResponse $response, HttpRequest $request) + public function getDummy(HttpResponse $response, HttpRequest $request): void { $data = $this->requireAuthenticated(); @@ -149,7 +150,7 @@ public function getDummy(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function listDummy(HttpResponse $response, HttpRequest $request) + public function listDummy(HttpResponse $response, HttpRequest $request): void { $data = $this->requireAuthenticated(); @@ -196,7 +197,7 @@ public function listDummy(HttpResponse $response, HttpRequest $request) tags: ["Dummy"] )] #[OA\RequestBody( - description: "The object DummyHex to be created", + description: "The object Dummy to be created", required: true, content: new OA\JsonContent( required: [ "field" ], @@ -222,9 +223,9 @@ public function listDummy(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function postDummy(HttpResponse $response, HttpRequest $request) + public function postDummy(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); @@ -281,9 +282,9 @@ public function postDummy(HttpResponse $response, HttpRequest $request) description: "Not Authorized", content: new OA\JsonContent(ref: "#/components/schemas/error") )] - public function putDummy(HttpResponse $response, HttpRequest $request) + public function putDummy(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); diff --git a/src/Rest/Login.php b/src/Rest/Login.php index 9a6bdb6..254eea2 100644 --- a/src/Rest/Login.php +++ b/src/Rest/Login.php @@ -354,24 +354,4 @@ public function postResetPassword(HttpResponse $response, HttpRequest $request) $response->write(['token' => $json->token]); } - - - - /** - * @param User|null $user - * @return array - * @throws Error401Exception - */ - private function createUserMetadata(?User $user): array - { - if (is_null($user)) { - throw new Error401Exception('Username or password is invalid'); - } - - return [ - 'role' => ($user->getAdmin() === 'yes' ? 'admin' : 'user'), - 'userid' => $user->getUserid(), - 'name' => $user->getName() - ]; - } } diff --git a/src/Rest/ServiceAbstractBase.php b/src/Rest/ServiceAbstractBase.php index ecfba3b..3148bed 100644 --- a/src/Rest/ServiceAbstractBase.php +++ b/src/Rest/ServiceAbstractBase.php @@ -5,6 +5,7 @@ use ByJG\ApiTools\Base\Schema; use ByJG\ApiTools\Exception\HttpMethodNotFoundException; use ByJG\ApiTools\Exception\PathNotFoundException; +use ByJG\Authenticate\Model\UserModel; use ByJG\Config\Exception\ConfigException; use ByJG\Config\Exception\ConfigNotFoundException; use ByJG\Config\Exception\DependencyInjectionException; @@ -18,11 +19,31 @@ use Exception; use Psr\SimpleCache\InvalidArgumentException; use ReflectionException; +use RestTemplate\Model\User; use RestTemplate\Psr11; class ServiceAbstractBase { + /** + * @param ?UserModel $user + * @return array + * @throws Error401Exception + * @throws Error403Exception + */ + protected function createUserMetadata(?UserModel $user): array + { + if (is_null($user)) { + throw new Error401Exception('Username or password is invalid'); + } + + return [ + 'role' => ($user->getAdmin() === User::VALUE_YES ? User::ROLE_ADMIN : User::ROLE_USER), + 'userid' => $user->getUserid(), + 'name' => $user->getName() + ]; + } + /** * @param array $properties * @return mixed diff --git a/src/Util/HexUuidLiteral.php b/src/Util/HexUuidLiteral.php index ea9e3cd..3348a7f 100644 --- a/src/Util/HexUuidLiteral.php +++ b/src/Util/HexUuidLiteral.php @@ -20,6 +20,10 @@ public static function getUuidFromLiteral($literal) public static function getFormattedUuid($item, $throwErrorIfInvalid = true) { + if (empty($item)) { + return $item; + } + if ($item instanceof Literal) { $item = preg_replace("/^X'(.*)'$/", "$1", $item->__toString()); } diff --git a/templates/codegen/repository.php.jinja b/templates/codegen/repository.php.jinja index f6c1b16..1843b96 100644 --- a/templates/codegen/repository.php.jinja +++ b/templates/codegen/repository.php.jinja @@ -45,14 +45,14 @@ class {{ className }}Repository extends BaseRepository {% for index in indexes -%} {% if index.key_name != 'PRIMARY' -%} /** - * @param mixed ${{ index.column_name }} - * @return {{ className }}[] + * @param mixed ${{ index.camelColumnName }} + * @return null|{{ className }}[] */ - public function getBy{{ index.column_name | capitalize }}(${{ index.column_name }}) + public function getBy{{ index.camelColumnName | capitalize }}(${{ index.camelColumnName }}) { $query = Query::getInstance() ->table('{{ tableName }}') - ->where('{{ tableName }}.{{ index.column_name }} = :value', ['value' => ${{ index.column_name }}]); + ->where('{{ tableName }}.{{ index.column_name }} = :value', ['value' => ${{ index.camelColumnName }}]); $result = $this->repository->getByQuery($query); return $result; } diff --git a/templates/codegen/rest.php.jinja b/templates/codegen/rest.php.jinja index b9409b6..7549c6e 100644 --- a/templates/codegen/rest.php.jinja +++ b/templates/codegen/rest.php.jinja @@ -22,6 +22,7 @@ use ReflectionException; use {{ namespace }}\Model\{{ className }}; use {{ namespace }}\Psr11; use {{ namespace }}\Repository\{{ className }}Repository; +use {{ namespace }}\Model\User; class {{ className }}Rest extends ServiceAbstractBase { @@ -196,7 +197,7 @@ class {{ className }}Rest extends ServiceAbstractBase tags: ["{{ restTag }}"] )] #[OA\RequestBody( - description: "The object DummyHex to be created", + description: "The object {{ className }} to be created", required: true, content: new OA\JsonContent( {% if nonNullableFields | count > 0 %}required: [ "{{ nonNullableFields | join('", "')}}" ],{% endif %} @@ -226,7 +227,7 @@ class {{ className }}Rest extends ServiceAbstractBase )] public function post{{ className }}(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); @@ -285,7 +286,7 @@ class {{ className }}Rest extends ServiceAbstractBase )] public function put{{ className }}(HttpResponse $response, HttpRequest $request): void { - $data = $this->requireRole("admin"); + $data = $this->requireRole(User::ROLE_ADMIN); $payload = $this->validateRequest($request); diff --git a/tests/Functional/Rest/DummyHexTest.php b/tests/Functional/Rest/DummyHexTest.php index 6e324ec..3cebb7f 100644 --- a/tests/Functional/Rest/DummyHexTest.php +++ b/tests/Functional/Rest/DummyHexTest.php @@ -5,19 +5,21 @@ use ByJG\RestServer\Exception\Error401Exception; use ByJG\RestServer\Exception\Error403Exception; use ByJG\Serializer\BinderObject; -use ByJG\Serializer\Exception\InvalidArgumentException; +use RestTemplate\Util\FakeApiRequester; use RestTemplate\Model\DummyHex; use RestTemplate\Repository\BaseRepository; -use RestTemplate\Util\FakeApiRequester; class DummyHexTest extends BaseApiTestCase { + protected function setUp(): void + { + parent::setUp(); + } + /** - * @param bool $array * @return DummyHex|array - * @throws InvalidArgumentException */ - protected function getSampleData(bool $array = false) + protected function getSampleData($array = false) { $sample = [ diff --git a/tests/Functional/Rest/DummyTest.php b/tests/Functional/Rest/DummyTest.php index 83f8787..64a2fe6 100644 --- a/tests/Functional/Rest/DummyTest.php +++ b/tests/Functional/Rest/DummyTest.php @@ -5,19 +5,21 @@ use ByJG\RestServer\Exception\Error401Exception; use ByJG\RestServer\Exception\Error403Exception; use ByJG\Serializer\BinderObject; -use ByJG\Serializer\Exception\InvalidArgumentException; -use RestTemplate\Model\Dummy; use RestTemplate\Util\FakeApiRequester; +use RestTemplate\Model\Dummy; +use RestTemplate\Repository\BaseRepository; class DummyTest extends BaseApiTestCase { + protected function setUp(): void + { + parent::setUp(); + } /** - * @param bool $array * @return Dummy|array - * @throws InvalidArgumentException */ - protected function getSampleData(bool $array = false) + protected function getSampleData($array = false) { $sample = [ diff --git a/tests/Functional/Rest/LoginTest.php b/tests/Functional/Rest/LoginTest.php index 070e5fd..f3a5451 100644 --- a/tests/Functional/Rest/LoginTest.php +++ b/tests/Functional/Rest/LoginTest.php @@ -5,6 +5,7 @@ use ByJG\Authenticate\UsersDBDataset; use ByJG\RestServer\Exception\Error401Exception; use ByJG\RestServer\Exception\Error422Exception; +use RestTemplate\Model\User; use RestTemplate\Psr11; use RestTemplate\Util\FakeApiRequester; @@ -42,19 +43,19 @@ public function testResetRequestOk() // Clear the reset token $userRepo = Psr11::container()->get(UsersDBDataset::class); $user = $userRepo->getByEmail($email); - $user->set("resettoken", null); - $user->set("resettokenexpire", null); - $user->set("resetcode", null); - $user->set("resetallowed", null); + $user->set(User::PROP_RESETTOKEN, null); + $user->set(User::PROP_RESETTOKENEXPIRE, null); + $user->set(User::PROP_RESETCODE, null); + $user->set(User::PROP_RESETALLOWED, null); $userRepo->save($user); // Check if the reset token was cleared $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertEmpty($user->get("resettoken")); - $this->assertEmpty($user->get("resettokenexpire")); - $this->assertEmpty($user->get("resetcode")); - $this->assertEmpty($user->get("resetallowed")); + $this->assertEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEmpty($user->get(User::PROP_RESETALLOWED)); // Execute the request $request = new FakeApiRequester(); @@ -71,10 +72,10 @@ public function testResetRequestOk() $userRepo = Psr11::container()->get(UsersDBDataset::class); $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertNotEmpty($user->get("resettoken")); - $this->assertNotEmpty($user->get("resettokenexpire")); - $this->assertNotEmpty($user->get("resetcode")); - $this->assertEmpty($user->get("resetallowed")); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertNotEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEmpty($user->get(User::PROP_RESETALLOWED)); } public function testConfirmCodeFail() @@ -85,10 +86,10 @@ public function testConfirmCodeFail() $userRepo = Psr11::container()->get(UsersDBDataset::class); $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertNotEmpty($user->get("resettoken")); - $this->assertNotEmpty($user->get("resettokenexpire")); - $this->assertNotEmpty($user->get("resetcode")); - $this->assertEmpty($user->get("resetallowed")); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertNotEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEmpty($user->get(User::PROP_RESETALLOWED)); $this->expectException(Error422Exception::class); @@ -98,7 +99,7 @@ public function testConfirmCodeFail() ->withPsr7Request($this->getPsr7Request()) ->withMethod('POST') ->withPath("/login/confirmcode") - ->withRequestBody(json_encode((["email" => $email, "code" => "123456", "token" => $user->get("resettoken")]))) + ->withRequestBody(json_encode((["email" => $email, "code" => "123456", "token" => $user->get(User::PROP_RESETTOKEN)]))) ->assertResponseCode(422) ; $this->assertRequest($request); @@ -112,10 +113,10 @@ public function testConfirmCodeOk() $userRepo = Psr11::container()->get(UsersDBDataset::class); $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertNotEmpty($user->get("resettoken")); - $this->assertNotEmpty($user->get("resettokenexpire")); - $this->assertNotEmpty($user->get("resetcode")); - $this->assertEmpty($user->get("resetallowed")); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertNotEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEmpty($user->get(User::PROP_RESETALLOWED)); // Execute the request, now with the correct code $request = new FakeApiRequester(); @@ -123,7 +124,7 @@ public function testConfirmCodeOk() ->withPsr7Request($this->getPsr7Request()) ->withMethod('POST') ->withPath("/login/confirmcode") - ->withRequestBody(json_encode((["email" => $email, "code" => $user->get("resetcode"), "token" => $user->get("resettoken")]))) + ->withRequestBody(json_encode((["email" => $email, "code" => $user->get(User::PROP_RESETCODE), "token" => $user->get(User::PROP_RESETTOKEN)]))) ->assertResponseCode(200) ; $this->assertRequest($request); @@ -131,10 +132,10 @@ public function testConfirmCodeOk() // Check if the reset token was created $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertNotEmpty($user->get("resettoken")); - $this->assertNotEmpty($user->get("resettokenexpire")); - $this->assertNotEmpty($user->get("resetcode")); - $this->assertEquals("yes", $user->get("resetallowed")); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertNotEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEquals(User::VALUE_YES, $user->get(User::PROP_RESETALLOWED)); } public function testPasswordResetOk() @@ -146,10 +147,10 @@ public function testPasswordResetOk() $userRepo = Psr11::container()->get(UsersDBDataset::class); $user = $userRepo->getByEmail($email); $this->assertNotNull($user); - $this->assertNotEmpty($user->get("resettoken")); - $this->assertNotEmpty($user->get("resettokenexpire")); - $this->assertNotEmpty($user->get("resetcode")); - $this->assertEquals("yes", $user->get("resetallowed")); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertNotEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertNotEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEquals(User::VALUE_YES, $user->get(User::PROP_RESETALLOWED)); // Execute the request, now with the correct code $request = new FakeApiRequester(); @@ -157,7 +158,11 @@ public function testPasswordResetOk() ->withPsr7Request($this->getPsr7Request()) ->withMethod('POST') ->withPath("/login/resetpassword") - ->withRequestBody(json_encode((["email" => $email, "token" => $user->get("resettoken"), "password" => "new$password"]))) + ->withRequestBody(json_encode([ + "email" => $email, + "token" => $user->get(User::PROP_RESETTOKEN), + "password" => "new$password" + ])) ->assertResponseCode(200) ; $this->assertRequest($request); @@ -166,10 +171,10 @@ public function testPasswordResetOk() $user = $userRepo->getByEmail($email); $this->assertNotNull($user); $this->assertEquals("83bfd34a3ebc0973609f5f2ec0080080286e3879", $user->getPassword()); - $this->assertEmpty($user->get("resettoken")); - $this->assertEmpty($user->get("resettokenexpire")); - $this->assertEmpty($user->get("resetcode")); - $this->assertEmpty($user->get("resetallowed")); + $this->assertEmpty($user->get(User::PROP_RESETTOKEN)); + $this->assertEmpty($user->get(User::PROP_RESETTOKENEXPIRE)); + $this->assertEmpty($user->get(User::PROP_RESETCODE)); + $this->assertEmpty($user->get(User::PROP_RESETALLOWED)); // Restore old password $user->setPassword($password);