WriteJsConnectTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 86
c 1
b 0
f 0
dl 0
loc 149
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B provideWriteJsConnectTests() 0 119 2
A testWriteJsConnect() 0 10 1
1
<?php
2
/**
3
 * @author Alexandre (DaazKu) Chouinard <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license GNU GPLv2 http://www.opensource.org/licenses/gpl-2.0.php
6
 */
7
8
namespace JsConnect\Tests;
9
10
/**
11
 * Unit tests signJsConnect
12
 */
13
class WriteJsConnectTest extends \PHPUnit\Framework\TestCase {
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
    /**
16
     * @param $user
17
     * @param $request
18
     * @param $clientID
19
     * @param $secret
20
     * @param $secure
21
     * @param $expectedResult
22
     *
23
     * @dataProvider provideWriteJsConnectTests
24
     */
25
    public function testWriteJsConnect($user, $request, $clientID, $secret, $secure, $expectedResult) {
26
        ob_start();
27
            writeJsConnect($user, $request, $clientID, $secret, $secure);
28
            $result = ob_get_contents();
29
        ob_end_clean();
30
31
        $result = json_decode($result, true);
32
        ksort($result);
33
34
        $this->assertEquals($result, $expectedResult);
35
    }
36
37
38
    /**
39
     * Provide signature to sign
40
     *
41
     * @return array Returns a test array.
42
     */
43
    public function provideWriteJsConnectTests() {
44
45
        $clientID = 'clientID';
46
        $timestamp = time();
47
        $secret = 'secret';
48
        $ip = '127.0.0.1';
49
        $secure = 'sha256';
50
        $nonce = 'nonceToken';
51
        $sig = jsHash($ip.$nonce.$timestamp.$secret, $secure);
52
        $version = '2';
53
54
        $userData = array(
55
            'name' => 'John PHP',
56
            'email' => '[email protected]',
57
            'unique_id' => '123',
58
        );
59
60
        $fnGenerateTestData = function() use (&$userData, &$clientID, &$timestamp, &$secret, &$ip, &$secure, &$nonce, &$sig, &$version) {
61
            $request = array(
62
                'client_id' => $clientID,
63
                'ip' => $ip,
64
                'nonce' => $nonce,
65
                'sig' => $sig,
66
                'timestamp' => $timestamp,
67
                'v' => $version,
68
            );
69
70
            $expectedResult = array(
71
                'sig' => signJsConnect(
72
                    $userData + ['ip' => $ip, 'nonce' => $nonce],
73
                    $clientID,
74
                    $secret,
75
                    $secure
76
                )
77
            ) + $request + $userData;
78
            unset($expectedResult['timestamp']);
79
            ksort($expectedResult);
80
81
            return [
82
                'userData' => $userData,
83
                'request' => $request,
84
                'clientID' => $clientID,
85
                'secret' => $secret,
86
                'secure' => $secure,
87
                'expectedResult' => $expectedResult,
88
            ];
89
        };
90
91
        $fnGenerateAlteratedData = function(&$var, $value, $expectedResult) use ($fnGenerateTestData) {
92
            $tmpVar = $var;
93
            $var = $value;
94
            $data = $fnGenerateTestData();
95
            $var = $tmpVar;
96
            $data['expectedResult'] = $expectedResult;
97
            return $data;
98
        };
99
100
        $data = [];
101
        // Default
102
        $data['default'] = $fnGenerateTestData();
103
104
        // Wrong version
105
        $data['wrongVersion'] = $fnGenerateAlteratedData(
106
            $version, 1, array('error' => 'invalid_request', 'message' => "Unsupported version 1.")
107
        );
108
109
        // Missings
110
        $missings = array('v', 'client_id', 'sig', 'nonce', 'ip');
111
        foreach ($missings as $missing) {
112
            $tmp = $fnGenerateTestData();
113
            unset($tmp['request'][$missing]);
114
            $tmp['expectedResult'] = array('error' => 'invalid_request', 'message' => "Missing the $missing parameter.");
115
            $data["missing[$missing]"] = $tmp;
116
        }
117
118
        // Missing timestamp
119
        $tmp = $fnGenerateTestData();
120
        unset($tmp['request']['timestamp']);
121
        $tmp['expectedResult'] = array('error' => 'invalid_request', 'message' => 'The timestamp parameter is missing or invalid.');
122
        $data['missing[timestamp]'] = $tmp;
123
124
        // Non numeric timestamp
125
        $data['invalidTimestamp'] = $fnGenerateAlteratedData(
126
            $timestamp, 'notatimestamp', array('error' => 'invalid_request', 'message' => 'The timestamp parameter is missing or invalid.')
127
        );
128
129
        // Timed Out timestamp
130
        $data['timedOutTimestamp'] = $fnGenerateAlteratedData(
131
            $timestamp, ($timestamp - (JS_TIMEOUT + 1)), array('error' => 'invalid_request', 'message' => 'The timestamp is invalid.')
132
        );
133
134
        // Bad client_id
135
        $tmp = $fnGenerateTestData();
136
        $tmp['request']['client_id'] = 'wrong'.$clientID;
137
        $tmp['expectedResult'] = array('error' => 'invalid_client', 'message' => "Unknown client {$tmp['request']['client_id']}.");
138
        $data['wrongClientID'] = $tmp;
139
140
        // No sig, no timestamp sent with user logged
141
        $tmp = $fnGenerateTestData();
142
        unset($tmp['request']['sig'], $tmp['request']['timestamp']);
143
        $tmp['expectedResult'] = array('name' => 'John PHP', 'photourl' => null, 'signedin' => true);
144
        $data['noSigNoTimestamp'] = $tmp;
145
146
        // No sig, no timestamp sent with user not logged
147
        $tmp = $fnGenerateTestData();
148
        $tmp['userData'] = [];
149
        unset($tmp['request']['sig'], $tmp['request']['timestamp']);
150
        $tmp['expectedResult'] = array('name' => '', 'photourl' => '');
151
        $data['noSigNoTimestamp'] = $tmp;
152
153
        // Bad signature
154
        $data['badSignature'] = $fnGenerateAlteratedData(
155
            $ip, '255.255.255.255', array('error' => 'access_denied', 'message' => 'Signature invalid.')
156
        );
157
158
        // Secure disabled
159
        $data['timedOutTimestamp'] = $fnGenerateAlteratedData($secure, null, $userData);
160
161
        return $data;
162
    }
163
}
164