|
24 | 24 |
|
25 | 25 | namespace Test\Security; |
26 | 26 |
|
27 | | -use OC\Security\CredentialsManager; |
28 | | -use OCP\DB\IResult; |
29 | | -use OCP\DB\QueryBuilder\IExpressionBuilder; |
30 | | -use OCP\DB\QueryBuilder\IQueryBuilder; |
31 | | -use OCP\IDBConnection; |
32 | | -use OCP\Security\ICrypto; |
33 | | - |
34 | 27 | /** |
35 | 28 | * @group DB |
36 | 29 | */ |
37 | 30 | class CredentialsManagerTest extends \Test\TestCase { |
38 | 31 |
|
39 | | - /** @var ICrypto */ |
40 | | - protected $crypto; |
41 | | - |
42 | | - /** @var IDBConnection */ |
43 | | - protected $dbConnection; |
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->manager = new CredentialsManager($this->crypto, $this->dbConnection); |
55 | | - } |
56 | | - |
57 | | - private function getQueryResult($row) { |
58 | | - $result = $this->createMock(IResult::class); |
59 | | - |
60 | | - $result->expects($this->any()) |
61 | | - ->method('fetch') |
62 | | - ->willReturn($row); |
63 | | - |
64 | | - return $result; |
65 | | - } |
66 | | - |
67 | | - public function testStore() { |
68 | | - $userId = 'abc'; |
69 | | - $identifier = 'foo'; |
70 | | - $credentials = 'bar'; |
71 | | - |
72 | | - $this->crypto->expects($this->once()) |
73 | | - ->method('encrypt') |
74 | | - ->with(json_encode($credentials)) |
75 | | - ->willReturn('baz'); |
76 | | - |
77 | | - $this->dbConnection->expects($this->once()) |
78 | | - ->method('setValues') |
79 | | - ->with(CredentialsManager::DB_TABLE, |
80 | | - ['user' => $userId, 'identifier' => $identifier], |
81 | | - ['credentials' => 'baz'] |
82 | | - ); |
83 | | - |
84 | | - $this->manager->store($userId, $identifier, $credentials); |
85 | | - } |
86 | | - |
87 | | - public function testRetrieve() { |
88 | | - $userId = 'abc'; |
89 | | - $identifier = 'foo'; |
90 | | - |
91 | | - $this->crypto->expects($this->once()) |
92 | | - ->method('decrypt') |
93 | | - ->with('baz') |
94 | | - ->willReturn(json_encode('bar')); |
95 | | - |
96 | | - $eb = $this->createMock(IExpressionBuilder::class); |
97 | | - $qb = $this->createMock(IQueryBuilder::class); |
98 | | - $qb->method('select')->willReturnSelf(); |
99 | | - $qb->method('from')->willReturnSelf(); |
100 | | - $qb->method('where')->willReturnSelf(); |
101 | | - $qb->method('expr')->willReturn($eb); |
102 | | - $qb->expects($this->once()) |
103 | | - ->method('execute') |
104 | | - ->willReturn($this->getQueryResult(['credentials' => 'baz'])); |
105 | | - |
106 | | - $this->dbConnection->expects($this->once()) |
107 | | - ->method('getQueryBuilder') |
108 | | - ->willReturn($qb); |
109 | | - |
110 | | - $this->manager->retrieve($userId, $identifier); |
111 | | - } |
112 | | - |
113 | 32 | /** |
114 | 33 | * @dataProvider credentialsProvider |
115 | 34 | */ |
|
0 commit comments