Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

DbServiceTest::testDeleteSuspiciousAttemptsForIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
/**
3
 * @author Semih Serhat Karakaya <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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 Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
23
namespace OCA\Security\Tests\Db;
24
25
use OCA\Security\Db\DbService;
26
use OC\AppFramework\Utility\TimeFactory;
27
use OCP\IDBConnection;
28
use Test\TestCase;
29
30
/**
31
 * @group DB
32
 */
33
class DbServiceTest extends TestCase {
34
35
    /** @var  DbService */
36
    private $dbService;
37
38
    /** @var  IDBConnection */
39
    private $connection;
40
41
    /** @var  TimeFactory */
42
    private $factory;
43
44
    /** @var string  */
45
    private $dbTable = 'failed_login_attempts';
46
47
    public function setUp() {
48
        parent::setUp();
49
50
        $this->connection = \OC::$server->getDatabaseConnection();
51
        $this->factory = new TimeFactory();
52
        $this->dbService = new DbService($this->connection, $this->factory);
53
54
        $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
55
        $result = $query->execute()->fetchAll();
56
        $this->assertEmpty($result, 'we need to start with a empty failed_login_attempts table');
57
    }
58
59
    public function tearDown() {
60
        parent::tearDown();
61
        $query = $this->connection->getQueryBuilder()->delete($this->dbTable);
62
        $query->execute();
63
    }
64
65
    public function testAddServer() {
66
        $ip = "192.168.1.1";
67
        $uid = "test";
68
        $id = $this->dbService->addFailedLoginAttempt($uid, $ip);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $id is correct as $this->dbService->addFai...LoginAttempt($uid, $ip) (which targets OCA\Security\Db\DbService::addFailedLoginAttempt()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70
        $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
71
        $result = $query->execute()->fetchAll();
72
        $this->assertSame(1, count($result));
73
        $this->assertSame($ip, $result[0]['ip']);
74
        $this->assertSame($uid, $result[0]['uid']);
75
    }
76
77
    public function testGetSuspiciousActivityCountForUid() {
78
        $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
79
        $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
80
        $this->assertEquals(2, $this->dbService->getSuspiciousActivityCountForUid('test1'));
81
    }
82
83
    public function testGetSuspiciousActivityCountForIp() {
84
        $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
85
        $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
86
        $this->assertEquals(2, $this->dbService->getSuspiciousActivityCountForIp('192.168.1.1'));
87
    }
88
89
    public function testDeleteSuspiciousAttemptsForIp() {
90
        $this->dbService->addFailedLoginAttempt("test1", "192.168.1.1");
91
        $this->dbService->addFailedLoginAttempt("test2", "192.168.1.2");
92
93
        $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
94
        $result = $query->execute()->fetchAll();
95
        $this->assertSame(2, count($result));
96
        $this->assertSame('test1', $result[0]['uid']);
97
        $this->assertSame('test2', $result[1]['uid']);
98
        $this->assertSame("192.168.1.1", $result[0]['ip']);
99
        $this->assertSame("192.168.1.2", $result[1]['ip']);
100
101
        $this->dbService->deleteSuspiciousAttemptsForIp("192.168.1.1");
102
        $query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
103
        $result = $query->execute()->fetchAll();
104
        $this->assertSame(1, count($result));
105
        $this->assertSame('test2', $result[0]['uid']);
106
        $this->assertSame("192.168.1.2", $result[0]['ip']);
107
    }
108
}
109