Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Completed unit tests for new flows
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikyt committed Jul 18, 2019
1 parent 490094f commit 6a886c3
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 28 deletions.
34 changes: 34 additions & 0 deletions ISSUE_TEMPLATE.md
@@ -0,0 +1,34 @@
*For bug reporting only! If you're posting a feature request or discussion, please ignore.*

## Expected Behavior

Please describe the behavior you are expecting.

## Current Behavior

Please describe the current behavior?

## Failure Information

Please help provide information about the failure if this is a bug.

### Steps to Reproduce

Please provide detailed steps for reproducing the issue.

1. Step 1
2. Step 2
3. ...

### Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

* Package Version: vX.Y.Z
* Laravel Version: vX.Y.Z
* PHP Version: vX.Y.Z
* Using a toolset (Docker, Laradock, Vagrant, etc.):

### Failure Logs

Please include any relevant log snippets or files here.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (C) 2018 Tyler King <tyler.n.king@gmail.com>
Copyright (C) 2019 Tyler King <tyler@ohmybrew.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -46,6 +46,10 @@ For full resources on this package, see the [wiki](https://github.com/ohmybrew/l

For internal documentation, it is [available here](https://ohmybrew.com/laravel-shopify/) from phpDocumentor.

## Issue or request?

If you have found a bug or would like to request a feature for discussion, please use the `ISSUE_TEMPLATE` in this repo when creating your issue. Any issue submitted without this template will be closed.

## LICENSE

This project is released under the MIT [license](https://github.com/ohmybrew/laravel-shopify/blob/master/LICENSE).
Expand Down
39 changes: 19 additions & 20 deletions src/ShopifyApp/Middleware/AuthShop.php
Expand Up @@ -47,38 +47,37 @@ protected function validateShop(Request $request)
$session = new ShopSession();

// Grab the shop's myshopify domain from query or session
$shopDomain = ShopifyApp::sanitizeShopDomain(
$request->filled('shop') ? $request->get('shop') : $session->getDomain()
);
$shopDomainParam = $request->get('shop');
$shopDomainSession = $session->getDomain();
$shopDomain = ShopifyApp::sanitizeShopDomain($shopDomainParam ?? $shopDomainSession);

// Get the shop based on domain and update the session service
$shop = ShopifyApp::shop($shopDomain);
$session->setShop($shop);

if (
$shop === null ||
$shop->trashed() ||
($shopDomain && $shopDomain !== $shop->shopify_domain) === true
) {
// We need to handle this issue...
return $this->handleBadSession(
AuthShopHandler::FLOW_FULL,
$session,
$request,
$shopDomain
);
$flowType = null;
if ($shop === null || $shop->trashed() || ($shopDomainParam && $shopDomainParam !== $shopDomainSession) === true) {
// We need to do a full flow
$flowType = AuthShopHandler::FLOW_FULL;
} elseif (!$session->isValid()) {
// We need to handle this issue...
// Just a session issue, do a partial flow if we can...
$flowType = $session->isType(ShopSession::GRANT_PERUSER) ?
AuthShopHandler::FLOW_FULL :
AuthShopHandler::FLOW_PARTIAL;
}

if ($flowType !== null) {
// We have a bad session
return $this->handleBadSession(
$session->isType(ShopSession::GRANT_PERUSER) ? AuthShopHandler::FLOW_FULL : AuthShopHandler::FLOW_PARTIAL,
$flowType,
$session,
$request,
$shopDomain
);
} else {
// Everything is fine!
return true;
}

// Everything is fine!
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ShopifyApp/Requests/AuthShop.php
Expand Up @@ -35,7 +35,7 @@ public function withValidator(Validator $validator)
$validator->after(function (Validator $validator) {
$type = $this->request->get('type', AuthShopHandler::FLOW_FULL);
if ($type === AuthShopHandler::FLOW_FULL && !$this->request->has('code')) {
// No code, continue
// No code, continue...
return;
}

Expand Down
9 changes: 6 additions & 3 deletions src/ShopifyApp/Services/AuthShopHandler.php
Expand Up @@ -155,7 +155,8 @@ public function dispatchWebhooks()
{
$webhooks = Config::get('shopify-app.webhooks');
if (count($webhooks) > 0) {
WebhookInstaller::dispatch($this->shop)->onQueue(Config::get('shopify-app.job_queues.webhooks'));
WebhookInstaller::dispatch($this->shop)
->onQueue(Config::get('shopify-app.job_queues.webhooks'));
}
}

Expand All @@ -168,7 +169,8 @@ public function dispatchScripttags()
{
$scripttags = Config::get('shopify-app.scripttags');
if (count($scripttags) > 0) {
ScripttagInstaller::dispatch($this->shop, $scripttags)->onQueue(Config::get('shopify-app.job_queues.scripttags'));
ScripttagInstaller::dispatch($this->shop, $scripttags)
->onQueue(Config::get('shopify-app.job_queues.scripttags'));
}
}

Expand Down Expand Up @@ -196,7 +198,8 @@ public function dispatchAfterAuthenticate()
$job::dispatchNow($this->shop);
} else {
// Run later
$job::dispatch($this->shop)->onQueue(Config::get('shopify-app.job_queues.after_authenticate'));
$job::dispatch($this->shop)
->onQueue(Config::get('shopify-app.job_queues.after_authenticate'));
}

return true;
Expand Down
7 changes: 4 additions & 3 deletions src/ShopifyApp/ShopifyApp.php
Expand Up @@ -154,14 +154,15 @@ public function createHmac(array $opts)
*
* @param string $message The message to send.
*
* @return void
* @return boolean
*/
public function debug(string $message)
{
if (!Config::get('shopify_app.debug')) {
return;
if (!Config::get('shopify-app.debug')) {
return false;
}

Log::debug($message);
return true;
}
}
10 changes: 10 additions & 0 deletions src/ShopifyApp/resources/config/shopify-app.php
@@ -1,6 +1,16 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Debug Mode
|--------------------------------------------------------------------------
|
| (Not yet complete) A verbose logged output of processes
|
*/
'debug' => (bool) env('SHOPIFY_DEBUG', false),

/*
|--------------------------------------------------------------------------
| Manual migrations
Expand Down
17 changes: 17 additions & 0 deletions tests/Middleware/AuthShopMiddlewareTest.php
Expand Up @@ -2,6 +2,7 @@

namespace OhMyBrew\ShopifyApp\Test\Middleware;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
Expand Down Expand Up @@ -82,6 +83,22 @@ public function testShopsWhichDoNotMatchShouldKillSessionAndDirectToReAuthentica
$this->assertEquals('example-different-shop.myshopify.com', Request::get('shop'));
}

public function testGrantTypePerUserWithInvalidSessionShouldDirectToReAuthenticate()
{
// Update config to be per-user
Config::set('shopify-app.api_grant_mode', 'per-user');

// Set a shop
$shop = factory(Shop::class)->create();
Session::put('shopify_domain', $shop->shopify_domain);

// Run the middleware
$result = $this->runAuthShop();

// Assert it was not called and the new shop was passed
$this->assertFalse($result[1]);
}

public function testShouldSaveReturnUrl()
{
// Set a shop
Expand Down
1 change: 1 addition & 0 deletions tests/Services/ShopSessionTest.php
Expand Up @@ -37,6 +37,7 @@ public function testCanSetAccessPerUser()
// Confirm changes
$this->assertTrue(Config::get('session.expire_on_close'));
$this->assertEquals($fixture->associated_user, $ss->getUser());
$this->assertNotNull($ss->hasUser());
$this->assertEquals($fixture->access_token, $ss->getToken());
$this->assertEquals(ShopSession::GRANT_PERUSER, $ss->getType());
$this->assertTrue($ss->isType(ShopSession::GRANT_PERUSER));
Expand Down
9 changes: 9 additions & 0 deletions tests/ShopifyAppTest.php
Expand Up @@ -137,4 +137,13 @@ public function testHmacCreator()
$this->shopifyApp->createHmac(['data' => $data, 'buildQuery' => true])
);
}

public function testDebugger()
{
$this->shopifyApp->debug('test');
$this->assertFalse($this->shopifyApp->debug('test'));

Config::set('shopify-app.debug', true);
$this->assertTrue($this->shopifyApp->debug('test'));
}
}

0 comments on commit 6a886c3

Please sign in to comment.