1
+ import { FastifyReply } from 'fastify/types/reply' ;
1
2
import globalMocks from '../../../config/jest/globalMocks' ;
2
3
import resetMocks from '../../../config/jest/resetMocks' ;
3
4
import { replyWithInternalError , replyWithItemNotFound , replyWithJson } from '../../common/api' ;
4
5
import { AppContext } from '../../common/context' ;
5
- import { getMockedFastifyReply } from '../../helpers/testing/mockObjects' ;
6
+ import { getDefaultDeviceConfig , getMockedFastifyReply } from '../../helpers/testing/mockObjects' ;
6
7
import { PowerStatus } from '../../models/common' ;
8
+ import { DeviceConfig } from '../../models/configuration' ;
9
+ import { validateDeviceIdentifier } from '../common/validators' ;
7
10
import { powerOnForDevice , powerOnForDevicesScheduled } from './powerOn' ;
8
11
9
12
import type { WakeOptions } from 'wake_on_lan' ;
@@ -13,25 +16,30 @@ const { wakeonlanMock } = globalMocks;
13
16
jest . mock ( '../../common/api' ) ;
14
17
jest . mock ( '../../common/logger' , ( ) => ( {
15
18
coreLogger : {
19
+ error : jest . fn ( ) ,
16
20
info : jest . fn ( ) ,
17
21
} ,
18
22
} ) ) ;
23
+ jest . mock ( '../common/validators' ) ;
19
24
20
25
const replyWithJsonMock = replyWithJson as jest . Mock ;
21
26
const replyWithInternalErrorMock = replyWithInternalError as jest . Mock ;
22
27
const replyWithItemNotFoundMock = replyWithItemNotFound as jest . Mock ;
28
+ const validateDeviceIdentifierMock = validateDeviceIdentifier as jest . Mock < DeviceConfig | undefined , [ string , FastifyReply ?] > ;
23
29
24
30
beforeEach ( ( ) => {
25
31
wakeonlanMock . wake . mockImplementation ( ( _a , _o , cb ) => {
26
32
cb ( ) ;
27
- } ) ;
33
+ } ) ;
34
+ validateDeviceIdentifierMock . mockReturnValue ( getDefaultDeviceConfig ( ) ) ;
28
35
} ) ;
29
36
30
37
afterEach ( ( ) => {
31
38
resetMocks ( ) ;
32
39
replyWithJsonMock . mockReset ( ) ;
33
40
replyWithInternalErrorMock . mockReset ( ) ;
34
41
replyWithItemNotFoundMock . mockReset ( ) ;
42
+ validateDeviceIdentifierMock . mockReset ( ) ;
35
43
} ) ;
36
44
37
45
describe ( 'powerOn service' , ( ) => {
@@ -86,12 +94,16 @@ describe('powerOn service', () => {
86
94
} ) ;
87
95
88
96
it ( 'should return 404 when device is not found' , async ( ) => {
89
- // given-when
97
+ // given
98
+ validateDeviceIdentifierMock . mockReturnValue ( undefined ) ;
99
+
100
+ // when
90
101
await powerOnForDevice ( 'foo' , defaultReply ) ;
91
102
92
103
// then
93
104
expect ( wakeonlanMock . wake ) . not . toHaveBeenCalled ( ) ;
94
- expect ( replyWithItemNotFoundMock ) . toHaveBeenCalledWith ( defaultReply , 'deviceId' , 'foo' ) ;
105
+ expect ( replyWithJsonMock ) . not . toHaveBeenCalled ( ) ;
106
+ expect ( replyWithItemNotFoundMock ) . not . toHaveBeenCalled ( ) ;
95
107
} ) ;
96
108
97
109
it ( 'should not attempt to wake device nor update diags context and return 204 when already powered ON' , async ( ) => {
@@ -123,5 +135,16 @@ describe('powerOn service', () => {
123
135
expect ( lastStartAttempt . reason ) . toBe ( 'scheduled' ) ;
124
136
expect ( wakeonlanMock . wake ) . toHaveBeenCalledTimes ( 1 ) ;
125
137
} ) ;
138
+
139
+ it ( 'should handle invalid device identifier' , async ( ) => {
140
+ // given
141
+ validateDeviceIdentifierMock . mockReturnValue ( undefined ) ;
142
+
143
+ // when
144
+ await powerOnForDevicesScheduled ( [ '0' ] ) ;
145
+
146
+ // then
147
+ expect ( wakeonlanMock . wake ) . not . toHaveBeenCalled ( ) ;
148
+ } ) ;
126
149
} ) ;
127
150
} ) ;
0 commit comments