Skip to content

Commit

Permalink
Default to using master credentials if the Credentials instance doesn…
Browse files Browse the repository at this point in the history
…'t have any
  • Loading branch information
jlevers committed Oct 12, 2024
1 parent 7bdf9d0 commit f8f7cf9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ composer.lock

vendor/
.idea/
.vscode/
.phpunit.cache/
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function up(): void
$table->enum('region', Region::values());

// The app credentials that the the refresh token was created with
$table->string('client_id');
$table->string('client_secret');
$table->string('client_id')->nullable();
$table->string('client_secret')->nullable();

// The LWA refresh token for this set of credentials
$table->string('refresh_token', 511);

Expand Down
8 changes: 4 additions & 4 deletions src/Models/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function sellerConnector(
?Client $authenticationClient = null
): SellerConnector {
$connector = SellingPartnerApi::seller(
clientId: $this->client_id,
clientSecret: $this->client_secret,
clientId: $this->client_id ?? config('spapi.single.lwa.client_id'),
clientSecret: $this->client_secret ?? config('spapi.single.lwa.client_secret'),
refreshToken: $this->refresh_token,
endpoint: Endpoint::byRegion($this->region),
dataElements: $dataElements,
Expand All @@ -57,8 +57,8 @@ public function vendorConnector(
?Client $authenticationClient = null
): VendorConnector {
$connector = SellingPartnerApi::vendor(
clientId: $this->client_id,
clientSecret: $this->client_secret,
clientId: $this->client_id ?? config('spapi.single.lwa.client_id'),
clientSecret: $this->client_secret ?? config('spapi.single.lwa.client_secret'),
refreshToken: $this->refresh_token,
endpoint: Endpoint::byRegion($this->region),
dataElements: $dataElements,
Expand Down
22 changes: 20 additions & 2 deletions tests/MultiSellerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@

class MultiSellerTest extends TestCase
{
private Seller $seller;

private Credentials $creds;

public function setUp(): void
{
parent::setUp();

$seller = Seller::create(['name' => 'seller-1']);
$this->seller = Seller::create(['name' => 'seller-1']);
$this->creds = Credentials::create([
'seller_id' => $seller->id,
'seller_id' => $this->seller->id,
'selling_partner_id' => 'spapi01',
'client_id' => 'client-id-1',
'client_secret' => 'client-secret-1',
Expand Down Expand Up @@ -48,4 +50,20 @@ public function testCanMakeVendorApis(): void
$this->assertEquals('client-id-1', $vendorConnector->clientId);
$this->assertEquals(Endpoint::NA, $vendorConnector->endpoint);
}

public function testCanMakeSellerApiWithNoClientCredentials(): void
{
$creds = Credentials::create([
'seller_id' => $this->seller->id,
'selling_partner_id' => 'spapi02',
'refresh_token' => 'refresh-token-2',
'region' => 'EU',
]);

$sellerConnector = $creds->sellerConnector();

$this->assertEquals('client-id', $sellerConnector->clientId);
$this->assertEquals('client-secret', $sellerConnector->clientSecret);
$this->assertEquals(Endpoint::EU, $sellerConnector->endpoint);
}
}
13 changes: 0 additions & 13 deletions tests/SingleSellerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,17 @@
namespace HighsideLabs\LaravelSpApi\Tests;

use Orchestra\Testbench\Attributes\WithConfig;
use Orchestra\Testbench\Attributes\WithEnv;
use SellingPartnerApi\Enums\Endpoint;
use SellingPartnerApi\Seller\SellerConnector;
use SellingPartnerApi\Vendor\VendorConnector;

#[WithEnv('SPAPI_LWA_CLIENT_ID', 'client-id')]
#[WithEnv('SPAPI_LWA_CLIENT_SECRET', 'client-secret')]
#[WithEnv('SPAPI_LWA_REFRESH_TOKEN', 'refresh-token')]
#[WithEnv('SPAPI_ENDPOINT_REGION', 'EU')]
#[WithConfig('spapi.debug', true)]
class SingleSellerTest extends TestCase
{
private SellerConnector $sellerConnector;

private VendorConnector $vendorConnector;

protected function resolveApplicationConfiguration($app): void
{
parent::resolveApplicationConfiguration($app);

$spapiConfig = require_once __DIR__.'/../config/spapi.php';
$app['config']->set('spapi', $spapiConfig);
}

public function setUp(): void
{
parent::setUp();
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@

use HighsideLabs\LaravelSpApi\SellingPartnerApiServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Attributes\WithEnv;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

#[WithEnv('SPAPI_LWA_CLIENT_ID', 'client-id')]
#[WithEnv('SPAPI_LWA_CLIENT_SECRET', 'client-secret')]
#[WithEnv('SPAPI_LWA_REFRESH_TOKEN', 'refresh-token')]
#[WithEnv('SPAPI_ENDPOINT_REGION', 'EU')]
class TestCase extends OrchestraTestCase
{
use RefreshDatabase;

protected function resolveApplicationConfiguration($app): void
{
parent::resolveApplicationConfiguration($app);

$spapiConfig = require __DIR__.'/../config/spapi.php';
$app['config']->set('spapi', $spapiConfig);
}

protected function defineDatabaseMigrations(): void
{
// Migrations cannot be loaded via artisan($this, 'vendor:publish', ['--tag' => 'spapi-multi-seller']),
Expand Down

0 comments on commit f8f7cf9

Please sign in to comment.