Kyon147 / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
353 stars 102 forks source link

Created Middleware to Verify Scopes and reauthenticate if required #187

Closed usmanpakistan closed 1 year ago

usmanpakistan commented 1 year ago

This PR adds code to resolve issues reported in #186 #185

Kyon147 commented 1 year ago

@usmanpakistan thanks for submitting the PR. It looks good but codecov is complaining because no unit tests have been set up for the new file.

Can you write a couple test for the middleware please.

usmanpakistan commented 1 year ago

Hi @Kyon147 I did try to create a test case (not good at doing it). However, need some help. Idea is to load scopes from Shopify Config file, remove one of the scopes and then run it through the middleware. The middleware will match the installed scope with the scope in the Shopify config file. However, I cannot figure out how to emulate the Shopify API calls. Can you help me out here?

<?php

namespace Osiset\ShopifyApp\Test\Http\Middleware;

use Illuminate\Auth\AuthManager;
use Osiset\ShopifyApp\Test\TestCase;
use Osiset\ShopifyApp\Util;

class VerifyScopesTest extends TestCase{
  /**
     * @var AuthManager
     */
    protected $auth;

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

        $this->auth = $this->app->make(AuthManager::class);
    }
     public function testMissingScopes(): void
    {
        $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);

        // Run the middleware
        $result = $this->runMiddleware(BillableMiddleware::class);
       $this->assertStatus(302);
     }
}
Kyon147 commented 1 year ago

No worries @usmanpakistan if you push up the test I'll take a look and edit it 👍

usmanpakistan commented 1 year ago

@Kyon147 Test Case added

RishabhTayal commented 1 year ago

Any update on the PR?

Kyon147 commented 1 year ago

Any update on the PR?

It's on my list to look at next - will see if I can squeeze this in at the weekend