|
17 | 17 |
|
18 | 18 | import '../test/setup';
|
19 | 19 | import { getFakeGreCAPTCHA, getFullApp } from '../test/util';
|
20 |
| -import { ReCaptchaV3Provider } from './providers'; |
| 20 | +import { ReCaptchaEnterpriseProvider, ReCaptchaV3Provider } from './providers'; |
21 | 21 | import * as client from './client';
|
22 | 22 | import * as reCAPTCHA from './recaptcha';
|
23 | 23 | import * as util from './util';
|
@@ -113,3 +113,90 @@ describe('ReCaptchaV3Provider', () => {
|
113 | 113 | expect(token.token).to.equal('fake-exchange-token');
|
114 | 114 | });
|
115 | 115 | });
|
| 116 | + |
| 117 | +describe('ReCaptchaEnterpriseProvider', () => { |
| 118 | + let app: FirebaseApp; |
| 119 | + let clock = useFakeTimers(); |
| 120 | + beforeEach(() => { |
| 121 | + clock = useFakeTimers(); |
| 122 | + app = getFullApp(); |
| 123 | + stub(util, 'getRecaptcha').returns(getFakeGreCAPTCHA()); |
| 124 | + stub(reCAPTCHA, 'getToken').returns( |
| 125 | + Promise.resolve('fake-recaptcha-token') |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + afterEach(() => { |
| 130 | + clock.restore(); |
| 131 | + clearState(); |
| 132 | + return deleteApp(app); |
| 133 | + }); |
| 134 | + it('getToken() gets a token from the exchange endpoint', async () => { |
| 135 | + const app = getFullApp(); |
| 136 | + const provider = new ReCaptchaEnterpriseProvider('fake-site-key'); |
| 137 | + stub(client, 'exchangeToken').resolves({ |
| 138 | + token: 'fake-exchange-token', |
| 139 | + issuedAtTimeMillis: 0, |
| 140 | + expireTimeMillis: 10 |
| 141 | + }); |
| 142 | + provider.initialize(app); |
| 143 | + const token = await provider.getToken(); |
| 144 | + expect(token.token).to.equal('fake-exchange-token'); |
| 145 | + }); |
| 146 | + it('getToken() throttles 1d on 403', async () => { |
| 147 | + const app = getFullApp(); |
| 148 | + const provider = new ReCaptchaEnterpriseProvider('fake-site-key'); |
| 149 | + stub(client, 'exchangeToken').rejects( |
| 150 | + new FirebaseError(AppCheckError.FETCH_STATUS_ERROR, 'some-message', { |
| 151 | + httpStatus: 403 |
| 152 | + }) |
| 153 | + ); |
| 154 | + provider.initialize(app); |
| 155 | + await expect(provider.getToken()).to.be.rejectedWith('1d'); |
| 156 | + // Wait 10s and try again to see if wait time string decreases. |
| 157 | + clock.tick(10000); |
| 158 | + await expect(provider.getToken()).to.be.rejectedWith('23h'); |
| 159 | + }); |
| 160 | + it('getToken() throttles exponentially on 503', async () => { |
| 161 | + const app = getFullApp(); |
| 162 | + const provider = new ReCaptchaEnterpriseProvider('fake-site-key'); |
| 163 | + let exchangeTokenStub = stub(client, 'exchangeToken').rejects( |
| 164 | + new FirebaseError(AppCheckError.FETCH_STATUS_ERROR, 'some-message', { |
| 165 | + httpStatus: 503 |
| 166 | + }) |
| 167 | + ); |
| 168 | + provider.initialize(app); |
| 169 | + await expect(provider.getToken()).to.be.rejectedWith('503'); |
| 170 | + expect(exchangeTokenStub).to.be.called; |
| 171 | + exchangeTokenStub.resetHistory(); |
| 172 | + // Try again immediately, should be rejected. |
| 173 | + await expect(provider.getToken()).to.be.rejectedWith('503'); |
| 174 | + expect(exchangeTokenStub).not.to.be.called; |
| 175 | + exchangeTokenStub.resetHistory(); |
| 176 | + // Times below are max range of each random exponential wait, |
| 177 | + // the possible range is 2^(backoff_count) plus or minus 50% |
| 178 | + // Wait for 1.5 seconds to pass, should call exchange endpoint again |
| 179 | + // (and be rejected again) |
| 180 | + clock.tick(1500); |
| 181 | + await expect(provider.getToken()).to.be.rejectedWith('503'); |
| 182 | + expect(exchangeTokenStub).to.be.called; |
| 183 | + exchangeTokenStub.resetHistory(); |
| 184 | + // Wait for 3 seconds to pass, should call exchange endpoint again |
| 185 | + // (and be rejected again) |
| 186 | + clock.tick(3000); |
| 187 | + await expect(provider.getToken()).to.be.rejectedWith('503'); |
| 188 | + expect(exchangeTokenStub).to.be.called; |
| 189 | + // Wait for 6 seconds to pass, should call exchange endpoint again |
| 190 | + // (and succeed) |
| 191 | + clock.tick(6000); |
| 192 | + exchangeTokenStub.restore(); |
| 193 | + exchangeTokenStub = stub(client, 'exchangeToken').resolves({ |
| 194 | + token: 'fake-exchange-token', |
| 195 | + issuedAtTimeMillis: 0, |
| 196 | + expireTimeMillis: 10 |
| 197 | + }); |
| 198 | + const token = await provider.getToken(); |
| 199 | + expect(token.token).to.equal('fake-exchange-token'); |
| 200 | + }); |
| 201 | +}); |
| 202 | + |
0 commit comments