trypost/tests/Feature/ApiKeyControllerTest.php
Paulo Castellano 1af705f3fd
Localize calendar date pickers to the active UI locale (#234)
* Localize calendar date pickers to the active UI locale.

Pass the Inertia locale into Reka calendars, translate a11y labels, and use dayjs LL/LLL for DatePicker display text.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Localize remaining dayjs date displays across the UI.

Route list/calendar/preview timestamps through localized LL/LLL helpers so formats follow the active UI locale instead of English or Portuguese-locked patterns.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Use scheduled-or-now timestamps in platform previews.

Drive preview labels from the post schedule when set, localize Discord without dayjs calendar(), and expose a single formatPreviewPostedAt helper.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix preview schedule wiring and Discord/Facebook timestamp labels.

Restore hasPickedTime from any saved scheduled_at, keep time on non-today Discord labels, and use a localized just-now fallback for unschedled Facebook previews.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix API key expiry day shift and tighten calendar range titles.

Format date-only expiry in UTC calendar days, and use compact dayjs titles for calendar day/week headers.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Replace frontend date source greps with Inertia API key assertions.

Drop the Vue/TS file scanner and assert expiry calendar days through ApiKeyController props instead.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Localize calendar week titles with day-first dayjs formats.

Avoid English month-day order in week headers and chart axis labels so locales like pt-BR keep natural day-first dates.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Split preview timestamp formatting into named platform helpers.

Replace the style-switch formatPreviewPostedAt with clear per-platform helpers so call sites read as formatDiscordPreview / formatXPreview / etc.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 21:02:34 -03:00

148 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\UserWorkspace\Role;
use App\Models\AccessToken;
use App\Models\User;
use App\Models\Workspace;
beforeEach(function () {
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create([
'account_id' => $this->user->account_id,
'user_id' => $this->user->id,
]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Admin->value]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
$this->user->refresh();
});
function makeWorkspaceToken(User $user, Workspace $workspace): AccessToken
{
$result = $user->createToken('Existing');
$token = AccessToken::find($result->token->id);
$token->forceFill(['workspace_id' => $workspace->id])->saveQuietly();
return $token->refresh();
}
it('shows api keys page', function () {
makeWorkspaceToken($this->user, $this->workspace);
$this->actingAs($this->user)
->get(route('app.api-keys.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/workspace/ApiKeys')
->has('apiTokens', 1)
);
});
it('creates an api key', function () {
$this->actingAs($this->user)
->post(route('app.api-keys.store'), ['name' => 'My API Key'])
->assertRedirect();
$tokens = AccessToken::where('user_id', $this->user->id)
->where('workspace_id', $this->workspace->id)
->get();
expect($tokens)->toHaveCount(1);
expect($tokens->first()->name)->toBe('My API Key');
expect($tokens->first()->revoked)->toBeFalse();
});
it('creates an api key with expiration', function () {
$expiresOn = now()->addDays(30)->format('Y-m-d');
$this->actingAs($this->user)
->post(route('app.api-keys.store'), [
'name' => 'Expiring Key',
'expires_at' => $expiresOn,
])
->assertRedirect();
$token = AccessToken::where('user_id', $this->user->id)
->where('workspace_id', $this->workspace->id)
->first();
expect($token->expires_at)->not->toBeNull()
->and($token->expires_at->toDateString())->toBe($expiresOn);
});
it('passes the chosen expiry calendar day to the api keys page', function () {
$expiresOn = '2026-10-31';
$result = $this->user->createToken('Expiring Key');
$token = AccessToken::find($result->token->id);
$token->forceFill([
'workspace_id' => $this->workspace->id,
'expires_at' => $expiresOn,
])->saveQuietly();
$this->actingAs($this->user)
->get(route('app.api-keys.index'))
->assertOk()
->assertInertia(fn ($page) => $page
->component('settings/workspace/ApiKeys')
->has('apiTokens', 1)
->where('apiTokens.0.name', 'Expiring Key')
->where('apiTokens.0.expires_at', fn ($value) => is_string($value) && str_starts_with($value, $expiresOn))
);
});
it('validates name is required', function () {
$this->actingAs($this->user)
->post(route('app.api-keys.store'), [])
->assertSessionHasErrors('name');
});
it('revokes an api key', function () {
$token = makeWorkspaceToken($this->user, $this->workspace);
$this->actingAs($this->user)
->delete(route('app.api-keys.destroy', $token->id))
->assertRedirect();
expect($token->refresh()->revoked)->toBeTrue();
});
it('cannot delete api key from another workspace', function () {
$otherUser = User::factory()->create();
$otherWorkspace = Workspace::factory()->create([
'account_id' => $otherUser->account_id,
'user_id' => $otherUser->id,
]);
$token = makeWorkspaceToken($otherUser, $otherWorkspace);
$this->actingAs($this->user)
->delete(route('app.api-keys.destroy', $token->id))
->assertNotFound();
});
it('member cannot create api key', function () {
$member = User::factory()->create();
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$this->actingAs($member)
->post(route('app.api-keys.store'), ['name' => 'Test Key'])
->assertForbidden();
});
it('member cannot delete api key', function () {
$member = User::factory()->create();
$this->workspace->members()->attach($member->id, ['role' => Role::Member->value]);
$member->update(['current_workspace_id' => $this->workspace->id]);
$token = makeWorkspaceToken($this->user, $this->workspace);
$this->actingAs($member)
->delete(route('app.api-keys.destroy', $token->id))
->assertForbidden();
});
it('api keys page requires authentication', function () {
$this->get(route('app.api-keys.index'))->assertRedirect(route('login'));
});