bootstrap and Hello World Test

This commit is contained in:
Einhornimmond 2023-06-08 18:06:33 +02:00 committed by einhorn_b
parent 0e6ae70c8d
commit c666b60e16
8 changed files with 756 additions and 17 deletions

View File

@ -21,6 +21,11 @@ KLICKTIPP_PASSWORD=secret321
KLICKTIPP_APIKEY_DE=SomeFakeKeyDE
KLICKTIPP_APIKEY_EN=SomeFakeKeyEN
# IOTA
IOTA=false
IOTA_API_URL=https://chrysalis-nodes.iota.org
IOTA_COMMUNITY_ALIAS=GRADIDO: TestHelloWelt1
# Community
COMMUNITY_NAME=Gradido Entwicklung
COMMUNITY_URL=http://localhost/

View File

@ -22,6 +22,11 @@ KLICKTIPP_PASSWORD=$KLICKTIPP_PASSWORD
KLICKTIPP_APIKEY_DE=$KLICKTIPP_APIKEY_DE
KLICKTIPP_APIKEY_EN=$KLICKTIPP_APIKEY_EN
# IOTA
IOTA=$IOTA
IOTA_API_URL=https://chrysalis-nodes.iota.org
IOTA_COMMUNITY_ALIAS=GRADIDO: TestHelloWelt1
# Community
COMMUNITY_NAME=$COMMUNITY_NAME
COMMUNITY_URL=$COMMUNITY_URL

View File

