Skip to content

Commit

Permalink
fix: mobile tests; handle invalid deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Oct 24, 2023
1 parent ac0843d commit 27fb780
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { JoinCommunityProps } from './JoinCommunity.types'
import { getInvitationCodes } from '@quiet/state-manager'

import { Splash } from '../Splash/Splash.component'
import { InvitationData } from '@quiet/types'

export const JoinCommunity: FC<JoinCommunityProps> = ({
joinCommunityAction,
Expand Down Expand Up @@ -39,7 +40,7 @@ export const JoinCommunity: FC<JoinCommunityProps> = ({
return
}

let submitValue
let submitValue: InvitationData | null = null
try {
submitValue = getInvitationCodes(joinCommunityInput.trim())
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InvitationPair } from '@quiet/types'
import { InvitationData } from '@quiet/types'

export interface JoinCommunityProps {
joinCommunityAction: (address: { pairs: InvitationPair[]; psk: string }) => void
joinCommunityAction: (address: InvitationData) => void
redirectionAction: () => void
networkCreated: boolean
invitationCode?: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { FC, useCallback, useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { identity, communities } from '@quiet/state-manager'
import { CommunityOwnership, CreateNetworkPayload, InvitationPair } from '@quiet/types'
import { CommunityOwnership, CreateNetworkPayload, InvitationData, InvitationPair } from '@quiet/types'
import { JoinCommunity } from '../../components/JoinCommunity/JoinCommunity.component'
import { navigationActions } from '../../store/navigation/navigation.slice'
import { ScreenNames } from '../../const/ScreenNames.enum'
Expand Down Expand Up @@ -35,7 +35,7 @@ export const JoinCommunityScreen: FC<JoinCommunityScreenProps> = ({ route }) =>
}, [dispatch, community, route.params?.code])

const joinCommunityAction = useCallback(
(data: { pairs: InvitationPair[]; psk: string }) => {
(data: InvitationData) => {
const payload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
peers: data.pairs,
Expand Down
64 changes: 52 additions & 12 deletions packages/mobile/src/store/init/deepLink/deepLink.saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ import { initActions } from '../init.slice'
import { navigationActions } from '../../navigation/navigation.slice'
import { ScreenNames } from '../../../const/ScreenNames.enum'
import { deepLinkSaga } from './deepLink.saga'
import { type Community, CommunityOwnership, ConnectionProcessInfo, type Identity } from '@quiet/types'
import { Site } from '@quiet/common'
import { type Community, CommunityOwnership, ConnectionProcessInfo, type Identity, InvitationData } from '@quiet/types'
import { Site, composeInvitationShareUrl } from '@quiet/common'
import { appImages } from '../../../assets'
import { replaceScreen } from '../../../RootNavigation'

describe('deepLinkSaga', () => {
let store: Store

const id = '00d045ab'
const validCode = `QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE=y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd&${Site.PSK_PARAM_KEY}=12345`
const validPairs = getInvitationCodes(validCode)
const validData: InvitationData = {
pairs: [
{
onionAddress: 'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
peerId: 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
},
],
psk: 'BNlxfE2WBF7LrlpIX0CvECN5o1oZtA16PkAb7GYiwYw=',
}
const validCode = composeInvitationShareUrl(validData)
const community: Community = {
id,
name: '',
Expand Down Expand Up @@ -70,8 +80,8 @@ describe('deepLinkSaga', () => {
.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validPairs.pairs,
psk: validPairs.psk,
peers: validData.pairs,
psk: validData.psk,
})
)
.run()
Expand Down Expand Up @@ -105,8 +115,8 @@ describe('deepLinkSaga', () => {
.not.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validPairs.pairs,
psk: validPairs.psk,
peers: validData.pairs,
psk: validData.psk,
})
)
.run()
Expand All @@ -129,8 +139,38 @@ describe('deepLinkSaga', () => {
.not.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validPairs.pairs,
psk: validPairs.psk,
peers: validData.pairs,
psk: validData.psk,
})
)
.run()
})

test('displays error if invitation code is invalid', async () => {
const invalidData: InvitationData = {
pairs: [
{
onionAddress: 'y7yczmugl2tekami7sbdz5pfaemvx7bahwthrdvcbzw5vex2crsr26qd',
peerId: 'QmZoiJNAvCffeEHBjk766nLuKVdkxkAT7wfFJDPPLsbKSE',
},
],
psk: 'BNlxfE=',
}
const invalidCode = composeInvitationShareUrl(invalidData)
store.dispatch(
initActions.setWebsocketConnected({
dataPort: 5001,
})
)
const reducer = combineReducers(reducers)
await expectSaga(deepLinkSaga, initActions.deepLink(invalidCode))
.withReducer(reducer)
.withState(store.getState())
.not.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validData.pairs,
psk: validData.psk,
})
)
.run()
Expand Down Expand Up @@ -169,8 +209,8 @@ describe('deepLinkSaga', () => {
.not.put(
communities.actions.createNetwork({
ownership: CommunityOwnership.User,
peers: validPairs.pairs,
psk: validPairs.psk,
peers: validData.pairs,
psk: validData.psk,
})
)
.run()
Expand Down
21 changes: 19 additions & 2 deletions packages/mobile/src/store/init/deepLink/deepLink.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { initSelectors } from '../init.selectors'
import { initActions } from '../init.slice'
import { appImages } from '../../../assets'
import { replaceScreen } from '../../../RootNavigation'
import { CommunityOwnership, CreateNetworkPayload } from '@quiet/types'
import { CommunityOwnership, CreateNetworkPayload, InvitationData } from '@quiet/types'

export function* deepLinkSaga(action: PayloadAction<ReturnType<typeof initActions.deepLink>['payload']>): Generator {
const code = action.payload
Expand Down Expand Up @@ -50,7 +50,24 @@ export function* deepLinkSaga(action: PayloadAction<ReturnType<typeof initAction
})
)

const data = getInvitationCodes(code) // TODO: handle thrown error
let data: InvitationData
try {
data = getInvitationCodes(code)
} catch (e) {
console.error(e.message)
yield* put(
navigationActions.replaceScreen({
screen: ScreenNames.ErrorScreen,
params: {
onPress: () => replaceScreen(ScreenNames.JoinCommunityScreen),
icon: appImages.quiet_icon_round,
title: 'Invalid invitation link',
message: 'Please check your invitation link and try again',
},
})
)
return
}

const payload: CreateNetworkPayload = {
ownership: CommunityOwnership.User,
Expand Down

0 comments on commit 27fb780

Please sign in to comment.