trypost/tests/Feature/Social/LinkedInControllerTest.php

542 lines
22 KiB
PHP
Raw Normal View History

<?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;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
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, ['role' => Role::Member->value]);
});
/**
* Build a Socialite user for the LinkedIn person behind the OAuth grant.
*/
function linkedInSocialiteUser(string $id = 'person-123'): SocialiteUser
{
$socialiteUser = Mockery::mock(SocialiteUser::class);
$socialiteUser->shouldReceive('getId')->andReturn($id);
$socialiteUser->shouldReceive('getName')->andReturn('John Doe');
$socialiteUser->shouldReceive('getAvatar')->andReturn(null);
$socialiteUser->token = 'test-access-token';
$socialiteUser->refreshToken = 'test-refresh-token';
$socialiteUser->expiresIn = 5184000; // 60 days
$socialiteUser->approvedScopes = ['openid', 'profile', 'email', 'w_member_social'];
return $socialiteUser;
}
test('linkedin connect redirects to oauth provider via the openid driver', function () {
$driverMock = Mockery::mock();
$driverMock->shouldReceive('scopes')->andReturnSelf();
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
'getTargetUrl' => 'https://www.linkedin.com/oauth/v2/authorization?test=1',
]));
Socialite::shouldReceive('driver')
->with('linkedin-openid')
->andReturn($driverMock);
$response = $this->actingAs($this->user)
->withHeader('X-Inertia', 'true')
->get(route('app.social.linkedin.connect'));
$response->assertStatus(409); // Inertia::location returns 409 with X-Inertia header
expect(session('social_connect_workspace'))->toBe($this->workspace->id);
});
/**
* Mock the openid driver, hit connect, and return the scopes the controller asked for.
*
* @return array<int, string>
*/
function captureLinkedInConnectScopes(object $test): array
{
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
$captured = [];
$driverMock = Mockery::mock();
$driverMock->shouldReceive('scopes')
->withArgs(function (array $scopes) use (&$captured) {
$captured = $scopes;
return true;
})
->andReturnSelf();
$driverMock->shouldReceive('redirect')->andReturn(Mockery::mock([
'getTargetUrl' => 'https://www.linkedin.com/oauth/v2/authorization?test=1',
]));
Socialite::shouldReceive('driver')->with('linkedin-openid')->andReturn($driverMock);
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
$test->actingAs($test->user)
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
->withHeader('X-Inertia', 'true')
->get(route('app.social.linkedin.connect'));
return $captured;
}
test('linkedin connect requests the union of personal and organization scopes', function () {
config(['trypost.platforms.linkedin.scopes' => ['openid', 'profile', 'email', 'w_member_social']]);
config(['trypost.platforms.linkedin-page.scopes' => ['openid', 'profile', 'email', 'w_organization_social', 'r_organization_social', 'rw_organization_admin', 'w_member_social']]);
expect(captureLinkedInConnectScopes($this))->toEqualCanonicalizing([
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
'openid', 'profile', 'email', 'w_member_social',
'w_organization_social', 'r_organization_social', 'rw_organization_admin',
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
]);
});
test('connect requests only personal scopes when company pages are disabled', function () {
config(['trypost.platforms.linkedin.enabled' => true]);
config(['trypost.platforms.linkedin-page.enabled' => false]);
config(['trypost.platforms.linkedin.scopes' => ['openid', 'profile', 'email', 'w_member_social']]);
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
expect(captureLinkedInConnectScopes($this))->toEqualCanonicalizing([
'openid', 'profile', 'email', 'w_member_social',
fix(linkedin): drop deprecated r_basicprofile from default scopes Make r_basicprofile opt-in via LINKEDIN_EXTRA_SCOPES so self-hosted users unblock by default and ops with legacy/enterprise products keep working. Why --- LinkedIn rejects OAuth authorize requests with a generic "Bummer, something went wrong" page when an app asks for a scope it can't grant. `r_basicprofile` is a legacy scope deprecated in 2018; new LinkedIn dev apps don't have it, so every self-hosted user hits the rejection immediately on `/connect/linkedin`. The two products LinkedIn actually grants to standard apps today are: - Sign In with LinkedIn using OpenID Connect → `openid profile email` - Share on LinkedIn → `w_member_social` That set is enough for the connect flow. The only piece of data `r_basicprofile` was buying us is `/v2/me`'s `vanityName` (pretty `linkedin.com/in/<slug>`). `fetchVanityName()` already handles HTTP failure gracefully (returns null), and the only downstream consumer — `LinkedInPagePublisher`'s post-URL builder — already falls back to a numeric `linkedin.com/feed/update/<id>` URL when `$account->username` is null. Backward compatibility ---------------------- Ops with legacy or enterprise LinkedIn products approved on their dev app (so they DO have `r_basicprofile`) can opt back in via env: LINKEDIN_EXTRA_SCOPES=r_basicprofile `LinkedInController::resolveScopes()` merges this comma-separated list into the default scope array. The connect flow's `Socialite::scopes()` call then includes the legacy scope, preserving the pre-PR behaviour end-to-end (including `vanityName` lookup). Net effect for users without `r_basicprofile`: - Connect flow works (was previously rejected by LinkedIn). - Posts publish exactly the same way. - Generated post URLs use the numeric form instead of the vanity slug. Tests ----- - `linkedin connect requests the default scope set when LINKEDIN_EXTRA_SCOPES is unset` - `linkedin connect appends LINKEDIN_EXTRA_SCOPES to the default scope set` - Existing `splits comma-separated approvedScopes` fixture updated to match the new default set.
2026-05-28 03:54:26 +00:00
]);
});
test('connect requests only organization scopes when the personal profile is disabled', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
config(['trypost.platforms.linkedin-page.enabled' => true]);
config(['trypost.platforms.linkedin-page.scopes' => ['openid', 'w_organization_social', 'r_organization_social', 'rw_organization_admin']]);
expect(captureLinkedInConnectScopes($this))->toEqualCanonicalizing([
'openid', 'w_organization_social', 'r_organization_social', 'rw_organization_admin',
]);
});
test('connect is forbidden when both linkedin capabilities are disabled', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
config(['trypost.platforms.linkedin-page.enabled' => false]);
$this->actingAs($this->user)
->get(route('app.social.linkedin.connect'))
->assertForbidden();
});
test('connect returns the popup callback when the user has no workspace', function () {
// Runs inside the OAuth popup, so it must not redirect away and strand it.
$this->user->update(['current_workspace_id' => null]);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.connect'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->component('accounts/PopupCallback'));
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
});
test('linkedin callback stores the person and organizations then redirects to the selector', function () {
session(['social_connect_workspace' => $this->workspace->id]);
Socialite::shouldReceive('driver')
->with('linkedin-openid')
->andReturn(Mockery::mock(['user' => linkedInSocialiteUser()]));
Http::fake([
config('trypost.platforms.linkedin.api').'/v2/me*' => Http::response(['id' => 'person-123', 'vanityName' => 'johndoe'], 200),
config('trypost.platforms.linkedin.api').'/v2/organizationAcls*' => Http::response([
'elements' => [
['organization~' => ['id' => 123456, 'localizedName' => 'Test Company', 'vanityName' => 'testcompany']],
],
], 200),
]);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.callback'));
$response->assertRedirect(route('app.social.linkedin.select-identity'));
expect(session('linkedin_pending.person.id'))->toBe('person-123');
expect(session('linkedin_pending.person.vanity_name'))->toBe('johndoe');
expect(session('linkedin_pending.organizations'))->toHaveCount(1);
});
test('linkedin callback still redirects to the selector when the member administers no organizations', function () {
session(['social_connect_workspace' => $this->workspace->id]);
Socialite::shouldReceive('driver')
->with('linkedin-openid')
->andReturn(Mockery::mock(['user' => linkedInSocialiteUser()]));
Http::fake([
config('trypost.platforms.linkedin.api').'/v2/me*' => Http::response(['vanityName' => 'johndoe'], 200),
config('trypost.platforms.linkedin.api').'/v2/organizationAcls*' => Http::response(['elements' => []], 200),
]);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.callback'));
$response->assertRedirect(route('app.social.linkedin.select-identity'));
expect(session('linkedin_pending.organizations'))->toBe([]);
});
test('linkedin callback fails with expired session', function () {
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.callback'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$response->assertInertia(fn (Assert $page) => $page->where('message', 'Session expired. Please try again.'));
});
test('linkedin callback handles oauth errors gracefully', function () {
session(['social_connect_workspace' => $this->workspace->id]);
$mock = Mockery::mock();
$mock->shouldReceive('user')->andThrow(new Exception('OAuth error'));
fix: merge-readiness — close two billing/network bugs, harden tests Bugs (both with regression tests): - OnboardingController::store now guards already-subscribed accounts (mirrors index), preventing a second Stripe Checkout / double subscription if a subscribed user re-POSTs /onboarding. - SocialAccountObserver: drop the `platform_user_id != …` clause from the creating-time one-per-network check. On create there is no "self" to exclude, so it only weakened the rule — the same account connected via two network variants (e.g. Instagram standalone + via Facebook, same id) could slip a second account into the network. Now any account in the network blocks. Robustness: - CreateWorkspace wraps create + member attach + switchWorkspace in a transaction (cache-forget / quantity-sync run after), so a partial failure can't leave an orphan workspace that inflates the Stripe seat count — covers both the signup and the add-workspace paths. Test honesty & coverage: - Scope the ten "connect multiple <platform> accounts" tests to self-hosted mode (config + name); they only passed because the test env defaults SELF_HOSTED=true, and in cloud the one-per-network rule blocks them. - network_taken popup now has controller-level tests on all six OAuth controllers (added Threads, YouTube, LinkedInPage, and a new InstagramFacebook test file; LinkedInPage/InstagramFacebook also exercise variant collapse). - Wiring tests that creating/deleting a workspace actually calls syncWorkspaceQuantity (guards per-seat billing against silent breakage). - Strengthen TrialLengthTest to assert the configured length reaches trial_ends_at; add a same-id network-variant block test.
2026-06-22 12:26:31 +00:00
Socialite::shouldReceive('driver')->with('linkedin-openid')->andReturn($mock);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.callback'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$response->assertInertia(fn (Assert $page) => $page->where('message', 'Error connecting account. Please try again.'));
});
test('select-identity screen renders the person and organizations', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_member_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [
['id' => 123456, 'name' => 'Test Company', 'vanity_name' => 'testcompany', 'logo' => null],
],
]]);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.select-identity'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page
->component('accounts/LinkedInSelect')
->where('person.name', 'John Doe')
->has('organizations', 1)
);
});
test('select-identity hides the personal profile when that capability is disabled', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
config(['trypost.platforms.linkedin-page.enabled' => true]);
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'w_organization_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => null],
'organizations' => [
['id' => 123456, 'name' => 'Test Company', 'vanity_name' => 'testcompany', 'logo' => null],
],
]]);
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.select-identity'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page
->component('accounts/LinkedInSelect')
->where('person', null)
->has('organizations', 1)
);
});
test('selecting the person is rejected when the personal profile capability is disabled', function () {
config(['trypost.platforms.linkedin.enabled' => false]);
config(['trypost.platforms.linkedin-page.enabled' => true]);
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'w_organization_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => null],
'organizations' => [],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'person',
]);
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$this->assertDatabaseMissing('social_accounts', ['platform_user_id' => 'person-123']);
});
test('selecting an organization is rejected when company pages are disabled', function () {
config(['trypost.platforms.linkedin.enabled' => true]);
config(['trypost.platforms.linkedin-page.enabled' => false]);
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_member_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'organization',
'organization_id' => 123456,
]);
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$this->assertDatabaseMissing('social_accounts', ['platform_user_id' => 123456]);
});
test('select-identity returns the popup callback when the session expired', function () {
// Rendered inside the OAuth popup, so a redirect would strand it — it must
// answer with the self-closing callback view instead.
$response = $this->actingAs($this->user)->get(route('app.social.linkedin.select-identity'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->component('accounts/PopupCallback'));
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
});
test('selecting the person creates a linkedin account', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_member_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'person',
]);
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->component('accounts/PopupCallback'));
$response->assertInertia(fn (Assert $page) => $page->where('success', true));
$this->assertDatabaseHas('social_accounts', [
'workspace_id' => $this->workspace->id,
'platform' => Platform::LinkedIn->value,
'platform_user_id' => 'person-123',
'username' => 'johndoe',
'display_name' => 'John Doe',
'status' => Status::Connected->value,
]);
expect(session('linkedin_pending'))->toBeNull();
});
test('selecting the person downloads and stores the avatar', function () {
Storage::fake();
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_member_social'],
'person' => ['id' => 'person-avatar', 'name' => 'John Doe', 'avatar' => 'https://media.example.com/avatar.jpg', 'vanity_name' => 'johndoe'],
'organizations' => [],
]]);
Http::fake([
'https://media.example.com/avatar.jpg' => Http::response('fake-image-bytes', 200, ['Content-Type' => 'image/jpeg']),
]);
$this->actingAs($this->user)->post(route('app.social.linkedin.select'), ['type' => 'person']);
// The avatar download (uploadFromUrl) ran and a stored path was persisted.
Http::assertSent(fn ($request) => $request->url() === 'https://media.example.com/avatar.jpg');
$account = SocialAccount::where('platform_user_id', 'person-avatar')->first();
expect($account->getRawOriginal('avatar_url'))->not->toBeNull();
});
test('selecting an organization creates a linkedin-page account with the admin recorded in meta', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_organization_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [
['id' => 123456, 'name' => 'Test Company', 'vanity_name' => 'testcompany', 'logo' => null],
],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'organization',
'organization_id' => 123456,
]);
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', true));
$account = SocialAccount::where('platform', Platform::LinkedInPage->value)
->where('platform_user_id', 123456)
->first();
expect($account)->not->toBeNull();
expect($account->display_name)->toBe('Test Company');
expect($account->username)->toBe('testcompany');
expect($account->meta['admin_user_id'])->toBe('person-123');
expect($account->meta['admin_name'])->toBe('John Doe');
});
test('selecting an organization the member does not administer is rejected', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'w_organization_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [
['id' => 111, 'name' => 'My Company', 'vanity_name' => 'myco', 'logo' => null],
],
]]);
// 999 is not in the admin-verified list — a tampered POST must not connect it.
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'organization',
'organization_id' => 999,
]);
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$this->assertDatabaseMissing('social_accounts', ['platform_user_id' => 999]);
});
test('selecting an organization splits comma-separated approvedScopes before saving', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
// Socialite returns LinkedIn scopes CSV-joined into a single element.
'approved_scopes' => ['email,openid,profile,w_organization_social,r_organization_social,rw_organization_admin,w_member_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [
['id' => 999888, 'name' => 'Scope Company', 'vanity_name' => 'scopeco', 'logo' => null],
],
]]);
$this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'organization',
'organization_id' => 999888,
]);
$account = SocialAccount::where('platform_user_id', 999888)->first();
expect($account->scopes)->toEqualCanonicalizing([
'email', 'openid', 'profile',
'w_organization_social', 'r_organization_social',
'rw_organization_admin', 'w_member_social',
]);
});
test('select rejects an invalid identity type without stranding the popup', function () {
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => [],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), ['type' => 'bogus']);
// A redirect-back would strand the popup; it must answer with the self-closing callback view.
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->component('accounts/PopupCallback'));
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$this->assertDatabaseMissing('social_accounts', ['platform_user_id' => 'person-123']);
});
test('select fails with expired session', function () {
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'person',
]);
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$response->assertInertia(fn (Assert $page) => $page->where('message', 'Session expired. Please try again.'));
});
test('selecting the person shows network_taken when a linkedin page already occupies the network', function () {
config()->set('trypost.self_hosted', false);
SocialAccount::factory()->linkedinPage()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => 'existing-linkedin-page',
]);
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'test-access-token',
'refresh_token' => 'test-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_member_social'],
'person' => ['id' => 'new-person', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'person',
]);
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', false));
$response->assertInertia(fn (Assert $page) => $page->where('message', __('accounts.popup_callback.network_taken')));
$this->assertDatabaseMissing('social_accounts', [
'platform' => Platform::LinkedIn->value,
'platform_user_id' => 'new-person',
]);
});
test('user can connect multiple linkedin organizations in self-hosted mode', function () {
config()->set('trypost.self_hosted', true);
SocialAccount::factory()->linkedinPage()->create([
'workspace_id' => $this->workspace->id,
'platform_user_id' => '123456',
]);
session(['linkedin_pending' => [
'workspace_id' => $this->workspace->id,
'token' => 'new-access-token',
'refresh_token' => 'new-refresh-token',
'expires_in' => 5184000,
'approved_scopes' => ['openid', 'profile', 'email', 'w_organization_social'],
'person' => ['id' => 'person-123', 'name' => 'John Doe', 'avatar' => null, 'vanity_name' => 'johndoe'],
'organizations' => [
['id' => 789012, 'name' => 'Another Company', 'vanity_name' => 'anothercompany', 'logo' => null],
],
]]);
$response = $this->actingAs($this->user)->post(route('app.social.linkedin.select'), [
'type' => 'organization',
'organization_id' => 789012,
]);
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page->where('success', true));
expect($this->workspace->socialAccounts()->where('platform', Platform::LinkedInPage)->count())->toBe(2);
});