Skip to content

Commit

Permalink
Merge 1d6117f into e2c0bf4
Browse files Browse the repository at this point in the history
  • Loading branch information
joyja committed May 22, 2020
2 parents e2c0bf4 + 1d6117f commit a7dcb75
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 77 deletions.
96 changes: 48 additions & 48 deletions src/__tests__/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const mockSparkplug = {
publishDeviceData: jest.fn(),
publishDeviceDeath: jest.fn(),
stop: jest.fn(),
subscribePrimaryHost: jest.fn()
subscribePrimaryHost: jest.fn(),
}

beforeAll(async () => {
Expand Down Expand Up @@ -64,20 +64,20 @@ test('login with default username/password returns appropriate results.', async
}
}`
const {
login: { user, token }
login: { user, token },
} = await request(host, mutation).catch((error) => {
throw error
})
expect(user).toEqual({
id: expect.any(String),
username: 'admin'
username: 'admin',
})
expect(Number.isInteger(parseInt(user.id))).toBe(true)
expect(token).toEqual(expect.any(String))
client = new GraphQLClient(host, {
headers: {
Authorization: `Bearer ${token}`
}
Authorization: `Bearer ${token}`,
},
})
})
test('user query returns currently logged in user', async () => {
Expand All @@ -92,7 +92,7 @@ test('user query returns currently logged in user', async () => {
})
expect(user).toEqual({
id: '1',
username: 'admin'
username: 'admin',
})
})
let scanClass = undefined
Expand All @@ -112,7 +112,7 @@ test('create scan class with the proper headers and fields returns valid results
id: expect.any(String),
name: 'default',
description: 'default scan class',
rate: 1000
rate: 1000,
})
scanClass = createScanClass
})
Expand Down Expand Up @@ -171,7 +171,7 @@ test('create tag with the proper headers and fields returns valid results', asyn
scanClassId: scanClass.id,
max: 200,
min: 0,
units: 'thingies'
units: 'thingies',
}
const { createTag } = await client
.request(mutation.createTag, tagFields)
Expand All @@ -184,9 +184,9 @@ test('create tag with the proper headers and fields returns valid results', asyn
scanClass: scanClass,
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: expect.any(String)
createdOn: expect.any(String),
})
tag = createTag
})
Expand Down Expand Up @@ -239,7 +239,7 @@ test('create modbus with the proper headers and fields returns valid results', a
reverseWords: true,
zeroBased: true,
timeout: 1000,
retryRate: 4000
retryRate: 4000,
}
const { createModbus } = await client
.request(mutation.createModbus, modbusFields)
Expand All @@ -256,13 +256,13 @@ test('create modbus with the proper headers and fields returns valid results', a
..._.omit(modbusFields, ['name', 'description', 'port']),
port: `${modbusFields.port}`,
sources: [],
status: 'connected'
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: expect.any(String)
createdOn: expect.any(String),
})
modbus = createModbus
})
Expand Down Expand Up @@ -301,19 +301,19 @@ test('updateModbus updates the modbus values', async () => {
..._.omit(modbusFields, ['name', 'description', 'port']),
port: `${modbusFields.port}`,
sources: [],
status: 'connected'
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: modbus.createdOn
createdOn: modbus.createdOn,
})
modbus = updateModbus
})
test('updateModbus without authorization headers returns error', async () => {
const result = await request(host, mutation.updateModbus, {
id: modbus.id
id: modbus.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
expect(ModbusRTU.prototype.connectTCP).toBeCalledTimes(0)
Expand All @@ -326,7 +326,7 @@ test('create ethernetip with the proper headers and fields returns valid results
name: 'aEthernetIP',
description: 'A EthernetIP',
host: 'localhost',
slot: 0
slot: 0,
}
const { createEthernetIP } = await client
.request(mutation.createEthernetIP, ethernetipFields)
Expand All @@ -343,13 +343,13 @@ test('create ethernetip with the proper headers and fields returns valid results
..._.omit(ethernetipFields, ['name', 'description', 'slot']),
slot: `${ethernetipFields.slot}`,
sources: [],
status: 'connected'
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: expect.any(String)
createdOn: expect.any(String),
})
ethernetip = createEthernetIP
})
Expand All @@ -369,7 +369,7 @@ test('updateEthernetIP updates the ethernetip values', async () => {
name: 'anotherEthernetIP',
description: 'Another EthernetIP',
host: '192.168.1.1',
slot: 1
slot: 1,
}
const { updateEthernetIP } = await client
.request(mutation.updateEthernetIP, ethernetipFields)
Expand All @@ -386,19 +386,19 @@ test('updateEthernetIP updates the ethernetip values', async () => {
..._.omit(ethernetipFields, ['id', 'name', 'description', 'slot']),
slot: `${ethernetipFields.slot}`,
sources: [],
status: 'connected'
status: 'connected',
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: ethernetip.createdOn
createdOn: ethernetip.createdOn,
})
ethernetip = updateEthernetIP
})
test('updateEthernetIP without authorization headers returns error', async () => {
const result = await request(host, mutation.updateEthernetIP, {
id: ethernetip.id
id: ethernetip.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
expect(Controller.prototype.connect).toBeCalledTimes(0)
Expand Down Expand Up @@ -430,7 +430,7 @@ test('create mqtt with the proper headers and fields returns valid results', asy
rate: 1000,
encrypt: true,
recordLimit: 50,
primaryHosts: ['aPrimaryHost']
primaryHosts: ['aPrimaryHost'],
}
const { createMqtt } = await client
.request(mutation.createMqtt, mqttFields)
Expand All @@ -454,30 +454,30 @@ test('create mqtt with the proper headers and fields returns valid results', asy
'description',
'port',
'devices',
'primaryHosts'
'primaryHosts',
]),
primaryHosts: mqttFields.primaryHosts.map((host) => {
return {
id: expect.any(String),
name: host,
status: 'UNKNOWN',
recordCount: expect.any(Number)
recordCount: expect.any(Number),
}
}),
port: `${mqttFields.port}`,
sources: [
{
device: {
id: modbus.id
}
}
]
id: modbus.id,
},
},
],
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: expect.any(String)
createdOn: expect.any(String),
})
mqtt = createMqtt
})
Expand Down Expand Up @@ -507,7 +507,7 @@ test('update mqtt with the proper headers and fields returns valid results', asy
password: 'anotherPassword',
rate: 2000,
encrypt: false,
recordLimit: 100
recordLimit: 100,
}
const { updateMqtt } = await client
.request(mutation.updateMqtt, mqttFields)
Expand All @@ -531,23 +531,23 @@ test('update mqtt with the proper headers and fields returns valid results', asy
'description',
'port',
'devices',
'primaryHosts'
'primaryHosts',
]),
primaryHosts: mqtt.config.primaryHosts,
port: `${mqttFields.port}`,
sources: [
{
device: {
id: modbus.id
}
}
]
id: modbus.id,
},
},
],
},
createdBy: {
id: '1',
username: 'admin'
username: 'admin',
},
createdOn: mqtt.createdOn
createdOn: mqtt.createdOn,
})
mqtt = updateMqtt
})
Expand Down Expand Up @@ -586,7 +586,7 @@ test('service query without authorization headers returns error', async () => {
})
test('delete service without authorization headers returns error', async () => {
const result = await request(host, mutation.deleteMqtt, {
id: mqtt.id
id: mqtt.id,
}).catch((e) => e)
expect(result.message).toContain('You are not authorized.')
})
Expand Down Expand Up @@ -614,7 +614,7 @@ test('delete mqtt with valid arguments and credentials returns deleted service',
})
test('delete modbus without authorization headers returns error', async () => {
const result = await request(host, mutation.deleteModbus, {
id: modbus.id
id: modbus.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
})
Expand All @@ -637,7 +637,7 @@ test('delete modbus with valid arguments and credentials returns deleted device'
})
test('delete ethernetip without authorization headers returns error', async () => {
const result = await request(host, mutation.deleteEthernetIP, {
id: ethernetip.id
id: ethernetip.id,
}).catch((e) => e)
expect(result.message).toContain(`You are not authorized.`)
})
Expand All @@ -648,7 +648,7 @@ test('delete ethernetip with valid arguments and credentials returns deleted dev
throw error
})
expect(Controller.prototype.connect).toBeCalledTimes(0)
expect(Controller.prototype.destroy).toBeCalledTimes(1)
expect(Controller.prototype.destroy).toBeCalledTimes(0)
expect(deleteEthernetIP.id).toEqual(ethernetip.id)
const { devices } = await client.request(query.devices).catch((error) => {
error
Expand Down
13 changes: 10 additions & 3 deletions src/device/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ class Device extends Model {
description,
type,
createdBy,
createdOn
createdOn,
}
return super.create(fields)
}
static async delete(selector) {
for (device of Device.instances) {
await instance.config.disconnect()
}
const deleted = await super.delete(selector)
await ModbusSource.getAll()
await Modbus.getAll()
await EthernetIPSource.getAll()
await EthernetIP.getAll()
for (device of Device.instances) {
await instance.config.connect()
}
return deleted
}
async init() {
Expand Down Expand Up @@ -69,11 +76,11 @@ Device.fields = [
{ colName: 'description', colType: 'TEXT' },
{ colName: 'type', colType: 'TEXT' },
{ colName: 'createdBy', colRef: 'user', onDelete: 'SET NULL' },
{ colName: 'createdOn', colType: 'INTEGER' }
{ colName: 'createdOn', colType: 'INTEGER' },
]
Device.instances = []
Device.initialized = false

module.exports = {
Device
Device,
}
Loading

0 comments on commit a7dcb75

Please sign in to comment.