|
| 1 | +const auth0 = require("auth0"); |
| 2 | +const _ = require("lodash"); |
| 3 | +const eventObj = require("./modules/event.json"); |
| 4 | +const { emailUser, ldapUser } = require("./modules/users.js"); |
| 5 | +const { |
| 6 | + onExecutePreUserRegistration, |
| 7 | +} = require("../actions/denyRegistrationByEmail.js"); |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + _event = _.cloneDeep(eventObj); |
| 11 | + _event.secrets = { |
| 12 | + mgmtClientId: "fake", |
| 13 | + mgmtClientSecrets: "fake", |
| 14 | + }; |
| 15 | + _emailUser = _.cloneDeep(emailUser); |
| 16 | + |
| 17 | + api = { |
| 18 | + access: { |
| 19 | + deny: jest.fn(), |
| 20 | + }, |
| 21 | + }; |
| 22 | + |
| 23 | + mockManagementClient = { |
| 24 | + usersByEmail: { |
| 25 | + getByEmail: jest.fn(), |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + auth0.ManagementClient = jest.fn(() => mockManagementClient); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | +}); |
| 35 | + |
| 36 | +test("Should not deny registration an app we haven't specified", async () => { |
| 37 | + await onExecutePreUserRegistration(_event, api); |
| 38 | + expect(api.access.deny).not.toHaveBeenCalled(); |
| 39 | +}); |
| 40 | + |
| 41 | +test("Should deny a new registration for Matrix", async () => { |
| 42 | + _event.connection.name = "email"; |
| 43 | + _event.client.client_id = "pFf6sBIfp4n3Wcs3F9Q7a9ry8MTrbi2F"; |
| 44 | + mockManagementClient.usersByEmail.getByEmail.mockReturnValue( |
| 45 | + Promise.resolve({ data: [] }) |
| 46 | + ); |
| 47 | + await onExecutePreUserRegistration(_event, api); |
| 48 | + expect(api.access.deny).toHaveBeenCalled(); |
| 49 | +}); |
| 50 | + |
| 51 | +test("Should allow an email login for Matrix", async () => { |
| 52 | + _event.connection.name = "email"; |
| 53 | + _event.client.client_id = "pFf6sBIfp4n3Wcs3F9Q7a9ry8MTrbi2F"; |
| 54 | + mockManagementClient.usersByEmail.getByEmail |
| 55 | + .mockReturnValueOnce(Promise.resolve({ data: [_emailUser] })) |
| 56 | + .mockReturnValueOnce(Promise.resolve({ data: [] })); |
| 57 | + await onExecutePreUserRegistration(_event, api); |
| 58 | + expect(api.access.deny).not.toHaveBeenCalled(); |
| 59 | +}); |
| 60 | + |
| 61 | +test("Should deny an email login for Matrix in the face of an exception", async () => { |
| 62 | + _event.connection.name = "email"; |
| 63 | + _event.client.client_id = "pFf6sBIfp4n3Wcs3F9Q7a9ry8MTrbi2F"; |
| 64 | + mockManagementClient.usersByEmail.getByEmail.mockImplementation(async () => { |
| 65 | + throw new Error("test"); |
| 66 | + }); |
| 67 | + await onExecutePreUserRegistration(_event, api); |
| 68 | + expect(api.access.deny).toHaveBeenCalled(); |
| 69 | +}); |
0 commit comments