|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | | -import { createTestServer, TestServerInstance } from '../support/helpers/test-server'; |
6 | 5 | import crypto from 'crypto'; |
7 | 6 |
|
8 | | -interface AuthServerError extends Error { |
9 | | - errno: number; |
10 | | - code: number; |
11 | | - email: string; |
12 | | -} |
| 7 | +import * as jwt from '../../lib/oauth/jwt'; |
| 8 | +import { createTestServer, TestServerInstance } from '../support/helpers/test-server'; |
| 9 | +import { AuthServerError, generateMetricsContext } from '../support/helpers/test-utils'; |
13 | 10 |
|
14 | 11 | interface TestConfig extends Record<string, unknown> { |
15 | 12 | smtp: { syncUrl: string }; |
16 | 13 | publicUrl: string; |
17 | 14 | } |
18 | 15 |
|
| 16 | +// eslint-disable-next-line @typescript-eslint/no-require-imports |
19 | 17 | const Client = require('../client')(); |
20 | | -const mocks = require('../mocks'); |
21 | 18 |
|
22 | 19 | let server: TestServerInstance; |
23 | 20 | let config: TestConfig; |
@@ -167,7 +164,6 @@ describe.each(testVersions)( |
167 | 164 |
|
168 | 165 | const stubResponse = await client.stubAccount('dcdb5ae7add825d2'); |
169 | 166 |
|
170 | | - const jwt = require('../../lib/oauth/jwt'); |
171 | 167 | const setupToken = jwt.sign( |
172 | 168 | { uid: stubResponse.uid, iat: Date.now() }, |
173 | 169 | { header: { typ: 'fin+JWT' } } |
@@ -236,7 +232,6 @@ describe.each(testVersions)( |
236 | 232 |
|
237 | 233 | const stubResponse = await client.stubAccount('dcdb5ae7add825d2'); |
238 | 234 |
|
239 | | - const jwt = require('../../lib/oauth/jwt'); |
240 | 235 | const setupToken = jwt.sign( |
241 | 236 | { |
242 | 237 | uid: stubResponse.uid, |
@@ -431,6 +426,195 @@ describe.each(testVersions)( |
431 | 426 | } |
432 | 427 | }); |
433 | 428 |
|
| 429 | + it('invalid entrypointExperiment', async () => { |
| 430 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 431 | + const email = server.uniqueEmail(); |
| 432 | + const authPW = |
| 433 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 434 | + const options = { |
| 435 | + ...testOptions, |
| 436 | + metricsContext: { |
| 437 | + entrypoint: 'foo', |
| 438 | + entrypointExperiment: ';', |
| 439 | + entrypointVariation: 'var', |
| 440 | + utmCampaign: 'bar', |
| 441 | + utmContent: 'baz', |
| 442 | + utmMedium: 'qux', |
| 443 | + utmSource: 'wibble', |
| 444 | + utmTerm: 'blee', |
| 445 | + }, |
| 446 | + }; |
| 447 | + |
| 448 | + try { |
| 449 | + await api.accountCreate(email, authPW, options); |
| 450 | + fail('should have thrown'); |
| 451 | + } catch (err: unknown) { |
| 452 | + expect((err as AuthServerError).errno).toBe(107); |
| 453 | + } |
| 454 | + }); |
| 455 | + |
| 456 | + it('invalid entrypointVariation', async () => { |
| 457 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 458 | + const email = server.uniqueEmail(); |
| 459 | + const authPW = |
| 460 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 461 | + const options = { |
| 462 | + ...testOptions, |
| 463 | + metricsContext: { |
| 464 | + entrypoint: 'foo', |
| 465 | + entrypointExperiment: 'exp', |
| 466 | + entrypointVariation: ';', |
| 467 | + utmCampaign: 'bar', |
| 468 | + utmContent: 'baz', |
| 469 | + utmMedium: 'qux', |
| 470 | + utmSource: 'wibble', |
| 471 | + utmTerm: 'blee', |
| 472 | + }, |
| 473 | + }; |
| 474 | + |
| 475 | + try { |
| 476 | + await api.accountCreate(email, authPW, options); |
| 477 | + fail('should have thrown'); |
| 478 | + } catch (err: unknown) { |
| 479 | + expect((err as AuthServerError).errno).toBe(107); |
| 480 | + } |
| 481 | + }); |
| 482 | + |
| 483 | + it('invalid utmCampaign', async () => { |
| 484 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 485 | + const email = server.uniqueEmail(); |
| 486 | + const authPW = |
| 487 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 488 | + const options = { |
| 489 | + ...testOptions, |
| 490 | + metricsContext: { |
| 491 | + entrypoint: 'foo', |
| 492 | + entrypointExperiment: 'exp', |
| 493 | + entrypointVariation: 'var', |
| 494 | + utmCampaign: ';', |
| 495 | + utmContent: 'bar', |
| 496 | + utmMedium: 'baz', |
| 497 | + utmSource: 'qux', |
| 498 | + utmTerm: 'wibble', |
| 499 | + }, |
| 500 | + }; |
| 501 | + |
| 502 | + try { |
| 503 | + await api.accountCreate(email, authPW, options); |
| 504 | + fail('should have thrown'); |
| 505 | + } catch (err: unknown) { |
| 506 | + expect((err as AuthServerError).errno).toBe(107); |
| 507 | + } |
| 508 | + }); |
| 509 | + |
| 510 | + it('invalid utmContent', async () => { |
| 511 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 512 | + const email = server.uniqueEmail(); |
| 513 | + const authPW = |
| 514 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 515 | + const options = { |
| 516 | + ...testOptions, |
| 517 | + metricsContext: { |
| 518 | + entrypoint: 'foo', |
| 519 | + entrypointExperiment: 'exp', |
| 520 | + entrypointVariation: 'var', |
| 521 | + utmCampaign: 'bar', |
| 522 | + utmContent: ';', |
| 523 | + utmMedium: 'baz', |
| 524 | + utmSource: 'qux', |
| 525 | + utmTerm: 'wibble', |
| 526 | + }, |
| 527 | + }; |
| 528 | + |
| 529 | + try { |
| 530 | + await api.accountCreate(email, authPW, options); |
| 531 | + fail('should have thrown'); |
| 532 | + } catch (err: unknown) { |
| 533 | + expect((err as AuthServerError).errno).toBe(107); |
| 534 | + } |
| 535 | + }); |
| 536 | + |
| 537 | + it('invalid utmMedium', async () => { |
| 538 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 539 | + const email = server.uniqueEmail(); |
| 540 | + const authPW = |
| 541 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 542 | + const options = { |
| 543 | + ...testOptions, |
| 544 | + metricsContext: { |
| 545 | + entrypoint: 'foo', |
| 546 | + entrypointExperiment: 'exp', |
| 547 | + entrypointVariation: 'var', |
| 548 | + utmCampaign: 'bar', |
| 549 | + utmContent: 'baz', |
| 550 | + utmMedium: ';', |
| 551 | + utmSource: 'qux', |
| 552 | + utmTerm: 'wibble', |
| 553 | + }, |
| 554 | + }; |
| 555 | + |
| 556 | + try { |
| 557 | + await api.accountCreate(email, authPW, options); |
| 558 | + fail('should have thrown'); |
| 559 | + } catch (err: unknown) { |
| 560 | + expect((err as AuthServerError).errno).toBe(107); |
| 561 | + } |
| 562 | + }); |
| 563 | + |
| 564 | + it('invalid utmSource', async () => { |
| 565 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 566 | + const email = server.uniqueEmail(); |
| 567 | + const authPW = |
| 568 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 569 | + const options = { |
| 570 | + ...testOptions, |
| 571 | + metricsContext: { |
| 572 | + entrypoint: 'foo', |
| 573 | + entrypointExperiment: 'exp', |
| 574 | + entrypointVariation: 'var', |
| 575 | + utmCampaign: 'bar', |
| 576 | + utmContent: 'baz', |
| 577 | + utmMedium: 'qux', |
| 578 | + utmSource: ';', |
| 579 | + utmTerm: 'wibble', |
| 580 | + }, |
| 581 | + }; |
| 582 | + |
| 583 | + try { |
| 584 | + await api.accountCreate(email, authPW, options); |
| 585 | + fail('should have thrown'); |
| 586 | + } catch (err: unknown) { |
| 587 | + expect((err as AuthServerError).errno).toBe(107); |
| 588 | + } |
| 589 | + }); |
| 590 | + |
| 591 | + it('invalid utmTerm', async () => { |
| 592 | + const api = new Client.Api(server.publicUrl, testOptions); |
| 593 | + const email = server.uniqueEmail(); |
| 594 | + const authPW = |
| 595 | + '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF'; |
| 596 | + const options = { |
| 597 | + ...testOptions, |
| 598 | + metricsContext: { |
| 599 | + entrypoint: 'foo', |
| 600 | + entrypointExperiment: 'exp', |
| 601 | + entrypointVariation: 'var', |
| 602 | + utmCampaign: 'bar', |
| 603 | + utmContent: 'baz', |
| 604 | + utmMedium: 'qux', |
| 605 | + utmSource: 'wibble', |
| 606 | + utmTerm: ';', |
| 607 | + }, |
| 608 | + }; |
| 609 | + |
| 610 | + try { |
| 611 | + await api.accountCreate(email, authPW, options); |
| 612 | + fail('should have thrown'); |
| 613 | + } catch (err: unknown) { |
| 614 | + expect((err as AuthServerError).errno).toBe(107); |
| 615 | + } |
| 616 | + }); |
| 617 | + |
434 | 618 | it('create account with service query parameter', async () => { |
435 | 619 | const email = server.uniqueEmail(); |
436 | 620 |
|
@@ -497,7 +681,7 @@ describe.each(testVersions)( |
497 | 681 | const email = server.uniqueEmail(); |
498 | 682 | const options = { |
499 | 683 | ...testOptions, |
500 | | - metricsContext: mocks.generateMetricsContext(), |
| 684 | + metricsContext: generateMetricsContext(), |
501 | 685 | }; |
502 | 686 |
|
503 | 687 | const client = await Client.create(server.publicUrl, email, 'foo', options); |
|
0 commit comments