Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace factor_token;
18
 
19
/**
20
 * Tests for MFA manager class.
21
 *
22
 * @package     factor_token
23
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
24
 * @author      Kevin Pham <kevinpham@catalyst-au.net>
25
 * @copyright   Catalyst IT
26
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
1441 ariadna 28
final class factor_test extends \advanced_testcase {
1 efrain 29
 
30
    /**
31
     * Holds specific requested factor, which is token factor.
32
     *
33
     * @var \factor_token\factor $factor
34
     */
35
    public \factor_token\factor $factor;
36
 
37
    public function setUp(): void {
1441 ariadna 38
        parent::setUp();
1 efrain 39
        $this->resetAfterTest();
40
        $this->factor = new \factor_token\factor('token');
41
    }
42
 
43
    /**
44
     * Test calculating expiry time in general
45
     *
46
     * @covers ::calculate_expiry_time
47
     * @return void
48
     */
11 efrain 49
    public function test_calculate_expiry_time_in_general(): void {
1 efrain 50
        $timestamp = 1642213800; // 1230 UTC.
51
 
52
        set_config('expireovernight', 0, 'factor_token');
53
        $method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
54
 
55
        // Test that non-overnight timestamps are just exactly as configured.
56
        // We don't need to care about 0 or negative ints, they will just make the cookie expire immediately.
57
        $expiry = $method->invoke($this->factor, $timestamp);
58
        $this->assertEquals(DAYSECS, $expiry[1]);
59
 
60
        set_config('expiry', HOURSECS, 'factor_token');
61
        $expiry = $method->invoke($this->factor, $timestamp);
62
        $this->assertGreaterThan(HOURSECS - 30, $expiry[1]);
63
        $this->assertLessThan(HOURSECS + 30, $expiry[1]);
64
 
65
        set_config('expireovernight', 1, 'factor_token');
66
        // Manually calculate the next reset time.
67
        $reset = strtotime('tomorrow 0200', $timestamp);
68
        $resetdelta = $reset - $timestamp;
69
        // Confirm that a timestamp that doesnt reach reset time.
70
        if ($timestamp + HOURSECS < $reset) {
71
            $expiry = $method->invoke($this->factor, $timestamp);
72
            $this->assertGreaterThan(HOURSECS - 30, $expiry[1]);
73
            $this->assertLessThan(HOURSECS + 30, $expiry[1]);
74
        }
75
 
76
        set_config('expiry', 2 * DAYSECS, 'factor_token');
77
        // Now confirm that the returned expiry is less than the absolute amount.
78
        $expiry = $method->invoke($this->factor, $timestamp);
79
        $this->assertGreaterThan(DAYSECS, $expiry[1]);
80
        $this->assertLessThan(2 * DAYSECS, $expiry[1]);
81
        $this->assertGreaterThan($resetdelta + DAYSECS - 30, $expiry[1]);
82
        $this->assertLessThan($resetdelta + DAYSECS + 30, $expiry[1]);
83
    }
84
 
85
    /**
86
     * Everything should end at 2am unless adding the hours lands it between
87
     * 0 <= x < 2am, which in that case it should just expire using the raw
88
     * value, provided it never goes past raw value expiry time, and when it
89
     * needs to be 2am, it's 2am on the following morning.
90
     *
91
     * @covers ::calculate_expiry_time
92
     * @param int $timestamp
93
     * @dataProvider timestamp_provider
94
     */
11 efrain 95
    public function test_calculate_expiry_time_for_overnight_expiry_with_one_day_expiry($timestamp): void {
1 efrain 96
        // Setup configuration.
97
        $method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
98
        set_config('expireovernight', 1, 'factor_token');
99
        set_config('expiry', DAYSECS, 'factor_token');
100
 
101
        // All the results here, should be for 2am the following morning from the timestamp provided.
102
        $expiry = $method->invoke($this->factor, $timestamp);
103
        list($expiresat, $secondstillexpiry) = $expiry;
104
 
105
        // Calculate the expected raw expiry if not considering 'overnight'.
106
        $timezone = \core_date::get_user_timezone_object();
107
        $datetime = new \DateTime();
108
        $datetime->setTimezone($timezone);
109
 
110
        $rawexpiry = $timestamp + DAYSECS;
111
        $datetime->setTimestamp($rawexpiry);
112
        $rawhour = $datetime->format('H');
113
        $rawminute = $datetime->format('m');
114
 
115
        // Sanity check, that the $secondstillexpiry is in the appropriate ranges.
116
        $this->assertGreaterThan(0, $secondstillexpiry);
117
        $this->assertLessThan(DAYSECS + 1, $secondstillexpiry);
118
 
119
        if ($rawhour >= 0 && $rawhour < 2 || $rawhour == 2 && $rawminute == 0) {
120
            // Should just use expiry time, if the hours will land between 0 and 2am.
121
            $this->assertEquals($datetime->getTimestamp(), $expiresat);
122
            // Ensure the $secondstillexpiry is calculated correctly.
123
            $this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
124
        } else {
125
            // Otherwise it should fall on 2am the following day.
126
            $followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
127
            $this->assertEquals($followingdayattwoam, $expiresat);
128
            // Ensure the $secondstillexpiry is calculated correctly.
129
            $this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
130
        }
131
    }
