Skip to content

Commit

Permalink
Merge b2572b3 into 81c9b6b
Browse files Browse the repository at this point in the history
  • Loading branch information
joyja committed Dec 1, 2020
2 parents 81c9b6b + b2572b3 commit 9727f2d
Show file tree
Hide file tree
Showing 20 changed files with 4,697 additions and 810 deletions.
4,387 changes: 3,585 additions & 802 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "jest --watchAll",
"test:ci": "jest",
"test:coverage": "jest --coverage",
"test:detect-open-handles": "jest --detectOpenHandles",
"fix": "eslint --ext .js,.vue --fix --ignore-path .gitignore .",
"coveralls": "jest --coverage --coverageReporters=text-lcov | coveralls"
},
Expand All @@ -29,7 +30,7 @@
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"graphql-request": "^3.1.0",
"jest": "^26.0.1",
"jest": "^26.6.3",
"jest-watch-select-projects": "^2.0.0",
"jest-watch-typeahead": "^0.6.0",
"nodemon": "^2.0.3",
Expand All @@ -50,11 +51,13 @@
"make-promises-safe": "^5.1.0",
"modbus-serial": "^8.0.1",
"mqtt": "^4.2.1",
"node-opcua": "^2.18.0",
"sparkplug-client": "^3.2.2",
"sqlite3": "^4.2.0",
"subscriptions-transport-ws": "^0.9.18",
"task-easy": "^1.0.1",
"tentacle-sparkplug-client": "0.0.6",
"treeify": "^1.1.0",
"uuid": "^8.3.0",
"winston": "^3.3.3"
}
Expand Down
163 changes: 162 additions & 1 deletion src/__tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,55 @@ const { start, stop } = require('../server')
const _ = require('lodash')
const { Headers } = require('cross-fetch')

const opcuaNodes = {
nodeId: '1',
browseName: 'testnode',
dataValue: {
value: {
value: 'something',
dataType: 'aDatatype',
},
},
children: [],
}

jest.mock(`node-opcua`, () => {
return {
OPCUAClient: {
create: () => {
return {
connect: jest.fn(async () => {}),
disconnect: jest.fn(),
on: jest.fn(),
createSession: jest.fn(() => {
return {
readVariableValue: jest.fn(),
writeSingleNode: jest.fn(),
close: jest.fn(),
}
}),
}
},
},
MessageSecurityMode: { None: null },
SecurityPolicy: { None: null },
DataType: {
Boolean: 1,
Float: 2,
Int32: 3,
String: 4,
},
NodeCrawler: function (session) {
return {
read: jest.fn((nodeId, callback) => {
callback(null, opcuaNodes)
}),
on: jest.fn(),
}
},
}
})

global.Headers = global.Headers || Headers

const host = 'http://localhost:4000'
Expand Down Expand Up @@ -411,11 +460,102 @@ test('updateEthernetIP without authorization headers returns error', async () =>
expect(Controller.prototype.connect).toBeCalledTimes(0)
expect(Controller.prototype.destroy).toBeCalledTimes(0)
})
let opcua = undefined
let opcuaFields = undefined
test('create opcua with the proper headers and fields returns valid results', async () => {
opcuaFields = {
name: 'aOpcua',
description: 'A Opcua',
host: 'localhost',
port: 4840,
retryRate: 10000,
}
const { createOpcua } = await client
.request(mutation.createOpcua, opcuaFields)
.catch((error) => {
throw error
})
expect(createOpcua).toEqual({
id: '3',
..._.pick(opcuaFields, ['name', 'description']),
config: {
id: '1',
..._.omit(opcuaFields, ['name', 'description', 'port']),
port: `${opcuaFields.port}`,
sources: [],
nodes: {
id: opcuaNodes.nodeId,
name: opcuaNodes.browseName,
value: JSON.stringify(opcuaNodes.dataValue.value.value),
datatype: opcuaNodes.dataValue.value.dataType,
children: opcuaNodes.children,
},
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin',
},
createdOn: expect.any(String),
})
opcua = createOpcua
})
test('create opcua without authorization headers returns error', async () => {
const result = await request(host, mutation.createOpcua, opcuaFields).catch(
(e) => e
)
expect(result.message).toContain(`You are not authorized.`)
})
test('updateOpcua updates the opcua values', async () => {
opcuaFields = {
id: opcua.id,
name: 'anotherOpcua',
description: 'Another Opcua',
host: '192.168.1.1',
port: 4841,
retryRate: 123456,
}
const { updateOpcua } = await client
.request(mutation.updateOpcua, opcuaFields)
.catch((error) => {
throw error
})
expect(updateOpcua).toEqual({
id: opcua.id,
..._.pick(opcuaFields, ['name', 'description']),
config: {
id: '1',
..._.omit(opcuaFields, ['id', 'name', 'description', 'port']),
port: `${opcuaFields.port}`,
nodes: {
id: opcuaNodes.nodeId,
name: opcuaNodes.browseName,
value: JSON.stringify(opcuaNodes.dataValue.value.value),
datatype: opcuaNodes.dataValue.value.dataType,
children: opcuaNodes.children,
},
sources: [],
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin',
},
createdOn: opcua.createdOn,
})
opcua = updateOpcua
})
test('updateOpcua without authorization headers returns error', async () => {
const result = await request(host, mutation.updateOpcua, {
id: opcua.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
})
test('device query returns a list of devices', async () => {
const { devices } = await client.request(query.devices).catch((error) => {
throw error
})
expect(devices).toEqual([modbus, ethernetip])
expect(devices).toEqual([modbus, ethernetip, opcua])
})
test('device query without authorization headers returns error', async () => {
const result = await request(host, query.devices).catch((e) => e)
Expand Down Expand Up @@ -665,6 +805,27 @@ test('delete ethernetip with valid arguments and credentials returns deleted dev
})
expect(isStillThere).toBe(false)
})
test('delete opcua without authorization headers returns error', async () => {
const result = await request(host, mutation.deleteOpcua, {
id: opcua.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
})
test('delete opcua with valid arguments and credentials returns deleted device', async () => {
const { deleteOpcua } = await client
.request(mutation.deleteOpcua, { id: opcua.id })
.catch((error) => {
throw error
})
expect(deleteOpcua.id).toEqual(opcua.id)
const { devices } = await client.request(query.devices).catch((error) => {
error
})
const isStillThere = devices.some((device) => {
device.id === deleteOpcua.id
})
expect(isStillThere).toBe(false)
})
test('delete tag without authorization headers returns error', async () => {
const result = await request(host, mutation.deleteTag, { id: tag.id }).catch(
(e) => e
Expand Down
Loading

0 comments on commit 9727f2d

Please sign in to comment.