Skip to content

Commit

Permalink
chore: add tests for saveUserCsr saga
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkabuki committed Nov 21, 2023
1 parent 19fd1af commit fc6eb2a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof communitiesActions.addNewCommunity>['payload']>('Community')

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

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

Expand Down

0 comments on commit fc6eb2a

Please sign in to comment.