From c0b42f93afe2933ce7b6b6f67dc0a88a5e9a3724 Mon Sep 17 00:00:00 2001 From: Kacper Michalik Date: Thu, 28 Sep 2023 13:23:43 +0200 Subject: [PATCH] registerUsername saga test for case with username taken --- .../registerUsername.saga.test.ts | 84 ++++++++++++++++++- .../registerUsername/registerUsername.saga.ts | 7 +- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.test.ts b/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.test.ts index b19a4b049c..da2c954b8b 100644 --- a/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.test.ts +++ b/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.test.ts @@ -1,7 +1,7 @@ import { combineReducers } from '@reduxjs/toolkit' import { expectSaga } from 'redux-saga-test-plan' import { call } from 'redux-saga-test-plan/matchers' -import { setupCrypto, createUserCsr, type UserCsr } from '@quiet/identity' +import { setupCrypto, createUserCsr, type UserCsr, getPubKey, loadPrivateKey, pubKeyFromCsr } from '@quiet/identity' import { prepareStore } from '../../../utils/tests/prepareStore' import { getFactory } from '../../../utils/tests/factories' import { reducers } from '../../reducers' @@ -86,6 +86,88 @@ describe('registerUsernameSaga', () => { ) .run() }) + + it.only('username taken - use current CSR and new nickname', 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['payload']>( + 'Community', + { + id: '1', + name: 'rockets', + registrarUrl: 'registrarUrl', + CA: null, + rootCa: 'rootCa', + peerList: [], + registrar: null, + onionAddress: '', + privateKey: '', + port: 0, + } + ) + const oldNickname = 'john' + const newNickname = 'paul' + const userCsr: UserCsr = { + userCsr: 'userCsr', + userKey: 'userKey', + pkcs10: jest.fn() as unknown as CertData, + } + + const identity = await factory.create['payload']>('Identity', { + nickname: oldNickname, + id: community.id, + userCsr: userCsr, + }) + if (!identity.userCsr?.userCsr) return + const pubKey = 'pubKey' + const privateKey = 'privateKey' + const publicKey = 'publicKey' + + const createUserCsrPayload: CreateUserCsrPayload = { + nickname: newNickname, + commonName: identity.hiddenService.onionAddress, + peerId: identity.peerId.id, + dmPublicKey: identity.dmKeys.publicKey, + signAlg: config.signAlg, + hashAlg: config.hashAlg, + existingKeyPair: { + privateKey: privateKey as unknown as CryptoKey, + publicKey: publicKey as unknown as CryptoKey, + }, + } + const reducer = combineReducers(reducers) + await expectSaga( + registerUsernameSaga, + socket, + identityActions.registerUsername({ nickname: newNickname, isUsernameTaken: true }) + ) + .withReducer(reducer) + .withState(store.getState()) + .provide([ + [call.fn(pubKeyFromCsr), pubKey], + [call.fn(loadPrivateKey), privateKey], + [call.fn(getPubKey), publicKey], + [call.fn(createUserCsr), userCsr], + ]) + .call(pubKeyFromCsr, identity.userCsr.userCsr) + .call(loadPrivateKey, identity.userCsr.userKey, config.signAlg) + .call(getPubKey, pubKey) + .call(createUserCsr, createUserCsrPayload) + .put( + identityActions.registerCertificate({ + communityId: community.id, + nickname: newNickname, + userCsr, + isUsernameTaken: true, + }) + ) + .run() + }) //outdated it.skip("reuse existing csr if provided username hasn't changed", async () => { setupCrypto() diff --git a/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.ts b/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.ts index 920cf77465..2e24050e6c 100644 --- a/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.ts +++ b/packages/state-manager/src/sagas/identity/registerUsername/registerUsername.saga.ts @@ -42,6 +42,7 @@ export function* registerUsernameSaga( } identity = yield* select(identitySelectors.currentIdentity) + if (!identity) { console.error('Could not register username, no identity') return @@ -49,9 +50,7 @@ export function* registerUsernameSaga( let userCsr = identity.userCsr - console.log({ identity }) if (userCsr !== null) { - console.log('userCsr !== null') try { if (identity.userCsr?.userCsr == null || identity.userCsr.userKey == null) { console.error('identity.userCsr?.userCsr == null || identity.userCsr.userKey == null') @@ -72,14 +71,14 @@ export function* registerUsernameSaga( hashAlg: config.hashAlg, existingKeyPair, } + userCsr = yield* call(createUserCsr, payload) - console.log({ userCsr }) + // console.log({ userCsr }) } catch (e) { console.error(e) return } } else { - console.log('userCsr null', userCsr) try { const payload: CreateUserCsrPayload = { nickname,