Skip to content
This repository has been archived by the owner on Jan 7, 2022. It is now read-only.

selectros for unregistered communities and change logic in launch sag… #90

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,31 @@ describe('launchCommunity', () => {
port: 0,
};

const identityAlpha: Identity = {
id: 'communityAlpha',
zbayNickname: 'nickname',
hiddenService: {
onionAddress: '',
privateKey: '',
},
dmKeys: {
publicKey: '',
privateKey: '',
},
peerId: {
id: '',
pubKey: '',
privKey: '',
},
userCsr: null,
userCertificate: 'userCert',
};

await expectSaga(initCommunities)
.withReducer(
combineReducers({
[StoreKeys.Communities]: communitiesReducer,
[StoreKeys.Identity]: identityReducer,
}),
{
[StoreKeys.Communities]: {
Expand All @@ -73,6 +94,13 @@ describe('launchCommunity', () => {
[community1, community2, community3]
),
},
[StoreKeys.Identity]: {
...new IdentityState(),
identities: identityAdapter.setAll(
identityAdapter.getInitialState(),
[identityAlpha]
),
},
}
)
.put(communitiesActions.launchCommunity(community1.id))
Expand Down
13 changes: 11 additions & 2 deletions src/sagas/communities/launchCommunity/launchCommunity.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ import { identitySelectors } from '../../identity/identity.selectors';

import { communitiesSelectors } from '../communities.selectors';
import { communitiesActions } from '../communities.slice';
import { identityActions } from '../../identity/identity.slice';

export function* initCommunities(): Generator {
const communities = yield* select(communitiesSelectors.allCommunities);
for (const community of communities) {

const joinedCommunities = yield* select(identitySelectors.joinedCommunities);
const unregisteredCommunities = yield* select(identitySelectors.unregisteredCommunities)
const userName = yield* select(identitySelectors.currentIdentity)

for (const community of unregisteredCommunities) {
yield* put(identityActions.registerUsername(userName.zbayNickname));
}

for (const community of joinedCommunities) {
yield* put(communitiesActions.launchCommunity(community.id));
}
}
Expand Down
254 changes: 254 additions & 0 deletions src/sagas/identity/identity.selectors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import { combineReducers, createStore, Store } from '@reduxjs/toolkit';
import { StoreKeys } from '../store.keys';
import { communitiesAdapter } from './../communities/communities.adapter';
import {
communitiesReducer,
CommunitiesState,
Community,
} from './../communities/communities.slice';
import { identityAdapter } from './identity.adapter';
import { identitySelectors } from './identity.selectors';
import { Identity, identityReducer, IdentityState } from './identity.slice';

describe('communitiesSelectors', () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more readable description that would be appropriate here? What are we checking?

let store: Store;
const communityAlpha: Community = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these different users or different communities? Why are we testing with different communities?

name: 'alpha',
id: 'communityAlpha',
CA: { rootCertString: 'certString', rootKeyString: 'keyString' },
registrarUrl: '',
rootCa: '',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
};
const communityBeta: Community = {
name: 'beta',
id: 'communityBeta',
CA: { rootCertString: 'certString', rootKeyString: 'keyString' },
registrarUrl: '',
rootCa: '',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
};

const communityDelta: Community = {
name: 'beta',
id: 'communityDelta',
CA: { rootCertString: 'certString', rootKeyString: 'keyString' },
registrarUrl: '',
rootCa: '',
peerList: [],
registrar: null,
onionAddress: '',
privateKey: '',
port: 0,
};

const identityAlpha: Identity = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these users? If so, should we just call them users?

id: 'communityAlpha',
zbayNickname: 'nickname',
hiddenService: {
onionAddress: '',
privateKey: '',
},
dmKeys: {
publicKey: '',
privateKey: '',
},
peerId: {
id: '',
pubKey: '',
privKey: '',
},
userCsr: null,
userCertificate: 'userCert',
};

const identityBeta: Identity = {
id: 'communityBeta',
zbayNickname: 'nickname',
hiddenService: {
onionAddress: '',
privateKey: '',
},
dmKeys: {
publicKey: '',
privateKey: '',
},
peerId: {
id: '',
pubKey: '',
privKey: '',
},
userCsr: null,
userCertificate: 'userCert',
};

const identityDelta: Identity = {
id: 'communityDelta',
zbayNickname: 'nickname',
hiddenService: {
onionAddress: '',
privateKey: '',
},
dmKeys: {
publicKey: '',
privateKey: '',
},
peerId: {
id: '',
pubKey: '',
privKey: '',
},
userCsr: null,
userCertificate: null,
};

beforeEach(() => {
store = createStore(
combineReducers({
[StoreKeys.Communities]: communitiesReducer,
[StoreKeys.Identity]: identityReducer,
}),
{
[StoreKeys.Communities]: {
...new CommunitiesState(),
currentCommunity: 'communityBeta',
communities: communitiesAdapter.setAll(
communitiesAdapter.getInitialState(),
[communityAlpha, communityBeta, communityDelta]
),
},
[StoreKeys.Identity]: {
...new IdentityState(),
identities: identityAdapter.setAll(
identityAdapter.getInitialState(),
[identityBeta, identityDelta]
),
},
}
);
});

it('select current id', () => {
const currentIdentity = identitySelectors.currentIdentity(store.getState());

expect(currentIdentity).toMatchInlineSnapshot(`
Object {
"dmKeys": Object {
"privateKey": "",
"publicKey": "",
},
"hiddenService": Object {
"onionAddress": "",
"privateKey": "",
},
"id": "communityBeta",
"peerId": Object {
"id": "",
"privKey": "",
"pubKey": "",
},
"userCertificate": "userCert",
"userCsr": null,
"zbayNickname": "nickname",
}
`);
});

it('select joined communities with user identity with certificate', () => {
const joinedCommunities = identitySelectors.joinedCommunities(
store.getState()
);
const idOfJoinedCommunities = joinedCommunities.map(
(community) => community.id
);

expect(idOfJoinedCommunities).toEqual(['communityBeta']);
expect(joinedCommunities).toMatchInlineSnapshot(`
Array [
Object {
"CA": Object {
"rootCertString": "certString",
"rootKeyString": "keyString",
},
"id": "communityBeta",
"name": "beta",
"onionAddress": "",
"peerList": Array [],
"port": 0,
"privateKey": "",
"registrar": null,
"registrarUrl": "",
"rootCa": "",
},
]
`);
});

it('select unregistered communities with user identity without certificate', () => {
const unregisteredCommunities = identitySelectors.unregisteredCommunities(
store.getState()
);
const idOfJoinedCommunities = unregisteredCommunities.map(
(community) => community.id
);

expect(idOfJoinedCommunities).toEqual(['communityDelta']);
expect(unregisteredCommunities).toMatchInlineSnapshot(`
Array [
Object {
"CA": Object {
"rootCertString": "certString",
"rootKeyString": "keyString",
},
"id": "communityDelta",
"name": "beta",
"onionAddress": "",
"peerList": Array [],
"port": 0,
"privateKey": "",
"registrar": null,
"registrarUrl": "",
"rootCa": "",
},
]
`);
});

it('select communities without user identity', () => {
const unregisteredCommunities = identitySelectors.unregisteredCommunitiesWithoutUserIdentity(
store.getState()
);
const idOfJoinedCommunities = unregisteredCommunities.map(
(community) => community.id
);

expect(idOfJoinedCommunities).toEqual(['communityAlpha']);
expect(unregisteredCommunities).toMatchInlineSnapshot(`
Array [
Object {
"CA": Object {
"rootCertString": "certString",
"rootKeyString": "keyString",
},
"id": "communityAlpha",
"name": "alpha",
"onionAddress": "",
"peerList": Array [],
"port": 0,
"privateKey": "",
"registrar": null,
"registrarUrl": "",
"rootCa": "",
},
]
`);
});
});
44 changes: 43 additions & 1 deletion src/sagas/identity/identity.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StoreKeys } from '../store.keys';
import { createSelector } from '@reduxjs/toolkit';
import { identityAdapter } from './identity.adapter';
import { CreatedSelectors, StoreState } from '../store.types';
import { communitiesSelectors } from '../communities/communities.selectors';
import { allCommunities, communitiesSelectors, _allCommunities } from '../communities/communities.selectors';

