-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
110 additions
and
55 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
packages/state-manager/src/sagas/identity/checkLocalCsr/checkLocalCsr.saga.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters