trypost/tests/Feature/Social/BlueskyControllerTest.php
Paulo Castellano 74c6442728 refactor: code review fixes — policies, enums, data_get, tests
- Refactor WorkspacePolicy to use pivot role instead of workspace.user_id
- Add manageBilling policy (owner only) to BillingController
- Fix ApiKeyController authorization (view → manageTeam for store/destroy)
- Fix WorkspaceInviteController using workspace.user_id for owner checks
- Fix WorkspaceController settings is_owner using workspace.user_id
- Create PostAction enum for UpdatePost/PostController action strings
- Create ApiToken\Status enum
- Add User::SUBSCRIPTION_NAME constant, replace all hardcoded 'default'
- Convert wantsEmailFor to accept NotificationType enum
- Convert all $data[] to data_get() across publishers, controllers, jobs
- Fix SocialLoginController callback missing try/catch
- Fix SocialController::toggleActive missing workspace null check
- Fix UpdatePost NPE on meta merge when postPlatform not found
- Remove HTML5 required attributes from form inputs
- Convert function declarations to arrow functions in Vue components
- Replace hardcoded URLs with Wayfinder route helpers
- Replace new Date() with dayjs
- Add 16 new test files covering policies, authorization, publishing
2026-03-31 00:40:18 -03:00

153 lines
5.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Enums\UserWorkspace\Role;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Http;
beforeEach(function () {
$this->user = User::factory()->create();
$this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]);
$this->user->update(['current_workspace_id' => $this->workspace->id]);
$this->workspace->members()->attach($this->user->id, ['role' => Role::Owner->value]);
});
test('bluesky connect page can be rendered', function () {
$response = $this->actingAs($this->user)->get(route('app.social.bluesky.connect'));
$response->assertOk();
});
test('user can connect bluesky account with valid credentials', function () {
Http::fake([
'https://bsky.social/xrpc/com.atproto.server.createSession' => Http::response([
'did' => 'did:plc:testuser123',
'handle' => 'testuser.bsky.social',
'accessJwt' => 'test-access-token',
'refreshJwt' => 'test-refresh-token',
], 200),
'https://bsky.social/xrpc/app.bsky.actor.getProfile*' => Http::response([
'did' => 'did:plc:testuser123',
'handle' => 'testuser.bsky.social',
'displayName' => 'Test User',
'avatar' => null,
], 200),
]);
$response = $this->actingAs($this->user)->post(route('app.social.bluesky.store'), [
'identifier' => 'testuser.bsky.social',
'password' => 'xxxx-xxxx-xxxx-xxxx',
]);
$response->assertOk();
$response->assertViewIs('auth.social-callback');
$response->assertViewHas('success', true);
$this->assertDatabaseHas('social_accounts', [
'workspace_id' => $this->workspace->id,
'platform' => Platform::Bluesky->value,
'platform_user_id' => 'did:plc:testuser123',
'username' => 'testuser.bsky.social',
'status' => Status::Connected->value,
]);
});
test('user cannot connect bluesky with invalid credentials', function () {
Http::fake([
'https://bsky.social/xrpc/com.atproto.server.createSession' => Http::response([
'error' => 'AuthenticationRequired',
'message' => 'Invalid identifier or password',
], 401),
]);
$response = $this->actingAs($this->user)->post(route('app.social.bluesky.store'), [
'identifier' => 'testuser.bsky.social',
'password' => 'wrong-password',
]);
$response->assertRedirect();
$response->assertSessionHasErrors('password');
$this->assertDatabaseMissing('social_accounts', [
'workspace_id' => $this->workspace->id,
'platform' => Platform::Bluesky->value,
]);
});
test('user cannot connect bluesky if already connected', function () {
SocialAccount::factory()->bluesky()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'did:plc:existing123',
]);
Http::fake([
'https://bsky.social/xrpc/com.atproto.server.createSession' => Http::response([
'did' => 'did:plc:newuser456',
'handle' => 'newuser.bsky.social',
'accessJwt' => 'test-access-token',
'refreshJwt' => 'test-refresh-token',
], 200),
'https://bsky.social/xrpc/app.bsky.actor.getProfile*' => Http::response([
'did' => 'did:plc:newuser456',
'handle' => 'newuser.bsky.social',
'displayName' => 'New User',
], 200),
]);
$response = $this->actingAs($this->user)->post(route('app.social.bluesky.store'), [
'identifier' => 'newuser.bsky.social',
'password' => 'xxxx-xxxx-xxxx-xxxx',
]);
$response->assertRedirect();
$response->assertSessionHasErrors('identifier');
expect($this->workspace->socialAccounts()->where('platform', Platform::Bluesky)->count())->toBe(1);
});
test('user can reconnect disconnected bluesky account', function () {
$existingAccount = SocialAccount::factory()->bluesky()->disconnected()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'did:plc:testuser123',
]);
Http::fake([
'https://bsky.social/xrpc/com.atproto.server.createSession' => Http::response([
'did' => 'did:plc:testuser123',
'handle' => 'testuser.bsky.social',
'accessJwt' => 'new-access-token',
'refreshJwt' => 'new-refresh-token',
], 200),
'https://bsky.social/xrpc/app.bsky.actor.getProfile*' => Http::response([
'did' => 'did:plc:testuser123',
'handle' => 'testuser.bsky.social',
'displayName' => 'Test User',
], 200),
]);
$response = $this->actingAs($this->user)->post(route('app.social.bluesky.store'), [
'identifier' => 'testuser.bsky.social',
'password' => 'xxxx-xxxx-xxxx-xxxx',
]);
$response->assertOk();
$response->assertViewHas('success', true);
$existingAccount->refresh();
expect($existingAccount->status)->toBe(Status::Connected);
expect($existingAccount->access_token)->toBe('new-access-token');
});
test('bluesky connection validates required fields', function () {
$response = $this->actingAs($this->user)->post(route('app.social.bluesky.store'), [
'identifier' => '',
'password' => '',
]);
$response->assertSessionHasErrors(['identifier', 'password']);
});