-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Integrated Dicebear Library To Enhance Security (#1585)
* feat: Integrated Dicebear Library To Enhance Security - Added the Dicebear library as a dependency to the project. - Imported the necessary functions from the library. - Created a new `Avatar` component that utilizes the Dicebear library locally. - Updated the component to accept `name`, `alt`, and `size` as props. - Utilized the Dicebear library to generate avatars with initials. - Removed direct HTTP API calls from the codebase. - Replaced instances of direct HTTP API calls with the newly created `Avatar` component. - Updated the exisiting tests and ensured the tests are still valid. - Mitigated security risks associated with SSRF and XSS attacks by eliminating direct HTTP API calls. - By using the Dicebear library locally, we reduce potential vulnerabilities. - Ensured no other functionality is affected. Signed-off-by: Akhilender <[email protected]> * fix: Reverted changes in the tests - Reverted the Avatar component in all the tests as they are redundant in tests except where it is needed. - Ensured all the remaning tests are succesfully passing. Signed-off-by: Akhilender <[email protected]> * fix: Added dynamic style - Handled case where no alt tag is provided by adding default alt value. - dynamic rendering for className for img tag in avatar component - Dynamic rendering for test-id - Replaced suitable changes for the above properties added Signed-off-by: Akhilender <[email protected]> * fix: modified test Signed-off-by: Akhilender <[email protected]> * fix: resolved conflict Signed-off-by: Akhilender <[email protected]> --------- Signed-off-by: Akhilender <[email protected]>
- Loading branch information
1 parent
092b9f6
commit 7828c40
Showing
14 changed files
with
1,291 additions
and
580 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export const initials = jest.fn(); |
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,5 @@ | ||
export const createAvatar = jest.fn(() => { | ||
return { | ||
toDataUriSync: jest.fn(() => 'mocked-data-uri'), | ||
}; | ||
}); |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import Avatar from './Avatar'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { I18nextProvider } from 'react-i18next'; | ||
import i18nForTest from 'utils/i18nForTest'; | ||
|
||
describe('Avatar component', () => { | ||
test('renders with name and alt attribute', () => { | ||
const testName = 'John Doe'; | ||
const testAlt = 'Test Alt Text'; | ||
const testSize = 64; | ||
|
||
const { getByAltText } = render( | ||
<BrowserRouter> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Avatar name={testName} alt={testAlt} size={testSize} /> | ||
</I18nextProvider> | ||
</BrowserRouter>, | ||
); | ||
const avatarElement = getByAltText(testAlt); | ||
|
||
expect(avatarElement).toBeInTheDocument(); | ||
expect(avatarElement.getAttribute('src')).toBeDefined(); | ||
}); | ||
|
||
test('renders with custom style and data-testid', () => { | ||
const testName = 'Jane Doe'; | ||
const testStyle = 'custom-avatar-style'; | ||
const testDataTestId = 'custom-avatar-test-id'; | ||
|
||
const { getByAltText } = render( | ||
<BrowserRouter> | ||
<I18nextProvider i18n={i18nForTest}> | ||
<Avatar | ||
name={testName} | ||
avatarStyle={testStyle} | ||
dataTestId={testDataTestId} | ||
/> | ||
</I18nextProvider> | ||
</BrowserRouter>, | ||
); | ||
const avatarElement = getByAltText('Dummy Avatar'); | ||
|
||
expect(avatarElement).toBeInTheDocument(); | ||
expect(avatarElement.getAttribute('src')).toBeDefined(); | ||
expect(avatarElement.getAttribute('class')).toContain(testStyle); | ||
expect(avatarElement.getAttribute('data-testid')).toBe(testDataTestId); | ||
}); | ||
}); |
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,39 @@ | ||
import React, { useMemo } from 'react'; | ||
import { createAvatar } from '@dicebear/core'; | ||
import { initials } from '@dicebear/collection'; | ||
|
||
interface InterfaceAvatarProps { | ||
name: string; | ||
alt?: string; | ||
size?: number; | ||
avatarStyle?: string; | ||
dataTestId?: string; | ||
} | ||
|
||
const Avatar = ({ | ||
name, | ||
alt = 'Dummy Avatar', | ||
size, | ||
avatarStyle, | ||
dataTestId, | ||
}: InterfaceAvatarProps): JSX.Element => { | ||
const avatar = useMemo(() => { | ||
return createAvatar(initials, { | ||
size: size || 128, | ||
seed: name, | ||
}).toDataUriSync(); | ||
}, [name, size]); | ||
|
||
const svg = avatar.toString(); | ||
|
||
return ( | ||
<img | ||
src={svg} | ||
alt={alt} | ||
className={avatarStyle ? avatarStyle : ''} | ||
data-testid={dataTestId ? dataTestId : ''} | ||
/> | ||
); | ||
}; | ||
|
||
export default Avatar; |
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