132
 
133
    /**
134
     * Everything should end at 2am unless adding the hours lands it between
135
     * 0 <= x < 2am, which in that case it should just expire using the raw
136
     * value, provided it never goes past raw value expiry time, and when it
137
     * needs to be 2am, it's 2am on the morning after tomorrow.
138
     *
139
     * @covers ::calculate_expiry_time
140
     * @param int $timestamp
141
     * @dataProvider timestamp_provider
142
     */
11 efrain 143
    public function test_calculate_expiry_time_for_overnight_expiry_with_two_day_expiry($timestamp): void {
1 efrain 144
        // Setup configuration.
145
        $method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
146
        set_config('expireovernight', 1, 'factor_token');
147
        set_config('expiry', 2 * DAYSECS, 'factor_token');
148
 
149
        // All the results here, should be for 2am the following morning from the timestamp provided.
150
        $expiry = $method->invoke($this->factor, $timestamp);
151
        list($expiresat, $secondstillexpiry) = $expiry;
152
 
153
        // Calculate the expected raw expiry if not considering 'overnight'.
154
        $timezone = \core_date::get_user_timezone_object();
155
        $datetime = new \DateTime();
156
        $datetime->setTimezone($timezone);
157
 
158
        $rawexpiry = $timestamp + (2 * DAYSECS);
159
        $datetime->setTimestamp($rawexpiry);
160
        $rawhour = $datetime->format('H');
161
        $rawminute = $datetime->format('m');
162
 
163
        // Sanity check, that the $secondstillexpiry is in the appropriate ranges.
164
        $this->assertGreaterThan(0, $secondstillexpiry);
165
        $this->assertLessThan((2 * DAYSECS) + 1, $secondstillexpiry);
166
 
167
        if ($rawhour >= 0 && $rawhour < 2 || $rawhour == 2 && $rawminute == 0) {
168
            // Should just use expiry time, if the hours will land between 0 and 2am.
169
            $this->assertEquals($datetime->getTimestamp(), $expiresat);
170
            // Ensure the $secondstillexpiry is calculated correctly.
171
            $this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
172
        } else {
173
            // Otherwise it should fall on 2am the following day after tomorrow.
174
            $followingdayattwoam = strtotime('tomorrow 0200', $timestamp) + DAYSECS;
175
            $this->assertEquals($followingdayattwoam, $expiresat);
176
            // Ensure the $secondstillexpiry is calculated correctly.
177
            $this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
178
        }
179
 
180
        // Expiry should always be more than one day for an expiry duration of
181
        // more than 1 day, but the overnight check should apply for the
182
        // duration of the final night.
183
        $this->assertGreaterThan(DAYSECS, $secondstillexpiry);
184
    }
185
 
186
    /**
187
     * This should check if the 3am expiry is pushed back to 2am as expected, but everything else appears as expected
188
     *
189
     * @covers ::calculate_expiry_time
190
     * @param int $timestamp
191
     * @dataProvider timestamp_provider
192
     */
