forked from Kyon147/laravel-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthControllerTest.php
82 lines (67 loc) · 2.59 KB
/
AuthControllerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Osiset\ShopifyApp\Test\Traits;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use Osiset\ShopifyApp\Exceptions\MissingShopDomainException;
use Osiset\ShopifyApp\Messaging\Events\ShopAuthenticatedEvent;
use Osiset\ShopifyApp\Test\Stubs\Api as ApiStub;
use Osiset\ShopifyApp\Test\TestCase;
use Osiset\ShopifyApp\Util;
class AuthControllerTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
// Setup API stub
$this->setApiStub();
}
public function testAuthRedirectsToShopifyWhenNoCode(): void
{
Event::fake();
// Run the request
$response = $this->call('post', '/authenticate', ['shop' => 'example.myshopify.com']);
// Check the redirect happens and location is set properly in the header.
$response->assertViewHas('shopDomain', 'example.myshopify.com');
$response->assertViewHas(
'url',
'https://example.myshopify.com/admin/oauth/authorize?client_id='.Util::getShopifyConfig('api_key').'&scope=read_products%2Cwrite_products%2Cread_themes&redirect_uri=https%3A%2F%2Flocalhost%2Fauthenticate'
);
Event::assertDispatched(ShopAuthenticatedEvent::class);
}
public function testAuthAcceptsShopWithCode(): void
{
// Stub the responses
ApiStub::stubResponses(['access_token_grant']);
// HMAC for regular tests
$hmac = '6f16da24e8185e717f22a3373a1928fcaea7ea2401be40ab0d160f5bed7fe55a';
$hmacParams = [
'hmac' => $hmac,
'shop' => 'example.myshopify.com',
'code' => '1234678',
'timestamp' => '1337178173',
];
$response = $this->call('get', '/authenticate', $hmacParams);
$response->assertRedirect();
}
public function testAuthThrowExceptionForBadHmac(): void
{
// Stub the responses
ApiStub::stubResponses(['access_token_grant']);
$hmacParams = [
'hmac' => 'badhmac',
'shop' => 'example.myshopify.com',
'code' => '1234678',
'timestamp' => '1337178173',
];
$response = $this->call('get', '/authenticate', $hmacParams);
$response->assertStatus(Response::HTTP_INTERNAL_SERVER_ERROR);
}
public function testAuthThrowExceptionForMissingShopAndAuthenticatedUser(): void
{
$this->withoutExceptionHandling();
$this->expectException(MissingShopDomainException::class);
// Call authenticate with no parameters
$this->call('get', '/authenticate');
}
}