Passed
Pull Request — master (#11)
by Simon
01:42
created

SlackAuthControllerTest::testSaveToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\StripeSlack\Test;
4
5
use Firesphere\StripeSlack\Controller\SlackAuthController;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\SiteConfig\SiteConfig;
12
13
/**
14
 * Test various items on the SlackAuthController
15
 *
16
 * Class SlackAuthControllerTest
17
 */
18
class SlackAuthControllerTest extends SapphireTest
19
{
20
    /**
21
     * @var SlackAuthController
22
     */
23
    protected $controller;
24
25
    protected function setUp()
26
    {
27
        $this->controller = Injector::inst()->get(SlackAuthController::class);
28
        parent::setUp();
29
    }
30
31
    protected function tearDown()
32
    {
33
        parent::tearDown();
34
    }
35
36
    public function testGetQuery()
37
    {
38
        $config = SiteConfig::current_site_config();
39
        $result = $this->controller->getQuery($config, '1234567890987654321');
40
        $expectedArray = [
41
            'client_id'     => $config->SlackClientID,
42
            'client_secret' => $config->SlackClientSecret,
43
            'code'          => '1234567890987654321',
44
            'redirect_uri'  => Director::absoluteURL('/SlackAuthorization/'),
45
        ];
46
        $expected = http_build_query($expectedArray);
47
        $this->assertEquals($expected, $result);
48
    }
49
50
    public function testSaveToken()
51
    {
52
        $response = new HTTPResponse('{"access_token":"12345678chdiyp67"}');
53
54
        $config = SiteConfig::current_site_config();
55
56
        $this->controller->saveToken($response, $config);
57
        $config = SiteConfig::current_site_config();
58
        // This seems to not work on tests?? SiteConfig seems to not write
59
        $this->assertEquals('12345678chdiyp67', $config->SlackToken);
60
    }
61
62
    public function testGetConfig()
63
    {
64
        $controller = SlackAuthController::create();
65
        $config = SiteConfig::current_site_config();
66
        $config->SlackURL = 'https://team.slack.com';
67
        $config->write();
68
        $result = $controller->getConfig(new HTTPRequest(
69
            'GET',
70
            'https://team.slack.com/api/something',
71
            ['code' => '1234567890']
72
        ));
73
        $this->assertEquals('1234567890', $result[0]);
74
        $this->assertInstanceOf(SiteConfig::class, $result[1]);
75
        $this->assertEquals($config->SlackURL . '/', $result[2]);
76
        $this->assertEquals('api/oauth.access?', $result[3]);
77
    }
78
}
79