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\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 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('youtube authorize url offers the account chooser', function () {
$response = $this->actingAs($this->user)->get(route('app.social.youtube.connect'));
expect(urldecode((string) $response->headers->get('Location')))
->toStartWith('https://accounts.google.com/')
->toContain('prompt=select_account consent');
});
test('youtube connect redirects to oauth provider', function () {
$driverMock = Mockery::mock();
$driverMock->shouldReceive('scopes')->andReturnSelf();
$driverMock->shouldReceive('with')->with([
'access_type' => 'offline',
'prompt' => 'select_account consent',
])->once()->andReturnSelf();
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
'getTargetUrl' => 'https://accounts.google.com/o/oauth2/v2/auth?test=1',
]));
Socialite::shouldReceive('driver')
->with('google')
->andReturn($driverMock);
$response = $this->actingAs($this->user)
->get(route('app.social.youtube.connect'));
$response->assertRedirect('https://accounts.google.com/o/oauth2/v2/auth?test=1');
expect(PendingConnection::current()?->workspaceId())->toBe($this->workspace->id);
});
test('youtube oauth callback creates account with single channel', function () {
startSocialConnect($this->workspace->id, Platform::YouTube);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_channel_123',
'snippet' => [
'title' => 'My YouTube Channel',
'description' => 'Channel description',
'customUrl' => '@mychannel',
'thumbnails' => [
'default' => ['url' => null],
],
],
'statistics' => [
'subscriberCount' => 1000,
],
],
],
], 200),
]);
$response = $this->actingAs($this->user)->get(route('app.social.youtube.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::YouTube));
finishSocialConnect(Platform::YouTube)->assertRedirect();
$this->assertDatabaseHas('social_accounts', [
'workspace_id' => $this->workspace->id,
'platform' => Platform::YouTube->value,
'platform_user_id' => 'UC_channel_123',
'username' => 'mychannel',
'display_name' => 'My YouTube Channel',
'status' => Status::Connected->value,
]);
});
test('youtube offers every channel google returns and connects the one picked', function () {
startSocialConnect($this->workspace->id, Platform::YouTube);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_channel_1',
'snippet' => [
'title' => 'Channel 1',
'customUrl' => '@channel1',
'thumbnails' => ['default' => ['url' => null]],
],
'statistics' => ['subscriberCount' => 500],
],
[
'id' => 'UC_channel_2',
'snippet' => [
'title' => 'Channel 2',
'customUrl' => '@channel2',
'thumbnails' => ['default' => ['url' => null]],
],
'statistics' => ['subscriberCount' => 1000],
],
],
], 200),
]);
$this->actingAs($this->user)
->get(route('app.social.youtube.callback'))
->assertRedirect(route('app.social.connect.show', Platform::YouTube));
expect(PendingConnection::current()->identityKeys())->toBe(['youtube:UC_channel_1', 'youtube:UC_channel_2']);
finishSocialConnect(Platform::YouTube, ['youtube:UC_channel_2'])->assertRedirect();
expect($this->workspace->socialAccounts()->where('platform', Platform::YouTube)->pluck('platform_user_id')->all())->toBe(['UC_channel_2']);
});
test('youtube callback fails when no channels found', function () {
startSocialConnect($this->workspace->id, Platform::YouTube);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [],
], 200),
]);
$response = $this->actingAs($this->user)->get(route('app.social.youtube.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::YouTube));
expect(socialConnectFailure())->toBe('no_youtube_channels');
});
test('youtube callback fails with expired session', function () {
// No session data - simulating expired session
$response = $this->actingAs($this->user)->get(route('app.social.youtube.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::YouTube));
expect(socialConnectFailure())->toBeNull();
});
test('user can connect multiple youtube accounts', function () {
SocialAccount::factory()->youtube()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'UC_channel_123',
]);
startSocialConnect($this->workspace->id, Platform::YouTube);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_456');
$socialiteUser->token = 'new-access-token';
$socialiteUser->refreshToken = 'new-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock([
'user' => $socialiteUser,
]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_another_channel',
'snippet' => [
'title' => 'Another Channel',
'customUrl' => '@anotherchannel',
'thumbnails' => ['default' => ['url' => null]],
],
'statistics' => ['subscriberCount' => 500],
],
],
], 200),
]);
$response = $this->actingAs($this->user)->get(route('app.social.youtube.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::YouTube));
finishSocialConnect(Platform::YouTube)->assertRedirect();
expect($this->workspace->socialAccounts()->where('platform', Platform::YouTube)->count())->toBe(2);
});
test('youtube callback handles oauth errors gracefully', function () {
startSocialConnect($this->workspace->id, Platform::YouTube);
$mock = Mockery::mock();
$mock->shouldReceive('user')->andThrow(new Exception('OAuth error'));
Socialite::shouldReceive('driver')
->with('google')
->andReturn($mock);
$response = $this->actingAs($this->user)->get(route('app.social.youtube.callback'));
$response->assertRedirect(route('app.social.connect.show', Platform::YouTube));
expect(socialConnectFailure())->toBe('error_connecting');
});
test('youtube reconnect keeps the original card for a single channel', function () {
$account = SocialAccount::factory()->youtube()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'UC_channel_123',
'username' => 'old',
'access_token' => 'expired-token',
]);
startSocialConnect($this->workspace->id, Platform::YouTube, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'fresh-access-token';
$socialiteUser->refreshToken = 'fresh-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_channel_123',
'snippet' => [
'title' => 'My YouTube Channel',
'customUrl' => '@mychannel',
'thumbnails' => ['default' => ['url' => null]],
],
'statistics' => ['subscriberCount' => 1000],
],
],
], 200),
]);
$this->actingAs($this->user)
->get(route('app.social.youtube.callback'))
->assertRedirect(route('app.social.connect.show', Platform::YouTube));
finishSocialConnect(Platform::YouTube)->assertRedirect();
expect($this->workspace->socialAccounts()->where('platform', Platform::YouTube)->count())->toBe(1)
->and($account->fresh()->access_token)->toBe('fresh-access-token')
->and($account->fresh()->username)->toBe('mychannel');
});
test('youtube reconnect shows channel_not_found when the channel is missing', function () {
$account = SocialAccount::factory()->youtube()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'UC_missing',
]);
startSocialConnect($this->workspace->id, Platform::YouTube, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_other',
'snippet' => [
'title' => 'Other Channel',
'customUrl' => '@other',
'thumbnails' => ['default' => ['url' => null]],
],
'statistics' => ['subscriberCount' => 1],
],
],
], 200),
]);
$this->actingAs($this->user)
->get(route('app.social.youtube.callback'))
->assertRedirect(route('app.social.connect.show', Platform::YouTube));
expect(socialConnectFailure())->toBe('channel_not_found');
});
test('youtube reconnect narrows a multi channel response to its own card', function () {
$account = SocialAccount::factory()->youtube()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'UC_target',
'username' => 'stale',
'access_token' => 'expired-token',
]);
startSocialConnect($this->workspace->id, Platform::YouTube, $account->id);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'fresh-access-token';
$socialiteUser->refreshToken = 'fresh-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
// The reconnect target is deliberately NOT first: without the reconnect
// filter the callback would connect UC_other.
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_other',
'snippet' => ['title' => 'Other', 'customUrl' => '@other', 'thumbnails' => ['default' => ['url' => null]]],
'statistics' => ['subscriberCount' => 5],
],
[
'id' => 'UC_target',
'snippet' => ['title' => 'Target', 'customUrl' => '@target', 'thumbnails' => ['default' => ['url' => null]]],
'statistics' => ['subscriberCount' => 10],
],
],
], 200),
]);
$this->actingAs($this->user)
->get(route('app.social.youtube.callback'))
->assertRedirect(route('app.social.connect.show', Platform::YouTube));
finishSocialConnect(Platform::YouTube)->assertRedirect();
expect($account->fresh()->platform_user_id)->toBe('UC_target')
->and($account->fresh()->access_token)->toBe('fresh-access-token')
->and($this->workspace->socialAccounts()->where('platform_user_id', 'UC_other')->exists())->toBeFalse();
});
test('youtube skips an already connected channel and takes the next one', function () {
SocialAccount::factory()->youtube()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'UC_taken',
]);
startSocialConnect($this->workspace->id, Platform::YouTube);
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn('google_user_123');
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 3600;
Socialite::shouldReceive('driver')
->with('google')
->andReturn(Mockery::mock(['user' => $socialiteUser]));
Http::fake([
'https://www.googleapis.com/youtube/v3/channels*' => Http::response([
'items' => [
[
'id' => 'UC_taken',
'snippet' => ['title' => 'Taken', 'customUrl' => '@taken', 'thumbnails' => ['default' => ['url' => null]]],
'statistics' => ['subscriberCount' => 5],
],
[
'id' => 'UC_free',
'snippet' => ['title' => 'Free', 'customUrl' => '@free', 'thumbnails' => ['default' => ['url' => null]]],
'statistics' => ['subscriberCount' => 10],
],
],
], 200),
]);
$this->actingAs($this->user)
->get(route('app.social.youtube.callback'))
->assertRedirect(route('app.social.connect.show', Platform::YouTube));
finishSocialConnect(Platform::YouTube)->assertRedirect();
expect($this->workspace->socialAccounts()->where('platform', Platform::YouTube)->pluck('platform_user_id')->sort()->values()->all())
->toBe(['UC_free', 'UC_taken']);
});