mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
homeCommunity tests, writing homeCom in federated communities is not
working
This commit is contained in:
parent
618569d6e6
commit
2537586a47
@ -6,7 +6,7 @@ module.exports = {
|
|||||||
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
|
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
|
||||||
coverageThreshold: {
|
coverageThreshold: {
|
||||||
global: {
|
global: {
|
||||||
lines: 78,
|
lines: 80,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
setupFiles: ['<rootDir>/test/testSetup.ts'],
|
setupFiles: ['<rootDir>/test/testSetup.ts'],
|
||||||
|
|||||||
@ -1,12 +1,19 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import { startDHT } from './index'
|
import {
|
||||||
|
CommunityApi,
|
||||||
|
startDHT,
|
||||||
|
writeFederatedHomeCommunityEntries,
|
||||||
|
writeHomeCommunityEntry,
|
||||||
|
} from './index'
|
||||||
import DHT from '@hyperswarm/dht'
|
import DHT from '@hyperswarm/dht'
|
||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
import { logger } from '@test/testSetup'
|
import { logger } from '@test/testSetup'
|
||||||
|
import { Community as DbCommunity } from '@entity/Community'
|
||||||
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
|
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
|
||||||
import { testEnvironment, cleanDB } from '@test/helpers'
|
import { testEnvironment, cleanDB } from '@test/helpers'
|
||||||
|
import { validate as validateUUID, version as versionUUID } from 'uuid'
|
||||||
|
|
||||||
CONFIG.FEDERATION_DHT_SEED = '64ebcb0e3ad547848fef4197c6e2332f'
|
CONFIG.FEDERATION_DHT_SEED = '64ebcb0e3ad547848fef4197c6e2332f'
|
||||||
|
|
||||||
@ -101,7 +108,7 @@ beforeAll(async () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await cleanDB()
|
// await cleanDB()
|
||||||
await con.close()
|
await con.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -155,6 +162,135 @@ describe('federation', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('home community', () => {
|
||||||
|
it('one in 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 communities', async () => {
|
||||||
|
const resultBefore = await DbCommunity.find({ foreign: false })
|
||||||
|
expect(resultBefore).toHaveLength(1)
|
||||||
|
const modifiedCom = DbCommunity.create()
|
||||||
|
modifiedCom.communityUuid = resultBefore[0].communityUuid
|
||||||
|
modifiedCom.creationDate = resultBefore[0].creationDate
|
||||||
|
modifiedCom.description = 'updated description'
|
||||||
|
modifiedCom.foreign = resultBefore[0].foreign
|
||||||
|
modifiedCom.id = resultBefore[0].id
|
||||||
|
modifiedCom.name = 'update name'
|
||||||
|
modifiedCom.publicKey = Buffer.from(
|
||||||
|
'1234567891abcdef7892abcdef7893abcdef7894abcdef7895abcdef7896abcd1234567891abcdef7892abcdef7893abcdef7894abcdef7895abcdef7896abcd',
|
||||||
|
'hex',
|
||||||
|
)
|
||||||
|
modifiedCom.url = 'updated url'
|
||||||
|
await DbCommunity.update(modifiedCom, { id: resultBefore[0].id })
|
||||||
|
|
||||||
|
await writeHomeCommunityEntry(modifiedCom.publicKey.toString('hex'))
|
||||||
|
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),
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('federated home community', () => {
|
||||||
|
it('three in 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', () => {
|
describe('server connection event', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
serverEventMocks.connection({
|
serverEventMocks.connection({
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import { Community as DbCommunity } from '@entity/Community'
|
|||||||
import DEVOP from '@/config/devop'
|
import DEVOP from '@/config/devop'
|
||||||
import { setDevOpEnvValue } from '@/config/tools'
|
import { setDevOpEnvValue } from '@/config/tools'
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
import { InsertResult } from '@dbTools/typeorm'
|
||||||
|
|
||||||
const KEY_SECRET_SEEDBYTES = 32
|
const KEY_SECRET_SEEDBYTES = 32
|
||||||
const getSeed = (): Buffer | null => {
|
const getSeed = (): Buffer | null => {
|
||||||
@ -30,7 +31,7 @@ enum ApiVersionType {
|
|||||||
V1_1 = '1_1',
|
V1_1 = '1_1',
|
||||||
V2_0 = '2_0',
|
V2_0 = '2_0',
|
||||||
}
|
}
|
||||||
type CommunityApi = {
|
export type CommunityApi = {
|
||||||
api: string
|
api: string
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
@ -44,7 +45,7 @@ export const startDHT = async (topic: string): Promise<void> => {
|
|||||||
// insert or update keyPair in .env.devop file
|
// insert or update keyPair in .env.devop file
|
||||||
setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex'))
|
setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex'))
|
||||||
setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex'))
|
setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex'))
|
||||||
await writeHomeCommunityEntry(keyPair.publicKey)
|
await writeHomeCommunityEntry(keyPair.publicKey.toString('hex'))
|
||||||
|
|
||||||
const ownApiVersions = await writeFederatedHomeCommunityEntries(keyPair.publicKey)
|
const ownApiVersions = await writeFederatedHomeCommunityEntries(keyPair.publicKey)
|
||||||
logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`)
|
logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`)
|
||||||
@ -194,7 +195,7 @@ export const startDHT = async (topic: string): Promise<void> => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeFederatedHomeCommunityEntries(pubKey: any): Promise<CommunityApi[]> {
|
export async function writeFederatedHomeCommunityEntries(pubKey: any): Promise<CommunityApi[]> {
|
||||||
const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) {
|
const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) {
|
||||||
const comApi: CommunityApi = {
|
const comApi: CommunityApi = {
|
||||||
api: apiEnum,
|
api: apiEnum,
|
||||||
@ -205,48 +206,69 @@ async function writeFederatedHomeCommunityEntries(pubKey: any): Promise<Communit
|
|||||||
try {
|
try {
|
||||||
// first remove privious existing homeCommunity entries
|
// first remove privious existing homeCommunity entries
|
||||||
DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute()
|
DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute()
|
||||||
|
// homeApiVersions.forEach(async function (homeApi) {
|
||||||
homeApiVersions.forEach(async function (homeApi) {
|
for (let i = 0; i < homeApiVersions.length; i++) {
|
||||||
const homeCom = new DbFederatedCommunity()
|
const result = await createFederatedCommunityEntity(homeApiVersions[i], pubKey)
|
||||||
homeCom.foreign = false
|
console.log(`ApiVersion:${JSON.stringify(homeApiVersions[i])}`, JSON.stringify(result))
|
||||||
homeCom.apiVersion = homeApi.api
|
logger.info(
|
||||||
homeCom.endPoint = homeApi.url
|
`federation home-community inserted successfully: ${JSON.stringify(homeApiVersions[i])}`,
|
||||||
homeCom.publicKey = pubKey.toString('hex')
|
)
|
||||||
|
}
|
||||||
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
|
||||||
await DbFederatedCommunity.insert(homeCom)
|
|
||||||
logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`)
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.log('Error1:', err)
|
||||||
throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`)
|
throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`)
|
||||||
}
|
}
|
||||||
return homeApiVersions
|
return homeApiVersions
|
||||||
}
|
}
|
||||||
|
|
||||||
async function writeHomeCommunityEntry(pubKey: any): Promise<void> {
|
async function createFederatedCommunityEntity(
|
||||||
|
homeApi: CommunityApi,
|
||||||
|
pubKey: string,
|
||||||
|
): Promise<boolean> {
|
||||||
|
let result: InsertResult
|
||||||
|
try {
|
||||||
|
const homeCom = DbFederatedCommunity.create()
|
||||||
|
homeCom.foreign = false
|
||||||
|
homeCom.apiVersion = homeApi.api
|
||||||
|
homeCom.endPoint = homeApi.url
|
||||||
|
homeCom.publicKey = Buffer.from(pubKey, 'hex')
|
||||||
|
|
||||||
|
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
||||||
|
result = await DbFederatedCommunity.insert(homeCom)
|
||||||
|
logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`)
|
||||||
|
console.log(`result: ${JSON.stringify(result)}`)
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Error2:', err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function writeHomeCommunityEntry(pubKey: string): Promise<void> {
|
||||||
|
console.log(`pubKey = `, pubKey)
|
||||||
try {
|
try {
|
||||||
// check for existing homeCommunity entry
|
// check for existing homeCommunity entry
|
||||||
let homeCom = await DbCommunity.findOne({ foreign: false, publicKey: pubKey })
|
let homeCom = await DbCommunity.findOne({ foreign: false, publicKey: Buffer.from(pubKey) })
|
||||||
if (!homeCom) {
|
if (!homeCom) {
|
||||||
// check if a homecommunity with a different publicKey still exists
|
// check if a homecommunity with a different publicKey still exists
|
||||||
homeCom = await DbCommunity.findOne({ foreign: false })
|
homeCom = await DbCommunity.findOne({ foreign: false })
|
||||||
}
|
}
|
||||||
if (homeCom) {
|
if (homeCom) {
|
||||||
// simply update the existing entry, but it MUST keep the ID and UUID because of possible relations
|
// simply update the existing entry, but it MUST keep the ID and UUID because of possible relations
|
||||||
homeCom.publicKey = pubKey.toString('hex')
|
homeCom.publicKey = Buffer.from(pubKey, 'hex') // pubKey.toString('hex')
|
||||||
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL
|
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL + '/api/'
|
||||||
homeCom.name = CONFIG.COMMUNITY_NAME
|
homeCom.name = CONFIG.COMMUNITY_NAME
|
||||||
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
|
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
|
||||||
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
||||||
await DbCommunity.save(homeCom)
|
await DbCommunity.save(homeCom)
|
||||||
logger.info(`home-community updated successfully: ${JSON.stringify(homeCom)}`)
|
logger.info(`home-community updated successfully: ${JSON.stringify(homeCom)}`)
|
||||||
} else {
|
} else {
|
||||||
// insert a new homecommunity entry including a new ID and UUID
|
// insert a new homecommunity entry including a new ID and a new but ensured unique UUID
|
||||||
homeCom = new DbCommunity()
|
homeCom = new DbCommunity()
|
||||||
homeCom.foreign = false
|
homeCom.foreign = false
|
||||||
homeCom.publicKey = pubKey.toString('hex')
|
homeCom.publicKey = Buffer.from(pubKey, 'hex') // pubKey.toString('hex')
|
||||||
homeCom.communityUuid = await newCommunityUuid()
|
homeCom.communityUuid = await newCommunityUuid()
|
||||||
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL
|
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL + '/api/'
|
||||||
homeCom.name = CONFIG.COMMUNITY_NAME
|
homeCom.name = CONFIG.COMMUNITY_NAME
|
||||||
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
|
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
|
||||||
homeCom.creationDate = new Date()
|
homeCom.creationDate = new Date()
|
||||||
|
|||||||
@ -22,8 +22,8 @@ const context = {
|
|||||||
|
|
||||||
export const cleanDB = async () => {
|
export const cleanDB = async () => {
|
||||||
// this only works as long we do not have foreign key constraints
|
// this only works as long we do not have foreign key constraints
|
||||||
for (let i = 0; i < entities.length; i++) {
|
for (const entity of entities) {
|
||||||
await resetEntity(entities[i])
|
await resetEntity(entity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user