Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

9.6 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Social\PendingConnection;
use Illuminate\Support\Facades\Http;
use Inertia\Testing\AssertableInertia;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\User as SocialiteUser;

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, membershipPivot('member'));

    Http::fake([
        config('trypost.platforms.x.api').'/users/me*' => Http::response(['data' => [
            'id' => '123456789',
            'subscription_type' => 'Premium',
            'verified_type' => 'blue',
        ]]),
    ]);
});

test('x authorize url uses the current host and pkce', function () {
    $response = $this->actingAs($this->user)->get(route('app.social.x.connect'));

    expect(urldecode((string) $response->headers->get('Location')))
        ->toStartWith('https://x.com/i/oauth2/authorize')
        ->toContain('code_challenge_method=S256')
        ->toContain('offline.access');
});

test('x connect redirects to oauth provider', function () {
    $driverMock = Mockery::mock();
    $driverMock->shouldReceive('scopes')->andReturnSelf();
    $driverMock->shouldReceive('with')->with([])->andReturnSelf();
    $driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
        'getTargetUrl' => 'https://twitter.com/i/oauth2/authorize?test=1',
    ]));

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn($driverMock);

    $response = $this->actingAs($this->user)
        ->get(route('app.social.x.connect'));

    $response->assertRedirect('https://twitter.com/i/oauth2/authorize?test=1');

    expect(PendingConnection::current()?->workspaceId())->toBe($this->workspace->id);
});

test('x oauth callback creates account', function () {
    startSocialConnect($this->workspace, Platform::X);

    $socialiteUser = Mockery::mock(SocialiteUser::class);
    $socialiteUser->shouldReceive('getId')->andReturn('123456789');
    $socialiteUser->shouldReceive('getNickname')->andReturn('testuser');
    $socialiteUser->shouldReceive('getName')->andReturn('Test User');
    $socialiteUser->shouldReceive('getAvatar')->andReturn(null);
    $socialiteUser->token = 'test-access-token';
    $socialiteUser->refreshToken = 'test-refresh-token';
    $socialiteUser->expiresIn = 7200;
    $socialiteUser->approvedScopes = ['tweet.read', 'tweet.write', 'users.read'];

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn(Mockery::mock([
            'user' => $socialiteUser,
        ]));

    $this->actingAs($this->user)
        ->get(route('app.social.x.callback'))
        ->assertRedirect(route('app.social.connect.show', Platform::X));

    $this->assertDatabaseCount('social_accounts', 0);

    assertFinishedOnChannel(finishSocialConnect(Platform::X))
        ->assertInertiaFlash('connectedChannel.accountId', $this->workspace->socialAccounts()->sole()->id)
        ->assertInertiaFlash('connectedChannel.created', true);

    $this->assertDatabaseHas('social_accounts', [
        'workspace_id' => $this->workspace->id,
        'platform' => Platform::X->value,
        'platform_user_id' => '123456789',
        'username' => 'testuser',
        'status' => Status::Connected->value,
    ]);
});

test('x oauth callback stores the subscription tier', function () {
    startSocialConnect($this->workspace, Platform::X);

    $socialiteUser = Mockery::mock(SocialiteUser::class);
    $socialiteUser->shouldReceive('getId')->andReturn('123456789');
    $socialiteUser->shouldReceive('getNickname')->andReturn('testuser');
    $socialiteUser->shouldReceive('getName')->andReturn('Test User');
    $socialiteUser->shouldReceive('getAvatar')->andReturn(null);
    $socialiteUser->token = 'test-access-token';
    $socialiteUser->refreshToken = 'test-refresh-token';
    $socialiteUser->expiresIn = 7200;
    $socialiteUser->approvedScopes = ['tweet.read', 'tweet.write', 'users.read'];

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn(Mockery::mock(['user' => $socialiteUser]));

    $this->actingAs($this->user)->get(route('app.social.x.callback'))->assertRedirect();
    finishSocialConnect(Platform::X)->assertRedirect();

    $account = $this->workspace->socialAccounts()->sole();

    expect($account->hasXLongPosts())->toBeTrue()
        ->and($account->maxContentLength())->toBe(SocialAccount::X_LONG_POST_LENGTH);
    Http::assertSentCount(1);
});

test('x callback fails with expired session', function () {
    // No session data - simulating expired session

    $this->actingAs($this->user)
        ->get(route('app.social.x.callback'))
        ->assertRedirect(route('app.social.connect.show', Platform::X));

    $this->get(route('app.social.connect.show', Platform::X))
        ->assertInertia(fn (AssertableInertia $page) => $page
            ->component('accounts/ConnectFinish')
            ->where('state', 'expired')
        );
});

