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 new file mode 100644 index 0000000000..3e9e5f2271 --- /dev/null +++ b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.test.ts @@ -0,0 +1,125 @@ +import { combineReducers } from '@reduxjs/toolkit' +import { expectSaga } from 'redux-saga-test-plan' +import { type Socket } from '../../../types' +import { communitiesReducer, CommunitiesState, type communitiesActions } from '../../communities/communities.slice' +import { StoreKeys } from '../../store.keys' +import { identityAdapter } from '../identity.adapter' +import { identityReducer, IdentityState, type identityActions } from '../identity.slice' +import { saveUserCsrSaga } from './saveUserCsr.saga' +import { type Store } from '../../store.types' +import { type FactoryGirl } from 'factory-girl' +import { setupCrypto } from '@quiet/identity' +import { prepareStore } from '../../../utils/tests/prepareStore' +import { getFactory } from '../../../utils/tests/factories' +import { SocketActionTypes } from '@quiet/types' +import { usersReducer, UsersState } from '../../users/users.slice' +import { certificatesAdapter } from '../../users/users.adapter' + +describe('saveUserCsr', () => { + let store: Store + let factory: FactoryGirl + + beforeEach(async () => { + setupCrypto() + store = prepareStore().store + factory = await getFactory(store) + }) + + test('saves user csr if user csr is absent 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', + }) + + await expectSaga(saveUserCsrSaga, socket) + .withReducer( + combineReducers({ + [StoreKeys.Communities]: communitiesReducer, + [StoreKeys.Identity]: identityReducer, + [StoreKeys.Users]: usersReducer + }), + { + [StoreKeys.Communities]: { + ...new CommunitiesState(), + currentCommunity: community.id, + communities: { + ids: [community.id], + entities: { + [community.id]: community, + }, + }, + }, + [StoreKeys.Identity]: { + ...new IdentityState(), + identities: identityAdapter.setAll(identityAdapter.getInitialState(), [identity]), + }, + [StoreKeys.Users]: { + ...new UsersState() + } + } + ) + .apply(socket, socket.emit, [ + SocketActionTypes.SAVE_USER_CSR, + { + csr: identity.userCsr?.userCsr + }, + ]) + .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', + }) + + console.log(identity.userCsr?.userCsr) + + await expectSaga(saveUserCsrSaga, socket) + .withReducer( + combineReducers({ + [StoreKeys.Communities]: communitiesReducer, + [StoreKeys.Identity]: identityReducer, + [StoreKeys.Users]: usersReducer + }), + { + [StoreKeys.Communities]: { + ...new CommunitiesState(), + currentCommunity: community.id, + communities: { + ids: [community.id], + entities: { + [community.id]: community, + }, + }, + }, + [StoreKeys.Identity]: { + ...new IdentityState(), + identities: identityAdapter.setAll(identityAdapter.getInitialState(), [identity]), + }, + [StoreKeys.Users]: { + ...new UsersState(), + csrs: certificatesAdapter.setAll(certificatesAdapter.getInitialState(), [identity.userCsr?.pkcs10.pkcs10]) + } + } + ) + .not + .apply(socket, socket.emit, [ + SocketActionTypes.SAVE_USER_CSR, + { + csr: identity.userCsr?.userCsr + }, + ]) + .run() + }) +}) diff --git a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts index 5615366511..1895662ef9 100644 --- a/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts +++ b/packages/state-manager/src/sagas/identity/saveUserCsr/saveUserCsr.saga.ts @@ -3,7 +3,7 @@ import { applyEmitParams, type Socket } from '../../../types' import { apply, call, select } from 'typed-redux-saga' import { identitySelectors } from '../identity.selectors' import { usersSelectors } from '../../users/users.selectors' -import { keyFromCertificate, parseCertificationRequest } from '@quiet/identity' +import { pubKeyFromCsr } from '@quiet/identity' export function* saveUserCsrSaga(socket: Socket): Generator { const identity = yield* select(identitySelectors.currentIdentity) @@ -13,8 +13,7 @@ export function* saveUserCsrSaga(socket: Socket): Generator { } // Because we run this saga everytime we launch community (to make sure that our csr is saved to db) we need below logic to avoid duplicates of csrs in the csr database. - const parsedCsr = parseCertificationRequest(identity.userCsr.userCsr) - const pubKey = yield* call(keyFromCertificate, parsedCsr) + const pubKey = yield* call(pubKeyFromCsr, identity.userCsr?.userCsr) const csrs = yield* select(usersSelectors.csrsMapping) if (Object.keys(csrs).includes(pubKey)) return