Skip to content

Commit

Permalink
added tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nahid committed Sep 13, 2023
1 parent c71e073 commit 37ef397
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
15 changes: 0 additions & 15 deletions src/Actions/InstallShop.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ public function __construct(
*/
public function __invoke(ShopDomain $shopDomain, ?string $code): array
{
if (!$this->isValidShop($shopDomain)) {
return [
'completed' => false,
'url' => null,
'shop_id' => null,
];
}
// Get the shop
$shop = $this->shopQuery->getByDomain($shopDomain, [], true);

Expand Down Expand Up @@ -126,12 +119,4 @@ public function __invoke(ShopDomain $shopDomain, ?string $code): array
];
}
}

public function isValidShop(ShopDomain $shopDomain): bool
{
$regex = '/^[a-zA-Z0-9][a-zA-Z0-9\-]*.myshopify.com/';
$isMatched = preg_match($regex, $shopDomain->toNative(), $matches, PREG_OFFSET_CAPTURE);

return $isMatched === 1;
}
}
24 changes: 24 additions & 0 deletions tests/Http/Middleware/IframeProtectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,28 @@ public function testIframeProtectionWithUnauthorizedShop(): void
$this->assertNotEmpty($currentHeader);
$this->assertEquals($expectedHeader, $currentHeader);
}

public function testIframeProtectionWithExistingAncestorsInConfig(): void
{
$shop = factory($this->model)->create();
$this->auth->login($shop);
$this->app['config']->set('shopify-app.iframe_ancestors', 'https://example.com');

$domain = auth()->user()->name;
$expectedHeader = "frame-ancestors https://$domain https://admin.shopify.com https://example.com";

$request = new Request();
$shopQueryStub = $this->createStub(ShopQuery::class);
$shopQueryStub->method('getByDomain')->willReturn($shop);
$next = function () {
return new Response('Test Response');
};

$middleware = new IframeProtection($shopQueryStub);
$response = $middleware->handle($request, $next);
$currentHeader = $response->headers->get('content-security-policy');

$this->assertNotEmpty($currentHeader);
$this->assertEquals($expectedHeader, $currentHeader);
}
}
32 changes: 32 additions & 0 deletions tests/Http/Middleware/VerifyShopifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,36 @@ public function testTokenProcessingAndMissMatchingShops(): void
$this->expectException(HttpException::class);
$this->runMiddleware(VerifyShopify::class, $newRequest);
}

public function testNotNativeAppbridgeWithTokenProcessingAndLoginShop(): void
{
// Create a shop that matches the token from buildToken
factory($this->model)->create(['name' => 'shop-name.myshopify.com']);
$this->app['config']->set('shopify-app.frontend_engine', 'REACT');

// Setup the request
$currentRequest = Request::instance();
$newRequest = $currentRequest->duplicate(
// Query Params
[
'shop' => 'shop-name.myshopify.com',
],
// Request Params
null,
// Attributes
null,
// Cookies
null,
// Files
null,
// Server vars
[
'HTTP_Authorization' => "Bearer {$this->buildToken()}",
]
);

// Run the middleware
$result = $this->runMiddleware(VerifyShopify::class, $newRequest);
$this->assertTrue($result[0]);
}
}

0 comments on commit 37ef397

Please sign in to comment.