From 6058eee2f1d879853132d3547f325e663122f45e Mon Sep 17 00:00:00 2001 From: siepra Date: Thu, 7 Dec 2023 09:54:14 +0100 Subject: [PATCH] test: checking local csr --- .../checkLocalCsr/checkLocalCsr.saga.test.ts | 104 ++++++++++++++++++ .../saveUserCsr/saveUserCsr.saga.test.ts | 61 +--------- 2 files changed, 110 insertions(+), 55 deletions(-) create mode 100644 packages/state-manager/src/sagas/identity/checkLocalCsr/checkLocalCsr.saga.test.ts diff --git a/packages/state-manager/src/sagas/identity/checkLocalCsr/checkLocalCsr.saga.test.ts b/packages/state-manager/src/sagas/identity/checkLocalCsr/checkLocalCsr.saga.test.ts new file mode 100644 index 0000000000..6091a17fc1 --- /dev/null +++ b/packages/state-manager/src/sagas/identity/checkLocalCsr/checkLocalCsr.saga.test.ts @@ -0,0 +1,104 @@ +import { createUserCsr, getPubKey, loadPrivateKey, pubKeyFromCsr, setupCrypto } from '@quiet/identity' +import { FactoryGirl } from 'factory-girl' +import { getFactory } from '../../../utils/tests/factories' +import { prepareStore, reducers } from '../../../utils/tests/prepareStore' +import { Store, combineReducers } from 'redux' +import { communitiesActions } from '../../communities/communities.slice' +import { identityActions } from '../identity.slice' +import { checkLocalCsrSaga } from './checkLocalCsr.saga' +import { CreateUserCsrPayload, SendCsrsResponse } from '@quiet/types' +import { expectSaga } from 'redux-saga-test-plan' +import { usersActions } from '../../users/users.slice' + +describe('checkLocalCsr', () => { + let store: Store + let factory: FactoryGirl + + beforeEach(async () => { + setupCrypto() + store = prepareStore().store + factory = await getFactory(store) + }) + + test('saves user csr if absent from the database', async () => { + const community = + await factory.create['payload']>('Community') + + const identity = await factory.create['payload']>('Identity', { + id: community.id, + nickname: 'john', + }) + + const payload: SendCsrsResponse = { + csrs: [], + } + + const reducer = combineReducers(reducers) + await expectSaga(checkLocalCsrSaga, usersActions.storeCsrs(payload)) + .withReducer(reducer) + .withState(store.getState()) + .put(identityActions.saveUserCsr()) + .run() + }) + + test('saves user csr if local and stored one differs', async () => { + const community = + await factory.create['payload']>('Community') + + const identity = await factory.create['payload']>('Identity', { + id: community.id, + nickname: 'john', + }) + + const _pubKey = pubKeyFromCsr(identity.userCsr!.userCsr) + + const privateKey = await loadPrivateKey(identity.userCsr!.userKey, 'ECDSA') + const publicKey = await getPubKey(_pubKey) + + const existingKeyPair: CryptoKeyPair = { privateKey, publicKey } + + const createUserCsrPayload: CreateUserCsrPayload = { + nickname: 'alice', + commonName: identity.hiddenService.onionAddress, + peerId: identity.peerId.id, + dmPublicKey: identity.dmKeys.publicKey, + signAlg: 'ECDSA', + hashAlg: 'sha-256', + existingKeyPair, + } + + const csr = await createUserCsr(createUserCsrPayload) + + const payload: SendCsrsResponse = { + csrs: [csr.userCsr], + } + + const reducer = combineReducers(reducers) + await expectSaga(checkLocalCsrSaga, usersActions.storeCsrs(payload)) + .withReducer(reducer) + .withState(store.getState()) + .put(identityActions.saveUserCsr()) + .run() + }) + + test('skips if stored csr equals local one', async () => { + const community = + await factory.create['payload']>('Community') + + const identity = await factory.create['payload']>('Identity', { + id: community.id, + nickname: 'john', + }) + + const payload: SendCsrsResponse = { + csrs: [identity.userCsr!.userCsr], + } + + const reducer = combineReducers(reducers) + await expectSaga(checkLocalCsrSaga, usersActions.storeCsrs(payload)) + .withReducer(reducer) + .withState(store.getState()) + .put(identityActions.saveUserCsr()) + .run() + }) +}) diff --git a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.test.ts b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.test.ts index 2c6f3425cf..64bd868e2c 100644 --- a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.test.ts +++ b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.test.ts @@ -1,17 +1,16 @@ import { combineReducers } from '@reduxjs/toolkit' import { expectSaga } from 'redux-saga-test-plan' import { type Socket } from '../../../types' -import { communitiesReducer, type communitiesActions } from '../../communities/communities.slice' -import { StoreKeys } from '../../store.keys' -import { identityReducer, type identityActions } from '../identity.slice' +import { type communitiesActions } from '../../communities/communities.slice' +import { type identityActions } from '../identity.slice' import { saveUserCsrSaga } from './saveUserCsr.saga' import { type Store } from '../../store.types' import { type FactoryGirl } from 'factory-girl' -import { pubKeyFromCsr, setupCrypto, createUserCertificateTestHelper } from '@quiet/identity' +import { setupCrypto } from '@quiet/identity' import { prepareStore } from '../../../utils/tests/prepareStore' import { getFactory } from '../../../utils/tests/factories' import { SocketActionTypes } from '@quiet/types' -import { usersReducer } from '../../users/users.slice' +import { reducers } from '../../reducers' describe('saveUserCsr', () => { let store: Store @@ -23,40 +22,21 @@ describe('saveUserCsr', () => { factory = await getFactory(store) }) - test('saves user csr if user csr is absent in csrs list', async () => { + test('saves user csr', async () => { const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket const community = await factory.create['payload']>('Community') - const csr = ( - await createUserCertificateTestHelper( - { - nickname: 'john', - commonName: 'commonName', - peerId: 'peerId', - dmPublicKey: 'dmPublicKey', - }, - community.CA - ) - ).userCsr - const identity = await factory.create['payload']>('Identity', { id: community.id, nickname: 'john', - userCsr: csr, - }) - - const reducer = combineReducers({ - [StoreKeys.Communities]: communitiesReducer, - [StoreKeys.Identity]: identityReducer, - [StoreKeys.Users]: usersReducer, }) + const reducer = combineReducers(reducers) await expectSaga(saveUserCsrSaga, socket) .withReducer(reducer) .withState(store.getState()) - .call(pubKeyFromCsr, identity.userCsr?.userCsr) .apply(socket, socket.emit, [ SocketActionTypes.SAVE_USER_CSR, { @@ -65,33 +45,4 @@ describe('saveUserCsr', () => { ]) .run() }) - test("do not save user csr if it's already included in csrs list", async () => { - const socket = { emit: jest.fn(), on: jest.fn() } as unknown as Socket - - const community = - await factory.create['payload']>('Community') - - const identity = await factory.create['payload']>('Identity', { - id: community.id, - nickname: 'john', - }) - - const reducer = combineReducers({ - [StoreKeys.Communities]: communitiesReducer, - [StoreKeys.Identity]: identityReducer, - [StoreKeys.Users]: usersReducer, - }) - - await expectSaga(saveUserCsrSaga, socket) - .withReducer(reducer) - .withState(store.getState()) - .call(pubKeyFromCsr, identity.userCsr?.userCsr) - .not.apply(socket, socket.emit, [ - SocketActionTypes.SAVE_USER_CSR, - { - csr: identity.userCsr?.userCsr, - }, - ]) - .run() - }) })