Skip to content

Commit

Permalink
Add unit tests and call done if no captcha is shown
Browse files Browse the repository at this point in the history
  • Loading branch information
srijonsaha committed Oct 11, 2024
1 parent 704f827 commit 49638f2
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 24 deletions.
4 changes: 2 additions & 2 deletions dist/auth0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* auth0-js v9.27.0
* Author: Auth0
* Date: 2024-10-04
* Date: 2024-10-11
* License: MIT
*/

Expand Down Expand Up @@ -8226,7 +8226,7 @@
if (!challenge.required) {
element.style.display = 'none';
element.innerHTML = '';
return;
return done();
}
element.style.display = '';
if (challenge.provider === AUTH0_PROVIDER) {
Expand Down
4 changes: 2 additions & 2 deletions dist/auth0.min.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.esm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/auth0.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/auth0.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cordova-auth0-plugin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* auth0-js v9.27.0
* Author: Auth0
* Date: 2024-10-04
* Date: 2024-10-11
* License: MIT
*/

Expand Down
2 changes: 1 addition & 1 deletion dist/cordova-auth0-plugin.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/web-auth/captcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ function render(auth0Client, flow, element, options, callback) {
if (!challenge.required) {
element.style.display = 'none';
element.innerHTML = '';
return;
return done();
}
element.style.display = '';
if (challenge.provider === AUTH0_PROVIDER) {
Expand Down
110 changes: 97 additions & 13 deletions test/web-auth/captcha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ describe('captcha rendering', function () {
describe('when challenge is not required', function () {
const { window } = new JSDOM('<body><div class="captcha" /></body>');
const element = window.document.querySelector('.captcha');
const callbackStub = sinon.stub();
let c;

beforeEach(function () {
const mockClient = {
getChallenge: cb => cb(null, { required: false })
};
c = captcha.render(mockClient, captcha.Flow.DEFAULT, element);
c = captcha.render(mockClient, captcha.Flow.DEFAULT, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should hide the element', function () {
Expand All @@ -25,9 +30,19 @@ describe('captcha rendering', function () {
expect(element.innerHTML).to.equal('');
});

it('should call optional callback on load', function () {
expect(callbackStub.calledOnce).to.be.ok();
});

it('should return undefined when calling getValue', function () {
expect(c.getValue()).to.be.equal(undefined);
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
expect(doneStub.calledOnce).to.be.ok();
});
});

describe('when challenge request fail', function () {
Expand All @@ -42,6 +57,10 @@ describe('captcha rendering', function () {
captcha.render(mockClient, captcha.Flow.DEFAULT, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should show the element', function () {
expect(element.style.display).to.equal('');
});
Expand All @@ -53,7 +72,7 @@ describe('captcha rendering', function () {
});

it('should call the optional callback with the error', function () {
expect(callbackStub.called).to.equal(true);
expect(callbackStub.calledOnce).to.be.ok();
expect(callbackStub.args[0][0].message).to.equal('network error');
});
});
Expand Down Expand Up @@ -123,10 +142,12 @@ describe('captcha rendering', function () {
expect(imgEl.src).to.equal(challenges[1].image);
});

it('should load a new image when calling the reload method', function () {
c.reload();
it('should load a new image when calling the reload method and call done callback', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
const imgEl = element.querySelector('img');
expect(imgEl.src).to.equal(challenges[1].image);
expect(doneStub.calledOnce).to.be.ok();
});
});

Expand Down Expand Up @@ -609,6 +630,13 @@ describe('captcha rendering', function () {
configOptions.onCompleted({ token: 'token' });
expect(called).to.be.ok();
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
configOptions.onReady();
expect(doneStub.calledOnce).to.be.ok();
});
});
});
});
Expand All @@ -617,13 +645,18 @@ describe('passwordless captcha rendering', function () {
describe('when challenge is not required', function () {
const { window } = new JSDOM('<body><div class="captcha" /></body>');
const element = window.document.querySelector('.captcha');
const callbackStub = sinon.stub();
let c;

beforeEach(function () {
const mockClient = {
passwordless: { getChallenge: cb => cb(null, { required: false }) }
};
c = captcha.render(mockClient, captcha.Flow.PASSWORDLESS, element);
c = captcha.render(mockClient, captcha.Flow.PASSWORDLESS, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should hide the element', function () {
Expand All @@ -634,9 +667,19 @@ describe('passwordless captcha rendering', function () {
expect(element.innerHTML).to.equal('');
});

it('should call optional callback on load', function () {
expect(callbackStub.calledOnce).to.be.ok();
});

it('should return undefined when calling getValue', function () {
expect(c.getValue()).to.be.equal(undefined);
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
expect(doneStub.calledOnce).to.be.ok();
});
});

describe('when challenge request fail', function () {
Expand All @@ -651,6 +694,10 @@ describe('passwordless captcha rendering', function () {
captcha.render(mockClient, captcha.Flow.PASSWORDLESS, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should show the element', function () {
expect(element.style.display).to.equal('');
});
Expand All @@ -662,7 +709,7 @@ describe('passwordless captcha rendering', function () {
});

it('should call the optional callback with the error', function () {
expect(callbackStub.called).to.equal(true);
expect(callbackStub.calledOnce).to.be.ok();
expect(callbackStub.args[0][0].message).to.equal('network error');
});
});
Expand Down Expand Up @@ -734,10 +781,12 @@ describe('passwordless captcha rendering', function () {
expect(imgEl.src).to.equal(challenges[1].image);
});

it('should load a new image when calling the reload method', function () {
c.reload();
it('should load a new image when calling the reload method and call done callback', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
const imgEl = element.querySelector('img');
expect(imgEl.src).to.equal(challenges[1].image);
expect(doneStub.calledOnce).to.be.ok();
});
});

Expand Down Expand Up @@ -1197,6 +1246,13 @@ describe('passwordless captcha rendering', function () {
configOptions.onCompleted({ token: 'token' });
expect(called).to.be.ok();
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
configOptions.onReady();
expect(doneStub.calledOnce).to.be.ok();
});
});
});
});
Expand All @@ -1205,13 +1261,18 @@ describe('password reset captcha rendering', function () {
describe('when challenge is not required', function () {
const { window } = new JSDOM('<body><div class="captcha" /></body>');
const element = window.document.querySelector('.captcha');
const callbackStub = sinon.stub();
let c;

beforeEach(function () {
const mockClient = {
dbConnection: { getPasswordResetChallenge: cb => cb(null, { required: false }) }
};
c = captcha.render(mockClient, captcha.Flow.PASSWORD_RESET, element);
c = captcha.render(mockClient, captcha.Flow.PASSWORD_RESET, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should hide the element', function () {
Expand All @@ -1222,9 +1283,19 @@ describe('password reset captcha rendering', function () {
expect(element.innerHTML).to.equal('');
});

it('should call optional callback on load', function () {
expect(callbackStub.calledOnce).to.be.ok();
});

it('should return undefined when calling getValue', function () {
expect(c.getValue()).to.be.equal(undefined);
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
expect(doneStub.calledOnce).to.be.ok();
});
});

describe('when challenge request fail', function () {
Expand All @@ -1239,6 +1310,10 @@ describe('password reset captcha rendering', function () {
captcha.render(mockClient, captcha.Flow.PASSWORD_RESET, element, {}, callbackStub);
});

afterEach(function () {
callbackStub.reset();
});

it('should show the element', function () {
expect(element.style.display).to.equal('');
});
Expand All @@ -1250,7 +1325,7 @@ describe('password reset captcha rendering', function () {
});

it('should call the optional callback with the error', function () {
expect(callbackStub.called).to.equal(true);
expect(callbackStub.calledOnce).to.be.ok();
expect(callbackStub.args[0][0].message).to.equal('network error');
});
});
Expand Down Expand Up @@ -1322,10 +1397,12 @@ describe('password reset captcha rendering', function () {
expect(imgEl.src).to.equal(challenges[1].image);
});

it('should load a new image when calling the reload method', function () {
c.reload();
it('should load a new image when calling the reload method and call done callback', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
const imgEl = element.querySelector('img');
expect(imgEl.src).to.equal(challenges[1].image);
expect(doneStub.calledOnce).to.be.ok();
});
});

Expand Down Expand Up @@ -1785,6 +1862,13 @@ describe('password reset captcha rendering', function () {
configOptions.onCompleted({ token: 'token' });
expect(called).to.be.ok();
});

it('should call done callback on reload', function () {
const doneStub = sinon.stub();
c.reload(doneStub);
configOptions.onReady();
expect(doneStub.calledOnce).to.be.ok();
});
});
});
});
});

0 comments on commit 49638f2

Please sign in to comment.