|
2 | 2 |
|
3 | 3 | namespace Tests;
|
4 | 4 |
|
| 5 | +use Illuminate\Support\Collection; |
5 | 6 | use Mockery as m;
|
6 | 7 | use BotMan\BotMan\Http\Curl;
|
7 | 8 | use BotMan\BotMan\Users\User;
|
@@ -207,6 +208,161 @@ public function it_calls_left_chat_member_event()
|
207 | 208 | $this->assertSame('Marcel', $event->getPayload()['first_name']);
|
208 | 209 | }
|
209 | 210 |
|
| 211 | + /** @test */ |
| 212 | + public function it_calls_telegram_login_event() |
| 213 | + { |
| 214 | + $token = 'randomtoken'; |
| 215 | + |
| 216 | + $queryParameters = [ |
| 217 | + 'id' => '12345', |
| 218 | + 'first_name' => 'Marcel', |
| 219 | + 'last_name' => 'Pociot', |
| 220 | + 'username' => 'marcelpociot', |
| 221 | + 'photo_url' => 'https://some/picture.jpg', |
| 222 | + 'auth_date' => time() |
| 223 | + ]; |
| 224 | + |
| 225 | + // Calculate hash |
| 226 | + $check = Collection::make($queryParameters) |
| 227 | + ->except('hash') |
| 228 | + ->map(function($value, $key) { |
| 229 | + return $key . '=' . $value; |
| 230 | + }) |
| 231 | + ->values() |
| 232 | + ->sort(); |
| 233 | + $check_string = implode("\n", $check->toArray()); |
| 234 | + |
| 235 | + $secret = hash('sha256', $token, true); |
| 236 | + $hash = hash_hmac('sha256', $check_string, $secret); |
| 237 | + |
| 238 | + $queryParameters['hash'] = $hash; |
| 239 | + |
| 240 | + $request = new Request($queryParameters); |
| 241 | + |
| 242 | + $driver = new TelegramDriver($request, [ |
| 243 | + 'telegram' => [ |
| 244 | + 'token' => $token |
| 245 | + ] |
| 246 | + ], m::mock(Curl::class)); |
| 247 | + |
| 248 | + $event = $driver->hasMatchingEvent(); |
| 249 | + $this->assertInstanceOf(GenericEvent::class, $event); |
| 250 | + $this->assertSame(TelegramDriver::LOGIN_EVENT, $event->getName()); |
| 251 | + $this->assertSame('12345', $event->getPayload()['id']); |
| 252 | + $this->assertSame('Marcel', $event->getPayload()['first_name']); |
| 253 | + $this->assertSame('Pociot', $event->getPayload()['last_name']); |
| 254 | + $this->assertSame('marcelpociot', $event->getPayload()['username']); |
| 255 | + $this->assertSame('https://some/picture.jpg', $event->getPayload()['photo_url']); |
| 256 | + } |
| 257 | + |
| 258 | + /** @test */ |
| 259 | + public function it_does_not_call_telegram_login_event_with_outdated_auth_date() |
| 260 | + { |
| 261 | + $token = 'randomtoken'; |
| 262 | + |
| 263 | + $queryParameters = [ |
| 264 | + 'id' => '12345', |
| 265 | + 'first_name' => 'Marcel', |
| 266 | + 'last_name' => 'Pociot', |
| 267 | + 'username' => 'marcelpociot', |
| 268 | + 'photo_url' => 'https://some/picture.jpg', |
| 269 | + 'auth_date' => time() - 90000 |
| 270 | + ]; |
| 271 | + |
| 272 | + // Calculate hash |
| 273 | + $check = Collection::make($queryParameters) |
| 274 | + ->except('hash') |
| 275 | + ->map(function($value, $key) { |
| 276 | + return $key . '=' . $value; |
| 277 | + }) |
| 278 | + ->values() |
| 279 | + ->sort(); |
| 280 | + $check_string = implode("\n", $check->toArray()); |
| 281 | + |
| 282 | + $secret = hash('sha256', $token, true); |
| 283 | + $hash = hash_hmac('sha256', $check_string, $secret); |
| 284 | + |
| 285 | + $queryParameters['hash'] = $hash; |
| 286 | + |
| 287 | + $request = new Request($queryParameters); |
| 288 | + |
| 289 | + $driver = new TelegramDriver($request, [ |
| 290 | + 'telegram' => [ |
| 291 | + 'token' => $token |
| 292 | + ] |
| 293 | + ], m::mock(Curl::class)); |
| 294 | + |
| 295 | + $this->assertFalse($driver->hasMatchingEvent()); |
| 296 | + } |
| 297 | + |
| 298 | + /** @test */ |
| 299 | + public function it_does_not_call_telegram_login_event_with_invalid_hash() |
| 300 | + { |
| 301 | + $token = 'randomtoken'; |
| 302 | + |
| 303 | + $queryParameters = [ |
| 304 | + 'id' => '12345', |
| 305 | + 'first_name' => 'Marcel', |
| 306 | + 'last_name' => 'Pociot', |
| 307 | + 'username' => 'marcelpociot', |
| 308 | + 'photo_url' => 'https://some/picture.jpg', |
| 309 | + 'auth_date' => time(), |
| 310 | + 'hash' => 'kajhsdkjhaskjdh' |
| 311 | + ]; |
| 312 | + |
| 313 | + $request = new Request($queryParameters); |
| 314 | + |
| 315 | + $driver = new TelegramDriver($request, [ |
| 316 | + 'telegram' => [ |
| 317 | + 'token' => $token |
| 318 | + ] |
| 319 | + ], m::mock(Curl::class)); |
| 320 | + |
| 321 | + $this->assertFalse($driver->hasMatchingEvent()); |
| 322 | + } |
| 323 | + |
| 324 | + /** @test */ |
| 325 | + public function telegram_login_event_has_the_correct_chat_id() |
| 326 | + { |
| 327 | + $token = 'randomtoken'; |
| 328 | + |
| 329 | + $queryParameters = [ |
| 330 | + 'id' => '12345', |
| 331 | + 'first_name' => 'Marcel', |
| 332 | + 'last_name' => 'Pociot', |
| 333 | + 'username' => 'marcelpociot', |
| 334 | + 'photo_url' => 'https://some/picture.jpg', |
| 335 | + 'auth_date' => time() |
| 336 | + ]; |
| 337 | + |
| 338 | + // Calculate hash |
| 339 | + $check = Collection::make($queryParameters) |
| 340 | + ->except('hash') |
| 341 | + ->map(function($value, $key) { |
| 342 | + return $key . '=' . $value; |
| 343 | + }) |
| 344 | + ->values() |
| 345 | + ->sort(); |
| 346 | + $check_string = implode("\n", $check->toArray()); |
| 347 | + |
| 348 | + $secret = hash('sha256', $token, true); |
| 349 | + $hash = hash_hmac('sha256', $check_string, $secret); |
| 350 | + |
| 351 | + $queryParameters['hash'] = $hash; |
| 352 | + |
| 353 | + $request = new Request($queryParameters); |
| 354 | + |
| 355 | + $driver = new TelegramDriver($request, [ |
| 356 | + 'telegram' => [ |
| 357 | + 'token' => $token |
| 358 | + ] |
| 359 | + ], m::mock(Curl::class)); |
| 360 | + |
| 361 | + $message = $driver->getMessages(); |
| 362 | + |
| 363 | + $this->assertSame('12345', $message[0]->getSender()); |
| 364 | + } |
| 365 | + |
210 | 366 | /** @test */
|
211 | 367 | public function it_calls_new_chat_title_event()
|
212 | 368 | {
|
|
0 commit comments