@@ -150,34 +150,58 @@ describe('MentorLoginComponent', () => {
150150 environment . BACKEND_ITA_CHALLENGE_BASE_URL + environment . BACKEND_GITHUB_VALIDATE_ENDPOINT ,
151151 { code : 'testCode' }
152152 )
153- expect ( localStorage . getItem ( 'username' ) ) . toBe ( 'testUser' )
154- expect ( localStorage . getItem ( 'authToken' ) ) . toBe ( '123456' )
153+ expect ( sessionStorage . getItem ( 'username' ) ) . toBe ( 'testUser' )
154+ expect ( sessionStorage . getItem ( 'authToken' ) ) . toBe ( '123456' )
155155 expect ( authServiceMock . updateUserRoleAndUserNameFromToken ) . toHaveBeenCalled ( )
156156 expect ( routerSpy ) . toHaveBeenCalledWith ( [ ] , { queryParams : { code : null } , queryParamsHandling : 'merge' } )
157157 expect ( modalSpy ) . toHaveBeenCalled ( )
158158 expect ( loginSuccessSpy ) . toHaveBeenCalledWith ( true )
159159 } )
160160
161+ it ( '❌ Should handle invalid GitHub response and clean storage' , ( ) => {
162+ sessionStorage . setItem ( 'username' , 'testUser' )
163+ sessionStorage . setItem ( 'authToken' , '123456' )
164+
165+ const mockResponse = { isValid : false , username : '' , token : '' }
166+ jest . spyOn ( component . http , 'post' ) . mockReturnValue ( of ( mockResponse ) )
167+ const errorSpy = jest . spyOn ( component , 'showError' )
168+
169+ component . authenticateWithGitHub ( 'testCode' )
170+
171+ expect ( errorSpy ) . toHaveBeenCalledWith ( 'unauthorized' )
172+ expect ( sessionStorage . getItem ( 'username' ) ) . toBeNull ( )
173+ expect ( sessionStorage . getItem ( 'authToken' ) ) . toBeNull ( )
174+ } )
175+
161176 it . each < ErrorHandlingTestCase > ( [
162177 { statusCode : 401 , expectedError : 'unauthorized' } ,
163178 { statusCode : 403 , expectedError : 'unauthorized' } ,
164- { statusCode : 500 , expectedError : 'server_error' }
179+ { statusCode : 500 , expectedError : 'server_error' } ,
180+ { statusCode : 404 , expectedError : 'unauthorized' }
165181 ] ) ( '❌ Should handle error %i and show error message' , ( { statusCode, expectedError } , done ) => {
166- localStorage . setItem ( 'username' , 'testUser' )
167- localStorage . setItem ( 'authToken' , '123456' )
182+ sessionStorage . setItem ( 'username' , 'testUser' )
183+ sessionStorage . setItem ( 'authToken' , '123456' )
168184
169185 const httpSpy = jest . spyOn ( component . http , 'post' ) . mockReturnValue (
170186 throwError ( ( ) => ( { status : statusCode } ) )
171187 )
172188
173189 const errorSpy = jest . spyOn ( component , 'showError' )
174-
190+ const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( )
175191 component . authenticateWithGitHub ( 'testCode' )
176192
177193 setTimeout ( ( ) => {
178194 expect ( httpSpy ) . toHaveBeenCalled ( )
179195 expect ( errorSpy ) . toHaveBeenCalledWith ( expectedError )
180196 expect ( component . isLoading ) . toBe ( false )
197+ if ( statusCode === 403 ) {
198+ expect ( sessionStorage . getItem ( 'username' ) ) . toBeNull ( )
199+ expect ( sessionStorage . getItem ( 'authToken' ) ) . toBeNull ( )
200+ }
201+ if ( statusCode === 404 ) {
202+ expect ( consoleSpy ) . toHaveBeenCalled ( )
203+ }
204+ consoleSpy . mockRestore ( )
181205
182206 done ( )
183207 } , 100 )
@@ -193,4 +217,13 @@ describe('MentorLoginComponent', () => {
193217 expect ( component . isErrorVisible ) . toBe ( false )
194218 expect ( component . isShowTermsError ) . toBe ( false )
195219 } )
220+
221+ it ( '✅ Should redirect to GitHub signup' , ( ) => {
222+ delete ( window as any ) . location
223+ window . location = { href : '' } as any
224+
225+ component . redirectToRegister ( )
226+
227+ expect ( window . location . href ) . toBe ( 'https://github.com/signup' )
228+ } )
196229} )
0 commit comments