|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Models\AgentAssignedAppliances; |
| 6 | +use App\Models\Device; |
| 7 | +use App\Models\SolarHomeSystem; |
| 8 | +use Database\Factories\ApplianceFactory; |
| 9 | +use Database\Factories\ManufacturerFactory; |
| 10 | +use Tests\CreateEnvironments; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class AgentUnassignedDevicesTest extends TestCase { |
| 14 | + use CreateEnvironments; |
| 15 | + |
| 16 | + public function testAgentListsOnlyUnassignedDevicesForTheirAssignedAppliance(): void { |
| 17 | + $this->seedAgentAndAssignedAppliance(); |
| 18 | + $applianceId = $this->assignedAppliance->appliance_id; |
| 19 | + |
| 20 | + $unassigned = $this->createShsDevice('SHS-FREE-0001', $applianceId, null); |
| 21 | + $assignedToPerson = $this->createShsDevice('SHS-SOLD-0001', $applianceId, $this->people[0]->id); |
| 22 | + |
| 23 | + $response = $this->actingAs($this->agent) |
| 24 | + ->get(sprintf('/api/app/agents/devices/unassigned?appliance_id=%d&type=solar_home_system', $applianceId)); |
| 25 | + |
| 26 | + $response->assertStatus(200); |
| 27 | + $serials = collect($response->json('data'))->pluck('device_serial')->all(); |
| 28 | + $this->assertContains($unassigned->device_serial, $serials); |
| 29 | + $this->assertNotContains($assignedToPerson->device_serial, $serials); |
| 30 | + } |
| 31 | + |
| 32 | + public function testListExcludesUnitsOfOtherAppliances(): void { |
| 33 | + $this->seedAgentAndAssignedAppliance(); |
| 34 | + $applianceId = $this->assignedAppliance->appliance_id; |
| 35 | + |
| 36 | + $siblingAppliance = ApplianceFactory::new()->create([ |
| 37 | + 'appliance_type_id' => $this->applianceType->id, |
| 38 | + ]); |
| 39 | + $siblingDevice = $this->createShsDevice('SHS-SIBLING-0001', $siblingAppliance->id, null); |
| 40 | + |
| 41 | + $response = $this->actingAs($this->agent) |
| 42 | + ->get(sprintf('/api/app/agents/devices/unassigned?appliance_id=%d&type=solar_home_system', $applianceId)); |
| 43 | + |
| 44 | + $response->assertStatus(200); |
| 45 | + $serials = collect($response->json('data'))->pluck('device_serial')->all(); |
| 46 | + $this->assertNotContains($siblingDevice->device_serial, $serials); |
| 47 | + } |
| 48 | + |
| 49 | + public function testAgentCannotListDevicesForApplianceNotAssignedToThem(): void { |
| 50 | + $this->seedAgentAndAssignedAppliance(); |
| 51 | + |
| 52 | + $foreignAppliance = ApplianceFactory::new()->create([ |
| 53 | + 'appliance_type_id' => $this->applianceType->id, |
| 54 | + ]); |
| 55 | + $this->createShsDevice('SHS-FOREIGN-0001', $foreignAppliance->id, null); |
| 56 | + |
| 57 | + $response = $this->actingAs($this->agent) |
| 58 | + ->get(sprintf( |
| 59 | + '/api/app/agents/devices/unassigned?appliance_id=%d&type=solar_home_system', |
| 60 | + $foreignAppliance->id |
| 61 | + )); |
| 62 | + |
| 63 | + $response->assertStatus(422); |
| 64 | + $response->assertJsonValidationErrors(['appliance_id']); |
| 65 | + } |
| 66 | + |
| 67 | + public function testApplianceIdAndTypeAreRequired(): void { |
| 68 | + $this->seedAgentAndAssignedAppliance(); |
| 69 | + |
| 70 | + $response = $this->actingAs($this->agent) |
| 71 | + ->get('/api/app/agents/devices/unassigned'); |
| 72 | + |
| 73 | + $response->assertStatus(422); |
| 74 | + $response->assertJsonValidationErrors(['appliance_id', 'type']); |
| 75 | + } |
| 76 | + |
| 77 | + public function testTypeMustBeSupported(): void { |
| 78 | + $this->seedAgentAndAssignedAppliance(); |
| 79 | + |
| 80 | + $response = $this->actingAs($this->agent) |
| 81 | + ->get(sprintf( |
| 82 | + '/api/app/agents/devices/unassigned?appliance_id=%d&type=bogus', |
| 83 | + $this->assignedAppliance->appliance_id |
| 84 | + )); |
| 85 | + |
| 86 | + $response->assertStatus(422); |
| 87 | + $response->assertJsonValidationErrors(['type']); |
| 88 | + } |
| 89 | + |
| 90 | + public function testUnauthenticatedRequestIsRejected(): void { |
| 91 | + $response = $this->get('/api/app/agents/devices/unassigned?appliance_id=1&type=solar_home_system'); |
| 92 | + |
| 93 | + $response->assertStatus(401); |
| 94 | + } |
| 95 | + |
| 96 | + private function seedAgentAndAssignedAppliance(): void { |
| 97 | + $this->createTestData(); |
| 98 | + $this->createCluster(); |
| 99 | + $this->createMiniGrid(); |
| 100 | + $this->createCity(); |
| 101 | + $this->createAgentCommission(); |
| 102 | + $this->createAgent(); |
| 103 | + $this->createPerson(1, 1); |
| 104 | + $this->createApplianceType(); |
| 105 | + |
| 106 | + $appliance = ApplianceFactory::new()->create([ |
| 107 | + 'appliance_type_id' => $this->applianceType->id, |
| 108 | + ]); |
| 109 | + $this->assignedAppliance = AgentAssignedAppliances::query()->create([ |
| 110 | + 'agent_id' => $this->agent->id, |
| 111 | + 'user_id' => $this->user->id, |
| 112 | + 'appliance_id' => $appliance->id, |
| 113 | + 'cost' => 100, |
| 114 | + ]); |
| 115 | + } |
| 116 | + |
| 117 | + private function createShsDevice(string $serial, int $applianceId, ?int $personId): Device { |
| 118 | + $manufacturer = ManufacturerFactory::new()->isShsManufacturer()->create(); |
| 119 | + $shs = SolarHomeSystem::query()->create([ |
| 120 | + 'serial_number' => $serial, |
| 121 | + 'manufacturer_id' => $manufacturer->id, |
| 122 | + 'appliance_id' => $applianceId, |
| 123 | + ]); |
| 124 | + |
| 125 | + return Device::query()->create([ |
| 126 | + 'person_id' => $personId, |
| 127 | + 'device_id' => $shs->id, |
| 128 | + 'device_type' => SolarHomeSystem::RELATION_NAME, |
| 129 | + 'device_serial' => $serial, |
| 130 | + ]); |
| 131 | + } |
| 132 | +} |
0 commit comments