test('user can connect multiple x accounts', function () {

    SocialAccount::factory()->x()->create([
        'workspace_id' => $this->workspace->id,
        'platform_user_id' => '123456789',
    ]);

    startSocialConnect($this->workspace, Platform::X);

    $socialiteUser = Mockery::mock(SocialiteUser::class);
    $socialiteUser->shouldReceive('getId')->andReturn('987654321');
    $socialiteUser->shouldReceive('getNickname')->andReturn('anotheruser');
    $socialiteUser->shouldReceive('getName')->andReturn('Another User');
    $socialiteUser->shouldReceive('getAvatar')->andReturn(null);
    $socialiteUser->token = 'new-access-token';
    $socialiteUser->refreshToken = 'new-refresh-token';
    $socialiteUser->expiresIn = 7200;
    $socialiteUser->approvedScopes = ['tweet.read', 'tweet.write'];

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn(Mockery::mock([
            'user' => $socialiteUser,
        ]));

    $this->actingAs($this->user)->get(route('app.social.x.callback'))->assertRedirect();
    finishSocialConnect(Platform::X)->assertRedirect();

    expect($this->workspace->socialAccounts()->where('platform', Platform::X)->count())->toBe(2);
});

test('x callback reconnects the original card', function () {
    $account = SocialAccount::factory()->x()->create([
        'workspace_id' => $this->workspace->id,
        'platform_user_id' => '123456789',
        'username' => 'old',
        'access_token' => 'expired-token',
    ]);

    startSocialConnect($this->workspace, Platform::X, $account);

    $socialiteUser = Mockery::mock(SocialiteUser::class);
    $socialiteUser->shouldReceive('getId')->andReturn('123456789');
    $socialiteUser->shouldReceive('getNickname')->andReturn('testuser');
    $socialiteUser->shouldReceive('getName')->andReturn('Test User');
    $socialiteUser->shouldReceive('getAvatar')->andReturn(null);
    $socialiteUser->token = 'fresh-access-token';
    $socialiteUser->refreshToken = 'fresh-refresh-token';
    $socialiteUser->expiresIn = 7200;
    $socialiteUser->approvedScopes = ['tweet.read', 'tweet.write', 'users.read'];

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn(Mockery::mock(['user' => $socialiteUser]));

    $this->actingAs($this->user)->get(route('app.social.x.callback'))->assertRedirect();

    finishSocialConnect(Platform::X)
        ->assertInertiaFlash('connectedChannel.accountId', $account->id)
        ->assertInertiaFlash('connectedChannel.created', false);

    expect($this->workspace->socialAccounts()->where('platform', Platform::X)->count())->toBe(1)
        ->and($account->fresh()->access_token)->toBe('fresh-access-token')
        ->and($account->fresh()->username)->toBe('testuser');
});

test('x callback handles oauth errors gracefully', function () {
    startSocialConnect($this->workspace, Platform::X);

    $mock = Mockery::mock();
    $mock->shouldReceive('user')->andThrow(new Exception('OAuth error'));

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn($mock);

    $this->actingAs($this->user)
        ->get(route('app.social.x.callback'))
        ->assertRedirect(route('app.social.connect.show', Platform::X));

    expect(socialConnectFailure())->toBe('error_connecting');
});

test('x reconnect that authorizes another account says so', function () {
    $account = SocialAccount::factory()->create([
        'workspace_id' => $this->workspace->id,
        'platform' => Platform::X,
        'platform_user_id' => 'x-brand',
        'username' => 'brand',
    ]);

    startSocialConnect($this->workspace, Platform::X, $account);

    $socialiteUser = Mockery::mock(SocialiteUser::class);
    $socialiteUser->shouldReceive('getId')->andReturn('x-personal');
    $socialiteUser->shouldReceive('getNickname')->andReturn('personal');
    $socialiteUser->shouldReceive('getName')->andReturn('Personal');
    $socialiteUser->shouldReceive('getAvatar')->andReturn(null);
    $socialiteUser->token = 'personal-token';
    $socialiteUser->refreshToken = 'personal-refresh';
    $socialiteUser->expiresIn = 3600;

    Socialite::shouldReceive('driver')
        ->with('x')
        ->andReturn(Mockery::mock(['user' => $socialiteUser]));

    $this->actingAs($this->user)
        ->get(route('app.social.x.callback'))
        ->assertRedirect(route('app.social.connect.show', Platform::X));

    expect(socialConnectFailure())->toBe('wrong_account');

    expect($account->fresh()->platform_user_id)->toBe('x-brand');
});