summaryrefslogtreecommitdiff
path: root/server.test.mjs
blob: 259b9133de0acca9caccfbce883a22a97a814467 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict'

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 params = new URLSearchParams({
    source: 5,
    reqid: '0000000000',
    eagleId: '',
    path: 'eagle/GET/genId',
    params: JSON.stringify({
      mac,
      key,
      version: '00.00.00',
      isDenoise: 1,
      hardVersion: 1
    }),
    sig: '00000000000000000000000000000000'
  })
  const response = await fetch(`${baseUrl}/eagle?${params.toString()}`)
  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', async () => {
  const res = await fetch(`${baseUrl}/gettime`)
  assert.strictEqual(res.status, 200)
  assert.strictEqual(res.headers.get('content-type'), 'application/json')
  const data = await res.json()
  const current = Math.floor(Date.now() / 1000)
  assert.ok(typeof data.time === 'number')
  assert.ok(current - data.time < 3)
})

test('device registration endpoint', async () => {
  const stubMacAddress = '0000000000'
  const stubKey = '00000000000000000000000000000000'
  const params = new URLSearchParams({
    source: 5,
    reqid: '0000000000',
    eagleId: '',
    path: 'eagle/GET/genId',
    params: JSON.stringify({
      mac: stubMacAddress,
      key: stubKey,
      version: '00.00.00',
      isDenoise: 1,
      hardVersion: 1
    }),
    sig: '00000000000000000000000000000000'
  })
  const res = await fetch(`${baseUrl}/eagle?${params.toString()}`)
  assert.strictEqual(res.status, 200)
  assert.strictEqual(res.headers.get('content-type'), 'application/json')
  const data = await res.json()
  assert.strictEqual(data.status, 200)
  assert.ok(typeof data.data?.eagleId === 'number')
})

test('device registration endpoint returns bad request if missing mac address', async () => {
  const stubKey = '00000000000000000000000000000000'
  const params = new URLSearchParams({
    source: 5,
    reqid: '0000000000',
    eagleId: '',
    path: 'eagle/GET/genId',
    params: JSON.stringify({
      key: stubKey,
      version: '00.00.00',
      isDenoise: 1,
      hardVersion: 1
    }),
    sig: '00000000000000000000000000000000'
  })
  const res = await fetch(`${baseUrl}/eagle?${params.toString()}`)
  assert.strictEqual(res.status, 400)
  assert.strictEqual(res.headers.get('content-type'), 'application/json')
})

test('device registration endpoint returns bad request if missing encryption key', async () => {
  const stubMacAddress = '0000000000'
  const params = new URLSearchParams({
    source: 5,
    reqid: '0000000000',
    eagleId: '',
    path: 'eagle/GET/genId',
    params: JSON.stringify({
      mac: stubMacAddress,
      version: '00.00.00',
      isDenoise: 1,
      hardVersion: 1
    }),
    sig: '00000000000000000000000000000000'
  })
  const res = await fetch(`${baseUrl}/eagle?${params.toString()}`)
  assert.strictEqual(res.status, 400)
  assert.strictEqual(res.headers.get('content-type'), 'application/json')
})

test('exchange endpoint', async () => {
  const stubMacAddress = '0000000000'
  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)
})