UniLoginUtilTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 76
c 3
b 1
f 0
dl 0
loc 202
rs 10
wmc 12

12 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 4 1
A testValidateUrlFingerprint() 0 6 1
A setUp() 0 8 1
A testGetTimestamp() 0 6 1
A testCalculateUrlFingerprint() 0 6 1
A testDecodeUrl() 0 6 1
A testParseTimestamp() 0 6 1
A testHmac() 0 15 1
A testGetProviderUrl() 0 5 1
A testValidateFingerprint() 0 36 1
A testEncodeUrl() 0 6 1
A testCalculateFingerprint() 0 7 1
1
<?php
2
App::uses('UniLoginUtil', 'UniLogin.Lib');
3
4
/**
5
 * UniLoginUtilTest class.
6
 *
7
 */
8
class UniLoginUtilTest extends CakeTestCase {
0 ignored issues
show
Bug introduced by
The type CakeTestCase 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...
9
10
/**
11
 * Contains the original plugin configuration.
12
 *
13
 * @var array|null
14
 */
15
	protected $_restore = null;
16
17
/**
18
 * setUp method.
19
 *
20
 * @return void
21
 */
22
	public function setUp() {
23
		parent::setUp();
24
25
		$this->_restore = Configure::read('UniLogin');
0 ignored issues
show
Bug introduced by
The type Configure 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...
26
27
		Configure::write('UniLogin.provider.secret', 'abc123');
28
		Configure::write('UniLogin.provider.url', 'https://sso.emu.dk/unilogin/login.cgi?id=%s');
29
		Configure::write('UniLogin.provider.applicationId', 'myId');
30
	}
31
32
/**
33
 * tearDown method.
34
 *
35
 * @return void
36
 */
37
	public function tearDown() {
38
		parent::tearDown();
39
40
		Configure::write('UniLogin', $this->_restore);
41
	}
42
43
/**
44
 * Tests `calculateFingerprint`.
45
 *
46
 * @return void
47
 */
48
	public function testCalculateFingerprint() {
49
		$timestamp = '20030505125952';
50
		$user = 'testuser';
51
		$expected = '5e55280df202c8820a7092746b991088';
52
		$actual = UniLoginUtil::calculateFingerprint($timestamp, $user);
53
54
		$this->assertEquals($expected, $actual);
55
	}
56
57
/**
58
 * Tests `getFormattedTimestamp`.
59
 *
60
 * @return void
61
 */
62
	public function testGetTimestamp() {
63
		$timestamp = 1052139592;
64
		$expected = '20030505125952';
65
		$actual = UniLoginUtil::getFormattedTimestamp($timestamp);
66
67
		$this->assertEquals($expected, $actual);
68
	}
69
70
/**
71
 * Tests `parseFormattedTimestamp`.
72
 *
73
 * @return void
74
 */
75
	public function testParseTimestamp() {
76
		$timestamp = '20030505125952';
77
		$expected = 1052139592;
78
		$actual = UniLoginUtil::parseFormattedTimestamp($timestamp);
79
80
		$this->assertEquals($expected, $actual);
81
	}
82
83
/**
84
 * Tests `validateFingerprint`.
85
 *
86
 * @return void
87
 */
88
	public function testValidateFingerprint() {
89
		// Good timestamp
90
		$timestamp = time();
91
		$formattedTimestamp = UniLoginUtil::getFormattedTimestamp($timestamp);
92
		$user = 'testuser';
93
		$fingerprint = UniLoginUtil::calculateFingerprint($formattedTimestamp, $user);
94
		$actual = UniLoginUtil::validateFingerprint($formattedTimestamp, $user, $fingerprint);
95
96
		$this->assertTrue($actual);
97
98
		// Timestamp in the future
99
		$timestamp = strtotime('+5 minutes');
100
		$formattedTimestamp = UniLoginUtil::getFormattedTimestamp($timestamp);
101
		$user = 'testuser';
102
		$fingerprint = UniLoginUtil::calculateFingerprint($formattedTimestamp, $user);
103
		$actual = UniLoginUtil::validateFingerprint($formattedTimestamp, $user, $fingerprint);
104
105
		$this->assertFalse($actual);
106
107
		// Timestamp in the past within 1 minute
108
		$timestamp = strtotime('-30 seconds');
109
		$formattedTimestamp = UniLoginUtil::getFormattedTimestamp($timestamp);
110
		$user = 'testuser';
111
		$fingerprint = UniLoginUtil::calculateFingerprint($formattedTimestamp, $user);
112
		$actual = UniLoginUtil::validateFingerprint($formattedTimestamp, $user, $fingerprint);
113
114
		$this->assertTrue($actual);
115
116
		// Timestamp in the past more than 1 minute ago
117
		$timestamp = strtotime('-2 minutes');
118
		$formattedTimestamp = UniLoginUtil::getFormattedTimestamp($timestamp);
119
		$user = 'testuser';
120
		$fingerprint = UniLoginUtil::calculateFingerprint($formattedTimestamp, $user);
121
		$actual = UniLoginUtil::validateFingerprint($formattedTimestamp, $user, $fingerprint);
122
123
		$this->assertFalse($actual);
124
	}
125
126
/**
127
 * Tests `getProviderUrl`.
128
 *
129
 * @return void
130
 */
131
	public function testGetProviderUrl() {
132
		$expected = 'https://sso.emu.dk/unilogin/login.cgi?id=myId';
133
		$actual = UniLoginUtil::getProviderUrl();
134
135
		$this->assertEquals($expected, $actual);
136
	}
137
138
/**
139
 * Tests `calculateUrlFingerprint`.
140
 *
141
 * @return void
142
 */
143
	public function testCalculateUrlFingerprint() {
144
		$url = 'http://www.emu.dk/appl';
145
		$expected = '59169cb39fab40cb0ad6ade6a6eb491e';
146
		$actual = UniLoginUtil::calculateUrlFingerprint($url);
147
148
		$this->assertEquals($expected, $actual);
149
	}
150
151
/**
152
 * Tests `decodeUrl`.
153
 *
154
 * @return void
155
 */
156
	public function testDecodeUrl() {
157
		$url = 'aHR0cDovL3d3dy5lbXUuZGsvYXBwbA%3D%3D';
158
		$expected = 'http://www.emu.dk/appl';
159
		$actual = UniLoginUtil::decodeUrl($url);
160
161
		$this->assertEquals($expected, $actual);
162
	}
163
164
/**
165
 * Tests `encodeUrl`.
166
 *
167
 * @return void
168
 */
169
	public function testEncodeUrl() {
170
		$url = 'http://www.emu.dk/appl';
171
		$expected = 'aHR0cDovL3d3dy5lbXUuZGsvYXBwbA==';
172
		$actual = UniLoginUtil::encodeUrl($url);
173
174
		$this->assertEquals($expected, $actual);
175
	}
176
177
/**
178
 * Tests `validateUrlFingerprint`.
179
 *
180
 * @return void
181
 */
182
	public function testValidateUrlFingerprint() {
183
		$url = 'http://www.emu.dk/appl';
184
		$fingerprint = '59169cb39fab40cb0ad6ade6a6eb491e';
185
		$actual = UniLoginUtil::validateUrlFingerprint($url, $fingerprint);
186
187
		$this->assertTrue($actual);
188
	}
189
190
/**
191
 * Tests `hmac`.
192
 *
193
 * @return void
194
 */
195
	public function testHmac() {
196
		$timestamp = strtotime('-2 minutes');
197
		$formattedTimestamp = UniLoginUtil::getFormattedTimestamp($timestamp);
0 ignored issues
show
Unused Code introduced by
The assignment to $formattedTimestamp is dead and can be removed.
Loading history...
198
		$user = 'testuser';
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
199
200
		$data = [
201
			'user' => 'testuser',
202
			'timestamp' => 1535028701,
203
			'auth' => '28241D17887D43328077A8046F210491',
204
			'validated' => 1,
205
		];
206
		$expected = 'effb812bb240a7ef7b8741cda9894529f84bc365e79ebe00eb0f255b97b36646';
207
		$actual = UniLoginUtil::hmac($data);
208
209
		$this->assertEquals($expected, $actual);
210
	}
211
}
212