Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import expect from "expect";
  2. import sinon from "sinon";
  3.  
  4. // Inline version of function to be tested
  5. const expiredOrExpiresWithin = (decodedJwt, seconds) =>
  6. Date.now() >= (decodedJwt.exp + (-Math.abs(seconds) || 0)) * 1000;
  7.  
  8. describe("jwtUtils", () => {
  9. describe("expiredOrExpiresWithin", () => {
  10. let clock;
  11. let decodedJwt;
  12. beforeEach(() => {
  13. clock = sinon.useFakeTimers(new Date(2018, 7, 29).getTime()); // sets date to Aug 29, 2018 at 00:00 UTC
  14. decodedJwt = {
  15. data: "foobar",
  16. iat: 1535566191,
  17. exp: 1535566197, // Wed Aug 29 2018 18:09:57 GMT+0000 (UTC)
  18. };
  19. /* const d = new Date(0); */
  20. /* d.setUTCSeconds(decodedJwt.exp); */
  21. /* console.log("JWT Date", d); */
  22. });
  23. afterEach(() => {
  24. clock.restore();
  25. });
  26.  
  27. it("should be expired if jwt exp is in the past", () => {
  28. clock.tick(60 * 60 * 19 * 1000); // move the fake time ahead to 19:00:00
  29. /* console.log("Now Date", new Date()); */
  30.  
  31. expect(expiredOrExpiresWithin(decodedJwt, 0)).toBe(true);
  32. });
  33.  
  34. it("should NOT be expired if jwt exp is in the future", () => {
  35. /* console.log("Now Date", new Date()); */
  36.  
  37. expect(expiredOrExpiresWithin(decodedJwt, 0)).toBe(false);
  38. });
  39.  
  40. it("should be expired if jwt exp is in the future out of tolerance (1 minute - dev)", () => {
  41. clock.tick(60 * 60 * 18.16 * 1000); // move the fake time ahead to 18:09:36
  42. /* console.log("Now Date", new Date()); */
  43.  
  44. expect(expiredOrExpiresWithin(decodedJwt, 60)).toBe(true);
  45. });
  46.  
  47. it("should NOT be expired if jwt exp is in the future greater than tolerance", () => {
  48. /* console.log("Now Date", new Date()); */
  49.  
  50. expect(expiredOrExpiresWithin(decodedJwt, 60)).toBe(false);
  51. });
  52.  
  53. it("should be expired if jwt exp is in the future out of tolerance (1 hour - production)", () => {
  54. clock.tick(60 * 60 * 18.16 * 1000); // move the fake time ahead to 18:09:36
  55. /* console.log("Now Date", new Date()); */
  56.  
  57. expect(expiredOrExpiresWithin(decodedJwt, 60 * 60)).toBe(true);
  58. });
  59. });
  60. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement