Skip to content

Commit

Permalink
tests: added api schemed tests for external lists (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Nov 11, 2024
1 parent 55e37e1 commit e374b7d
Show file tree
Hide file tree
Showing 24 changed files with 3,052 additions and 248 deletions.
50 changes: 0 additions & 50 deletions app/Actions/Models/List/BaseStoreExternalProfileAction.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

declare(strict_types=1);

namespace App\Actions\Models\List\ExternalProfile\ExternalEntry\Site;
namespace App\Actions\Models\List\ExternalProfile\ExternalEntry\Token;

use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryTokenAction;
use App\Enums\Models\List\ExternalEntryWatchStatus;
use App\Models\List\External\ExternalEntry;
use App\Models\Wiki\ExternalResource;
use Exception;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -76,19 +75,13 @@ public function getId(): ?int
return $this->id;
}

try {
[, $payload] = explode('.', $this->getToken());
[, $payload] = explode('.', $this->getToken());

$decodedArray = json_decode(base64_decode($payload), true);
$decodedArray = json_decode(base64_decode($payload), true);

$this->id = intval(Arr::get($decodedArray, 'sub'));
$this->id = intval(Arr::get($decodedArray, 'sub'));

return $this->id;
} catch (Exception $e) {
Log::error($e->getMessage());

return null;
}
return $this->id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Actions\Models\List\ExternalProfile\ExternalEntry\Site;
namespace App\Actions\Models\List\ExternalProfile\ExternalEntry\Username;

