|
277 | 277 | "script": {
|
278 | 278 | "exec": [
|
279 | 279 | "var jsonData = pm.response.json();",
|
280 |
| - "if (jsonData.token) {", |
281 |
| - " pm.environment.set('jwt_token', jsonData.token);", |
| 280 | + "if (jsonData.access_token) {", |
| 281 | + " pm.environment.set('jwt_token', jsonData.access_token);", |
282 | 282 | " console.log('JWT token saved to environment');",
|
283 | 283 | "}",
|
| 284 | + "if (jsonData.refresh_token) {", |
| 285 | + " pm.environment.set('refresh_token', jsonData.refresh_token);", |
| 286 | + " console.log('Refresh token saved to environment');", |
| 287 | + "}", |
284 | 288 | "",
|
285 | 289 | "pm.test('Status code is 200', function() {",
|
286 | 290 | " pm.response.to.have.status(200);",
|
287 | 291 | "});",
|
288 | 292 | "",
|
289 |
| - "pm.test('Response has token', function() {", |
290 |
| - " pm.expect(jsonData.token).to.exist;", |
| 293 | + "pm.test('Response has access token', function() {", |
| 294 | + " pm.expect(jsonData.access_token).to.exist;", |
| 295 | + "});", |
| 296 | + "", |
| 297 | + "pm.test('Response has refresh token', function() {", |
| 298 | + " pm.expect(jsonData.refresh_token).to.exist;", |
291 | 299 | "});"
|
292 | 300 | ],
|
293 | 301 | "type": "text/javascript"
|
|
317 | 325 | }
|
318 | 326 | },
|
319 | 327 | {
|
320 |
| - "name": "5. Get Todos with JWT", |
| 328 | + "name": "5. Refresh Token While Logged In", |
| 329 | + "event": [ |
| 330 | + { |
| 331 | + "listen": "test", |
| 332 | + "script": { |
| 333 | + "exec": [ |
| 334 | + "var jsonData = pm.response.json();", |
| 335 | + "pm.test('Status code is 200', function() {", |
| 336 | + " pm.response.to.have.status(200);", |
| 337 | + "});", |
| 338 | + "", |
| 339 | + "pm.test('Response has new access token', function() {", |
| 340 | + " pm.expect(jsonData.access_token).to.exist;", |
| 341 | + " pm.environment.set('jwt_token', jsonData.access_token);", |
| 342 | + " console.log('New access token saved to environment');", |
| 343 | + "});" |
| 344 | + ], |
| 345 | + "type": "text/javascript" |
| 346 | + } |
| 347 | + } |
| 348 | + ], |
| 349 | + "request": { |
| 350 | + "method": "POST", |
| 351 | + "header": [ |
| 352 | + { |
| 353 | + "key": "Content-Type", |
| 354 | + "value": "application/json", |
| 355 | + "type": "text" |
| 356 | + } |
| 357 | + ], |
| 358 | + "body": { |
| 359 | + "mode": "raw", |
| 360 | + "raw": "{\n \"refresh_token\": \"{{refresh_token}}\"\n}" |
| 361 | + }, |
| 362 | + "url": { |
| 363 | + "raw": "http://localhost:8000/auth/refresh", |
| 364 | + "protocol": "http", |
| 365 | + "host": ["localhost"], |
| 366 | + "port": "8000", |
| 367 | + "path": ["auth", "refresh"] |
| 368 | + } |
| 369 | + } |
| 370 | + }, |
| 371 | + { |
| 372 | + "name": "6. Get Todos with JWT", |
321 | 373 | "event": [
|
322 | 374 | {
|
323 | 375 | "listen": "test",
|
|
350 | 402 | }
|
351 | 403 | },
|
352 | 404 | {
|
353 |
| - "name": "6. Logout with JWT", |
| 405 | + "name": "7. Logout with JWT", |
354 | 406 | "event": [
|
355 | 407 | {
|
356 | 408 | "listen": "test",
|
357 | 409 | "script": {
|
358 | 410 | "exec": [
|
| 411 | + "// Save the current token as blacklisted_token for later tests", |
| 412 | + "pm.environment.set('blacklisted_token', pm.environment.get('jwt_token'));", |
| 413 | + "", |
359 | 414 | "pm.test('Status code is 200', function() {",
|
360 | 415 | " pm.response.to.have.status(200);",
|
| 416 | + "});", |
| 417 | + "", |
| 418 | + "pm.test('Logout successful message', function() {", |
| 419 | + " var jsonData = pm.response.json();", |
| 420 | + " pm.expect(jsonData.message).to.equal('Logout successful');", |
361 | 421 | "});"
|
362 | 422 | ],
|
363 | 423 | "type": "text/javascript"
|
|
366 | 426 | ],
|
367 | 427 | "request": {
|
368 | 428 | "method": "POST",
|
| 429 | + "header": [ |
| 430 | + { |
| 431 | + "key": "Content-Type", |
| 432 | + "value": "application/json", |
| 433 | + "type": "text" |
| 434 | + }, |
| 435 | + { |
| 436 | + "key": "Authorization", |
| 437 | + "value": "Bearer {{jwt_token}}", |
| 438 | + "type": "text" |
| 439 | + } |
| 440 | + ], |
| 441 | + "body": { |
| 442 | + "mode": "raw", |
| 443 | + "raw": "{\n \"refresh_token\": \"{{refresh_token}}\"\n}" |
| 444 | + }, |
369 | 445 | "url": {
|
370 | 446 | "raw": "http://localhost:8000/auth/logout",
|
371 | 447 | "protocol": "http",
|
|
376 | 452 | }
|
377 | 453 | },
|
378 | 454 | {
|
379 |
| - "name": "7. Try to Get Todos with Invalid JWT (Should Fail)", |
| 455 | + "name": "8. Try to Get Todos with Invalid JWT (Should Fail)", |
380 | 456 | "event": [
|
381 | 457 | {
|
382 | 458 | "listen": "test",
|
|
388 | 464 | "",
|
389 | 465 | "pm.test('Error message is correct', function() {",
|
390 | 466 | " var jsonData = pm.response.json();",
|
391 |
| - " pm.expect(jsonData.error).to.contain('Invalid JWT token');", |
| 467 | + " pm.expect(jsonData.error).to.include('Invalid JWT token');", |
392 | 468 | "});"
|
393 | 469 | ],
|
394 | 470 | "type": "text/javascript"
|
|
414 | 490 | }
|
415 | 491 | },
|
416 | 492 | {
|
417 |
| - "name": "8. Get API Documentation with JWT", |
| 493 | + "name": "9. Try to Refresh with Invalidated Token (Should Fail)", |
| 494 | + "event": [ |
| 495 | + { |
| 496 | + "listen": "test", |
| 497 | + "script": { |
| 498 | + "exec": [ |
| 499 | + "pm.test('Status code is 401', function() {", |
| 500 | + " pm.response.to.have.status(401);", |
| 501 | + "});", |
| 502 | + "", |
| 503 | + "pm.test('Error message is correct', function() {", |
| 504 | + " var jsonData = pm.response.json();", |
| 505 | + " pm.expect(jsonData.error).to.equal('Invalid refresh token');", |
| 506 | + "});" |
| 507 | + ], |
| 508 | + "type": "text/javascript" |
| 509 | + } |
| 510 | + } |
| 511 | + ], |
| 512 | + "request": { |
| 513 | + "method": "POST", |
| 514 | + "header": [ |
| 515 | + { |
| 516 | + "key": "Content-Type", |
| 517 | + "value": "application/json", |
| 518 | + "type": "text" |
| 519 | + } |
| 520 | + ], |
| 521 | + "body": { |
| 522 | + "mode": "raw", |
| 523 | + "raw": "{\n \"refresh_token\": \"{{refresh_token}}\"\n}" |
| 524 | + }, |
| 525 | + "url": { |
| 526 | + "raw": "http://localhost:8000/auth/refresh", |
| 527 | + "protocol": "http", |
| 528 | + "host": ["localhost"], |
| 529 | + "port": "8000", |
| 530 | + "path": ["auth", "refresh"] |
| 531 | + } |
| 532 | + } |
| 533 | + }, |
| 534 | + { |
| 535 | + "name": "10. Try to Use Blacklisted Token (Should Fail)", |
| 536 | + "event": [ |
| 537 | + { |
| 538 | + "listen": "test", |
| 539 | + "script": { |
| 540 | + "exec": [ |
| 541 | + "pm.test('Status code is 401', function() {", |
| 542 | + " pm.response.to.have.status(401);", |
| 543 | + "});", |
| 544 | + "", |
| 545 | + "pm.test('Error message is correct', function() {", |
| 546 | + " var jsonData = pm.response.json();", |
| 547 | + " pm.expect(jsonData.error).to.equal('You have been logged out. Please log in again.');", |
| 548 | + "});" |
| 549 | + ], |
| 550 | + "type": "text/javascript" |
| 551 | + } |
| 552 | + } |
| 553 | + ], |
| 554 | + "request": { |
| 555 | + "method": "GET", |
| 556 | + "header": [ |
| 557 | + { |
| 558 | + "key": "Authorization", |
| 559 | + "value": "Bearer {{blacklisted_token}}", |
| 560 | + "type": "text" |
| 561 | + } |
| 562 | + ], |
| 563 | + "url": { |
| 564 | + "raw": "http://localhost:8000/todos", |
| 565 | + "protocol": "http", |
| 566 | + "host": ["localhost"], |
| 567 | + "port": "8000", |
| 568 | + "path": ["todos"] |
| 569 | + } |
| 570 | + } |
| 571 | + }, |
| 572 | + { |
| 573 | + "name": "11. Get API Documentation with JWT", |
418 | 574 | "event": [
|
419 | 575 | {
|
420 | 576 | "listen": "test",
|
|
0 commit comments