|
21 | 21 |
|
22 | 22 | namespace Test\Security; |
23 | 23 |
|
24 | | -use OC\DB\ConnectionAdapter; |
25 | | -use OC\Security\CredentialsManager; |
26 | | -use OC\SystemConfig; |
27 | | -use OCP\IDBConnection; |
28 | | -use OCP\ILogger; |
29 | | -use OCP\Security\ICrypto; |
30 | | - |
31 | 24 | /** |
32 | 25 | * @group DB |
33 | 26 | */ |
34 | 27 | class CredentialsManagerTest extends \Test\TestCase { |
35 | 28 |
|
36 | | - /** @var ICrypto */ |
37 | | - protected $crypto; |
38 | | - |
39 | | - /** @var IDBConnection */ |
40 | | - protected $dbConnection; |
41 | | - |
42 | | - /** @var ConnectionAdapter */ |
43 | | - protected $dbConnectionAdapter; |
44 | | - |
45 | | - /** @var CredentialsManager */ |
46 | | - protected $manager; |
47 | | - |
48 | | - protected function setUp(): void { |
49 | | - parent::setUp(); |
50 | | - $this->crypto = $this->createMock(ICrypto::class); |
51 | | - $this->dbConnection = $this->getMockBuilder(IDBConnection::class) |
52 | | - ->disableOriginalConstructor() |
53 | | - ->getMock(); |
54 | | - $this->dbConnectionAdapter = $this->getMockBuilder(ConnectionAdapter::class) |
55 | | - ->disableOriginalConstructor() |
56 | | - ->getMock(); |
57 | | - $this->manager = new CredentialsManager($this->crypto, $this->dbConnection); |
58 | | - } |
59 | | - |
60 | | - private function getQueryResult($row) { |
61 | | - $result = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement') |
62 | | - ->disableOriginalConstructor() |
63 | | - ->getMock(); |
64 | | - |
65 | | - $result->expects($this->any()) |
66 | | - ->method('fetch') |
67 | | - ->willReturn($row); |
68 | | - |
69 | | - return $result; |
70 | | - } |
71 | | - |
72 | | - public function testStore() { |
73 | | - $userId = 'abc'; |
74 | | - $identifier = 'foo'; |
75 | | - $credentials = 'bar'; |
76 | | - |
77 | | - $this->crypto->expects($this->once()) |
78 | | - ->method('encrypt') |
79 | | - ->with(json_encode($credentials)) |
80 | | - ->willReturn('baz'); |
81 | | - |
82 | | - $this->dbConnection->expects($this->once()) |
83 | | - ->method('setValues') |
84 | | - ->with(CredentialsManager::DB_TABLE, |
85 | | - ['user' => $userId, 'identifier' => $identifier], |
86 | | - ['credentials' => 'baz'] |
87 | | - ); |
88 | | - |
89 | | - $this->manager->store($userId, $identifier, $credentials); |
90 | | - } |
91 | | - |
92 | | - public function testRetrieve() { |
93 | | - $userId = 'abc'; |
94 | | - $identifier = 'foo'; |
95 | | - |
96 | | - $this->crypto->expects($this->once()) |
97 | | - ->method('decrypt') |
98 | | - ->with('baz') |
99 | | - ->willReturn(json_encode('bar')); |
100 | | - |
101 | | - $qb = $this->getMockBuilder(\OC\DB\QueryBuilder\QueryBuilder::class) |
102 | | - ->setConstructorArgs([ |
103 | | - $this->dbConnectionAdapter, |
104 | | - $this->createMock(SystemConfig::class), |
105 | | - $this->createMock(ILogger::class), |
106 | | - ]) |
107 | | - ->setMethods(['execute']) |
108 | | - ->getMock(); |
109 | | - $qb->expects($this->once()) |
110 | | - ->method('execute') |
111 | | - ->willReturn($this->getQueryResult(['credentials' => 'baz'])); |
112 | | - |
113 | | - $this->dbConnectionAdapter->expects($this->once()) |
114 | | - ->method('getQueryBuilder') |
115 | | - ->willReturn($qb); |
116 | | - |
117 | | - $this->manager->retrieve($userId, $identifier); |
118 | | - } |
119 | | - |
120 | 29 | /** |
121 | 30 | * @dataProvider credentialsProvider |
122 | 31 | */ |
|
0 commit comments