Skip to content

Commit

Permalink
test: checking local csr
Browse files Browse the repository at this point in the history
  • Loading branch information
siepra committed Dec 7, 2023
1 parent 67ab5ee commit 6058eee
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['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()
})
})
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const csr = (
await createUserCertificateTestHelper(
{
nickname: 'john',
commonName: 'commonName',
peerId: 'peerId',
dmPublicKey: 'dmPublicKey',
},
community.CA
)
).userCsr

const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['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,
{
Expand All @@ -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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

const identity = await factory.create<ReturnType<typeof identityActions.addNewIdentity>['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()
})
})

0 comments on commit 6058eee

Please sign in to comment.