EventsControllerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 98
dl 0
loc 184
rs 10
c 2
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 3 1
B testIndex() 0 168 1
1
<?php
2
3
namespace App\Test\TestCase\Controller;
4
5
use Cake\ORM\TableRegistry;
6
use Cake\TestSuite\IntegrationTestTrait;
7
use Cake\TestSuite\TestCase;
8
9
use function json_encode;
10
11
class EventsControllerTest extends TestCase
12
{
13
    use IntegrationTestTrait;
14
15
    /**
16
     * Fixtures.
17
     *
18
     * @var array
19
     */
20
    public $fixtures = ['app.Reports'];
21
22
    public function setUp(): void
23
    {
24
        $this->Reports = TableRegistry::getTableLocator()->get('Reports');
25
    }
26
27
    public function testIndex(): void
28
    {
29
        /* Test case 1 */
30
        // Invalid User Agent
31
        $this->configRequest([
32
            'headers' => ['User-Agent' => 'Invalid-GitHub-Hookshot-abcdef'],
33
        ]);
34
        $this->post('/events');
35
        $this->assertResponseCode(403);
36
37
        /* Test case 2 */
38
        // Invalid Event Type
39
        $this->configRequest([
40
            'headers' => [
41
                'User-Agent' => 'GitHub-Hookshot-abcdef',
42
                'X-GitHub-Event' => 'anything-except-issues',
43
            ],
44
        ]);
45
        $this->post('/events');
46
        $this->assertResponseCode(400);
47
48
        /* Test case 3 */
49
        // Invalid Hash in headers
50
        $this->configRequest([
51
            'headers' => [
52
                'User-Agent' => 'GitHub-Hookshot-abcdef',
53
                'X-GitHub-Event' => 'issues',
54
                'X-Hub-Signature' => 'sha1=89db05030cf4c1fbcfb4d590deda30fa40a247ed',
55
            ],
56
        ]);
57
        $this->post(
58
            '/events',
59
            json_encode(
60
                [
61
                    'action' => 'closed',
62
                    'issue' => ['number' => 4],
63
                ]
64
            )
65
        );
66
        $this->assertResponseCode(401);
67
68
        /* Test case 4 */
69
        // Invalid issues action
70
        $this->configRequest([
71
            'headers' => [
72
                'User-Agent' => 'GitHub-Hookshot-abcdef',
73
                'X-GitHub-Event' => 'issues',
74
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
75
            ],
76
        ]);
77
        $this->post(
78
            '/events',
79
            json_encode(
80
                [
81
                    'action' => 'anything-invalid',
82
                    'issue' => ['number' => 4],
83
                ]
84
            )
85
        );
86
        $this->assertResponseCode(204);
87
88
        /* Test case 5 */
89
        // Event for an unlinked issue
90
        $this->configRequest([
91
            'headers' => [
92
                'User-Agent' => 'GitHub-Hookshot-abcdef',
93
                'X-GitHub-Event' => 'issues',
94
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
95
            ],
96
        ]);
97
        $this->post(
98
            '/events',
99
            json_encode(
100
                [
101
                    'action' => 'closed',
102
                    'issue' => ['number' => 1234],
103
                ]
104
            )
105
        );
106
        $this->assertResponseCode(204);
107
108
        // Prepare for testcase
109
        $report = $this->Reports->get(4);
110
        $report->status = 'resolved';
111
        $this->Reports->save($report);
112
113
        /* Test case 6 */
114
        // Event 'opened' for a linked issue
115
        $this->configRequest([
116
            'headers' => [
117
                'User-Agent' => 'GitHub-Hookshot-abcdef',
118
                'X-GitHub-Event' => 'issues',
119
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
120
            ],
121
        ]);
122
        $this->post(
123
            '/events',
124
            json_encode(
125
                [
126
                    'action' => 'opened',
127
                    'issue' => ['number' => 4],
128
                ]
129
            )
130
        );
131
        $this->assertResponseCode(201);
132
        $report = $this->Reports->get(4);
133
        $this->assertEquals($report->status, 'forwarded');
134
135
        /* Test case 7 */
136
        // Event 'closed' for a linked issue
137
        $this->configRequest([
138
            'headers' => [
139
                'User-Agent' => 'GitHub-Hookshot-abcdef',
140
                'X-GitHub-Event' => 'issues',
141
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
142
            ],
143
        ]);
144
        $this->post(
145
            '/events',
146
            json_encode(
147
                [
148
                    'action' => 'closed',
149
                    'issue' => ['number' => 4],
150
                ]
151
            )
152
        );
153
        $this->assertResponseCode(201);
154
        $report = $this->Reports->get(4);
155
        $this->assertEquals($report->status, 'resolved');
156
157
        // Prepare for testcase
158
        $report = $this->Reports->get(4);
159
        $report->status = 'resolved';
160
        $this->Reports->save($report);
161
162
        /* Test case 8 */
163
        // Event 'reopened' for a linked issue
164
        $this->configRequest([
165
            'headers' => [
166
                'User-Agent' => 'GitHub-Hookshot-abcdef',
167
                'X-GitHub-Event' => 'issues',
168
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
169
            ],
170
        ]);
171
        $this->post(
172
            '/events',
173
            json_encode(
174
                [
175
                    'action' => 'reopened',
176
                    'issue' => ['number' => 4],
177
                ]
178
            )
179
        );
180
        $this->assertResponseCode(201);
181
        $report = $this->Reports->get(4);
182
        $this->assertEquals($report->status, 'forwarded');
183
184
        /* Test case 9 */
185
        // Event 'ping'
186
        $this->configRequest([
187
            'headers' => [
188
                'User-Agent' => 'GitHub-Hookshot-abcdef',
189
                'X-GitHub-Event' => 'ping',
190
                'X-Hub-Signature' => 'sha1=ddcf5dadbbb716e43da25344989dde547c6c3a03',
191
            ],
192
        ]);
193
        $this->post('/events');
194
        $this->assertResponseCode(200);
195
    }
196
}
197