From 00b5dd78528bff804a502dcaa5cd43b18652e0a9 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 17 Jun 2023 18:18:17 +0100 Subject: [PATCH] Users API: Fixed incorrect created_at date on index endpoint For #4325 --- app/Activity/Models/Activity.php | 2 ++ app/Users/Controllers/UserApiController.php | 2 +- dev/api/responses/users-list.json | 2 +- tests/Api/UsersApiTest.php | 23 +++++++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Activity/Models/Activity.php b/app/Activity/Models/Activity.php index 1fa36e5be..9e4cb7858 100644 --- a/app/Activity/Models/Activity.php +++ b/app/Activity/Models/Activity.php @@ -19,6 +19,8 @@ use Illuminate\Support\Str; * @property string $entity_type * @property int $entity_id * @property int $user_id + * @property Carbon $created_at + * @property Carbon $updated_at */ class Activity extends Model { diff --git a/app/Users/Controllers/UserApiController.php b/app/Users/Controllers/UserApiController.php index 759aafbd8..880165e1b 100644 --- a/app/Users/Controllers/UserApiController.php +++ b/app/Users/Controllers/UserApiController.php @@ -73,7 +73,7 @@ class UserApiController extends ApiController */ public function list() { - $users = User::query()->select(['*']) + $users = User::query()->select(['users.*']) ->scopes('withLastActivityAt') ->with(['avatar']); diff --git a/dev/api/responses/users-list.json b/dev/api/responses/users-list.json index e070ee6a6..cbc7fb104 100644 --- a/dev/api/responses/users-list.json +++ b/dev/api/responses/users-list.json @@ -18,7 +18,7 @@ "id": 2, "name": "Benny", "email": "benny@example.com", - "created_at": "2022-01-31T20:39:24.000000Z", + "created_at": "2020-01-15T04:43:11.000000Z", "updated_at": "2021-11-18T17:10:58.000000Z", "external_auth_id": "", "slug": "benny", diff --git a/tests/Api/UsersApiTest.php b/tests/Api/UsersApiTest.php index 6af9c2a7a..e2a04b528 100644 --- a/tests/Api/UsersApiTest.php +++ b/tests/Api/UsersApiTest.php @@ -3,7 +3,9 @@ namespace Tests\Api; use BookStack\Activity\ActivityType; +use BookStack\Activity\Models\Activity as ActivityModel; use BookStack\Entities\Models\Entity; +use BookStack\Facades\Activity; use BookStack\Notifications\UserInvite; use BookStack\Users\Models\Role; use BookStack\Users\Models\User; @@ -67,6 +69,27 @@ class UsersApiTest extends TestCase ]]); } + public function test_index_endpoint_has_correct_created_and_last_activity_dates() + { + $user = $this->users->editor(); + $user->created_at = now()->subYear(); + $user->save(); + + $this->actingAs($user); + Activity::add(ActivityType::AUTH_LOGIN, 'test login activity'); + /** @var ActivityModel $activity */ + $activity = ActivityModel::query()->where('user_id', '=', $user->id)->latest()->first(); + + $resp = $this->asAdmin()->getJson($this->baseEndpoint . '?filter[id]=3'); + $resp->assertJson(['data' => [ + [ + 'id' => $user->id, + 'created_at' => $user->created_at->toJSON(), + 'last_activity_at' => $activity->created_at->toJson(), + ], + ]]); + } + public function test_create_endpoint() { $this->actingAsApiAdmin();