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.

5.3 KB
Raw Blame
<?php

declare(strict_types=1);

use App\Exceptions\Social\ErrorCategory;
use App\Exceptions\Social\GoogleBusinessPublishException;
use App\Exceptions\TokenExpiredException;
use Illuminate\Support\Facades\Http;

test('unauthenticated status throws a token expired exception', function () {
    $response = Http::response(['error' => ['status' => 'UNAUTHENTICATED', 'message' => 'bad token']], 401);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    GoogleBusinessPublishException::fromApiResponse($fakeResponse);
})->throws(TokenExpiredException::class);

test('permission denied maps to the permission category', function () {
    $response = Http::response(['error' => ['status' => 'PERMISSION_DENIED', 'message' => 'no access']], 403);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::Permission);
});

test('not found maps to the content policy category', function () {
    $response = Http::response(['error' => ['status' => 'NOT_FOUND', 'message' => 'location gone']], 404);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::ContentPolicy);
});

test('invalid argument maps to the content policy category', function () {
    $response = Http::response(['error' => ['status' => 'INVALID_ARGUMENT', 'message' => 'Invalid post format']], 400);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::ContentPolicy)
        ->and($exception->userMessage)->toBe('Invalid post format');
});

test('resource exhausted maps to the rate limit category', function () {
    $response = Http::response(['error' => ['status' => 'RESOURCE_EXHAUSTED', 'message' => 'slow down']], 429);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::RateLimit);
});

test('500 status maps to the server error category', function () {
    $response = Http::response(['error' => ['status' => 'INTERNAL', 'message' => 'oops']], 500);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::ServerError);
});

test('unknown errors use a generic fallback instead of the raw api body', function () {
    $response = Http::response(['error' => ['status' => 'FAILED_PRECONDITION', 'message' => 'opaque google dump']], 409);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusiness.googleapis.com/test');

    $exception = GoogleBusinessPublishException::fromApiResponse($fakeResponse);

    expect($exception->category)->toBe(ErrorCategory::Unknown)
        ->and($exception->userMessage)->toBe(__('posts.errors.google_business.rejected'))
        ->and($exception->userMessage)->not->toContain('opaque google dump');
});

test('isConfirmedDeadToken is true for 401 status', function () {
    $response = Http::response([], 401);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    expect(GoogleBusinessPublishException::isConfirmedDeadToken($fakeResponse))->toBeTrue();
});

test('isConfirmedDeadToken is true for UNAUTHENTICATED status', function () {
    $response = Http::response(['error' => ['status' => 'UNAUTHENTICATED']], 400);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    expect(GoogleBusinessPublishException::isConfirmedDeadToken($fakeResponse))->toBeTrue();
});

test('isConfirmedDeadToken is false for PERMISSION_DENIED status', function () {
    $response = Http::response(['error' => ['status' => 'PERMISSION_DENIED']], 403);
    $fakeResponse = Http::fake(['*' => $response])->post('https://mybusinessaccountmanagement.googleapis.com/test');

    expect(GoogleBusinessPublishException::isConfirmedDeadToken($fakeResponse))->toBeFalse();
});

test('no Google Business error is marked as a network rejection', function (string $reason, int $status) {
    $fakeResponse = Http::fake(['*' => Http::response(['error' => ['status' => $reason, 'message' => 'Rejected']], $status)])
        ->post(config('trypost.platforms.google_business.local_posts_api').'/accounts/1/locations/2/localPosts');

    expect(GoogleBusinessPublishException::fromApiResponse($fakeResponse)->isNetworkRejection())->toBeFalse();
})->with([
    'permission denied, also returned for our project' => ['PERMISSION_DENIED', 403],
    'a location we sent that does not exist' => ['NOT_FOUND', 404],
    'an argument we sent' => ['INVALID_ARGUMENT', 400],
    'our project quota' => ['RESOURCE_EXHAUSTED', 429],
]);