1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
'use strict'
import * as http from 'node:http'
import assert from 'node:assert'
import test, { after, before } from 'node:test'
import { spawn } from 'node:child_process'
const hostname = '127.0.0.1'
const port = 8090
const baseUrl = `http://${hostname}:${port}`
let server = null
const createDevice = async (mac, key) => {
const response = await fetch(`${baseUrl}/eagle?mac=${mac}&key=${key}`)
const data = await response.json()
return data.data.eagleId
}
before(() => new Promise((resolve, reject) => {
const nodeBinary = process.argv[0]
server = spawn(nodeBinary, ['server.mjs'], {
env: {
HOSTNAME: hostname,
PORT: port
}
})
server.stdout.on('data', (data) => {
console.log(`server stdout: ${data}`)
if (data.toString().includes('Listening on')) {
resolve()
}
})
server.stderr.on('data', (data) => {
console.log(`server stderr: ${data}`)
})
server.on('error', (err) => {
reject(new Error(`Server failed to start: ${err}`))
})
}))
after(() => {
if (server) {
server.kill()
server = null
}
})
test('current time endpoint', (t, done) => {
http.get(`${baseUrl}/gettime`, (res) => {
assert.strictEqual(res.statusCode, 200)
assert.strictEqual(res.headers['content-type'], 'application/json')
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
const decoded = JSON.parse(data)
const current = Math.floor(Date.now() / 1000)
assert.ok('time' in decoded)
assert.ok(typeof decoded.time === 'number')
assert.ok(current - decoded.time < 3)
done()
})
}).on('error', (err) => {
assert.fail(`HTTP request failed: ${err.message}`)
})
})
test('device registration endpoint', (t, done) => {
const stubMacAddress = '00:00:00:00:00'
const stubKey = '00000000000000000000000000000000'
http.get(`${baseUrl}/eagle?mac=${stubMacAddress}&key=${stubKey}`, (res) => {
assert.strictEqual(res.statusCode, 200)
assert.strictEqual(res.headers['content-type'], 'application/json')
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('end', () => {
const decoded = JSON.parse(data)
assert.strictEqual(decoded.status, 200)
assert.ok(typeof decoded.data?.eagleId === 'number')
done()
})
}).on('error', (err) => {
assert.fail(`HTTP request failed: ${err.message}`)
})
})
test('device registration endpoint returns bad request if missing mac address', (t, done) => {
const stubKey = '00000000000000000000000000000000'
http.get(`${baseUrl}/eagle?key=${stubKey}`, (res) => {
assert.strictEqual(res.statusCode, 400)
assert.strictEqual(res.headers['content-type'], 'application/json')
done()
}).on('error', (err) => {
assert.fail(`HTTP request failed: ${err.message}`)
})
})
test('device registration endpoint returns bad request if missing encryption key', (t, done) => {
const stubMacAddress = '00:00:00:00:00'
http.get(`${baseUrl}/eagle?mac=${stubMacAddress}`, (res) => {
assert.strictEqual(res.statusCode, 400)
assert.strictEqual(res.headers['content-type'], 'application/json')
done()
}).on('error', (err) => {
assert.fail(`HTTP request failed: ${err.message}`)
})
})
test('exchange endpoint', async () => {
const stubMacAddress = '00:00:00:00:00'
const stubKey = '00000000000000000000000000000000'
const deviceId = await createDevice(stubMacAddress, stubKey)
const res = await fetch(`${baseUrl}/exchange?device=${deviceId}`)
assert.strictEqual(res.status, 200)
const data = await res.json()
assert.strictEqual(data.key, stubKey)
})
test('exchange endpoint returns unprocessable content when device id is missing', async () => {
const res = await fetch(`${baseUrl}/exchange`)
assert.strictEqual(res.status, 422)
})
test('exchange endpoint returns not found when device does not exist', async () => {
const res = await fetch(`${baseUrl}/exchange?device=9999`)
assert.strictEqual(res.status, 404)
})
|