This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
244 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
# Dummy values for local testing to avoid errors | ||
VITE_CF_DOMAIN='https://localhost:3000' | ||
VITE_USER_POOL_ID='us-east-1_123456789' | ||
VITE_USER_POOL_CLIENT_ID='1234567890123456789012' | ||
|
||
# AWS | ||
VITE_AWS_REGION='us-east-1' | ||
VITE_CF_DOMAIN='https://localhost:3000' | ||
|
||
# AWS Cognito | ||
VITE_COGNITO_DOMAIN='https://cognito-idp.us-east-1.amazonaws.com/us-east-1_abcd1234' | ||
VITE_USER_POOL_ID='us-east-1_abcd1234' | ||
VITE_USER_POOL_CLIENT_ID='12345678901234567890123456' | ||
VITE_COGNITO_REDIRECT_SIGN_IN='http://localhost:3000/signin' | ||
VITE_COGNITO_REDIRECT_SIGN_OUT='http://localhost:3000/signout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { AuthActions } from '@/actions/actionTypes' | ||
import loginUserFederated from '@/actions/loginUserFederated' | ||
import { Auth } from 'aws-amplify' | ||
|
||
jest.mock('aws-amplify') | ||
|
||
describe('loginUserFederated', () => { | ||
let mockDispatch: jest.Mock | ||
|
||
beforeEach(() => { | ||
mockDispatch = jest.fn() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should dispatch LOGIN_REQUEST action', async () => { | ||
const mockUser = { | ||
accessKeyId: 'testAccessKeyId', | ||
sessionToken: 'testSessionToken', | ||
secretAccessKey: 'testSecretAccessKey', | ||
identityId: 'testIdentityId', | ||
authenticated: true, | ||
expiration: new Date(), | ||
} | ||
|
||
const mockToken = 'testToken' | ||
|
||
;(Auth.federatedSignIn as jest.Mock).mockResolvedValue(mockUser) | ||
;(Auth.currentSession as jest.Mock).mockResolvedValue({ | ||
getAccessToken: () => ({ | ||
getJwtToken: () => mockToken, | ||
}), | ||
}) | ||
|
||
await loginUserFederated(mockDispatch) | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: AuthActions.LOGIN_REQUEST, | ||
}) | ||
}) | ||
|
||
it('should dispatch LOGIN_SUCCESS action when login is successful', async () => { | ||
const mockUser = { | ||
accessKeyId: 'testAccessKeyId', | ||
sessionToken: 'testSessionToken', | ||
secretAccessKey: 'testSecretAccessKey', | ||
identityId: 'testIdentityId', | ||
authenticated: true, | ||
expiration: new Date(), | ||
} | ||
|
||
const mockToken = 'testToken' | ||
|
||
;(Auth.federatedSignIn as jest.Mock).mockResolvedValue(mockUser) | ||
;(Auth.currentSession as jest.Mock).mockResolvedValue({ | ||
getAccessToken: () => ({ | ||
getJwtToken: () => mockToken, | ||
}), | ||
}) | ||
|
||
await loginUserFederated(mockDispatch) | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: AuthActions.LOGIN_SUCCESS, | ||
payload: mockUser, | ||
}) | ||
}) | ||
|
||
it('should dispatch LOGIN_FAILURE action when login fails', async () => { | ||
const mockError = new Error('login failed') | ||
|
||
;(Auth.federatedSignIn as jest.Mock).mockRejectedValue(mockError) | ||
|
||
await loginUserFederated(mockDispatch) | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: AuthActions.LOGIN_FAILURE, | ||
error: mockError, | ||
}) | ||
}) | ||
|
||
it('should dispatch LOGIN_FAILURE action when no JWT token is found', async () => { | ||
const mockUser = { | ||
accessKeyId: 'testAccessKeyId', | ||
sessionToken: 'testSessionToken', | ||
secretAccessKey: 'testSecretAccessKey', | ||
identityId: 'testIdentityId', | ||
authenticated: true, | ||
expiration: new Date(), | ||
} | ||
|
||
const mockToken = '' | ||
|
||
;(Auth.federatedSignIn as jest.Mock).mockResolvedValue(mockUser) | ||
;(Auth.currentSession as jest.Mock).mockResolvedValue({ | ||
getAccessToken: () => ({ | ||
getJwtToken: () => mockToken, | ||
}), | ||
}) | ||
|
||
await loginUserFederated(mockDispatch) | ||
|
||
expect(mockDispatch).toHaveBeenCalledWith({ | ||
type: AuthActions.LOGIN_FAILURE, | ||
error: new Error('No JWT token found'), | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @module @sbom-harbor-ui/dashboard/actions/loginUserFederated | ||
*/ | ||
import React from 'react' | ||
import { Auth } from 'aws-amplify' | ||
import { AuthActions } from '@/actions/actionTypes' | ||
|
||
export default async function loginUserFederated( | ||
dispatch: React.Dispatch<{ | ||
type: AuthActions | ||
payload?: unknown | ||
error?: Error | ||
}> | ||
) { | ||
try { | ||
dispatch({ type: AuthActions.LOGIN_REQUEST }) | ||
const user = await Auth.federatedSignIn() | ||
const jwtToken = await (await Auth.currentSession()) | ||
.getAccessToken() | ||
.getJwtToken() | ||
|
||
if (!jwtToken) { | ||
throw new Error('No JWT token found') | ||
} | ||
|
||
const { | ||
accessKeyId, | ||
sessionToken, | ||
secretAccessKey, | ||
identityId, | ||
authenticated, | ||
expiration, | ||
} = user | ||
|
||
const data = { | ||
accessKeyId, | ||
sessionToken, | ||
secretAccessKey, | ||
identityId, | ||
authenticated, | ||
expiration, | ||
} | ||
|
||
dispatch({ type: AuthActions.LOGIN_SUCCESS, payload: data }) | ||
return data | ||
} catch (error) { | ||
dispatch({ type: AuthActions.LOGIN_FAILURE, error: error as Error }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.