@@ -26,7 +26,7 @@ test('generates the correct app private key', () => {
2626 expect ( appPrivateKey ) . toEqual ( expectedKey ) ;
2727} ) ;
2828
29- describe ( makeAuthResponse , ( ) => {
29+ describe ( makeAuthResponse . name , ( ) => {
3030 test ( 'generates an auth response' , async ( ) => {
3131 const account = mockAccount ;
3232 const appDomain = 'https://banter.pub' ;
@@ -128,4 +128,96 @@ describe(makeAuthResponse, () => {
128128 expect ( appPrivateKey ) . toEqual ( expectedKey ) ;
129129 expect ( payload . appPrivateKeyFromWalletSalt ) . toEqual ( appPrivateKeyFromWalletSalt ) ;
130130 } ) ;
131+
132+ describe ( 'Gaia optional functionality' , ( ) => {
133+ const TEST_CASES = [
134+ {
135+ mock : ( ) => fetchMock . mockRejectOnce ( new Error ( 'Network error' ) ) ,
136+ opts : { } ,
137+ } ,
138+ {
139+ mock : ( ) => fetchMock . mockResponseOnce ( 'Internal Server Error' , { status : 500 } ) ,
140+ opts : { } ,
141+ } ,
142+ {
143+ mock : ( ) => fetchMock . mockRejectOnce ( new Error ( 'Connection timeout' ) ) ,
144+ opts : { scopes : [ 'publish_data' ] } ,
145+ } ,
146+ {
147+ mock : ( ) => fetchMock . mockResponseOnce ( 'Not Found' , { status : 404 } ) ,
148+ opts : { scopes : [ 'publish_data' ] } ,
149+ } ,
150+ {
151+ mock : ( ) =>
152+ fetchMock . mockImplementationOnce ( ( ) => Promise . reject ( new Error ( 'Request timeout' ) ) ) ,
153+ opts : { scopes : [ 'read_write' ] } ,
154+ } ,
155+ ] ;
156+ test . each ( TEST_CASES ) ( makeAuthResponse . name , async ( { mock, opts } ) => {
157+ mock ( ) ;
158+
159+ const account = mockAccount ;
160+ const transitPrivateKey = makeECPrivateKey ( ) ;
161+ const transitPublicKey = getPublicKeyFromPrivate ( transitPrivateKey ) ;
162+
163+ const authResponse = await makeAuthResponse ( {
164+ appDomain : 'https://banter.pub' ,
165+ gaiaHubUrl,
166+ transitPublicKey,
167+ account,
168+ ...opts ,
169+ } ) ;
170+
171+ // Common verifications
172+ expect ( authResponse ) . toBeTruthy ( ) ;
173+ const decoded = decodeToken ( authResponse ) ;
174+ const { payload } = decoded as Decoded ;
175+
176+ expect ( payload . profile_url ) . toBeNull ( ) ;
177+ expect ( payload . profile ) . toBeDefined ( ) ;
178+ expect ( payload . profile . stxAddress ) . toBeDefined ( ) ;
179+ expect ( payload . profile . stxAddress . testnet ) . toBeDefined ( ) ;
180+ expect ( payload . profile . stxAddress . mainnet ) . toBeDefined ( ) ;
181+
182+ expect ( fetchMock . mock . calls . length ) . toEqual ( 1 ) ;
183+ } ) ;
184+
185+ test ( 'makeAuthResponse handles mixed success/failure gracefully' , async ( ) => {
186+ const account = mockAccount ;
187+ const transitPrivateKey = makeECPrivateKey ( ) ;
188+ const transitPublicKey = getPublicKeyFromPrivate ( transitPrivateKey ) ;
189+
190+ // First call succeeds with hub info, second call (profile fetch) fails
191+ fetchMock
192+ . mockResponseOnce ( mockGaiaHubInfo )
193+ . mockResponseOnce ( '' , { status : 404 } ) // profile fetch fails
194+ . mockResponseOnce ( JSON . stringify ( { publicUrl : 'asdf' } ) ) ; // profile upload succeeds
195+
196+ const authResponse = await makeAuthResponse ( {
197+ appDomain : 'https://banter.pub' ,
198+ gaiaHubUrl,
199+ transitPublicKey,
200+ account,
201+ scopes : [ 'publish_data' ] ,
202+ } ) ;
203+
204+ // Verify auth response is generated successfully
205+ expect ( authResponse ) . toBeTruthy ( ) ;
206+ const decoded = decodeToken ( authResponse ) ;
207+ const { payload } = decoded as Decoded ;
208+
209+ // Verify we get profile_url since hub info succeeded
210+ expect ( payload . profile_url ) . toEqual (
211+ `https://gaia.blockstack.org/hub/${ getGaiaAddress ( account ) } /profile.json`
212+ ) ;
213+
214+ // Verify profile upload was successful and profile apps were added
215+ expect ( payload . profile ) . toBeDefined ( ) ;
216+ expect ( payload . profile . apps ) . toBeDefined ( ) ;
217+ expect ( payload . profile . apps [ 'https://banter.pub' ] ) . toBeDefined ( ) ;
218+
219+ // Verify three fetch calls were made (hub info, profile fetch, profile upload)
220+ expect ( fetchMock . mock . calls . length ) . toEqual ( 3 ) ;
221+ } ) ;
222+ } ) ;
131223} ) ;
0 commit comments