diff --git a/src/__tests__/connection/database/actions.test.js b/src/__tests__/connection/database/actions.test.js index 03d6513f1..3f83b5221 100644 --- a/src/__tests__/connection/database/actions.test.js +++ b/src/__tests__/connection/database/actions.test.js @@ -1,13 +1,31 @@ import Immutable, { List, Map } from 'immutable'; -import { signUp } from '../../../connection/database/actions'; +import { + signUp, + resetPasswordSuccess, + showResetPasswordActivity, + showLoginActivity, showSignUpActivity +} from '../../../connection/database/actions'; import { swap, setEntity } from '../../../store'; +import { swapCaptcha } from "../../../connection/captcha"; const webApiMock = () => require('core/web_api'); const coreActionsMock = () => require('core/actions'); + jest.mock('core/actions', () => ({ validateAndSubmit: jest.fn() })); +jest.mock('../../../connection/captcha', () => { + const originalCaptcha = jest.requireActual('../../../connection/captcha'); + return { + __esModule: true, + ...originalCaptcha, + swapCaptcha: jest.fn((id, flow, wasInvalid, next) => { + next(); + }), + } +}); + jest.mock('core/web_api', () => ({ signUp: jest.fn() })); @@ -208,4 +226,85 @@ describe('database/actions.js', () => { } }); }); -}); + + describe('exported functions', () => { + const id = 2; + const mCaptcha = Immutable.fromJS({ + field: { + email: { + value: 'test@email.com' + }, + password: { + value: 'testpass' + }, + family_name: { + value: 'test-family-name' + }, + given_name: { + value: 'test-given-name' + }, + name: { + value: 'test-name' + }, + nickname: { + value: 'test-nickname' + }, + picture: { + value: 'test-pic' + }, + other_prop: { + value: 'test-other' + } + }, + database: { + additionalSignUpFields: [ + { name: 'family_name', storage: 'root' }, + { name: 'given_name', storage: 'root' }, + { name: 'name', storage: 'root' }, + { name: 'nickname', storage: 'root' }, + { name: 'picture', storage: 'root' }, + { name: 'other_prop' } + ] + }, + captcha: { + provider: 'auth0' + }, + passwordResetCaptcha: { + provider: 'auth0' + }, + }); + + describe('resetPasswordSuccess', () => { + it('runs swap CAPTCHA', () => { + swap(setEntity, 'lock', id, mCaptcha); + resetPasswordSuccess(id); + expect(swapCaptcha.mock.calls.length).toEqual(1); + }); + }); + + describe('showResetPasswordActivity', () => { + it('runs swap CAPTCHA', () => { + swap(setEntity, 'lock', id, mCaptcha); + showResetPasswordActivity(id); + expect(swapCaptcha.mock.calls.length).toEqual(1); + }); + }); + + describe('showLoginActivity', () => { + it('runs swap CAPTCHA', () => { + swap(setEntity, 'lock', id, mCaptcha); + showLoginActivity(id); + expect(swapCaptcha.mock.calls.length).toEqual(1); + }); + }); + + describe('showSignupActivity', () => { + it('runs swap CAPTCHA', () => { + swap(setEntity, 'lock', id, mCaptcha); + showSignUpActivity(id); + expect(swapCaptcha.mock.calls.length).toEqual(1); + }); + }); + }); + }) + diff --git a/src/connection/database/actions.js b/src/connection/database/actions.js index 0e428bd03..e42948e35 100644 --- a/src/connection/database/actions.js +++ b/src/connection/database/actions.js @@ -259,15 +259,17 @@ export function resetPassword(id) { }); } -function resetPasswordSuccess(id) { +export function resetPasswordSuccess(id) { const m = read(getEntity, 'lock', id); if (hasScreen(m, 'login')) { - swap( - updateEntity, - 'lock', - id, - m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field - ); + swapCaptcha(id, Flow.PASSWORD_RESET, false, () => { + swap( + updateEntity, + 'lock', + id, + m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field + ); + }); // TODO: should be handled by box setTimeout(() => { @@ -278,7 +280,9 @@ function resetPasswordSuccess(id) { if (l.ui.autoclose(m)) { closeLock(id); } else { - swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true)); + swapCaptcha(id, Flow.PASSWORD_RESET, false, () => { + swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true)); + }); } } } @@ -305,15 +309,39 @@ function resetPasswordError(id, error) { } export function showLoginActivity(id, fields = ['password']) { - swap(updateEntity, 'lock', id, setScreen, 'login', fields); + const m = read(getEntity, 'lock', id); + const captchaConfig = l.captcha(m); + if (captchaConfig && captchaConfig.get('provider') === 'arkose') { + swap(updateEntity, 'lock', id, setScreen, 'login', fields); + } else { + swapCaptcha(id, 'login', false, () => { + swap(updateEntity, 'lock', id, setScreen, 'login', fields); + }); + } } export function showSignUpActivity(id, fields = ['password']) { - swap(updateEntity, 'lock', id, setScreen, 'signUp', fields); + const m = read(getEntity, 'lock', id); + const captchaConfig = l.captcha(m); + if (captchaConfig && captchaConfig.get('provider') === 'arkose') { + swap(updateEntity, 'lock', id, setScreen, 'signUp', fields); + } else { + swapCaptcha(id, 'login', false, () => { + swap(updateEntity, 'lock', id, setScreen, 'signUp', fields); + }); + } } export function showResetPasswordActivity(id, fields = ['password']) { - swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields); + const m = read(getEntity, 'lock', id); + const captchaConfig = l.passwordResetCaptcha(m); + if (captchaConfig && captchaConfig.get('provider') === 'arkose') { + swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields); + } else { + swapCaptcha(id, 'login', false, () => { + swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields); + }); + } } export function cancelResetPassword(id) {