Skip to content

Commit

Permalink
Update the VerifyScopes.php and VerifyScopesTest.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyon147 committed Aug 24, 2023
1 parent a1f8a2d commit 1abe73d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/Http/Middleware/VerifyScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public function handle(Request $request, Closure $next)
/** @var $shop IShopModel */
$shop = auth()->user();
$scopesResponse = $shop->api()->rest('GET', '/admin/oauth/access_scopes.json');
if ($scopesResponse['errors']) {
if ($scopesResponse && $scopesResponse['errors']) {
return $next($request);
}
$scopes = json_decode(json_encode($scopesResponse['body']['access_scopes']));
$scopes = array_map(function ($scope) {
$scopes = json_decode(json_encode($scopesResponse['body']['access_scopes']), false);
$scopes = array_map(static function ($scope) {
return $scope->handle;
}, $scopes);

$requiredScopes = explode(',', env('SHOPIFY_API_SCOPES'));
$requiredScopes = explode(',', Util::getShopifyConfig('api_scopes'));
$missingScopes = array_diff($requiredScopes, $scopes);
if (count($missingScopes) == 0) {
if (count($missingScopes) === 0) {
return $next($request);
}

Expand Down
25 changes: 17 additions & 8 deletions tests/Http/Middleware/VerifyScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Osiset\ShopifyApp\Test\Http\Middleware;

use Illuminate\Auth\AuthManager;
use Illuminate\Http\Request;
use Osiset\ShopifyApp\Http\Middleware\VerifyScopes as VerifyScopesMiddleware;
use Osiset\ShopifyApp\Test\Stubs\Api as ApiStub;
use Osiset\ShopifyApp\Test\TestCase;

class VerifyScopesTest extends TestCase
Expand All @@ -18,19 +20,26 @@ public function setUp(): void
parent::setUp();
$this->auth = $this->app->make(AuthManager::class);
}

public function testMissingScopes(): void
{
// Setup API stub
$this->setApiStub();
ApiStub::stubResponses(['access_scopes']);

$this->app['config']->set('shopify-app.api_scopes', 'read_products,write_products,read_orders');

$shop = factory($this->model)->create();
$this->auth->login($shop);
// Get current Scopes
$scopes = $this->app['config']->get('shopify-app.access_scopes');
$scopes = explode(',', $scopes);
unset($scopes[0]);
$newScopes = implode(',', $scopes);
$this->app['config']->set('shopify-app.access_scopes', $newScopes);

$request = Request::create('/', 'GET', ['shop' => $shop->getDomain()->toNative()]);

// Run the middleware
$result = $this->runMiddleware(VerifyScopesMiddleware::class);
$middleware = new VerifyScopesMiddleware();
$result = $middleware->handle($request, function () {
});

//this line needs to assert if proper redirect was made
$this->assertTrue($result[0]);
$this->assertEquals(302, $result->getStatusCode());
}
}
10 changes: 10 additions & 0 deletions tests/fixtures/access_scopes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"access_scopes": [
{
"handle": "read_products"
},
{
"handle": "write_products"
}
]
}

0 comments on commit 1abe73d

Please sign in to comment.