11import { submitComponentEvent as submitComponentEventMock } from '../client/ophan/ophan' ;
2- import { getRedirectUrl , initializeFedCM } from './GoogleOneTap.importable' ;
2+ import {
3+ extractEmailFromToken ,
4+ getRedirectUrl ,
5+ initializeFedCM ,
6+ } from './GoogleOneTap.importable' ;
37
48jest . mock ( '../client/ophan/ophan' , ( ) => ( {
59 submitComponentEvent : jest . fn ( ) ,
@@ -36,38 +40,52 @@ describe('GoogleOneTap', () => {
3640 expect (
3741 getRedirectUrl ( {
3842 stage : 'PROD' ,
39- token : 'test-token ' ,
43+ 4044 currentLocation : 'https://www.theguardian.com/uk' ,
4145 } ) ,
4246 ) . toEqual (
43- 'https://profile.theguardian.com/signin/google?token=test-token &returnUrl=https%3A%2F%2Fwww.theguardian.com%2Fuk' ,
47+ 'https://profile.theguardian.com/signin/google?signInEmail=valid%40email.com &returnUrl=https%3A%2F%2Fwww.theguardian.com%2Fuk' ,
4448 ) ;
4549
4650 expect (
4751 getRedirectUrl ( {
4852 stage : 'CODE' ,
49- token : 'test-token ' ,
53+ 5054 currentLocation : 'https://m.code.dev-theguardian.com/uk' ,
5155 } ) ,
5256 ) . toEqual (
53- 'https://profile.code.dev-theguardian.com/signin/google?token=test-token &returnUrl=https%3A%2F%2Fm.code.dev-theguardian.com%2Fuk' ,
57+ 'https://profile.code.dev-theguardian.com/signin/google?signInEmail=valid%40email.com &returnUrl=https%3A%2F%2Fm.code.dev-theguardian.com%2Fuk' ,
5458 ) ;
5559
5660 expect (
5761 getRedirectUrl ( {
5862 stage : 'DEV' ,
59- token : 'test-token ' ,
63+ 6064 currentLocation :
6165 'http://localhost/Front/https://m.code.dev-theguardian.com/uk' ,
6266 } ) ,
6367 ) . toEqual (
64- 'https://profile.thegulocal.com/signin/google?token=test-token &returnUrl=http%3A%2F%2Flocalhost%2FFront%2Fhttps%3A%2F%2Fm.code.dev-theguardian.com%2Fuk' ,
68+ 'https://profile.thegulocal.com/signin/google?signInEmail=valid%40email.com &returnUrl=http%3A%2F%2Flocalhost%2FFront%2Fhttps%3A%2F%2Fm.code.dev-theguardian.com%2Fuk' ,
6569 ) ;
6670 } ) ;
6771
72+ it ( 'should return email address from a valid JWT token' , ( ) => {
73+ expect (
74+ extractEmailFromToken (
75+ 'NULL.eyJlbWFpbCI6InZhbGlkQGVtYWlsLmNvbSJ9.NULL' ,
76+ ) ,
77+ 78+ } ) ;
79+
80+ it ( 'should return undefined from a malformed JWT token' , ( ) => {
81+ expect ( extractEmailFromToken ( 'NULL' ) ) . toEqual ( undefined ) ;
82+ } ) ;
83+
6884 it ( 'should initializeFedCM and redirect to Gateway with token on success' , async ( ) => {
6985 const navigatorGet = jest . fn ( ( ) =>
70- Promise . resolve ( { token : 'test-token' } ) ,
86+ Promise . resolve ( {
87+ token : 'NULL.eyJlbWFpbCI6InZhbGlkQGVtYWlsLmNvbSJ9.NULL' ,
88+ } ) ,
7189 ) ;
7290 const locationReplace = jest . fn ( ) ;
7391
@@ -83,7 +101,8 @@ describe('GoogleOneTap', () => {
83101 context : 'continue' ,
84102 providers : [
85103 {
86- clientId : '774465807556.apps.googleusercontent.com' ,
104+ clientId :
105+ '774465807556-4d50ur6svcjj90l7fe6i0bnp4t4qhkga.apps.googleusercontent.com' ,
87106 configURL : 'https://accounts.google.com/gsi/fedcm.json' ,
88107 } ,
89108 ] ,
@@ -92,7 +111,7 @@ describe('GoogleOneTap', () => {
92111 } ) ;
93112
94113 expect ( locationReplace ) . toHaveBeenCalledWith (
95- 'https://profile.theguardian.com/signin/google?token=test-token &returnUrl=https%3A%2F%2Fwww.theguardian.com%2Fuk' ,
114+ 'https://profile.theguardian.com/signin/google?signInEmail=valid%40email.com &returnUrl=https%3A%2F%2Fwww.theguardian.com%2Fuk' ,
96115 ) ;
97116
98117 expect ( submitComponentEventMock ) . toHaveBeenNthCalledWith (
@@ -273,7 +292,7 @@ describe('GoogleOneTap', () => {
273292 } ) ;
274293
275294 await initializeFedCM ( { isSignedIn : true } ) ;
276-
295+
277296 expect ( submitComponentEventMock ) . toHaveBeenCalledTimes ( 1 ) ;
278297 expect ( submitComponentEventMock ) . toHaveBeenCalledWith (
279298 {
0 commit comments