const identitySlice: CreatedSelectors[StoreKeys.Identity] = (
state: StoreState
Expand All @@ -27,8 +27,50 @@ export const currentIdentity = createSelector(
}
);

export const joinedCommunities = createSelector(
allCommunities,
identitySlice,
(allCommunities, reducerState) => {
return allCommunities.filter((community) => {
const identityFromCommunity = identityAdapter
.getSelectors()
.selectById(reducerState.identities, community.id)
return identityFromCommunity && identityFromCommunity.userCertificate
})
}
);

export const unregisteredCommunities = createSelector(
allCommunities,
identitySlice,
(allCommunities, reducerState) => {
return allCommunities.filter((community) => {
const identityFromCommunity = identityAdapter
.getSelectors()
.selectById(reducerState.identities, community.id)
return !identityFromCommunity?.userCertificate && identityFromCommunity
})
}
);

export const unregisteredCommunitiesWithoutUserIdentity = createSelector(
allCommunities,
identitySlice,
(allCommunities, reducerState) => {
return allCommunities.filter((community) => {
const identityFromCommunity = identityAdapter
.getSelectors()
.selectById(reducerState.identities, community.id)
return !identityFromCommunity
})
}
);

export const identitySelectors = {
selectById,
selectEntities,
currentIdentity,
joinedCommunities,
unregisteredCommunities,
unregisteredCommunitiesWithoutUserIdentity,
};
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Object {
exports[`publicChannelsSelectors get sliced messages 7`] = `
Object {
"channelId": "general",
"createdAt": 1639169400,
"createdAt": 1639687800,
"id": "9",
"message": "message_9",
"pubKey": Any<String>,
Expand Down
Loading