Passed
Push — devel-3.0 ( 09ea81...3c7891 )
by Rubén
03:32
created

KeepassImportTest::checkImportedData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 88
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 58
nc 1
nop 0
dl 0
loc 88
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Tests\Services\Import;
26
27
use DI\Container;
28
use SP\Core\Crypt\Crypt;
29
use SP\Services\Account\AccountSearchFilter;
30
use SP\Services\Account\AccountService;
31
use SP\Services\Category\CategoryService;
32
use SP\Services\Client\ClientService;
33
use SP\Services\Import\FileImport;
34
use SP\Services\Import\ImportException;
35
use SP\Services\Import\ImportParams;
36
use SP\Services\Import\KeepassImport;
37
use SP\Services\Import\XmlFileImport;
38
use SP\Storage\Database\DatabaseConnectionData;
39
use SP\Tests\DatabaseTestCase;
40
use function SP\Tests\setupContext;
41
42
/**
43
 * Class KeepassImportTest
44
 *
45
 * @package SP\Tests\Services\Import
46
 */
47
class KeepassImportTest extends DatabaseTestCase
48
{
49
    /**
50
     * @var Container
51
     */
52
    protected static $dic;
53
54
    /**
55
     * @throws \DI\NotFoundException
56
     * @throws \SP\Core\Context\ContextException
57
     * @throws \DI\DependencyException
58
     */
59
    public static function setUpBeforeClass()
60
    {
61
        self::$dic = setupContext();
62
63
        self::$dataset = 'syspass_import.xml';
64
65
        // Datos de conexión a la BBDD
66
        self::$databaseConnectionData = self::$dic->get(DatabaseConnectionData::class);
67
    }
68
69
    /**
70
     * @throws ImportException
71
     * @throws \DI\DependencyException
72
     * @throws \DI\NotFoundException
73
     * @throws \Defuse\Crypto\Exception\CryptoException
74
     * @throws \SP\Core\Exceptions\ConstraintException
75
     * @throws \SP\Core\Exceptions\QueryException
76
     * @throws \SP\Core\Exceptions\SPException
77
     * @throws \SP\Repositories\NoSuchItemException
78
     * @throws \SP\Storage\File\FileException
79
     */
80
    public function testDoImport()
81
    {
82
        $file = RESOURCE_DIR . DIRECTORY_SEPARATOR . 'import' . DIRECTORY_SEPARATOR . 'data_keepass.xml';
83
84
        $params = new ImportParams();
85
        $params->setDefaultUser(1);
86
        $params->setDefaultGroup(2);
87
88
        $import = new KeepassImport(self::$dic, new XmlFileImport(FileImport::fromFilesystem($file)), $params);
89
        $import->doImport();
90
91
        $this->checkImportedData();
92
    }
93
94
    /**
95
     * @throws \DI\DependencyException
96
     * @throws \DI\NotFoundException
97
     * @throws \Defuse\Crypto\Exception\CryptoException
98
     * @throws \SP\Core\Exceptions\ConstraintException
99
     * @throws \SP\Core\Exceptions\QueryException
100
     * @throws \SP\Repositories\NoSuchItemException
101
     * @throws \SP\Core\Exceptions\SPException
102
     */
103
    private function checkImportedData()
104
    {
105
        // Checkout categories
106
        $categoryService = self::$dic->get(CategoryService::class);
107
108
        $this->assertEquals('Linux', $categoryService->getByName('Linux')->getName());
109
        $this->assertEquals('Windows', $categoryService->getByName('Windows')->getName());
110
        $this->assertEquals('Servers', $categoryService->getByName('Servers')->getName());
111
        $this->assertEquals('General', $categoryService->getByName('General')->getName());
112
113
        $this->assertEquals(11, $this->conn->getRowCount('Category'));
114
115
        // Checkout clients
116
        $client = self::$dic->get(ClientService::class)->getByName('KeePass');
117
118
        $this->assertEquals('KeePass', $client->getName());
119
120
        $this->assertEquals(4, $this->conn->getRowCount('Client'));
121
122
        // Checkout accounts
123
        $accountService = self::$dic->get(AccountService::class);
124
125
        // 1st account
126
        $filter = new AccountSearchFilter();
127
        $filter->setClientId($client->getId());
128
129
        $data = $accountService->getByFilter($filter);
130
131
        $this->assertCount(5, $data);
132
133
        $this->assertEquals(3, $data[0]->getId());
134
        $this->assertEquals(1, $data[0]->getUserId());
135
        $this->assertEquals(2, $data[0]->getUserGroupId());
136
        $this->assertEquals('DC1', $data[0]->getName());
137
        $this->assertEquals('KeePass', $data[0]->getClientName());
138
        $this->assertEquals('Windows', $data[0]->getCategoryName());
139
        $this->assertEquals('192.168.100.1', $data[0]->getUrl());
140
        $this->assertEquals('ADS server', $data[0]->getNotes());
141
        $this->assertEquals('administrator', $data[0]->getLogin());
142
143
        $pass = $accountService->getPasswordForId($data[0]->getId());
144
145
        $this->assertEquals('k6V4iIAeR9SBOprLMUGV', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
146
147
        // 2nd account
148
149
        $this->assertEquals(4, $data[1]->getId());
150
        $this->assertEquals(1, $data[1]->getUserId());
151
        $this->assertEquals(2, $data[1]->getUserGroupId());
152
        $this->assertEquals('debian', $data[1]->getName());
153
        $this->assertEquals('KeePass', $data[1]->getClientName());
154
        $this->assertEquals('Linux', $data[1]->getCategoryName());
155
        $this->assertEquals('http://debian.org', $data[1]->getUrl());
156
        $this->assertEquals("Some notes about the server", $data[1]->getNotes());
157
        $this->assertEquals('root', $data[1]->getLogin());
158
159
        $pass = $accountService->getPasswordForId($data[1]->getId());
160
161
        $this->assertEquals('TKr321zqCZhgbzmmAX13', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
162
163
        // 3rd account
164
        $this->assertEquals(5, $data[2]->getId());
165
        $this->assertEquals(1, $data[2]->getUserId());
166
        $this->assertEquals(2, $data[2]->getUserGroupId());
167
        $this->assertEquals('proxy', $data[2]->getName());
168
        $this->assertEquals('KeePass', $data[2]->getClientName());
169
        $this->assertEquals('Linux', $data[2]->getCategoryName());
170
        $this->assertEquals('192.168.0.1', $data[2]->getUrl());
171
        $this->assertEquals('Some notes about proxy server', $data[2]->getNotes());
172
        $this->assertEquals('admin', $data[2]->getLogin());
173
174
        $pass = $accountService->getPasswordForId($data[2]->getId());
175
176
        $this->assertEquals('TKr321zqCZhgbzmmAX13', Crypt::decrypt($pass->getPass(), $pass->getKey(), '12345678900'));
177
178
        $this->assertEquals(6, $data[3]->getId());
179
        $this->assertEquals(1, $data[3]->getUserId());
180
        $this->assertEquals(2, $data[3]->getUserGroupId());
181
        $this->assertEquals('Sample Entry', $data[3]->getName());
182
        $this->assertEquals('NewDatabase', $data[3]->getCategoryName());
183
184
        $this->assertEquals(7, $data[4]->getId());
185
        $this->assertEquals(1, $data[4]->getUserId());
186
        $this->assertEquals(2, $data[4]->getUserGroupId());
187
        $this->assertEquals('Sample Entry #2', $data[4]->getName());
188
        $this->assertEquals('NewDatabase', $data[4]->getCategoryName());
189
190
        $this->assertEquals(7, $this->conn->getRowCount('Account'));
191
    }
192
}
193