You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

75 rivejä
2.7 KiB

  1. /* eslint-env mocha */
  2. import expect, { spyOn } from "expect"
  3. import win from "core/window"
  4. import oauth2Authorize from "core/oauth2-authorize"
  5. import * as utils from "core/utils"
  6. describe("oauth2", function () {
  7. let mockSchema = {
  8. flow: "accessCode",
  9. authorizationUrl: "https://testAuthorizationUrl"
  10. }
  11. let authConfig = {
  12. auth: { schema: { get: (key)=> mockSchema[key] } },
  13. authActions: {},
  14. errActions: {},
  15. configs: { oauth2RedirectUrl: "" },
  16. authConfigs: {}
  17. }
  18. describe("authorize redirect", function () {
  19. it("should build authorize url", function() {
  20. const windowOpenSpy = spyOn(win, "open")
  21. oauth2Authorize(authConfig)
  22. expect(windowOpenSpy.calls.length).toEqual(1)
  23. expect(windowOpenSpy.calls[0].arguments[0]).toMatch("https://testAuthorizationUrl?response_type=code&redirect_uri=&state=")
  24. windowOpenSpy.restore()
  25. })
  26. it("should append query parameters to authorizeUrl with query parameters", function() {
  27. const windowOpenSpy = spyOn(win, "open")
  28. mockSchema.authorizationUrl = "https://testAuthorizationUrl?param=1"
  29. oauth2Authorize(authConfig)
  30. expect(windowOpenSpy.calls.length).toEqual(1)
  31. expect(windowOpenSpy.calls[0].arguments[0]).toMatch("https://testAuthorizationUrl?param=1&response_type=code&redirect_uri=&state=")
  32. windowOpenSpy.restore()
  33. })
  34. it("should send code_challenge when using authorizationCode flow with usePkceWithAuthorizationCodeGrant enabled", function () {
  35. const windowOpenSpy = spyOn(win, "open")
  36. mockSchema.flow = "authorizationCode"
  37. const expectedCodeVerifier = "mock_code_verifier"
  38. const expectedCodeChallenge = "mock_code_challenge"
  39. const generateCodeVerifierSpy = spyOn(utils, "generateCodeVerifier").andReturn(expectedCodeVerifier)
  40. const createCodeChallengeSpy = spyOn(utils, "createCodeChallenge").andReturn(expectedCodeChallenge)
  41. authConfig.authConfigs.usePkceWithAuthorizationCodeGrant = true
  42. oauth2Authorize(authConfig)
  43. expect(win.open.calls.length).toEqual(1)
  44. const actualUrl = new URLSearchParams(win.open.calls[0].arguments[0])
  45. expect(actualUrl.get("code_challenge")).toBe(expectedCodeChallenge)
  46. expect(actualUrl.get("code_challenge_method")).toBe("S256")
  47. expect(createCodeChallengeSpy.calls.length).toEqual(1)
  48. expect(createCodeChallengeSpy.calls[0].arguments[0]).toBe(expectedCodeVerifier)
  49. // The code_verifier should be stored to be able to send in
  50. // on the TokenUrl call
  51. expect(authConfig.auth.codeVerifier).toBe(expectedCodeVerifier)
  52. // Restore spies
  53. windowOpenSpy.restore()
  54. generateCodeVerifierSpy.restore()
  55. createCodeChallengeSpy.restore()
  56. })
  57. })
  58. })