Passed
Push — master ( 4cf584...63442e )
by Tony
10:02
created

AuthHTTPTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * AuthHTTP.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       https://librenms.org
22
 * @copyright  2017 Adam Bishop
23
 * @author     Adam Bishop <[email protected]>
24
 */
25
26
namespace LibreNMS\Tests;
27
28
use LibreNMS\Authentication\LegacyAuth;
29
use LibreNMS\Config;
30
use LibreNMS\Exceptions\AuthenticationException;
31
32
// Note that as this test set depends on mres(), it is a DBTestCase even though the database is unused
33
class AuthHTTPTest extends DBTestCase
34
{
35
    private $original_auth_mech;
36
    private $server;
37
38
    public function setUp(): void
39
    {
40
        parent::setUp();
41
42
        $this->original_auth_mech = Config::get('auth_mechanism');
43
        Config::set('auth_mechanism', 'http-auth');
44
        $this->server = $_SERVER;
45
    }
46
47
    public function tearDown(): void
48
    {
49
        Config::set('auth_mechanism', $this->original_auth_mech);
50
        $_SERVER = $this->server;
51
        parent::tearDown();
52
    }
53
54
    // Document the modules current behaviour, so that changes trigger test failures
55
    public function testCapabilityFunctions()
56
    {
57
        $a = LegacyAuth::reset();
58
59
        $this->assertTrue($a->canUpdatePasswords() === 0);
60
        $this->assertTrue($a->changePassword(null, null) === 0);
61
        $this->assertTrue($a->canManageUsers() === 1);
62
        $this->assertTrue($a->canUpdateUsers() === 1);
63
        $this->assertTrue($a->authIsExternal() === 1);
64
    }
65
66
    public function testOldBehaviourAgainstCurrent()
67
    {
68
        $old_username = null;
69
        $new_username = null;
70
71
        $users = array('steve',  '   steve', 'steve   ', '   steve   ', '    steve   ', '', 'CAT');
72
        $vars = array('REMOTE_USER', 'PHP_AUTH_USER');
73
74
        $a = LegacyAuth::reset();
75
76
        foreach ($vars as $v) {
77
            foreach ($users as $u) {
78
                $_SERVER[$v] = $u;
79
80
                // Old Behaviour
81
                if (isset($_SERVER['REMOTE_USER'])) {
82
                    $old_username = clean($_SERVER['REMOTE_USER']);
83
                } elseif (isset($_SERVER['PHP_AUTH_USER']) && Config::get('auth_mechanism') === 'http-auth') {
84
                    $old_username = clean($_SERVER['PHP_AUTH_USER']);
85
                }
86
87
                // Current Behaviour
88
                if ($a->authIsExternal()) {
89
                    $new_username = $a->getExternalUsername();
90
                }
91
92
                $this->assertFalse($old_username === null);
93
                $this->assertFalse($new_username === null);
94
95
                $this->assertTrue($old_username === $new_username);
96
            }
97
98
            unset($_SERVER[$v]);
99
        }
100
    }
101
}
102