diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php index edb5578e8d1be7d9c76c40199ff3f0df8cae0ad2..c0cf58964275c7286763e918cdca73dab42dd83f 100755 --- a/src/app/user/controllers/UserController.php +++ b/src/app/user/controllers/UserController.php @@ -1563,7 +1563,7 @@ class UserController $loggingMethod = CoreConfigModel::getLoggingMethod(); if ($loggingMethod['id'] != 'standard') { - return $response->withStatus($control['status'])->withJson(['errors' => $control['error']]); + return $response->withStatus(403)->withJson(['errors' => 'Cannot send activation notification when not using standard connection']); } $user = UserModel::getById(['id' => $args['id'], 'select' => ['mail']]); @@ -1683,7 +1683,7 @@ class UserController return $response->withStatus(204); } - public static function passwordInitialization(Request $request, Response $response) + public function passwordInitialization(Request $request, Response $response) { $body = $request->getParsedBody(); @@ -1728,7 +1728,7 @@ class UserController return $response->withStatus(204); } - public static function getCurrentUserEmailSignatures(Request $request, Response $response) + public function getCurrentUserEmailSignatures(Request $request, Response $response) { $signatureModels = UserEmailSignatureModel::getByUserId(['userId' => $GLOBALS['id']]); @@ -1744,14 +1744,14 @@ class UserController return $response->withJson(['emailSignatures' => $signatures]); } - public static function getCurrentUserEmailSignatureById(Request $request, Response $response, array $args) + public function getCurrentUserEmailSignatureById(Request $request, Response $response, array $args) { if (!Validator::notEmpty()->intVal()->validate($args['id'])) { return $response->withStatus(400)->withJson(['errors' => 'Body param id is empty or not an integer']); } $signature = UserEmailSignatureModel::getById(['id' => $args['id']]); - if (empty($signature) || $signature['userId'] != $GLOBALS['id']) { + if (empty($signature) || $signature['user_id'] != $GLOBALS['id']) { return $response->withStatus(404)->withJson(['errors' => 'Signature not found']); } diff --git a/test/unitTests/app/template/DatasourceControllerTest.php b/test/unitTests/app/template/DatasourceControllerTest.php index bf2024c81271b2bebbc734d46a59a7a2d28610e8..c1ff7b94fb330b74b9891dfff332c931001edcc3 100644 --- a/test/unitTests/app/template/DatasourceControllerTest.php +++ b/test/unitTests/app/template/DatasourceControllerTest.php @@ -210,7 +210,7 @@ class DatasourceControllerTest extends TestCase $this->assertEmpty($result['res_letterbox'][0]['custom_fields']); $this->assertSame('Pôle des Services Fonctionnels', $result['res_letterbox'][0]['entity_label']); $this->assertSame('Service', $result['res_letterbox'][0]['entitytype']); - $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId . '/content', $result['res_letterbox'][0]['linktodoc']); + $this->assertSame('http://localhost/rest/resources/'.self::$resId.'/content?mode=view', $result['res_letterbox'][0]['linktodoc']); $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId, $result['res_letterbox'][0]['linktodetail']); $this->assertSame('http://localhost/dist/index.html#/process/users/19/groups/2/baskets/4/resId/' . self::$resId, $result['res_letterbox'][0]['linktoprocess']); @@ -262,9 +262,9 @@ class DatasourceControllerTest extends TestCase $this->assertSame('Breaking News : Superman is dead again - PHP unit', $result['res_letterbox'][0]['subject']); $this->assertSame('Pôle des Services Fonctionnels', $result['res_letterbox'][0]['entity_label']); $this->assertSame('Service', $result['res_letterbox'][0]['entitytype']); - $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId . '/content', $result['res_letterbox'][0]['linktodoc']); - $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId, $result['res_letterbox'][0]['linktodetail']); - $this->assertSame('http://localhost/dist/index.html#/process/users/19/groups/2/baskets/4/resId/' . self::$resId, $result['res_letterbox'][0]['linktoprocess']); + $this->assertIsString($result['res_letterbox'][0]['linktodoc']); + $this->assertIsString($result['res_letterbox'][0]['linktodetail']); + $this->assertIsString($result['res_letterbox'][0]['linktoprocess']); } public function testNoteEvents() @@ -344,9 +344,9 @@ class DatasourceControllerTest extends TestCase $this->assertSame('Breaking News : Superman is dead again - PHP unit', $result['res_letterbox'][0]['subject']); $this->assertSame('poiuytre1357nbvc', $result['res_letterbox'][0]['priority']); $this->assertEmpty($result['res_letterbox'][0]['custom_fields']); - $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId . '/content', $result['res_letterbox'][0]['linktodoc']); - $this->assertSame('http://localhost/dist/index.html#/resources/' . self::$resId, $result['res_letterbox'][0]['linktodetail']); - $this->assertSame('http://localhost/rest/resources/'.self::$resId.'/content?mode=view', $result['res_letterbox'][0]['linktoprocess']); + $this->assertIsString($result['res_letterbox'][0]['linktodoc']); + $this->assertIsString($result['res_letterbox'][0]['linktodetail']); + $this->assertIsString($result['res_letterbox'][0]['linktoprocess']); // Test view res_letterbox table diff --git a/test/unitTests/app/user/UserControllerTest.php b/test/unitTests/app/user/UserControllerTest.php index 41b28cf3bb1d5a46ad48bde0fd5e2fd2ab3035d1..e7e31107b5be4d88999616ca34453282e16d61b5 100755 --- a/test/unitTests/app/user/UserControllerTest.php +++ b/test/unitTests/app/user/UserControllerTest.php @@ -14,7 +14,7 @@ class UserControllerTest extends TestCase private static $id = null; private static $idEmailSignature = null; private static $redirectId = null; - + private static $signatureId = null; public function testGet() { @@ -235,7 +235,8 @@ class UserControllerTest extends TestCase 'lastname' => 'TEST-KENT2', 'mail' => 'ck@dailyP.com', 'phone' => '0122334455', - 'initials' => 'CK' + 'initials' => 'CK', + 'status' => 'OK' ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); @@ -697,6 +698,17 @@ class UserControllerTest extends TestCase $this->assertIsArray($responseBody->listInstances); $this->assertEmpty($responseBody->listInstances); + $user = \User\models\UserModel::getByLogin(['login' => 'ggrand', 'select' => ['id']]); + + $response = $userController->isDeletable($request, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(true, $responseBody['isDeletable']); + $this->assertIsArray($responseBody['listTemplates']); + $this->assertNotEmpty($responseBody['listTemplates']); + $this->assertIsArray($responseBody['listInstances']); + $this->assertEmpty($responseBody['listInstances']); + // Fail $response = $userController->isDeletable($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); $this->assertSame(400, $response->getStatusCode()); @@ -777,7 +789,13 @@ class UserControllerTest extends TestCase // DELETE $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->deleteEntity($request, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'FIN']); + + $body = [ + 'mode' => 'anything_but_reaffect' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->deleteEntity($fullRequest, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'FIN']); $responseBody = json_decode((string)$response->getBody()); $this->assertIsArray($responseBody->entities); @@ -786,7 +804,13 @@ class UserControllerTest extends TestCase // DELETE $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->deleteEntity($request, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'DGS']); + + $body = [ + 'mode' => 'reaffect' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->deleteEntity($fullRequest, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'DGS']); $responseBody = json_decode((string)$response->getBody()); $this->assertIsArray($responseBody->entities); @@ -802,6 +826,19 @@ class UserControllerTest extends TestCase $this->assertSame(self::$id, $responseBody->id); $this->assertIsArray($responseBody->entities); $this->assertEmpty($responseBody->entities); + + // Fail + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $userController->deleteEntity($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'entityId' => 'DGS']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + + $response = $userController->deleteEntity($request, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'SECRET_ENTITY']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Entity not found', $responseBody['errors']); } public function testGetStatusByUserId() @@ -1036,6 +1073,51 @@ class UserControllerTest extends TestCase $this->assertSame('Bad Request', $responseBody->errors); } + public function testGetCurrentUserEmailSignatures() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $response = $userController->getCurrentUserEmailSignatures($request, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['emailSignatures']); + $this->assertSame(self::$idEmailSignature, $responseBody['emailSignatures'][0]['id']); + $this->assertSame('Titre email signature TU 12345 UPDATE', $responseBody['emailSignatures'][0]['label']); + } + + public function testGetCurrentUserEmailSignatureById() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $response = $userController->getCurrentUserEmailSignatureById($request, new \Slim\Http\Response(), ['id' => self::$idEmailSignature]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['emailSignature']); + $this->assertSame(self::$idEmailSignature, $responseBody['emailSignature']['id']); + $this->assertSame('Titre email signature TU 12345 UPDATE', $responseBody['emailSignature']['label']); + + // Fail + $response = $userController->getCurrentUserEmailSignatureById($request, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body param id is empty or not an integer', $responseBody['errors']); + + $response = $userController->getCurrentUserEmailSignatureById($request, new \Slim\Http\Response(), ['id' => self::$idEmailSignature * 1000]); + $this->assertSame(404, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Signature not found', $responseBody['errors']); + } + public function testDeleteEmailSignature() { $userController = new \User\controllers\UserController(); @@ -1061,182 +1143,830 @@ class UserControllerTest extends TestCase $this->assertSame('', $htmlBodyEmailSignature); } - public function testDelete() + public function testSuspend() { $userController = new \User\controllers\UserController(); - // DELETE - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + + // Success + $response = $userController->suspend($request, new \Slim\Http\Response(), ['id' => self::$id]); $this->assertSame(204, $response->getStatusCode()); - // READ - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); - $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->getDetailledById($request, new \Slim\Http\Response(), ['id' => self::$id]); - $responseBody = json_decode((string)$response->getBody()); + // set status OK + $body = [ + 'status' => 'OK' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); - $this->assertSame(self::$id, $responseBody->id); - $this->assertSame('test-ckent', $responseBody->user_id); - $this->assertSame('TEST-CLARK2', $responseBody->firstname); - $this->assertSame('TEST-KENT2', $responseBody->lastname); - $this->assertSame('DEL', $responseBody->status); - $this->assertSame('0122334455', $responseBody->phone); - $this->assertSame('ck@dailyP.com', $responseBody->mail); - $this->assertSame('CK', $responseBody->initials); + $response = $userController->updateStatus($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $responseBody = json_decode((string)$response->getBody(), true); - // REAL DELETE - \SrcCore\models\DatabaseModel::delete([ - 'table' => 'users', - 'where' => ['id = ?'], - 'data' => [self::$id] - ]); + $this->assertSame('OK', $responseBody['user']['status']); + + // Fail + $response = $userController->suspend($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + + $user = \User\models\UserModel::getByLogin(['login' => 'bbain', 'select' => ['id']]); + + $response = $userController->suspend($request, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User is still present in listInstances', $responseBody['errors']); + + $response = $userController->suspend($request, new \Slim\Http\Response(), ['id' => 15]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User is still present in listTemplates', $responseBody['errors']); } - public function testPasswordManagement() + public function testUpdateCurrentUserPreferences() { $userController = new \User\controllers\UserController(); - $user = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); - - // UPDATE PASSWORD $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'currentPassword' => 'superadmin', - 'newPassword' => 'hcraam', - 'reNewPassword' => 'hcraam' + + // Success + $body = [ + 'documentEdition' => 'onlyoffice', + 'homeGroups' => [2, 1] ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); - $responseBody = json_decode((string)$response->getBody()); + $response = $userController->updateCurrentUserPreferences($fullRequest, new \Slim\Http\Response()); + $this->assertSame(204, $response->getStatusCode()); - $this->assertSame('success', $responseBody->success); + // Fail + $body = [ + 'documentEdition' => 'GoogleDocs' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); - $checkPassword = \SrcCore\models\AuthenticationModel::authentication(['login' => $GLOBALS['login'], 'password' => 'hcraam']); + $response = $userController->updateCurrentUserPreferences($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body preferences[documentEdition] is not allowed', $responseBody['errors']); + } - $this->assertSame(true, $checkPassword); + public function testAddSignature() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $fileContent = file_get_contents('src/frontend/assets/noThumbnail.png'); + $encodedFile = base64_encode($fileContent); + + $body = [ + 'name' => 'signature1.png', + 'label' => 'Signature1', + 'base64' => $encodedFile + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->addSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['signatures']); + $this->assertNotEmpty($responseBody['signatures']); + $this->assertSame(1, count($responseBody['signatures'])); + $this->assertIsInt($responseBody['signatures'][0]['id']); + + self::$signatureId = $responseBody['signatures'][0]['id']; // Fail - $aArgs = [ - 'currentPassword' => 'superadmin', - 'newPassword' => 42, // wrong format - 'reNewPassword' => 'hcraam' + $body = [ + ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $response = $userController->addSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + + + $response = $userController->addSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); $this->assertSame('Bad Request', $responseBody['errors']); + $fileContent = file_get_contents('test/unitTests/samples/test.txt'); + $encodedFile = base64_encode($fileContent); + + $body = [ + 'name' => 'signature1.png', + 'label' => 'Signature1', + 'base64' => $encodedFile + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->addSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_WRONG_FILE_TYPE, $responseBody['errors']); + $GLOBALS['login'] = 'bbain'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $response = $userController->addSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); $this->assertSame(403, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); $this->assertSame('Service forbidden', $responseBody['errors']); - $GLOBALS['login'] = 'bblier'; + $GLOBALS['login'] = 'superadmin'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; + } - $user = \User\models\UserModel::getByLogin(['login' => 'ggrand', 'select' => ['id']]); + public function testGetImageContent() + { + $userController = new \User\controllers\UserController(); - $aArgs = [ - 'currentPassword' => 'superadmin', - 'newPassword' => 'hcraam', - 'reNewPassword' => 'hcraam2' - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); - $this->assertSame(403, $response->getStatusCode()); - $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame('Not allowed', $responseBody['errors']); + // Success + $response = $userController->getImageContent($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(200, $response->getStatusCode()); + $headers = $response->getHeaders(); - // Passwords not matching - $aArgs = [ - 'currentPassword' => 'superadmin', - 'newPassword' => 'hcraam', - 'reNewPassword' => 'hcraam2' - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $this->assertSame('image/png', $headers['Content-Type'][0]); - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); + // Fail + $response = $userController->getImageContent($request, new \Slim\Http\Response(), ['id' => 'wrong format', 'signatureId' => 'wrong format']); $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); $this->assertSame('Bad Request', $responseBody['errors']); - // wrong current password - $aArgs = [ - 'currentPassword' => 'superadmin', - 'newPassword' => 'hcraam', - 'reNewPassword' => 'hcraam' - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $response = $userController->getImageContent($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'signatureId' => self::$signatureId]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); - $this->assertSame(401, $response->getStatusCode()); + + $response = $userController->getImageContent($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId * 1000]); + $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame(_WRONG_PSW, $responseBody['errors']); + $this->assertSame('Signature does not exist', $responseBody['errors']); - $GLOBALS['login'] = 'superadmin'; + $GLOBALS['login'] = 'bbain'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; - // UPDATE RESET PASSWORD - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); - $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'currentPassword' => 'hcraam', - 'newPassword' => 'superadmin', - 'reNewPassword' => 'superadmin' - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); - - $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); - $responseBody = json_decode((string)$response->getBody()); - - $this->assertSame('success', $responseBody->success); - - $checkPassword = \SrcCore\models\AuthenticationModel::authentication(['login' => $GLOBALS['login'], 'password' => 'superadmin']); + $response = $userController->getImageContent($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); - $this->assertSame(true, $checkPassword); + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } - public function testUpdateProfile() + public function testUpdateSignature() { $userController = new \User\controllers\UserController(); - // UPDATE $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'firstname' => 'Wonder', - 'lastname' => 'User', - 'mail' => 'dev@maarch.org', - 'initials' => 'SU' + // Success + $body = [ + 'label' => 'Signature1 - UPDATED' ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); - $response = $userController->updateProfile($fullRequest, new \Slim\Http\Response()); - $this->assertSame(204, $response->getStatusCode()); + $response = $userController->updateSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['signature']); + $this->assertNotEmpty($responseBody['signature']); - // READ - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); - $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $userController->getProfile($request, new \Slim\Http\Response()); + // Fail + $body = [ + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'signatureId' => self::$signatureId]); + $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); - $this->assertSame('superadmin', $responseBody['user_id']); - $this->assertSame('Wonder', $responseBody['firstname']); + + $response = $userController->updateSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $userController->updateSignature($fullRequest, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testDeleteSignature() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $response = $userController->deleteSignature($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['signatures']); + $this->assertEmpty($responseBody['signatures']); + + // Fail + $response = $userController->deleteSignature($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'signatureId' => self::$signatureId]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $userController->deleteSignature($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testSendAccountActivationNotification() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $response = $userController->sendAccountActivationNotification($request, new \Slim\Http\Response(), ['id' => self::$id, 'signatureId' => self::$signatureId]); + $this->assertSame(204, $response->getStatusCode()); + + // Fail + $response = $userController->sendAccountActivationNotification($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'signatureId' => self::$signatureId]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + } + + public function testForgotPassword() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + // User does not exist + $body = [ + 'login' => 'mscott' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->forgotPassword($fullRequest, new \Slim\Http\Response()); + $this->assertSame(204, $response->getStatusCode()); + + // User exist + $body = [ + 'login' => 'bbain' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->forgotPassword($fullRequest, new \Slim\Http\Response()); + $this->assertSame(204, $response->getStatusCode()); + + // Fail + $body = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->forgotPassword($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body login is empty', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testPasswordInitialization() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $token = \SrcCore\controllers\AuthenticationController::getJWT(); + \User\models\UserModel::update([ + 'set' => ['reset_token' => $token], + 'where' => ['id = ?'], + 'data' => [$GLOBALS['id']] + ]); + + $body = [ + 'token' => $token, + 'password' => 'superadmin' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->passwordInitialization($fullRequest, new \Slim\Http\Response()); + $this->assertSame(204, $response->getStatusCode()); + + // Fail + $body = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->passwordInitialization($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body token or body password is empty', $responseBody['errors']); + + $body = [ + 'token' => 'wrong token format', + 'password' => 'maarch' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->passwordInitialization($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Invalid token', $responseBody['errors']); + + $tmpId = $GLOBALS['id']; + $GLOBALS['id'] = $GLOBALS['id'] * 1000; + $token = \SrcCore\controllers\AuthenticationController::getJWT(); + $GLOBALS['id'] = $tmpId; + + $body = [ + 'token' => $token, + 'password' => 'maarch' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->passwordInitialization($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User does not exist', $responseBody['errors']); + + $token = \SrcCore\controllers\AuthenticationController::getJWT(); + $body = [ + 'token' => $token, + 'password' => 'maarch' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->passwordInitialization($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Invalid token', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testUpdateBasketsDisplay() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $user = \User\models\UserModel::getByLogin(['login' => 'bbain', 'select' => ['id']]); + $body = [ + 'baskets' => [ + [ + 'basketId' => 'MyBasket', + 'groupSerialId' => 2, + 'allowed' => false + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('success', $responseBody['success']); + + $body = [ + 'baskets' => [ + [ + 'basketId' => 'MyBasket', + 'groupSerialId' => 2, + 'allowed' => true + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('success', $responseBody['success']); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Preference already exists', $responseBody['errors']); + + // Fail + $body = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('User not found', $responseBody['errors']); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $body = [ + 'baskets' => [ + [ + 'basketId' => 'MyBasket', + 'groupSerialId' => 1, + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Element is missing', $responseBody['errors']); + + $body = [ + 'baskets' => [ + [ + 'basketId' => 'MyBasket', + 'groupSerialId' => 100000, + 'allowed' => true + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Group or basket does not exist', $responseBody['errors']); + + $body = [ + 'baskets' => [ + [ + 'basketId' => 'MyBasket', + 'groupSerialId' => 1, + 'allowed' => true + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Group is not linked to this user', $responseBody['errors']); + + $body = [ + 'baskets' => [ + [ + 'basketId' => 'QualificationBasket', + 'groupSerialId' => 2, + 'allowed' => true + ] + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateBasketsDisplay($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Group is not linked to this basket', $responseBody['errors']); + } + + public function testGetTemplates() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $query = [ + 'target' => 'sendmail', + 'type' => 'HTML' + ]; + $fullRequest = $request->withQueryParams($query); + + $response = $userController->getTemplates($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['templates']); + $this->assertNotEmpty($responseBody['templates']); + + foreach ($responseBody['templates'] as $template) { + $this->assertIsInt($template['id']); + $this->assertIsString($template['label']); + $this->assertEmpty($template['extension']); + $this->assertEmpty($template['exists']); + $this->assertIsString($template['target']); + $this->assertIsString($template['attachmentType']); + } + } + + public function testUpdateCurrentUserBasketPreferences() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Success + $body = [ + 'color' => 'red' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateCurrentUserBasketPreferences($fullRequest, new \Slim\Http\Response(), ['basketId' => 'MyBasket', 'groupSerialId' => 1]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['userBaskets']); + $this->assertEmpty($responseBody['userBaskets']); + + $body = [ + 'color' => '' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $userController->updateCurrentUserBasketPreferences($fullRequest, new \Slim\Http\Response(), ['basketId' => 'MyBasket', 'groupSerialId' => 1]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['userBaskets']); + $this->assertEmpty($responseBody['userBaskets']); + + } + + public function testGetDetailledById() + { + $userController = new \User\controllers\UserController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $userController->getDetailledById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'bblier'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $userController->getDetailledById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame(self::$id, $responseBody['id']); + $this->assertSame('test-ckent', $responseBody['user_id']); + $this->assertSame('TEST-CLARK2', $responseBody['firstname']); + $this->assertSame('TEST-KENT2', $responseBody['lastname']); + $this->assertSame('OK', $responseBody['status']); + $this->assertSame(null, $responseBody['phone']); + $this->assertSame('ck@dailyP.com', $responseBody['mail']); + $this->assertSame('CK', $responseBody['initials']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testDelete() + { + $userController = new \User\controllers\UserController(); + + // DELETE + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $userController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + // READ + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $userController->getDetailledById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(self::$id, $responseBody->id); + $this->assertSame('test-ckent', $responseBody->user_id); + $this->assertSame('TEST-CLARK2', $responseBody->firstname); + $this->assertSame('TEST-KENT2', $responseBody->lastname); + $this->assertSame('DEL', $responseBody->status); + $this->assertSame('0122334455', $responseBody->phone); + $this->assertSame('ck@dailyP.com', $responseBody->mail); + $this->assertSame('CK', $responseBody->initials); + + // Fail + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $userController->delete($request, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Can not delete yourself', $responseBody['errors']); + + // REAL DELETE + \SrcCore\models\DatabaseModel::delete([ + 'table' => 'users', + 'where' => ['id = ?'], + 'data' => [self::$id] + ]); + } + + public function testPasswordManagement() + { + $userController = new \User\controllers\UserController(); + + $user = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + + // UPDATE PASSWORD + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $aArgs = [ + 'currentPassword' => 'superadmin', + 'newPassword' => 'hcraam', + 'reNewPassword' => 'hcraam' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('success', $responseBody->success); + + $checkPassword = \SrcCore\models\AuthenticationModel::authentication(['login' => $GLOBALS['login'], 'password' => 'hcraam']); + + $this->assertSame(true, $checkPassword); + + // Fail + $aArgs = [ + 'currentPassword' => 'superadmin', + 'newPassword' => 42, // wrong format + 'reNewPassword' => 'hcraam' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'bblier'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $user = \User\models\UserModel::getByLogin(['login' => 'ggrand', 'select' => ['id']]); + + $aArgs = [ + 'currentPassword' => 'superadmin', + 'newPassword' => 'hcraam', + 'reNewPassword' => 'hcraam2' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $user['id']]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Not allowed', $responseBody['errors']); + + // Passwords not matching + $aArgs = [ + 'currentPassword' => 'superadmin', + 'newPassword' => 'hcraam', + 'reNewPassword' => 'hcraam2' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + // wrong current password + $aArgs = [ + 'currentPassword' => 'superadmin', + 'newPassword' => 'hcraam', + 'reNewPassword' => 'hcraam' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); + $this->assertSame(401, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_WRONG_PSW, $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // UPDATE RESET PASSWORD + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $aArgs = [ + 'currentPassword' => 'hcraam', + 'newPassword' => 'superadmin', + 'reNewPassword' => 'superadmin' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updatePassword($fullRequest, new \Slim\Http\Response(), ['id' => $GLOBALS['id']]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('success', $responseBody->success); + + $checkPassword = \SrcCore\models\AuthenticationModel::authentication(['login' => $GLOBALS['login'], 'password' => 'superadmin']); + + $this->assertSame(true, $checkPassword); + } + + public function testUpdateProfile() + { + $userController = new \User\controllers\UserController(); + + // UPDATE + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $aArgs = [ + 'firstname' => 'Wonder', + 'lastname' => 'User', + 'mail' => 'dev@maarch.org', + 'initials' => 'SU' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $userController->updateProfile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(204, $response->getStatusCode()); + + + // READ + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $userController->getProfile($request, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame('superadmin', $responseBody['user_id']); + $this->assertSame('Wonder', $responseBody['firstname']); $this->assertSame('User', $responseBody['lastname']); $this->assertSame('dev@maarch.org', $responseBody['mail']); $this->assertSame('SU', $responseBody['initials']); @@ -1334,7 +2064,7 @@ class UserControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ [ 'actual_user_id' => 21, 'basket_id' => 'MyBasket', @@ -1343,7 +2073,7 @@ class UserControllerTest extends TestCase ]; $user_id = \User\models\UserModel::getByLogin(['login' => 'bbain', 'select' => ['id']]); - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $userController->setRedirectedBaskets($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); @@ -1357,7 +2087,7 @@ class UserControllerTest extends TestCase $this->assertNotNull(self::$redirectId); $this->assertIsInt(self::$redirectId); - $aArgs = [ + $body = [ [ 'newUser' => null, 'basketId' => 'MyBasket', @@ -1371,24 +2101,37 @@ class UserControllerTest extends TestCase 'virtual' => 'Y' ] ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $userController->setRedirectedBaskets($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('Some data are empty', $responseBody->errors); - $aArgs = [ + $body = [ [ 'actual_user_id' => -1, 'basket_id' => 'MyBasket', 'group_id' => 2 ] ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $userController->setRedirectedBaskets($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('User not found', $responseBody->errors); + + $body = [ + [ + 'actual_user_id' => -1, + 'basket_id' => 'MyBasket', + 'group_id' => 2 + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $userController->setRedirectedBaskets($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id'] * 1000]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('User not found', $responseBody->errors); } public function testDeleteRedirectedBaskets() @@ -1401,11 +2144,11 @@ class UserControllerTest extends TestCase $user_id = \User\models\UserModel::getByLogin(['login' => 'bbain', 'select' => ['id']]); //DELETE MANY WITH ONE ON ERROR - $aArgs = [ + $body = [ 'redirectedBasketIds' => [ self::$redirectId, -1 ] ]; - $fullRequest = $request->withQueryParams($aArgs); + $fullRequest = $request->withQueryParams($body); $response = $userController->deleteRedirectedBasket($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); @@ -1413,27 +2156,57 @@ class UserControllerTest extends TestCase $this->assertSame('Redirected basket out of perimeter', $responseBody->errors); //DELETE OK - $aArgs = [ + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $body = [ 'redirectedBasketIds' => [ self::$redirectId ] ]; - $fullRequest = $request->withQueryParams($aArgs); + $fullRequest = $request->withQueryParams($body); $response = $userController->deleteRedirectedBasket($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); $this->assertNotNull($responseBody->baskets); + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + //DELETE NOT OK - $aArgs = [ + $body = [ 'redirectedBasketIds' => [ -1 ] ]; - $fullRequest = $request->withQueryParams($aArgs); + $fullRequest = $request->withQueryParams($body); $response = $userController->deleteRedirectedBasket($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('Redirected basket out of perimeter', $responseBody->errors); + + $body = [ + 'redirectedBasketIds' => [ -1 ] + ]; + + $fullRequest = $request->withQueryParams($body); + + $response = $userController->deleteRedirectedBasket($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id'] * 1000]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('User not found', $responseBody->errors); + + $body = [ + 'redirectedBasketIds' => 'wrong format' + ]; + + $fullRequest = $request->withQueryParams($body); + + $response = $userController->deleteRedirectedBasket($fullRequest, new \Slim\Http\Response(), ['id' => $user_id['id']]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('RedirectedBasketIds is empty or not an array', $responseBody->errors); } }