DevelopersControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
dl 0
loc 135
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testLogout() 0 7 1
A setUp() 0 3 1
A testCallback() 0 87 1
A testLogin() 0 8 1
1
<?php
2
3
/**
4
 * Tests for Developers Controller
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Test\TestCase\Controller;
20
21
use Cake\ORM\TableRegistry;
22
use Cake\TestSuite\IntegrationTestTrait;
23
use Cake\TestSuite\TestCase;
24
use phpmock\phpunit\PHPMock;
25
26
use function file_get_contents;
27
use function json_encode;
28
29
use const DS;
30
31
/**
32
 * App\Controller\DevelopersController Test Case
33
 */
34
class DevelopersControllerTest extends TestCase
35
{
36
    use PHPMock;
37
    use IntegrationTestTrait;
38
39
    /**
40
     * Fixtures.
41
     *
42
     * @var array
43
     */
44
    public $fixtures = [
45
        'app.Developers',
46
        'app.Notifications',
47
    ];
48
49
    public function setUp(): void
50
    {
51
        $this->Developers = TableRegistry::getTableLocator()->get('Developers');
52
    }
53
54
    /**
55
     * Test login method
56
     */
57
    public function testLogin(): void
58
    {
59
        // Empty session during initiation
60
        $this->session([]);
61
62
        $this->get('developers/login');
63
        $this->assertRedirectContains('https://github.com/login/oauth/authorize');
64
        $this->assertRedirectContains('developers%2Fcallback');
65
    }
66
67
    /**
68
     * Test callback method
69
     */
70
    public function testCallback(): void
71
    {
72
        // Mock functions `curl_exec` and `curl_getinfo` in GithubApiComponent
73
        // so that we don't actually hit the Github Api
74
        $curlExecMock = $this->getFunctionMock('\App\Controller\Component', 'curl_exec');
75
        $curlGetInfoMock = $this->getFunctionMock('\App\Controller\Component', 'curl_getinfo');
76
77
        $accessTokenResponse = json_encode(['access_token' => 'abc']);
78
        $emptyAccessTokenResponse = json_encode(['access_token' => null]);
79
80
        $nonSuccessUserResponse = json_encode(['message' => 'Unauthorized access']);
81
        $userResponse = file_get_contents(TESTS . 'Fixture' . DS . 'user_response.json');
82
83
        // Github unsuccessful response followed by successful response
84
        $curlExecMock->expects($this->exactly(10))->willReturnOnConsecutiveCalls(
85
            $emptyAccessTokenResponse,
86
            $emptyAccessTokenResponse,
87
            $accessTokenResponse,
88
            $nonSuccessUserResponse,
89
            $accessTokenResponse,
90
            $userResponse,
91
            null,
92
            $accessTokenResponse,
93
            $userResponse,
94
            null
95
        );
96
        $curlGetInfoMock->expects($this->exactly(5))->willReturnOnConsecutiveCalls(
97
            401,
98
            200,
99
            404,
100
            200,
101
            204
102
        );
103
104
        // Case 1.1 Test no access_token in Github response (with last_page not set in session)
105
        // So, empty the session
106
        $this->session([]);
107
108
        $this->get('developers/callback/?code=123123123');
109
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
110
111
        // Case 1.2 Test no access_token in Github response (with last_page set in session)
112
        $this->session(
113
            [
114
                'last_page' => [
115
                    'controller' => 'notifications',
116
                    'action' => 'index',
117
                ],
118
            ]
119
        );
120
121
        $this->get('developers/callback/?code=123123123');
122
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
123
124
        // Case 2. Non successful response code from Github
125
        $this->session(
126
            [
127
                'last_page' => [
128
                    'controller' => 'reports',
129
                    'action' => 'index',
130
                ],
131
            ]
132
        );
133
        $this->get('developers/callback/?code=123123123');
134
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
135
136
        // Case 3. Successful response code (new user), check whether session variables are init
137
        $this->get('developers/callback/?code=123123123');
138
        $this->assertSession(3, 'Developer.id');
139
        $this->assertSession(true, 'read_only');
140
        $this->assertSession('abc', 'access_token');
141
142
        $developer = $this->Developers->get(3);
143
        $this->assertEquals('abc', $developer->access_token);
144
        $this->assertEquals('[email protected]', $developer->email);
145
146
        // Case 4. Successful response code (returning user)
147
        // check whether session variables are init
148
        $this->session(['last_page' => null]);
149
150
        $this->get('developers/callback/?code=123123123');
151
        $this->assertSession(3, 'Developer.id');
152
        $this->assertSession(false, 'read_only');
153
        $this->assertSession('abc', 'access_token');
154
155
        $developer = $this->Developers->get(3);
156
        $this->assertEquals(1, $developer->has_commit_access);
157
    }
158
159
    /**
160
     * Test logout method
161
     */
162
    public function testLogout(): void
163
    {
164
        $this->session(['Developer.id' => 1]);
165
166
        $this->get('developers/logout');
167
        $this->assertSession(null, 'Developer.id');
168
        $this->assertRedirect(['controller' => '', 'action' => 'index']);
169
    }
170
}
171