Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9380b6c

Browse files
DIOHz0rajsb85
authored andcommitted
test(client): added lostPassword end point tests
1 parent f0dc63c commit 9380b6c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/unit/Client.php

+64
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
namespace Glpi\Api\Rest\tests\units;
2929

3030
use Glpi\Api\Rest\tests\BaseTestCase;
31+
use GuzzleHttp\Psr7\Response;
3132

3233
/**
3334
* @engine inline
@@ -95,4 +96,67 @@ public function testGetGlpiConfig() {
9596
$this->boolean(property_exists($arrayOfStdClass, 'cfg_glpi'))->isTrue();
9697
}
9798

99+
/**
100+
* @tags testLostPassword
101+
*/
102+
public function testLostPassword() {
103+
$mockedClient = new \mock\Glpi\Api\Rest\Client(GLPI_URL);
104+
105+
// check for bad request
106+
$response = $mockedClient->lostPassword('lorem@ipsum.test');
107+
$this->assertJsonResponse($response, parent::HTTP_BAD_REQUEST);
108+
109+
// check for missing params request
110+
$this->exception(function () use ($mockedClient) {
111+
$mockedClient->lostPassword('lorem@ipsum.test', 'lorem');
112+
})->hasMessage('The recovery and new password are mandatory');
113+
114+
$this->exception(function () use ($mockedClient) {
115+
$mockedClient->lostPassword('lorem@ipsum.test', '', 'lorem');
116+
})->hasMessage('The recovery and new password are mandatory');
117+
118+
// as we can't check for the email message let's mock the response.
119+
$messages = [
120+
'emailSentMessage' => 'An email has been sent to your email address.',
121+
'invalidTokenMessage' => 'Your password reset request has expired or is invalid.',
122+
'successMessage' => 'Reset password successful.',
123+
];
124+
// first call
125+
$mockedClient->getMockController()->request[1] = $this->changeMockedResponse(parent::HTTP_OK,
126+
$messages['emailSentMessage']);
127+
// second call
128+
$mockedClient->getMockController()->request[2] = $this->changeMockedResponse(parent::HTTP_BAD_REQUEST,
129+
$messages['invalidTokenMessage']);
130+
// third call
131+
$mockedClient->getMockController()->request[3] = $this->changeMockedResponse(parent::HTTP_OK,
132+
$messages['successMessage']);
133+
134+
// check for "valid" request for email notification
135+
$response = $mockedClient->lostPassword('lorem@ipsum.test');
136+
$this->assertJsonResponse($response);
137+
$this->string(json_decode($response['body'])[0])->isEqualTo($messages['emailSentMessage']);
138+
139+
// check for "invalid" request for reset password
140+
$response = $mockedClient->lostPassword('lorem@ipsum.test', 'invalidToken', 'newFakePassword');
141+
$this->assertJsonResponse($response, parent::HTTP_BAD_REQUEST);
142+
$this->string(json_decode($response['body'])[0])->isEqualTo($messages['invalidTokenMessage']);
143+
144+
// check for "valid" request for reset password
145+
$response = $mockedClient->lostPassword('lorem@ipsum.test', 'm0ck3dT0k3n', 'newFakePassword');
146+
$this->assertJsonResponse($response);
147+
$this->string(json_decode($response['body'])[0])->isEqualTo($messages['successMessage']);
148+
149+
}
150+
151+
/**
152+
* @param integer $httpStatusCode
153+
* @param string $messages
154+
* @return Response
155+
*/
156+
private function changeMockedResponse($httpStatusCode, $messages) {
157+
return new Response($httpStatusCode,
158+
['Content-Type' => 'application/json; charset=UTF-8'],
159+
json_encode([$messages]));
160+
}
161+
98162
}

0 commit comments

Comments
 (0)