2023-02-23 12:38:09 +01:00
import { fireEvent } from '@testing-library/react' ;
import React from 'react' ;
import configureMockStore from 'redux-mock-store' ;
import { renderWithProvider } from '../../../../test/lib/render-helpers' ;
2023-06-23 20:08:22 +02:00
import { SECURITY _PROVIDER _MESSAGE _SEVERITY } from '../../../../shared/constants/security-provider' ;
2023-02-23 12:38:09 +01:00
import SecurityProviderBannerMessage from './security-provider-banner-message' ;
describe ( 'Security Provider Banner Message' , ( ) => {
const store = configureMockStore ( ) ( { } ) ;
2023-05-04 20:21:46 +02:00
const thisIsBasedOnText = 'OpenSea' ;
2023-02-23 12:38:09 +01:00
it ( 'should render SecurityProviderBannerMessage component properly when flagAsDangerous is malicious' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . MALICIOUS ,
2023-02-23 12:38:09 +01:00
reason :
'Approval is to an unverified smart contract known for stealing NFTs in the past.' ,
reason _header : 'This could be a scam' ,
} ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( securityProviderResponse . reason ) ) . toBeInTheDocument ( ) ;
expect (
getByText ( securityProviderResponse . reason _header ) ,
) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly when flagAsDangerous is not safe' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . NOT _SAFE ,
2023-02-23 12:38:09 +01:00
reason : 'Some reason...' ,
reason _header : 'Some reason header...' ,
} ;
const requestMayNotBeSafe = 'Request may not be safe' ;
const requestMayNotBeSafeError =
"The security provider didn't detect any known malicious activity, but it still may not be safe to continue." ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( requestMayNotBeSafe ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( requestMayNotBeSafeError ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly when flagAsDangerous is undefined' , ( ) => {
const securityProviderResponse = {
flagAsDangerous : '?' ,
reason : 'Some reason...' ,
reason _header : 'Some reason header...' ,
} ;
const requestNotVerified = 'Request not verified' ;
const requestNotVerifiedError =
'Because of an error, this request was not verified by the security provider. Proceed with caution.' ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( requestNotVerified ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( requestNotVerifiedError ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly when securityProviderResponse is empty' , ( ) => {
const securityProviderResponse = { } ;
const requestNotVerified = 'Request not verified' ;
const requestNotVerifiedError =
'Because of an error, this request was not verified by the security provider. Proceed with caution.' ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( requestNotVerified ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( requestNotVerifiedError ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should navigate to the OpenSea web page when clicked on the OpenSea button' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . NOT _SAFE ,
2023-02-23 12:38:09 +01:00
reason : 'Some reason...' ,
reason _header : 'Some reason header...' ,
} ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
const link = getByText ( 'OpenSea' ) ;
expect ( link ) . toBeInTheDocument ( ) ;
fireEvent . click ( link ) ;
expect ( link . closest ( 'a' ) ) . toHaveAttribute ( 'href' , 'https://opensea.io/' ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly, with predefined reason message, when a request is malicious and there is no reason given' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . MALICIOUS ,
2023-02-23 12:38:09 +01:00
reason : '' ,
reason _header : 'Some reason header...' ,
} ;
const reason = 'The security provider has not shared additional details' ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect (
getByText ( securityProviderResponse . reason _header ) ,
) . toBeInTheDocument ( ) ;
expect ( getByText ( reason ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly, with predefined reason_header message, when a request is malicious and there is no reason header given' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . MALICIOUS ,
2023-02-23 12:38:09 +01:00
reason : 'Some reason...' ,
reason _header : '' ,
} ;
const reasonHeader = 'Request flagged as malicious' ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( reasonHeader ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( securityProviderResponse . reason ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
it ( 'should render SecurityProviderBannerMessage component properly, with predefined reason and reason_header messages, when a request is malicious and there are no reason and reason header given' , ( ) => {
const securityProviderResponse = {
2023-06-23 20:08:22 +02:00
flagAsDangerous : SECURITY _PROVIDER _MESSAGE _SEVERITY . MALICIOUS ,
2023-02-23 12:38:09 +01:00
reason : '' ,
reason _header : '' ,
} ;
const reasonHeader = 'Request flagged as malicious' ;
const reason = 'The security provider has not shared additional details' ;
const { getByText } = renderWithProvider (
< SecurityProviderBannerMessage
securityProviderResponse = { securityProviderResponse }
/ > ,
store ,
) ;
expect ( getByText ( reasonHeader ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( reason ) ) . toBeInTheDocument ( ) ;
expect ( getByText ( thisIsBasedOnText ) ) . toBeInTheDocument ( ) ;
} ) ;
} ) ;