Skip to content

Commit

Permalink
🐛 Fix status update via API1 and added tests (#1170)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Nov 9, 2022
1 parent bbef68b commit 4bee269
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 13 deletions.
25 changes: 12 additions & 13 deletions app/Http/Controllers/API/v1/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,30 +340,29 @@ public function update(Request $request, int $statusId): JsonResponse {
->first();

if ($stopover === null) {
return $this->sendv1Error('Invalid stopover given', 400);
return $this->sendError('Invalid stopover given', 400);
}

TrainCheckinController::changeDestination(
checkin: $status->trainCheckin,
newDestinationStopover: $stopover,
);
}
$status->fresh();

$editStatusResponse = StatusBackend::EditStatus(
user: Auth::user(),
statusId: $statusId,
body: $validated['body'],
business: Business::from($validated['business']),
visibility: StatusVisibility::from($validated['visibility']),
);
return $this->sendResponse(new StatusResource($editStatusResponse));
$status->update([
'body' => $validated['body'] ?? null,
'business' => Business::from($validated['business']),
'visibility' => StatusVisibility::from($validated['visibility']),
]);

$status->fresh();
return $this->sendResponse(new StatusResource($status));
} catch (ModelNotFoundException) {
return $this->sendv1Error('Status not found');
return $this->sendError('Status not found');
} catch (PermissionException|AuthorizationException) {
return $this->sendv1Error('You are not authorized to edit this status', 403);
return $this->sendError('You are not authorized to edit this status', 403);
} catch (InvalidArgumentException) {
return $this->sendv1Error('Invalid Arguments', 400);
return $this->sendError('Invalid Arguments', 400);
}
}

Expand Down
111 changes: 111 additions & 0 deletions tests/Feature/APIv1/StatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests\Feature\APIv1;

use App\Enum\Business;
use App\Enum\StatusVisibility;
use App\Http\Controllers\UserController as UserBackend;
use App\Models\HafasTrip;
use App\Models\Status;
Expand Down Expand Up @@ -138,4 +140,113 @@ public function testActiveStatusesWithInactiveStatus(): void {
$response->assertNotFound();
$this->assertEquals('No active status', $response->json('message'));
}

public function testStatusUpdate() {
$user = User::factory()->create();
$userToken = $user->createToken('token')->accessToken;

$status = Status::factory([
'user_id' => $user->id,
'body' => 'old body',
'visibility' => StatusVisibility::PRIVATE->value,
'business' => Business::PRIVATE->value,
])->create();
TrainCheckin::factory([
'status_id' => $status->id,
'user_id' => $user->id,
])->create();

$this->assertEquals('old body', $status->body);
$this->assertEquals(StatusVisibility::PRIVATE->value, $status->visibility->value);
$this->assertEquals(Business::PRIVATE->value, $status->business->value);

$response = $this->put(
uri: '/api/v1/statuses/' . $status->id,
data: [
'body' => 'new body',
'visibility' => StatusVisibility::PUBLIC->value,
'business' => Business::BUSINESS->value,
],
headers: ['Authorization' => 'Bearer ' . $userToken],
);
$response->assertOk();

$status->refresh();
$this->assertEquals('new body', $status->body);
$this->assertEquals(StatusVisibility::PUBLIC->value, $status->visibility->value);
$this->assertEquals(Business::BUSINESS->value, $status->business->value);
}


public function testStatusUpdateWithChangedDestination() {
$user = User::factory()->create();
$userToken = $user->createToken('token')->accessToken;

$firstDeparture = Date::now()->addHour();
$secondArrival = Date::now()->addHours(2);
$thirdArrival = Date::now()->addHours(3);

$firstStation = TrainStation::factory()->create();
$secondStation = TrainStation::factory()->create();
$thirdStation = TrainStation::factory()->create();

$status = Status::factory([
'user_id' => $user->id,
'visibility' => StatusVisibility::PRIVATE->value,
'business' => Business::PRIVATE->value,
])->create();
$checkin = TrainCheckin::factory([
'status_id' => $status->id,
'user_id' => $user->id,
'origin' => $firstStation->ibnr,
'departure' => $firstDeparture->toDateTimeString(),
'destination' => $secondStation->ibnr,
'arrival' => $secondArrival->toDateTimeString(),
])->create();

TrainStopover::factory([
'trip_id' => $checkin->trip_id,
'train_station_id' => $firstStation->id,
'arrival_planned' => $firstDeparture,
'arrival_real' => $firstDeparture,
'departure_planned' => $firstDeparture,
'departure_real' => $firstDeparture,
])->create();
TrainStopover::factory([
'trip_id' => $checkin->trip_id,
'train_station_id' => $secondStation->id,
'arrival_planned' => $secondArrival,
'arrival_real' => $secondArrival,
'departure_planned' => $secondArrival,
'departure_real' => $secondArrival,
])->create();
TrainStopover::factory([
'trip_id' => $checkin->trip_id,
'train_station_id' => $thirdStation->id,
'arrival_planned' => $thirdArrival,
'arrival_real' => $thirdArrival,
'departure_planned' => $thirdArrival,
'departure_real' => $thirdArrival,
])->create();

$this->assertEquals($checkin->originStation->id, $firstStation->id);
$this->assertEquals($checkin->destinationStation->id, $secondStation->id);

$response = $this->put(
uri: '/api/v1/statuses/' . $status->id,
data: [
'visibility' => StatusVisibility::PUBLIC->value,
'business' => Business::BUSINESS->value,
'destinationId' => $thirdStation->id,
'destinationArrivalPlanned' => $thirdArrival->toDateTimeString(),
],
headers: ['Authorization' => 'Bearer ' . $userToken],
);
$response->assertOk();

$checkin = $checkin->fresh();

$this->assertEquals($checkin->originStation->id, $firstStation->id);
$this->assertEquals($checkin->destinationStation->id, $thirdStation->id);
}
}

0 comments on commit 4bee269

Please sign in to comment.