diff --git a/dht-node/src/dht_node/index.test.ts b/dht-node/src/dht_node/index.test.ts index 04a45c51a..ec172c4f8 100644 --- a/dht-node/src/dht_node/index.test.ts +++ b/dht-node/src/dht_node/index.test.ts @@ -1,12 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { - CommunityApi, - startDHT, - writeFederatedHomeCommunityEntries, - writeHomeCommunityEntry, -} from './index' +import { startDHT } from './index' import DHT from '@hyperswarm/dht' import CONFIG from '@/config' import { logger } from '@test/testSetup' @@ -121,6 +116,9 @@ describe('federation', () => { const hashSpy = jest.spyOn(DHT, 'hash') const keyPairSpy = jest.spyOn(DHT, 'keyPair') beforeEach(async () => { + CONFIG.FEDERATION_COMMUNITY_URL = 'https://test.gradido.net' + CONFIG.COMMUNITY_NAME = 'Gradido Test Community' + CONFIG.COMMUNITY_DESCRIPTION = 'Community to test the federation' DHT.mockClear() jest.clearAllMocks() await cleanDB() @@ -139,6 +137,64 @@ describe('federation', () => { expect(DHT).toBeCalledWith({ keyPair: keyPairMock }) }) + it('stores the home community in community table ', async () => { + const result = await DbCommunity.find() + expect(result).toEqual([ + expect.objectContaining({ + id: expect.any(Number), + foreign: false, + url: 'https://test.gradido.net/api/', + publicKey: expect.any(Buffer), + communityUuid: expect.any(String), + authenticatedAt: null, + name: 'Gradido Test Community', + description: 'Community to test the federation', + creationDate: expect.any(Date), + createdAt: expect.any(Date), + updatedAt: null, + }), + ]) + expect(validateUUID(result[0].communityUuid ? result[0].communityUuid : '')).toEqual(true) + expect(versionUUID(result[0].communityUuid ? result[0].communityUuid : '')).toEqual(4) + }) + + it('creates 3 entries in table federated_communities', async () => { + const result = await DbFederatedCommunity.find({ order: { id: 'ASC' } }) + await expect(result).toHaveLength(3) + await expect(result).toEqual([ + expect.objectContaining({ + id: expect.any(Number), + foreign: false, + publicKey: expect.any(Buffer), + apiVersion: '1_0', + endPoint: 'https://test.gradido.net/api/', + lastAnnouncedAt: null, + createdAt: expect.any(Date), + updatedAt: null, + }), + expect.objectContaining({ + id: expect.any(Number), + foreign: false, + publicKey: expect.any(Buffer), + apiVersion: '1_1', + endPoint: 'https://test.gradido.net/api/', + lastAnnouncedAt: null, + createdAt: expect.any(Date), + updatedAt: null, + }), + expect.objectContaining({ + id: expect.any(Number), + foreign: false, + publicKey: expect.any(Buffer), + apiVersion: '2_0', + endPoint: 'https://test.gradido.net/api/', + lastAnnouncedAt: null, + createdAt: expect.any(Date), + updatedAt: null, + }), + ]) + }) + describe('DHT node', () => { it('creates a server', () => { expect(nodeCreateServerMock).toBeCalled() @@ -162,131 +218,6 @@ describe('federation', () => { }) }) - describe('home community', () => { - it('one in table communities', async () => { - const result = await DbCommunity.find({ foreign: false }) - expect(result).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(Number), - foreign: false, - url: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - publicKey: expect.any(Buffer), - communityUuid: expect.any(String), - authenticatedAt: null, - name: CONFIG.COMMUNITY_NAME, - description: CONFIG.COMMUNITY_DESCRIPTION, - creationDate: expect.any(Date), - createdAt: expect.any(Date), - updatedAt: null, - }), - ]), - ) - const valUUID = validateUUID( - result[0].communityUuid != null ? result[0].communityUuid : '', - ) - const verUUID = versionUUID( - result[0].communityUuid != null ? result[0].communityUuid : '', - ) - expect(valUUID).toEqual(true) - expect(verUUID).toEqual(4) - }) - it('update the one in table communities', async () => { - const resultBefore = await DbCommunity.find({ foreign: false }) - expect(resultBefore).toHaveLength(1) - const modifiedCom = DbCommunity.create() - modifiedCom.description = 'updated description' - modifiedCom.name = 'update name' - modifiedCom.publicKey = Buffer.from( - '1234567891abcdef7892abcdef7893abcdef7894abcdef7895abcdef7896abcd', - ) - modifiedCom.url = 'updated url' - await DbCommunity.update(modifiedCom, { id: resultBefore[0].id }) - - await writeHomeCommunityEntry(modifiedCom.publicKey.toString()) - const resultAfter = await DbCommunity.find({ foreign: false }) - expect(resultAfter).toHaveLength(1) - expect(resultAfter).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: resultBefore[0].id, - foreign: false, - url: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - publicKey: modifiedCom.publicKey, - communityUuid: resultBefore[0].communityUuid, - authenticatedAt: null, - name: CONFIG.COMMUNITY_NAME, - description: CONFIG.COMMUNITY_DESCRIPTION, - creationDate: expect.any(Date), - createdAt: expect.any(Date), - updatedAt: expect.any(Date), - }), - ]), - ) - }) - }) - - // skipped because ot timing problems in testframework - describe.skip('federated home community', () => { - it('three in table federated_communities', async () => { - const homeApiVersions: CommunityApi[] = await writeFederatedHomeCommunityEntries( - keyPairMock.publicKey.toString('hex'), - ) - expect(homeApiVersions).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - api: '1_0', - url: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - }), - expect.objectContaining({ - api: '1_1', - url: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - }), - expect.objectContaining({ - api: '2_0', - url: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - }), - ]), - ) - const result = await DbFederatedCommunity.find({ foreign: false }) - expect(result).toHaveLength(3) - expect(result).toEqual( - expect.arrayContaining([ - expect.objectContaining({ - id: expect.any(Number), - foreign: false, - publicKey: expect.any(Buffer), - apiVersion: '1_0', - endPoint: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - lastAnnouncedAt: null, - createdAt: expect.any(Date), - updatedAt: null, - }), - expect.objectContaining({ - id: expect.any(Number), - foreign: false, - publicKey: expect.any(Buffer), - apiVersion: '1_1', - endPoint: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - lastAnnouncedAt: null, - createdAt: expect.any(Date), - updatedAt: null, - }), - expect.objectContaining({ - id: expect.any(Number), - foreign: false, - publicKey: expect.any(Buffer), - apiVersion: '2_0', - endPoint: CONFIG.FEDERATION_COMMUNITY_URL + '/api/', - lastAnnouncedAt: null, - createdAt: expect.any(Date), - updatedAt: null, - }), - ]), - ) - }) - }) - describe('server connection event', () => { beforeEach(() => { serverEventMocks.connection({ @@ -918,15 +849,15 @@ describe('federation', () => { JSON.stringify([ { api: '1_0', - url: 'http://localhost/api/', + url: 'https://test.gradido.net/api/', }, { api: '1_1', - url: 'http://localhost/api/', + url: 'https://test.gradido.net/api/', }, { api: '2_0', - url: 'http://localhost/api/', + url: 'https://test.gradido.net/api/', }, ]), ), @@ -936,5 +867,101 @@ describe('federation', () => { }) }) }) + + describe('restart DHT', () => { + let homeCommunity: DbCommunity + let federatedCommunities: DbFederatedCommunity[] + + describe('without changes', () => { + beforeEach(async () => { + DHT.mockClear() + jest.clearAllMocks() + homeCommunity = (await DbCommunity.find())[0] + federatedCommunities = await DbFederatedCommunity.find({ order: { id: 'ASC' } }) + await startDHT(TEST_TOPIC) + }) + + it('does not change home community in community table except updated at column ', async () => { + await expect(DbCommunity.find()).resolves.toEqual([ + { + ...homeCommunity, + updatedAt: expect.any(Date), + }, + ]) + }) + + it('rewrites the 3 entries in table federated_communities', async () => { + const result = await DbFederatedCommunity.find() + await expect(result).toHaveLength(3) + await expect(result).toEqual([ + { + ...federatedCommunities[0], + id: expect.any(Number), + createdAt: expect.any(Date), + }, + { + ...federatedCommunities[1], + id: expect.any(Number), + createdAt: expect.any(Date), + }, + { + ...federatedCommunities[2], + id: expect.any(Number), + createdAt: expect.any(Date), + }, + ]) + }) + }) + + describe('changeing URL, name and description', () => { + beforeEach(async () => { + CONFIG.FEDERATION_COMMUNITY_URL = 'https://test2.gradido.net' + CONFIG.COMMUNITY_NAME = 'Second Gradido Test Community' + CONFIG.COMMUNITY_DESCRIPTION = 'Another Community to test the federation' + DHT.mockClear() + jest.clearAllMocks() + homeCommunity = (await DbCommunity.find())[0] + federatedCommunities = await DbFederatedCommunity.find({ order: { id: 'ASC' } }) + await startDHT(TEST_TOPIC) + }) + + it('updates URL, name, description and updated at columns ', async () => { + await expect(DbCommunity.find()).resolves.toEqual([ + { + ...homeCommunity, + url: 'https://test2.gradido.net/api/', + name: 'Second Gradido Test Community', + description: 'Another Community to test the federation', + updatedAt: expect.any(Date), + }, + ]) + }) + + it('rewrites the 3 entries in table federated_communities with new endpoint', async () => { + const result = await DbFederatedCommunity.find() + await expect(result).toHaveLength(3) + await expect(result).toEqual([ + { + ...federatedCommunities[0], + id: expect.any(Number), + createdAt: expect.any(Date), + endPoint: 'https://test2.gradido.net/api/', + }, + { + ...federatedCommunities[1], + id: expect.any(Number), + createdAt: expect.any(Date), + endPoint: 'https://test2.gradido.net/api/', + }, + { + ...federatedCommunities[2], + id: expect.any(Number), + createdAt: expect.any(Date), + endPoint: 'https://test2.gradido.net/api/', + }, + ]) + }) + }) + }) }) }) diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index bd9c95a7e..f74588904 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -197,16 +197,16 @@ export async function writeFederatedHomeCommunityEntries(pubKey: string): Promis }) try { // first remove privious existing homeCommunity entries - DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() - for (let i = 0; i < homeApiVersions.length; i++) { + await DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() + for (const homeApiVersion of homeApiVersions) { const homeCom = DbFederatedCommunity.create() homeCom.foreign = false - homeCom.apiVersion = homeApiVersions[i].api - homeCom.endPoint = homeApiVersions[i].url + homeCom.apiVersion = homeApiVersion.api + homeCom.endPoint = homeApiVersion.url homeCom.publicKey = Buffer.from(pubKey) await DbFederatedCommunity.insert(homeCom) logger.info( - `federation home-community inserted successfully: ${JSON.stringify(homeApiVersions[i])}`, + `federation home-community inserted successfully: ${JSON.stringify(homeApiVersion)}`, ) } } catch (err) {