|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { ForbiddenException, NotFoundException } from '@nestjs/common'; |
| 3 | +import { db } from '@db'; |
| 4 | +import { FleetService } from '../lib/fleet.service'; |
| 5 | +import { DevicesService } from './devices.service'; |
| 6 | + |
| 7 | +jest.mock('@db', () => ({ |
| 8 | + db: { |
| 9 | + organization: { findUnique: jest.fn() }, |
| 10 | + member: { findFirst: jest.fn() }, |
| 11 | + device: { deleteMany: jest.fn() }, |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +describe('DevicesService', () => { |
| 16 | + let service: DevicesService; |
| 17 | + |
| 18 | + const mockFleetService = { |
| 19 | + getHostsByLabel: jest.fn(), |
| 20 | + getMultipleHosts: jest.fn(), |
| 21 | + }; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + const module: TestingModule = await Test.createTestingModule({ |
| 25 | + providers: [ |
| 26 | + DevicesService, |
| 27 | + { provide: FleetService, useValue: mockFleetService }, |
| 28 | + ], |
| 29 | + }).compile(); |
| 30 | + |
| 31 | + service = module.get<DevicesService>(DevicesService); |
| 32 | + jest.clearAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('removeDeviceById', () => { |
| 36 | + it('throws when organization does not exist', async () => { |
| 37 | + (db.organization.findUnique as jest.Mock).mockResolvedValue(null); |
| 38 | + |
| 39 | + await expect( |
| 40 | + service.removeDeviceById({ |
| 41 | + organizationId: 'org_missing', |
| 42 | + deviceId: 'dev_1', |
| 43 | + userId: 'usr_1', |
| 44 | + }), |
| 45 | + ).rejects.toThrow(NotFoundException); |
| 46 | + }); |
| 47 | + |
| 48 | + it('throws when user id is missing', async () => { |
| 49 | + (db.organization.findUnique as jest.Mock).mockResolvedValue({ |
| 50 | + id: 'org_1', |
| 51 | + }); |
| 52 | + |
| 53 | + await expect( |
| 54 | + service.removeDeviceById({ |
| 55 | + organizationId: 'org_1', |
| 56 | + deviceId: 'dev_1', |
| 57 | + }), |
| 58 | + ).rejects.toThrow(ForbiddenException); |
| 59 | + }); |
| 60 | + |
| 61 | + it('throws when user is not a member of organization', async () => { |
| 62 | + (db.organization.findUnique as jest.Mock).mockResolvedValue({ |
| 63 | + id: 'org_1', |
| 64 | + }); |
| 65 | + (db.member.findFirst as jest.Mock).mockResolvedValue(null); |
| 66 | + |
| 67 | + await expect( |
| 68 | + service.removeDeviceById({ |
| 69 | + organizationId: 'org_1', |
| 70 | + deviceId: 'dev_1', |
| 71 | + userId: 'usr_1', |
| 72 | + }), |
| 73 | + ).rejects.toThrow('User is not a member of this organization'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('throws when member is not an owner', async () => { |
| 77 | + (db.organization.findUnique as jest.Mock).mockResolvedValue({ |
| 78 | + id: 'org_1', |
| 79 | + }); |
| 80 | + (db.member.findFirst as jest.Mock).mockResolvedValue({ |
| 81 | + role: 'admin', |
| 82 | + }); |
| 83 | + |
| 84 | + await expect( |
| 85 | + service.removeDeviceById({ |
| 86 | + organizationId: 'org_1', |
| 87 | + deviceId: 'dev_1', |
| 88 | + userId: 'usr_1', |
| 89 | + }), |
| 90 | + ).rejects.toThrow('Only organization owners can remove devices'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws when device does not exist in organization', async () => { |
| 94 | + (db.organization.findUnique as jest.Mock).mockResolvedValue({ |
| 95 | + id: 'org_1', |
| 96 | + }); |
| 97 | + (db.member.findFirst as jest.Mock).mockResolvedValue({ |
| 98 | + role: 'owner', |
| 99 | + }); |
| 100 | + (db.device.deleteMany as jest.Mock).mockResolvedValue({ count: 0 }); |
| 101 | + |
| 102 | + await expect( |
| 103 | + service.removeDeviceById({ |
| 104 | + organizationId: 'org_1', |
| 105 | + deviceId: 'dev_missing', |
| 106 | + userId: 'usr_1', |
| 107 | + }), |
| 108 | + ).rejects.toThrow(NotFoundException); |
| 109 | + }); |
| 110 | + |
| 111 | + it('deletes device when caller is owner', async () => { |
| 112 | + (db.organization.findUnique as jest.Mock).mockResolvedValue({ |
| 113 | + id: 'org_1', |
| 114 | + }); |
| 115 | + (db.member.findFirst as jest.Mock).mockResolvedValue({ |
| 116 | + role: ' employee , owner ', |
| 117 | + }); |
| 118 | + (db.device.deleteMany as jest.Mock).mockResolvedValue({ count: 1 }); |
| 119 | + |
| 120 | + await service.removeDeviceById({ |
| 121 | + organizationId: 'org_1', |
| 122 | + deviceId: 'dev_1', |
| 123 | + userId: 'usr_1', |
| 124 | + }); |
| 125 | + |
| 126 | + expect(db.device.deleteMany).toHaveBeenCalledWith({ |
| 127 | + where: { |
| 128 | + id: 'dev_1', |
| 129 | + organizationId: 'org_1', |
| 130 | + }, |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments