Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/mobile init redirection #1956

Merged
merged 6 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const PossibleImpersonationAttackComponent: React.FC<PossibleImpersonationAttack
return (
<View
style={{ flex: 1, backgroundColor: defaultTheme.palette.background.white }}
data-testid={'possible-impersonation-attack-component'}
testID={'possible-impersonation-attack-component'}
>
<Appbar title={'Warning!'} back={handleBackButton} crossBackIcon />
<View style={classes.mainWrapper}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ describe('PossibleImpersonationAttack component', () => {

expect(toJSON()).toMatchInlineSnapshot(`
<View
data-testid="possible-impersonation-attack-component"
style={
{
"backgroundColor": "#ffffff",
"flex": 1,
}
}
testID="possible-impersonation-attack-component"
>
<View
style={
Expand Down
29 changes: 26 additions & 3 deletions packages/mobile/src/screens/ChannelList/ChannelList.screen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { FC, useCallback } from 'react'
import React, { FC, useCallback, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import { communities, publicChannels } from '@quiet/state-manager'
import { communities, identity, users, publicChannels } from '@quiet/state-manager'
import { getChannelNameFromChannelId } from '@quiet/common'

import { ChannelList as ChannelListComponent } from '../../components/ChannelList/ChannelList.component'
import { ChannelTileProps } from '../../components/ChannelTile/ChannelTile.types'
Expand All @@ -12,11 +13,31 @@ import { formatMessageDisplayDate } from '../../utils/functions/formatMessageDis

import { useContextMenu } from '../../hooks/useContextMenu'
import { MenuName } from '../../const/MenuNames.enum'
import { getChannelNameFromChannelId } from '@quiet/common'

export const ChannelListScreen: FC = () => {
const dispatch = useDispatch()

const usernameTaken = useSelector(identity.selectors.usernameTaken)
const duplicateCerts = useSelector(users.selectors.duplicateCerts)

useEffect(() => {
if (usernameTaken) {
dispatch(
navigationActions.replaceScreen({
screen: ScreenNames.UsernameTakenScreen,
})
)
}

if (duplicateCerts) {
dispatch(
navigationActions.replaceScreen({
screen: ScreenNames.PossibleImpersonationAttackScreen,
})
)
}
}, [dispatch, usernameTaken, duplicateCerts])

const redirect = useCallback(
(id: string) => {
dispatch(
Expand All @@ -34,10 +55,12 @@ export const ChannelListScreen: FC = () => {
)

const community = useSelector(communities.selectors.currentCommunity)

const channelsStatusSorted = useSelector(publicChannels.selectors.channelsStatusSorted)

const tiles = channelsStatusSorted.map(status => {
const newestMessage = status.newestMessage

const message = newestMessage?.message || '...'
const date = newestMessage?.createdAt ? formatMessageDisplayDate(newestMessage.createdAt) : undefined

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initSelectors } from '../../init/init.selectors'
import { navigationSelectors } from '../navigation.selectors'
import { navigationActions } from '../navigation.slice'
import { ScreenNames } from '../../../const/ScreenNames.enum'
import { identity, publicChannels, users } from '@quiet/state-manager'
import { identity } from '@quiet/state-manager'
import { initActions } from '../../init/init.slice'

export function* redirectionSaga(): Generator {
Expand All @@ -25,34 +25,10 @@ export function* redirectionSaga(): Generator {
return
}

const isUsernameTaken = yield* select(identity.selectors.usernameTaken)

if (isUsernameTaken) {
yield* put(
navigationActions.replaceScreen({
screen: ScreenNames.UsernameTakenScreen,
})
)
}

const duplicateCerts = yield* select(users.selectors.duplicateCerts)

if (duplicateCerts) {
yield* put(
navigationActions.replaceScreen({
screen: ScreenNames.PossibleImpersonationAttackScreen,
})
)
return
}

// If user belongs to a community, let him directly into the app
// Check while QA session!
const currentChannelDisplayableMessages = yield* select(publicChannels.selectors.currentChannelMessagesMergedBySender)
const areMessagesLoaded = Object.values(currentChannelDisplayableMessages).length > 0
const communityMembership = yield* select(identity.selectors.communityMembership)

if (communityMembership && areMessagesLoaded) {
if (communityMembership) {
yield* put(
navigationActions.replaceScreen({
screen: ScreenNames.ChannelListScreen,
Expand Down
32 changes: 9 additions & 23 deletions packages/mobile/src/tests/possibleImpersonationAttack.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import React from 'react'
import '@testing-library/jest-native/extend-expect'
import { act } from '@testing-library/react-native'
import MockedSocket from 'socket.io-mock'
import { ioMock } from '../setupTests'
import { prepareStore } from './utils/prepareStore'
import { renderComponent } from './utils/renderComponent'
import { FactoryGirl } from 'factory-girl'
import { getFactory, communities, identity, users } from '@quiet/state-manager'
import { navigationActions } from '../store/navigation/navigation.slice'
import { PossibleImpersonationAttackScreen } from '../screens/PossibleImpersonationAttack/PossibleImpersonationAttack.screen'
import { ScreenNames } from '../const/ScreenNames.enum'
import { navigationSelectors } from '../store/navigation/navigation.selectors'
import { ChannelListScreen } from '../screens/ChannelList/ChannelList.screen'
import { initActions } from '../store/init/init.slice'
import { navigationSelectors } from '../store/navigation/navigation.selectors'

describe('Possible Impersonation Attack', () => {
let socket: MockedSocket
Expand All @@ -23,9 +21,11 @@ describe('Possible Impersonation Attack', () => {
ioMock.mockImplementation(() => socket)
})

it('Open modal when certifcates are duplicated', async () => {
it('Display warning when certifcates are duplicated', async () => {
const { store, root } = await prepareStore({}, socket)

store.dispatch(initActions.setStoreReady())

factory = await getFactory(store)

const community = await factory.create<ReturnType<typeof communities.actions.addNewCommunity>['payload']>(
Expand All @@ -37,17 +37,6 @@ describe('Possible Impersonation Attack', () => {
nickname: 'alice',
})

const route: { key: string; name: ScreenNames.PossibleImpersonationAttackScreen; path?: string | undefined } = {
key: '',
name: ScreenNames.PossibleImpersonationAttackScreen,
}
renderComponent(
<>
<PossibleImpersonationAttackScreen route={route} />
</>,
store
)

const cert1 =
'MIIDeDCCAx6gAwIBAgIGAYr6Jw3hMAoGCCqGSM49BAMCMA8xDTALBg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521GsfuHfbMAiBEYQ4l074oUEF2DTVK1agJlhMR5USRxav5xEpx2ujMeA=='

Expand All @@ -66,17 +55,14 @@ describe('Possible Impersonation Attack', () => {
})
)

await act(async () => {})

store.dispatch(navigationActions.redirection())

await act(async () => {})
renderComponent(<ChannelListScreen />, store)

const duplicateCerts = users.selectors.duplicateCerts(store.getState())
const currentScreen = navigationSelectors.currentScreen(store.getState())
expect(duplicateCerts).toBe(true)

const currentScreen = navigationSelectors.currentScreen(store.getState())
expect(currentScreen).toBe(ScreenNames.PossibleImpersonationAttackScreen)
expect(duplicateCerts).toBeTruthy()

root?.cancel()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StoreKeys } from '../store.keys'
import { createSelector } from '@reduxjs/toolkit'
import { identityAdapter } from './identity.adapter'
import { type CreatedSelectors, type StoreState } from '../store.types'
import { communitiesSelectors, selectCommunities } from '../communities/communities.selectors'
import { communitiesSelectors, selectCommunities, currentCommunity } from '../communities/communities.selectors'
import { certificatesMapping } from '../users/users.selectors'

const identitySlice: CreatedSelectors[StoreKeys.Identity] = (state: StoreState) => state[StoreKeys.Identity]
Expand All @@ -22,8 +22,8 @@ export const currentIdentity = createSelector(
}
)

export const communityMembership = createSelector(currentIdentity, identity => {
return Boolean(identity?.userCsr)
export const communityMembership = createSelector(currentIdentity, currentCommunity, (identity, community) => {
return Boolean(identity?.userCsr && community?.name)
})

export const hasCertificate = createSelector(currentIdentity, identity => {
Expand Down
Loading