string_token.test.js 616 B

123456789101112131415161718192021222324
  1. const jwt = require("jsonwebtoken");
  2. const assert = require("assert");
  3. const koajwt = require("../lib");
  4. const UnauthorizedError = require("../lib/errors/UnauthorizedError");
  5. const mockContext = require("./context");
  6. describe("string tokens", function() {
  7. let ctx;
  8. beforeEach(() => {
  9. ctx = mockContext();
  10. });
  11. it("should work with a valid string token", async () => {
  12. const secret = "shhhhhh";
  13. const token = jwt.sign("foo", secret);
  14. ctx.headers.authorization = "Bearer " + token;
  15. await koajwt({ secret })(ctx, function() {
  16. assert.equal("foo", ctx.state.user);
  17. });
  18. });
  19. });