|
| 1 | +import request from 'supertest'; |
| 2 | + |
| 3 | +import app from '../app'; |
| 4 | + |
| 5 | +import { User } from '../models/User'; |
| 6 | +import { UserRole } from '../models/UserRole'; |
| 7 | +import { HomeAssistantService } from '../services/HomeAssistantService'; |
| 8 | + |
| 9 | +jest.mock('../models/User', () => { |
| 10 | + return { |
| 11 | + User: { |
| 12 | + findOne: jest.fn(), |
| 13 | + }, |
| 14 | + }; |
| 15 | +}); |
| 16 | + |
| 17 | +jest.mock('../services/HomeAssistantService', () => { |
| 18 | + return { |
| 19 | + HomeAssistantService: { |
| 20 | + getMobileAppDevices: jest.fn(), |
| 21 | + }, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('GET /homeassistant/mobile-app-devices', () => { |
| 30 | + it('should return 400 if the x-remote-user-id header is missing', async () => { |
| 31 | + const response = await request(app) |
| 32 | + .get('/homeassistant/mobile-app-devices') |
| 33 | + .set('x-remote-user-name', 'johndoe') |
| 34 | + .set('x-remote-user-display-name', 'John Doe'); |
| 35 | + expect(response.body).toStrictEqual({ |
| 36 | + message: 'Missing required header: x-remote-user-id.', |
| 37 | + }); |
| 38 | + expect(response.status).toBe(400); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return 400 if the x-remote-user-id is not a valid user id', async () => { |
| 42 | + const response = await request(app) |
| 43 | + .get('/homeassistant/mobile-app-devices') |
| 44 | + .set('x-remote-user-id', 'bad home assistant id') |
| 45 | + .set('x-remote-user-name', 'johndoe') |
| 46 | + .set('x-remote-user-display-name', 'John Doe'); |
| 47 | + expect(response.body).toStrictEqual({ |
| 48 | + message: 'Invalid home assistant user id in x-remote-user-id.', |
| 49 | + }); |
| 50 | + expect(response.status).toBe(400); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return 400 if the x-remote-user-name header is missing', async () => { |
| 54 | + const response = await request(app) |
| 55 | + .get('/homeassistant/mobile-app-devices') |
| 56 | + .set('x-remote-user-id', 'c355d2aaeee44e4e84ff8394fa4794a9') |
| 57 | + .set('x-remote-user-display-name', 'John Doe'); |
| 58 | + expect(response.body).toStrictEqual({ |
| 59 | + message: 'Missing required header: x-remote-user-name.', |
| 60 | + }); |
| 61 | + expect(response.status).toBe(400); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return 400 if the x-remote-user-display-name header is missing', async () => { |
| 65 | + const response = await request(app) |
| 66 | + .get('/homeassistant/mobile-app-devices') |
| 67 | + .set('x-remote-user-id', 'c355d2aaeee44e4e84ff8394fa4794a9') |
| 68 | + .set('x-remote-user-name', 'johndoe'); |
| 69 | + expect(response.body).toStrictEqual({ |
| 70 | + message: 'Missing required header: x-remote-user-display-name.', |
| 71 | + }); |
| 72 | + expect(response.status).toBe(400); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return 401 if user is not in the database', async () => { |
| 76 | + (User.findOne as jest.Mock).mockResolvedValue(null); |
| 77 | + |
| 78 | + const response = await request(app) |
| 79 | + .get('/homeassistant/mobile-app-devices') |
| 80 | + .set('x-remote-user-id', 'c355d2aaeee44e4e84ff8394fa4794a9') |
| 81 | + .set('x-remote-user-name', 'johndoe') |
| 82 | + .set('x-remote-user-display-name', 'John Doe'); |
| 83 | + expect(response.body).toStrictEqual({ message: 'User not registered.' }); |
| 84 | + expect(response.status).toBe(401); |
| 85 | + expect(User.findOne).toHaveBeenCalledTimes(1); |
| 86 | + expect(User.findOne).toHaveBeenCalledWith({ |
| 87 | + where: { homeAssistantUserId: 'c355d2aaeee44e4e84ff8394fa4794a9' }, |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return the list of the names of the available devices', async () => { |
| 92 | + (User.findOne as jest.Mock).mockResolvedValue({ |
| 93 | + id: 1, |
| 94 | + homeAssistantUserId: 'c355d2aaeee44e4e84ff8394fa4794a9', |
| 95 | + role: UserRole.HELPED, |
| 96 | + mobileAppDevice: 'Redmi Note 8T', |
| 97 | + }); |
| 98 | + |
| 99 | + (HomeAssistantService.getMobileAppDevices as jest.Mock).mockResolvedValue([ |
| 100 | + { name: 'Redmi Note 8T' }, |
| 101 | + { name: 'Redmi Note 9' }, |
| 102 | + ]); |
| 103 | + |
| 104 | + const response = await request(app) |
| 105 | + .get('/homeassistant/mobile-app-devices') |
| 106 | + .set('x-remote-user-id', 'c355d2aaeee44e4e84ff8394fa4794a9') |
| 107 | + .set('x-remote-user-name', 'johndoe') |
| 108 | + .set('x-remote-user-display-name', 'John Doe'); |
| 109 | + expect(response.body).toStrictEqual(['Redmi Note 8T', 'Redmi Note 9']); |
| 110 | + expect(response.status).toBe(200); |
| 111 | + expect(User.findOne).toHaveBeenCalledTimes(1); |
| 112 | + expect(User.findOne).toHaveBeenCalledWith({ |
| 113 | + where: { homeAssistantUserId: 'c355d2aaeee44e4e84ff8394fa4794a9' }, |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments