. */ namespace Tests\Feature; use App\Admin; use App\StatisticsMessageDevice; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class ApiStatisticsMessagesTest extends TestCase { use WithFaker, RefreshDatabase; protected $route = '/api/statistics/messages'; public function testMessages() { $admin = Admin::factory()->create(); $admin->account->generateApiKey(); $id = '1234'; $this->keyAuthenticated($admin->account) ->json('POST', $this->route, [ 'id' => $id, 'from' => $this->faker->email(), 'sent_at' => $this->faker->iso8601(), 'encrypted' => false ]) ->assertStatus(200); $this->assertDatabaseHas('statistics_messages', [ 'id' => $id ]); $this->keyAuthenticated($admin->account) ->json('POST', $this->route, [ 'id' => $id, 'from' => $this->faker->email(), 'sent_at' => $this->faker->iso8601(), 'encrypted' => false ]) ->assertStatus(422); $this->keyAuthenticated($admin->account) ->json('POST', $this->route, [ 'id' => $id, 'from' => $this->faker->email(), 'sent_at' => 'bad_date', 'encrypted' => false ]) ->assertJsonValidationErrors(['sent_at']); // Patch previous message with devices $to = $this->faker->email(); $device = $this->faker->uuid(); $receivedAt = $this->faker->iso8601(); $lastStatus = 200; $newReceivedAt = $this->faker->iso8601(); $newLastStatus = 201; $this->keyAuthenticated($admin->account) ->json('PATCH', $this->route . '/' . $id . '/to/' . $to . ' /devices/' . $device, [ 'last_status' => $lastStatus, 'received_at' => $receivedAt ]) ->assertStatus(201); $this->keyAuthenticated($admin->account) ->json('PATCH', $this->route . '/' . $id . '/to/' . $to . ' /devices/' . $device, [ 'last_status' => $newLastStatus, 'received_at' => $newReceivedAt ]) ->assertStatus(200); $this->assertSame(1, StatisticsMessageDevice::count()); $this->assertDatabaseHas('statistics_message_devices', [ 'message_id' => $id, 'received_at' => $newReceivedAt, 'last_status' => $newLastStatus ]); $this->keyAuthenticated($admin->account) ->json('PATCH', $this->route . '/' . $id . '/to/' . $this->faker->email() . ' /devices/' . $this->faker->uuid(), [ 'last_status' => $newLastStatus, 'received_at' => $newReceivedAt ]) ->assertStatus(201); $this->assertSame(2, StatisticsMessageDevice::count()); } }