Skip to content

Commit

Permalink
Fix/Use CSRs in mocking test data #1900 (#2053)
Browse files Browse the repository at this point in the history
* test: use factory-girl in users selectors tests

* test: include csrs in factory-girl models

* test: add tests for 'duplicate' label #1900

* fix: register username test

* fix: public channel selectors test

* fix: remaining tests

* fix: lint

* fix: rtl tests

* fix: lint
  • Loading branch information
siepra authored Nov 13, 2023
1 parent 27e7916 commit f94151b
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 127 deletions.
5 changes: 3 additions & 2 deletions packages/desktop/src/rtl-tests/community.join.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ describe('User', () => {
},
communityHelper.CA
)
const certificate = certificateHelper.userCert.userCertString
const certificate = certificateHelper.userCert?.userCertString
const rootCa = communityHelper.CA?.rootCertString
return socket.socketClient.emit<SendOwnerCertificatePayload>(SocketActionTypes.SEND_USER_CERTIFICATE, {
communityId: payload.communityId,
payload: {
// @ts-expect-error - This is statically mocked data so it'll never be undefined
certificate: certificate,
// @ts-expect-error
// @ts-expect-error - This is statically mocked data so it'll never be undefined
rootCa: rootCa,
peers: [],
},
Expand Down
24 changes: 15 additions & 9 deletions packages/identity/src/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export const createUserCertificateTestHelper = async (
peerId: string
dmPublicKey: string
},
rootCA: Pick<RootCA, 'rootCertString' | 'rootKeyString'>
rootCA?: Pick<RootCA, 'rootCertString' | 'rootKeyString'> | null
): Promise<{
userCert: UserCert
userCsr: UserCsr
userCert?: UserCert
}> => {
const userCsr = await createUserCsr({
nickname: user.nickname,
Expand All @@ -77,13 +77,19 @@ export const createUserCertificateTestHelper = async (
signAlg: config.signAlg,
hashAlg: config.hashAlg,
})
const userCert = await createUserCert(
rootCA.rootCertString,
rootCA.rootKeyString,
userCsr.userCsr,
notBeforeDate,
notAfterDate
)

let userCert

if (rootCA) {
userCert = await createUserCert(
rootCA.rootCertString,
rootCA.rootKeyString,
userCsr.userCsr,
notBeforeDate,
notAfterDate
)
}

return {
userCsr,
userCert,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { type communitiesActions } from '../../communities/communities.slice'
import { config } from '../../users/const/certFieldTypes'
import { CertData, CreateUserCsrPayload, SocketActionTypes } from '@quiet/types'
import { Socket } from '../../../types'
import { connectionActions } from '../../appConnection/connection.slice'

describe('registerUsernameSaga', () => {
it('create user csr', async () => {
setupCrypto()

const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket

const store = prepareStore().store
Expand All @@ -25,23 +25,15 @@ describe('registerUsernameSaga', () => {
const community = await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>(
'Community',
{
id: '1',
name: 'rockets',
registrarUrl: 'registrarUrl',
CA: null,
rootCa: 'rootCa',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
rootCa: 'rootCertString',
}
)

// Identity won't have userCsr as long as its corresponding community has no CA (factory specific logic)
const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['payload']>('Identity', {
nickname: undefined,
id: community.id,
nickname: undefined,
userCsr: null,
})

Expand All @@ -59,6 +51,7 @@ describe('registerUsernameSaga', () => {
signAlg: config.signAlg,
hashAlg: config.hashAlg,
}

const reducer = combineReducers(reducers)
await expectSaga(registerUsernameSaga, socket, identityActions.registerUsername({ nickname: 'nickname' }))
.withReducer(reducer)
Expand Down Expand Up @@ -95,23 +88,12 @@ describe('registerUsernameSaga', () => {

const factory = await getFactory(store)

const community = await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>(
'Community',
{
id: '1',
name: 'rockets',
registrarUrl: 'registrarUrl',
CA: null,
rootCa: 'rootCa',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
}
)
const community =
await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const oldNickname = 'john'
const newNickname = 'paul'

const userCsr: UserCsr = {
userCsr: 'userCsr',
userKey: 'userKey',
Expand All @@ -123,6 +105,7 @@ describe('registerUsernameSaga', () => {
id: community.id,
userCsr: userCsr,
})

if (!identity.userCsr?.userCsr) return
const pubKey = 'pubKey'
const privateKey = 'privateKey'
Expand All @@ -140,6 +123,7 @@ describe('registerUsernameSaga', () => {
publicKey: publicKey as unknown as CryptoKey,
},
}

const reducer = combineReducers(reducers)
await expectSaga(
registerUsernameSaga,
Expand Down Expand Up @@ -168,30 +152,19 @@ describe('registerUsernameSaga', () => {
)
.run()
})
//outdated

// Outdated
it.skip("reuse existing csr if provided username hasn't changed", async () => {
setupCrypto()

const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket

const store = prepareStore().store

const factory = await getFactory(store)

const community = await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>(
'Community',
{
id: '1',
name: 'rockets',
registrarUrl: 'registrarUrl',
CA: null,
rootCa: 'rootCa',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
}
)
const community =
await factory.create<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const userCsr: UserCsr = {
userCsr: 'userCsr',
Expand All @@ -209,6 +182,7 @@ describe('registerUsernameSaga', () => {
identity.userCsr = userCsr

store.dispatch(identityActions.addNewIdentity(identity))

const reducer = combineReducers(reducers)
await expectSaga(registerUsernameSaga, socket, identityActions.registerUsername({ nickname: identity.nickname }))
.withReducer(reducer)
Expand All @@ -234,9 +208,11 @@ describe('registerUsernameSaga', () => {
)
.run()
})
//outdated

// Outdated
it.skip("don't reuse existing csr if provided username has changed", async () => {
setupCrypto()

const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket

const store = prepareStore().store
Expand Down Expand Up @@ -290,6 +266,7 @@ describe('registerUsernameSaga', () => {
signAlg: config.signAlg,
hashAlg: config.hashAlg,
}

const reducer = combineReducers(reducers)
await expectSaga(
registerUsernameSaga,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,18 @@ describe('publicChannelsSelectors', () => {
id: community.id,
nickname: 'alice',
})

const generalChannelState = publicChannelsSelectors.generalChannel(store.getState())
if (generalChannelState) generalChannel = generalChannelState

expect(generalChannel).not.toBeUndefined()

channelIdes = [...channelIdes, generalChannel.id]
john = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['payload']>('Identity', {
id: community.id,
nickname: 'john',
})

store.dispatch(publicChannelsActions.setCurrentChannel({ channelId: generalChannel.id }))
// Setup channels
const channelNames = ['croatia', 'allergies', 'sailing', 'pets', 'antiques']
Expand Down Expand Up @@ -317,6 +321,8 @@ describe('publicChannelsSelectors', () => {

if (!elouise.userCertificate) throw new Error('no elouise.userCertificate')
store.dispatch(usersActions.test_remove_user_certificate({ certificate: elouise.userCertificate }))
// @ts-expect-error - This is statically mocked data so it'll never be undefined
store.dispatch(usersActions.test_remove_user_csr({ csr: elouise.userCsr?.userCsr }))

store.dispatch(
publicChannelsActions.setCurrentChannel({
Expand Down
Loading

0 comments on commit f94151b

Please sign in to comment.