11 efrain 193
    public function test_calculate_expiry_time_for_overnight_expiry_with_three_hour_expiry($timestamp): void {
1 efrain 194
        // Setup configuration.
195
        $method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
196
        set_config('expireovernight', 1, 'factor_token');
197
        set_config('expiry', 3 * HOURSECS, 'factor_token');
198
 
199
        // All the results here, should be for 2am the following morning from the timestamp provided.
200
        $expiry = $method->invoke($this->factor, $timestamp);
201
        list($expiresat, $secondstillexpiry) = $expiry;
202
 
203
        // Calculate the expected raw expiry if not considering 'overnight'.
204
        $timezone = \core_date::get_user_timezone_object();
205
        $datetime = new \DateTime();
206
        $datetime->setTimezone($timezone);
207
 
208
        $rawexpiry = $timestamp + (3 * HOURSECS);
209
        $datetime->setTimestamp($rawexpiry);
210
 
211
        // Sanity check, that the $secondstillexpiry is in the appropriate ranges.
212
        $this->assertGreaterThan(0, $secondstillexpiry);
213
        $this->assertLessThan((3 * HOURSECS) + 1, $secondstillexpiry);
214
 
215
        // If the raw timestamp of the expiry, is less than tomorrow at 2am,
216
        // then use the raw expiry time.
217
        $followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
218
        if ($datetime->getTimestamp() < $followingdayattwoam) {
219
            $this->assertEquals($datetime->getTimestamp(), $expiresat);
220
            // Ensure the $secondstillexpiry is calculated correctly.
221
            $this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
222
        } else {
223
            // Otherwsie it should be pushed back to 2am.
224
            $this->assertEquals($followingdayattwoam, $expiresat);
225
            // Ensure the $secondstillexpiry is calculated correctly.
226
            $this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
227
        }
228
    }
229
 
230
    /**
231
     * Only relevant based on the hour padding used, which is currently set to 2 hours (2am).
232
     *
233
     * @covers ::calculate_expiry_time
234
     * @param int $timestamp
235
     * @dataProvider timestamp_provider
236
     */
11 efrain 237
    public function test_calculate_expiry_time_for_overnight_expiry_with_an_hour_expiry($timestamp): void {
1 efrain 238
        // Setup configuration.
239
        $method = new \ReflectionMethod($this->factor, 'calculate_expiry_time');
240
        set_config('expireovernight', 1, 'factor_token');
241
        set_config('expiry', HOURSECS, 'factor_token');
242
 
243
        // All the results here, should be for 2am the following morning from the timestamp provided.
244
        $expiry = $method->invoke($this->factor, $timestamp);
245
        list($expiresat, $secondstillexpiry) = $expiry;
246
 
247
        // Calculate the expected raw expiry if not considering 'overnight'.
248
        $timezone = \core_date::get_user_timezone_object();
249
        $datetime = new \DateTime();
250
        $datetime->setTimezone($timezone);
251
 
252
        $rawexpiry = $timestamp + HOURSECS;
253
        $datetime->setTimestamp($rawexpiry);
254
 
255
        // Sanity check, that the $secondstillexpiry is in the appropriate ranges.
256
        $this->assertGreaterThan(0, $secondstillexpiry);
257
        $this->assertLessThan(HOURSECS + 1, $secondstillexpiry);
258
 
259
        // If the raw timestamp of the expiry, is less than tomorrow at 2am,
260
        // then use the raw expiry time.
261
        $followingdayattwoam = strtotime('tomorrow 0200', $timestamp);
262
        if ($datetime->getTimestamp() < $followingdayattwoam) {
263
            $this->assertEquals($datetime->getTimestamp(), $expiresat);
264
            // Ensure the $secondstillexpiry is calculated correctly.
265
            $this->assertEquals($expiresat - $timestamp, $secondstillexpiry);
266
        } else {
267
            // Otherwsie it should be pushed back to 2am.
268
            $this->assertEquals($followingdayattwoam, $expiresat);
269
            // Ensure the $secondstillexpiry is calculated correctly.
270
            $this->assertEquals($followingdayattwoam - $timestamp, $secondstillexpiry);
271
        }
272
    }
273
 
274
    /**
275
     * Timestamps for a 24 hour period starting from a fixed time.
276
     * Increments by 30 minutes to cover half hour and hour cases.
277
     * Starting timestamp: 2022-01-15 07:30:00 Australia/Melbourne time.
278
     */
1441 ariadna 279
    public static function timestamp_provider(): array {
1 efrain 280
        $starttimestamp = 1642192200;
281
        foreach (range(0, 23) as $i) {
282
            $timestamps[] = [$starttimestamp + ($i * HOURSECS)];
283
            $timestamps[] = [$starttimestamp + ($i * HOURSECS) + (30 * MINSECS)];
284
        }
285
        return $timestamps;
286
    }
287
}