Skip to content

Commit

Permalink
registerUsername saga test for case with username taken
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacper-RF committed Sep 28, 2023
1 parent 3332578 commit c0b42f9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<ReturnType<typeof communitiesActions.addNewCommunity>['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<ReturnType<typeof identityActions.addNewIdentity>['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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ export function* registerUsernameSaga(
}

identity = yield* select(identitySelectors.currentIdentity)

if (!identity) {
console.error('Could not register username, no identity')
return
}

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')
Expand All @@ -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,
Expand Down

0 comments on commit c0b42f9

Please sign in to comment.