2
0

webauthn-registration.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2002-2024 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. import "./bootstrap.js";
  18. import { expect, util, Assertion } from "chai";
  19. import { setupRegistration } from "../lib/webauthn-registration.js";
  20. import webauthn from "../lib/webauthn-core.js";
  21. import { assert, fake, match, stub } from "sinon";
  22. describe("webauthn-registration", () => {
  23. before(() => {
  24. Assertion.addProperty("visible", function () {
  25. const obj = util.flag(this, "object");
  26. new Assertion(obj).to.have.nested.property("style.display", "block");
  27. });
  28. Assertion.addProperty("hidden", function () {
  29. const obj = util.flag(this, "object");
  30. new Assertion(obj).to.have.nested.property("style.display", "none");
  31. });
  32. });
  33. describe("bootstrap", () => {
  34. let registerStub;
  35. let registerButton;
  36. let labelField;
  37. let errorPopup;
  38. let successPopup;
  39. let deleteForms;
  40. let ui;
  41. beforeEach(() => {
  42. registerStub = stub(webauthn, "register").resolves(undefined);
  43. errorPopup = {
  44. style: {
  45. display: undefined,
  46. },
  47. textContent: undefined,
  48. };
  49. successPopup = {
  50. style: {
  51. display: undefined,
  52. },
  53. textContent: undefined,
  54. };
  55. registerButton = {
  56. addEventListener: fake(),
  57. };
  58. labelField = {
  59. value: undefined,
  60. };
  61. deleteForms = [];
  62. ui = {
  63. getSuccess: function () {
  64. return successPopup;
  65. },
  66. getError: function () {
  67. return errorPopup;
  68. },
  69. getRegisterButton: function () {
  70. return registerButton;
  71. },
  72. getLabelInput: function () {
  73. return labelField;
  74. },
  75. getDeleteForms: function () {
  76. return deleteForms;
  77. },
  78. };
  79. global.window = {
  80. location: {
  81. href: {},
  82. },
  83. };
  84. global.console = {
  85. error: stub(),
  86. };
  87. });
  88. afterEach(() => {
  89. registerStub.restore();
  90. delete global.window;
  91. });
  92. describe("when webauthn is not supported", () => {
  93. beforeEach(() => {
  94. delete global.window.PublicKeyCredential;
  95. });
  96. it("does not set up a click event listener", async () => {
  97. await setupRegistration({}, "/", ui);
  98. assert.notCalled(registerButton.addEventListener);
  99. });
  100. it("shows an error popup", async () => {
  101. await setupRegistration({}, "/", ui);
  102. expect(errorPopup).to.be.visible;
  103. expect(errorPopup.textContent).to.equal("WebAuthn is not supported");
  104. expect(successPopup).to.be.hidden;
  105. });
  106. });
  107. describe("when webauthn is supported", () => {
  108. beforeEach(() => {
  109. global.window.PublicKeyCredential = fake();
  110. });
  111. it("hides the popups", async () => {
  112. await setupRegistration({}, "/", ui);
  113. expect(successPopup).to.be.hidden;
  114. expect(errorPopup).to.be.hidden;
  115. });
  116. it("sets up a click event listener on the register button", async () => {
  117. await setupRegistration({}, "/some/path", ui);
  118. assert.calledOnceWithMatch(registerButton.addEventListener, "click", match.typeOf("function"));
  119. });
  120. describe(`when the query string contains "success"`, () => {
  121. beforeEach(() => {
  122. global.window.location.search = "?success&continue=true";
  123. });
  124. it("shows the success popup", async () => {
  125. await setupRegistration({}, "/", ui);
  126. expect(successPopup).to.be.visible;
  127. expect(errorPopup).to.be.hidden;
  128. });
  129. });
  130. describe("when the register button is clicked", () => {
  131. const headers = { "x-header": "value" };
  132. const contextPath = "/some/path";
  133. beforeEach(async () => {
  134. await setupRegistration(headers, contextPath, ui);
  135. });
  136. it("hides all the popups", async () => {
  137. successPopup.textContent = "dummy-content";
  138. successPopup.style.display = "block";
  139. errorPopup.textContent = "dummy-content";
  140. errorPopup.style.display = "block";
  141. await registerButton.addEventListener.firstCall.lastArg();
  142. expect(successPopup).to.be.hidden;
  143. expect(errorPopup).to.be.hidden;
  144. });
  145. it("calls register", async () => {
  146. labelField.value = "passkey name";
  147. await registerButton.addEventListener.firstCall.lastArg();
  148. assert.calledOnceWithExactly(registerStub, headers, contextPath, labelField.value);
  149. });
  150. it("navigates to success page", async () => {
  151. labelField.value = "passkey name";
  152. await registerButton.addEventListener.firstCall.lastArg();
  153. expect(global.window.location.href).to.equal(`${contextPath}/webauthn/register?success`);
  154. });
  155. it("handles errors", async () => {
  156. registerStub.rejects(new Error("The registration failed"));
  157. await registerButton.addEventListener.firstCall.lastArg();
  158. expect(errorPopup.textContent).to.equal("The registration failed");
  159. expect(errorPopup).to.be.visible;
  160. expect(successPopup).to.be.hidden;
  161. assert.calledOnceWithMatch(
  162. global.console.error,
  163. match.instanceOf(Error).and(match.has("message", "The registration failed")),
  164. );
  165. });
  166. });
  167. describe("delete", () => {
  168. beforeEach(() => {
  169. global.fetch = fake.resolves({ ok: true });
  170. });
  171. afterEach(() => {
  172. delete global.fetch;
  173. });
  174. it("no errors when no forms", async () => {
  175. await setupRegistration({}, "/some/path", ui);
  176. });
  177. it("sets up forms for fetch", async () => {
  178. const deleteFormOne = {
  179. addEventListener: fake(),
  180. };
  181. const deleteFormTwo = {
  182. addEventListener: fake(),
  183. };
  184. deleteForms = [deleteFormOne, deleteFormTwo];
  185. await setupRegistration({}, "", ui);
  186. assert.calledOnceWithMatch(deleteFormOne.addEventListener, "submit", match.typeOf("function"));
  187. assert.calledOnceWithMatch(deleteFormTwo.addEventListener, "submit", match.typeOf("function"));
  188. });
  189. describe("when the delete button is clicked", () => {
  190. it("calls POST to the form action", async () => {
  191. const contextPath = "/some/path";
  192. const deleteForm = {
  193. addEventListener: fake(),
  194. action: `${contextPath}/webauthn/1234`,
  195. };
  196. deleteForms = [deleteForm];
  197. const headers = {
  198. "X-CSRF-TOKEN": "token",
  199. };
  200. await setupRegistration(headers, contextPath, ui);
  201. const clickEvent = {
  202. preventDefault: fake(),
  203. };
  204. await deleteForm.addEventListener.firstCall.lastArg(clickEvent);
  205. assert.calledOnce(clickEvent.preventDefault);
  206. assert.calledOnceWithExactly(global.fetch, `/some/path/webauthn/1234`, {
  207. method: "DELETE",
  208. headers: {
  209. "Content-Type": "application/json",
  210. ...headers,
  211. },
  212. });
  213. expect(global.window.location.href).to.equal(`/some/path/webauthn/register?success`);
  214. });
  215. });
  216. it("handles errors", async () => {
  217. global.fetch = fake.rejects("Server threw an error");
  218. global.window.location.href = "/initial/location";
  219. const deleteForm = {
  220. addEventListener: fake(),
  221. };
  222. deleteForms = [deleteForm];
  223. await setupRegistration({}, "", ui);
  224. const clickEvent = { preventDefault: fake() };
  225. await deleteForm.addEventListener.firstCall.lastArg(clickEvent);
  226. expect(errorPopup).to.be.visible;
  227. expect(errorPopup.textContent).to.equal("Server threw an error");
  228. // URL does not change
  229. expect(global.window.location.href).to.equal("/initial/location");
  230. });
  231. });
  232. });
  233. });
  234. });