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.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
<?php
declare(strict_types=1);
use App\Enums\Post\Status as PostStatus;
use App\Enums\PostPlatform\ContentType;
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Support\Social\PendingConnection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
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'));
});
test('instagram authorize url forces reauth so a second account is reachable', function () {
$response = $this->actingAs($this->user)->get(route('app.social.instagram.connect'));
expect($response->headers->get('Location'))
->toStartWith(config('trypost.platforms.instagram.oauth_url').'/oauth/authorize')
->toContain('force_reauth=true');
});
test('instagram connect redirects to oauth provider', function () {
$driverMock = Mockery::mock();
$driverMock->shouldReceive('scopes')->andReturnSelf();
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
'getTargetUrl' => 'https://www.instagram.com/oauth/authorize?test=1',
]));
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn($driverMock);
$this->withoutExceptionHandling();
$response = $this->actingAs($this->user)
->get(route('app.social.instagram.connect'));
$response->assertRedirect('https://www.instagram.com/oauth/authorize?test=1');
expect(PendingConnection::current()?->workspaceId())->toBe($this->workspace->id);
});
test('instagram oauth callback creates account', function () {
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('12345678');
$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 = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
$response = $this->actingAs($this->user)->get(route('app.social.instagram.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::Instagram));
finishSocialConnect(Platform::Instagram)->assertRedirect();
$this->assertDatabaseHas('social_accounts', [
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram->value,
'platform_user_id' => '12345678',
'username' => 'testuser',
'status' => Status::Connected->value,
]);
});
test('instagram callback fails with expired session', function () {
$response = $this->actingAs($this->user)->get(route('app.social.instagram.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBeNull();
});
test('user can connect multiple instagram accounts', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => '12345678',
'status' => Status::Connected,
]);
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('87654321');
$socialiteUser->shouldReceive('getNickname')->andReturn('newuser');
$socialiteUser->shouldReceive('getName')->andReturn('New User');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'new-access-token';
$socialiteUser->refreshToken = 'new-refresh-token';
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
$response = $this->actingAs($this->user)->get(route('app.social.instagram.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::Instagram));
finishSocialConnect(Platform::Instagram)->assertRedirect();
expect($this->workspace->socialAccounts()->where('platform', Platform::Instagram)->count())->toBe(2);
});
test('instagram callback handles oauth errors gracefully', function () {
startSocialConnect($this->workspace->id, Platform::Instagram);
$mock = Mockery::mock();
$mock->shouldReceive('user')->andThrow(new Exception('OAuth error'));
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn($mock);
$response = $this->actingAs($this->user)->get(route('app.social.instagram.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBe('error_connecting');
});
test('instagram connect redirects to create workspace if none exists', function () {
$this->user->update(['current_workspace_id' => null]);
$response = $this->actingAs($this->user)->get(route('app.social.instagram.connect'));
$response->assertRedirect(route('app.workspaces.create'));
});
test('instagram callback refuses an identity already connected via the facebook variant', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::InstagramFacebook,
'platform_user_id' => 'shared-ig-id',
'username' => 'brand',
]);
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('shared-ig-id');
$socialiteUser->shouldReceive('getNickname')->andReturn('brand');
$socialiteUser->shouldReceive('getName')->andReturn('Brand');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
$response = $this->actingAs($this->user)->get(route('app.social.instagram.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBe('all_connected');
expect($this->workspace->socialAccounts()->where('platform_user_id', 'shared-ig-id')->count())->toBe(1);
});
test('instagram callback refuses an account seated through facebook under its professional account id', function () {
SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::InstagramFacebook,
'platform_user_id' => '17841400000000001',
'username' => 'brand',
]);
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('app-scoped-1');
$socialiteUser->shouldReceive('getNickname')->andReturn('brand');
$socialiteUser->shouldReceive('getName')->andReturn('Brand');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = null;
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS', 'user_id' => 17841400000000001];
Socialite::shouldReceive('driver')->with('instagram')->andReturn(Mockery::mock(['user' => $socialiteUser]));
$this->actingAs($this->user)
->get(route('app.social.instagram.callback'))
->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBe('all_connected')
->and($this->workspace->socialAccounts()->count())->toBe(1);
});
test('instagram callback reconnects the original card', function () {
$account = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => '12345678',
'username' => 'old',
'access_token' => 'expired-token',
'status' => Status::TokenExpired,
]);
startSocialConnect($this->workspace->id, Platform::Instagram, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('12345678');
$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 = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
$this->actingAs($this->user)
->get(route('app.social.instagram.callback'))
->assertRedirect(route('app.social.connect.show', Platform::Instagram));
finishSocialConnect(Platform::Instagram)->assertRedirect();
expect($this->workspace->socialAccounts()->count())->toBe(1)
->and($account->fresh()->access_token)->toBe('fresh-access-token')
->and($account->fresh()->username)->toBe('testuser')
->and($account->fresh()->status)->toBe(Status::Connected);
});
test('instagram reconnect that authorizes another account says so instead of connecting', function () {
$account = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::Instagram,
'platform_user_id' => '12345678',
'username' => 'old',
]);
startSocialConnect($this->workspace->id, Platform::Instagram, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('99999999');
$socialiteUser->shouldReceive('getNickname')->andReturn('someone-else');
$socialiteUser->shouldReceive('getName')->andReturn('Someone Else');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'other-access-token';
$socialiteUser->refreshToken = 'other-refresh-token';
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
$this->actingAs($this->user)
->get(route('app.social.instagram.callback'))
->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBe('wrong_account');
expect($this->workspace->socialAccounts()->count())->toBe(1)
->and($account->fresh()->platform_user_id)->toBe('12345678')
->and($account->fresh()->username)->toBe('old');
});
test('a connect racing another on the same network says it is busy without filing an error', function () {
Log::spy();
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('12345678');
$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 = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS'];
Socialite::shouldReceive('driver')
->with('instagram')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
$lock = Cache::lock("social_connect:{$this->workspace->id}:".Platform::Instagram->network(), 10);
expect($lock->get())->toBeTrue();
$this->actingAs($this->user)
->get(route('app.social.instagram.callback'))
->assertRedirect(route('app.social.connect.show', Platform::Instagram));
try {
finishSocialConnect(Platform::Instagram)->assertRedirect(route('app.social.connect.show', Platform::Instagram));
expect(socialConnectFailure())->toBe('busy');
} finally {
$lock->release();
}
expect($this->workspace->socialAccounts()->count())->toBe(0);
Log::shouldNotHaveReceived('error');
});
test('instagram callback stores the professional account id', function () {
startSocialConnect($this->workspace->id, Platform::Instagram);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('app-scoped-1');
$socialiteUser->shouldReceive('getNickname')->andReturn('testuser');
$socialiteUser->shouldReceive('getName')->andReturn('Test User');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = null;
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS', 'user_id' => 17841400000000001];
Socialite::shouldReceive('driver')->with('instagram')->andReturn(Mockery::mock(['user' => $socialiteUser]));
$this->actingAs($this->user)->get(route('app.social.instagram.callback'));
finishSocialConnect(Platform::Instagram)->assertRedirect();
expect(SocialAccount::where('platform_user_id', 'app-scoped-1')->sole()->meta)
->toEqual(['account_type' => 'BUSINESS', 'ig_user_id' => '17841400000000001']);
});
test('instagram callback moves a card connected through facebook back to instagram login', function () {
$account = SocialAccount::factory()->create([
'workspace_id' => $this->workspace->id,
'platform' => Platform::InstagramFacebook,
'platform_user_id' => '17841400000000001',
'username' => 'old',
]);
$reel = Post::factory()->forAccount($account, ContentType::InstagramReel)->create([
'user_id' => $this->user->id,
'status' => PostStatus::Scheduled,
'scheduled_at' => now()->addDay(),
'meta' => ['audio' => ['id' => '123', 'title' => 'Song']],
]);
startSocialConnect($this->workspace->id, Platform::Instagram, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('app-scoped-1');
$socialiteUser->shouldReceive('getNickname')->andReturn('testuser');
$socialiteUser->shouldReceive('getName')->andReturn('Test User');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'fresh-access-token';
$socialiteUser->refreshToken = null;
$socialiteUser->expiresIn = 5184000;
$socialiteUser->user = ['account_type' => 'BUSINESS', 'user_id' => '17841400000000001'];
Socialite::shouldReceive('driver')->with('instagram')->andReturn(Mockery::mock(['user' => $socialiteUser]));
$this->actingAs($this->user)
->get(route('app.social.instagram.callback'))
->assertRedirect(route('app.social.connect.show', Platform::Instagram));
finishSocialConnect(Platform::Instagram)->assertRedirect();
expect($this->workspace->socialAccounts()->count())->toBe(1)
->and($account->fresh()->platform)->toBe(Platform::Instagram)
->and($account->fresh()->platform_user_id)->toBe('app-scoped-1')
->and($account->fresh()->access_token)->toBe('fresh-access-token')
->and($reel->fresh()->platform)->toBe(Platform::Instagram)
->and(data_get($reel->fresh()->meta, 'audio.id'))->toBe('123');
});