@ -19,6 +19,7 @@
"locales": "scripts/sort.sh"
},
"dependencies": {
"@iota/client": "^2",
"apollo-server-express": "^2.25.2",
"await-semaphore": "^0.1.3",
"axios": "^0.21.1",

View File

@ -0,0 +1,59 @@
import { IndexationPayload } from '@iota/client/lib/types'
import { sendDataMessage, getMessage, getAllMessages } from '@/apis/IotaConnector'
import { CONFIG } from '@/config'
jest.mock('@/config', () => ({
CONFIG: {
IOTA: true,
IOTA_COMMUNITY_ALIAS: 'GRADIDO: TestHelloWelt1',
IOTA_API_URL: 'https://chrysalis-nodes.iota.org',
},
}))
describe('apis/IotaConnector/enabled', () => {
describe('Hello World', () => {
const now = new Date()
let messageId = ''
const messageString = 'Hello World - ' + now.toString()
const messageHexString = Buffer.from(messageString, 'utf8').toString('hex')
const indexHexString = Buffer.from(CONFIG.IOTA_COMMUNITY_ALIAS, 'utf8').toString('hex')
it('send hello world message to iota tangle', async () => {
const iotaMessage = await sendDataMessage(messageString)
expect(iotaMessage).not.toBeNull()
if (iotaMessage) {
const indexationPayload = iotaMessage.message.payload as IndexationPayload
expect(indexationPayload.data).toBe(messageHexString)
expect(indexationPayload.index).toBe(indexHexString)
messageId = iotaMessage.messageId
}
})
it('receive hello world message from iota tangle by message id', async () => {
const iotaMessage = await getMessage(messageId)
expect(iotaMessage).not.toBeNull()
if (iotaMessage) {
const indexationPayload = iotaMessage.message.payload as IndexationPayload
expect(indexationPayload.data).toBe(messageHexString)
expect(indexationPayload.index).toBe(indexHexString)
}
})
it('receive hello world message from iota tangle by searching throw all', async () => {
const iotaMessages = await getAllMessages()
expect(iotaMessages).not.toBeNull()
if (iotaMessages) {
let foundMessage = false
for (const messageId of iotaMessages) {
const iotaMessage = await getMessage(messageId)
if (iotaMessage) {
const indexationPayload = iotaMessage.message.payload as IndexationPayload
if (indexationPayload.data.toString() === messageHexString) {
foundMessage = true
break
}
}
}
expect(foundMessage).toBeTruthy()
}
})
})
})

View File

@ -0,0 +1,71 @@
import { ClientBuilder } from '@iota/client'
import { MessageWrapper } from '@iota/client/lib/types'
import { CONFIG } from '@/config'
import { backendLogger as logger } from '@/server/logger'
const IotaClient = CONFIG?.IOTA_API_URL
? new ClientBuilder().node(CONFIG.IOTA_API_URL).build()
: null
/**
* send data message onto iota tangle
* use CONFIG.IOTA_COMMUNITY_ALIAS for index
* @param {string} message - the message as utf based string, will be converted to hex automatically from @iota/client
* @return {MessageWrapper|null} the iota message as json object with transaction data from iota or null if iota is disabled in config
*/
export const sendDataMessage = (message: string): Promise<MessageWrapper> | null => {
if (!CONFIG.IOTA || !IotaClient) {
logger.info(`Iota are disabled via config...`)
return null
}
return IotaClient.message().index(CONFIG.IOTA_COMMUNITY_ALIAS).data(message).submit()
}
/**
* receive message for known message id from iota tangle
* @param {string} messageId - as hex string
* @return {MessageWrapper|null} the iota message as json object with transaction data from iota or null if iota is disabled in config
*/
export const getMessage = (messageId: string): Promise<MessageWrapper> | null => {
if (!CONFIG.IOTA || !IotaClient) {
logger.info(`Iota are disabled via config...`)
return null
}
return IotaClient.getMessage().data(messageId)
}
/**
* receive all message ids belonging to our topic from iota tangle
* @returns array of messageIds
*/
export const getAllMessages = (): Promise<string[]> | null => {
if (!CONFIG.IOTA || !IotaClient) {
logger.info(`Iota are disabled via config...`)
return null
}
return IotaClient.getMessage().index(CONFIG.IOTA_COMMUNITY_ALIAS)
}
/**
* example for message:
```json
{
message: {
networkId: '1454675179895816119',
parentMessageIds: [
'5f30efecca59fdfef7c103e85ef691b2b1dc474e9eae9056888a6d58605083e7',
'77cef2fb405daedcd7469e009bb87a6d9a4840e618cdb599cd21a30a9fec88dc',
'7d2cfb39f40585ba568a29ad7e85c1478b2584496eb736d4001ac344f6a6cacf',
'c66da602874220dfa26925f6be540d37c0084d37cd04726fcc5be9d80b36f850'
],
payload: {
type: 2,
index: '4752414449444f3a205465737448656c6c6f57656c7431',
data: '48656c6c6f20576f726c64202d20546875204a756e20303820323032332031343a35393a343520474d542b3030303020284b6f6f7264696e69657274652057656c747a65697429'
},
nonce: '13835058055282465157'
},
messageId: '5498130bc3918e1a7143969ce05805502417e3e1bd596d3c44d6a0adeea22710'
}
```
*/

View File

@ -0,0 +1,19 @@
import { sendDataMessage, getMessage, getAllMessages } from '@/apis/IotaConnector'
jest.mock('@/config', () => ({
CONFIG: { IOTA: false },
}))
describe('apis/IotaConnector/disabled', () => {
describe('disabled', () => {
it('sendDataMessage return null if iota is disabled', () => {
expect(sendDataMessage('empty')).toBeNull()
})
it('getMessage return null if iota is disabled', () => {
expect(getMessage('empty')).toBeNull()
})
it('getAllMessages return null if iota is disabled', () => {
expect(getAllMessages()).toBeNull()
})
})
})

View File

@ -19,7 +19,7 @@ const constants = {
LOG_LEVEL: process.env.LOG_LEVEL ?? 'info',
CONFIG_VERSION: {
DEFAULT: 'DEFAULT',
EXPECTED: 'v15.2023-02-07',
EXPECTED: 'v16.2023-06-08',
CURRENT: '',
},
}
@ -51,6 +51,12 @@ const klicktipp = {
KLICKTIPP_APIKEY_EN: process.env.KLICKTIPP_APIKEY_EN ?? 'SomeFakeKeyEN',
}
const iota = {
IOTA: process.env.IOTA === 'true' || false,
IOTA_API_URL: process.env.IOTA_API_URL ?? 'https://chrysalis-nodes.iota.org',
IOTA_COMMUNITY_ALIAS: process.env.IOTA_COMMUNITY_ALIAS ?? 'GRADIDO: TestHelloWelt1',
}
const community = {
COMMUNITY_NAME: process.env.COMMUNITY_NAME ?? 'Gradido Entwicklung',
COMMUNITY_URL: process.env.COMMUNITY_URL ?? 'http://localhost/',
@ -126,6 +132,7 @@ export const CONFIG = {
...server,
...database,
...klicktipp,
...iota,
...community,
...email,
...loginServer,

File diff suppressed because it is too large Load Diff