GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TotpTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A testTooManyReplays() 0 11 3
1
<?php
2
3
/**
4
 * eduVPN - End-user friendly VPN.
5
 *
6
 * Copyright: 2016-2017, The Commons Conservancy eduVPN Programme
7
 * SPDX-License-Identifier: AGPL-3.0+
8
 */
9
10
namespace SURFnet\VPN\Server\Tests;
11
12
use DateTime;
13
use PDO;
14
use PHPUnit_Framework_TestCase;
15
use SURFnet\VPN\Server\Exception\TotpException;
16
use SURFnet\VPN\Server\Storage;
17
use SURFnet\VPN\Server\Totp;
18
19
class TotpTest extends PHPUnit_Framework_TestCase
20
{
21
    /** @var Totp */
22
    private $totp;
23
24
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        $storage = new Storage(
27
            new PDO(
28
                $GLOBALS['DB_DSN'],
29
                $GLOBALS['DB_USER'],
30
                $GLOBALS['DB_PASSWD']
31
            ),
32
            new DateTime()
33
        );
34
        $storage->init();
35
        $storage->setTotpSecret('foo', 'CN2XAL23SIFTDFXZ');
36
37
        $this->totp = new Totp($storage);
38
    }
39
40
    /**
41
     * @expectedException \SURFnet\VPN\Server\Exception\TotpException
42
     * @expectedExceptionMessage too many attempts at TOTP
43
     */
44
    public function testTooManyReplays()
45
    {
46
        for ($i = 0; $i < 10; ++$i) {
47
            try {
48
                $this->totp->verify('foo', (string) 123456 + $i);
49
            } catch (TotpException $e) {
50
                $this->assertSame('invalid TOTP key', $e->getMessage());
51
            }
52
        }
53
        $this->totp->verify('foo', '555555');
54
    }
55
}
56