28
28
namespace Glpi \Api \Rest \tests \units ;
29
29
30
30
use Glpi \Api \Rest \tests \BaseTestCase ;
31
+ use GuzzleHttp \Psr7 \Response ;
31
32
32
33
/**
33
34
* @engine inline
@@ -95,4 +96,67 @@ public function testGetGlpiConfig() {
95
96
$ this ->boolean (property_exists ($ arrayOfStdClass , 'cfg_glpi ' ))->isTrue ();
96
97
}
97
98
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
+
98
162
}
0 commit comments