use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryAction;
use App\Enums\Models\List\ExternalEntryWatchStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class AnilistExternalTokenAction extends BaseExternalTokenAction
* Use the authorization code to get the tokens and store them.
*
* @param string $code
* @return ExternalToken|null
* @return ExternalToken
*
* @throws Exception
*/
public function store(string $code): ?ExternalToken
public function store(string $code): ExternalToken
{
try {
$response = Http::acceptJson()
Expand All @@ -45,7 +45,7 @@ public function store(string $code): ?ExternalToken
$token = Arr::get($response, 'access_token');

if ($token === null) {
return null;
throw new Error('Failed to get token');
}

return ExternalToken::query()->create([
Expand All @@ -54,7 +54,7 @@ public function store(string $code): ?ExternalToken
} catch (Exception $e) {
Log::error($e->getMessage());

return null;
throw $e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
namespace App\Actions\Models\List\ExternalProfile;

use App\Actions\Http\Api\StoreAction;
use App\Actions\Models\List\BaseStoreExternalProfileAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryTokenAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Site\AnilistExternalEntryTokenAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Token\AnilistExternalEntryTokenAction;
use App\Enums\Models\List\ExternalProfileSite;
use App\Enums\Models\List\ExternalProfileVisibility;
use App\Models\List\External\ExternalEntry;
use App\Models\List\External\ExternalToken;
use App\Models\List\ExternalProfile;
use Error;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand All @@ -22,12 +19,12 @@
/**
* Class StoreExternalProfileTokenAction.
*/
class StoreExternalProfileTokenAction extends BaseStoreExternalProfileAction
class StoreExternalProfileTokenAction
{
protected Collection $resources;

/**
* Find or store external profile and its entries given determined external token.
* Find or store external profile given determined external token.
*
* @param ExternalToken $token
* @param array $parameters
Expand All @@ -48,31 +45,7 @@ public function findOrCreate(ExternalToken $token, array $parameters): ?External

$userId = $action->getId();

// TODO: if the profile already exists, the list should be synced.
$profile = $this->searchForUserId($userId, $site, $action, $parameters);

$entries = $action->getEntries();

$this->preloadResources($site, $entries);

$token->externalprofile()->associate($profile);

$externalEntries = [];
foreach ($entries as $entry) {
$externalId = Arr::get($entry, 'external_id');

foreach ($this->getAnimesByExternalId($externalId) as $anime) {
$externalEntries[] = [
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, ExternalEntry::ATTRIBUTE_SCORE),
ExternalEntry::ATTRIBUTE_IS_FAVORITE => Arr::get($entry, ExternalEntry::ATTRIBUTE_IS_FAVORITE),
ExternalEntry::ATTRIBUTE_WATCH_STATUS => Arr::get($entry, ExternalEntry::ATTRIBUTE_WATCH_STATUS),
ExternalEntry::ATTRIBUTE_ANIME => $anime->getKey(),
ExternalEntry::ATTRIBUTE_PROFILE => $profile->getKey(),
];
}
}

ExternalEntry::insert($externalEntries);
$profile = $this->findForUserIdOrCreate($userId, $site, $action, $parameters);

return $profile;

Expand All @@ -90,9 +63,9 @@ public function findOrCreate(ExternalToken $token, array $parameters): ?External
* @param ExternalProfileSite $site
* @param BaseExternalEntryTokenAction $action
* @param array $parameters
* @return ExternalProfile|null
* @return ExternalProfile
*/
protected function searchForUserId(int $userId, ExternalProfileSite $site, BaseExternalEntryTokenAction $action, array $parameters): ?ExternalProfile
protected function findForUserIdOrCreate(int $userId, ExternalProfileSite $site, BaseExternalEntryTokenAction $action, array $parameters): ExternalProfile
{
$claimedProfile = ExternalProfile::query()
->where(ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID, $userId)
Expand Down Expand Up @@ -122,6 +95,7 @@ protected function searchForUserId(int $userId, ExternalProfileSite $site, BaseE

$storeAction = new StoreAction();

/** @var ExternalProfile $profile */
$profile = $storeAction->store(ExternalProfile::query(), [
ExternalProfile::ATTRIBUTE_EXTERNAL_USER_ID => $userId,
ExternalProfile::ATTRIBUTE_USER => Arr::get($parameters, ExternalProfile::ATTRIBUTE_USER),
Expand All @@ -130,11 +104,7 @@ protected function searchForUserId(int $userId, ExternalProfileSite $site, BaseE
ExternalProfile::ATTRIBUTE_VISIBILITY => ExternalProfileVisibility::PRIVATE->value,
]);

if ($profile instanceof ExternalProfile) {
return $profile;
}

return null;
return $profile;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace App\Actions\Models\List\ExternalProfile;

use App\Actions\Http\Api\StoreAction;
use App\Actions\Models\List\BaseStoreExternalProfileAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\BaseExternalEntryAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Site\AnilistExternalEntryAction;
use App\Actions\Models\List\ExternalProfile\ExternalEntry\Username\AnilistExternalEntryAction;
use App\Enums\Models\List\ExternalProfileSite;
use App\Enums\Models\List\ExternalProfileVisibility;
use App\Models\List\External\ExternalEntry;
use App\Models\List\ExternalProfile;
use Error;
use Exception;
Expand All @@ -22,18 +20,18 @@
/**
* Class StoreExternalProfileUsernameAction.
*/
class StoreExternalProfileUsernameAction extends BaseStoreExternalProfileAction
class StoreExternalProfileUsernameAction
{
/**
* Find or store an external profile and its entries given determined username.
*
* @param Builder $builder
* @param array $profileParameters
* @return ExternalProfile|null
* @return ExternalProfile
*
* @throws Exception
*/
public function findOrCreate(Builder $builder, array $profileParameters): ?ExternalProfile
public function findOrCreate(Builder $builder, array $profileParameters): ExternalProfile
{
try {
$profileSite = ExternalProfileSite::fromLocalizedName(Arr::get($profileParameters, 'site'));
Expand All @@ -52,13 +50,9 @@ public function findOrCreate(Builder $builder, array $profileParameters): ?Exter
$action = $this->getActionClass($profileSite, $profileParameters);

if ($action === null) {
return null;
throw new Error("Action not found for site {$profileSite->localize()}", 404);
}

$entries = $action->getEntries();

$this->preloadResources($profileSite, $entries);

$storeAction = new StoreAction();

/** @var ExternalProfile $profile */
Expand All @@ -69,23 +63,6 @@ public function findOrCreate(Builder $builder, array $profileParameters): ?Exter
ExternalProfile::ATTRIBUTE_VISIBILITY => ExternalProfileVisibility::fromLocalizedName(Arr::get($profileParameters, ExternalProfile::ATTRIBUTE_VISIBILITY))->value,
]);

$externalEntries = [];
foreach ($entries as $entry) {
$externalId = Arr::get($entry, 'external_id');

foreach ($this->getAnimesByExternalId($externalId) as $anime) {
$externalEntries[] = [
ExternalEntry::ATTRIBUTE_SCORE => Arr::get($entry, ExternalEntry::ATTRIBUTE_SCORE),
ExternalEntry::ATTRIBUTE_IS_FAVORITE => Arr::get($entry, ExternalEntry::ATTRIBUTE_IS_FAVORITE),
ExternalEntry::ATTRIBUTE_WATCH_STATUS => Arr::get($entry, ExternalEntry::ATTRIBUTE_WATCH_STATUS),
ExternalEntry::ATTRIBUTE_ANIME => $anime->getKey(),
ExternalEntry::ATTRIBUTE_PROFILE => $profile->getKey(),
];
}
}

ExternalEntry::insert($externalEntries);

DB::commit();

return $profile;
Expand All @@ -94,7 +71,7 @@ public function findOrCreate(Builder $builder, array $profileParameters): ?Exter

DB::rollBack();

return null;
throw $e;
}
}

Expand Down

This file was deleted.

Loading

0 comments on commit e374b7d

Please sign in to comment.