]> git.mxchange.org Git - friendica.git/blob - tests/src/Console/ServerBlockConsoleTest.php
Fix ApiResponseTest
[friendica.git] / tests / src / Console / ServerBlockConsoleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (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 Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Console;
23
24 use Friendica\Console\ServerBlock;
25 use Friendica\Core\Config\Capability\IManageConfigValues;
26 use Mockery;
27
28 class ServerBlockConsoleTest extends ConsoleTest
29 {
30         protected $defaultBlockList = [
31                 [
32                         'domain' => 'social.nobodyhasthe.biz',
33                         'reason' => 'Illegal content',
34                 ],
35                 [
36                         'domain' => 'pod.ordoevangelistarum.com',
37                         'reason' => 'Illegal content',
38                 ]
39         ];
40         /**
41          * @var IManageConfigValues|Mockery\LegacyMockInterface|Mockery\MockInterface
42          */
43         private $configMock;
44
45         protected function setUp() : void
46         {
47                 parent::setUp();
48
49                 $this->configMock = Mockery::mock(IManageConfigValues::class);
50         }
51
52         /**
53          * Test to list the default blocked servers
54          */
55         public function testBlockedServersList()
56         {
57                 $this->configMock
58                         ->shouldReceive('get')
59                         ->with('system', 'blocklist', [])
60                         ->andReturn($this->defaultBlockList)
61                         ->once();
62
63                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
64                 $txt = $this->dumpExecute($console);
65
66                 $output = <<<CONS
67 +----------------------------+-----------------+
68 | Domain                     | Reason          |
69 +----------------------------+-----------------+
70 | social.nobodyhasthe.biz    | Illegal content |
71 | pod.ordoevangelistarum.com | Illegal content |
72 +----------------------------+-----------------+
73
74
75 CONS;
76
77                 self::assertEquals($output, $txt);
78         }
79
80         /**
81          * Test blockedservers add command
82          */
83         public function testAddBlockedServer()
84         {
85                 $this->configMock
86                         ->shouldReceive('get')
87                         ->with('system', 'blocklist', [])
88                         ->andReturn($this->defaultBlockList)
89                         ->once();
90
91                 $newBlockList = $this->defaultBlockList;
92                 $newBlockList[] = [
93                         'domain' => 'testme.now',
94                         'reason' => 'I like it!',
95                 ];
96
97                 $this->configMock
98                         ->shouldReceive('set')
99                         ->with('system', 'blocklist', $newBlockList)
100                         ->andReturn(true)
101                         ->once();
102
103                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
104                 $console->setArgument(0, 'add');
105                 $console->setArgument(1, 'testme.now');
106                 $console->setArgument(2, 'I like it!');
107                 $txt = $this->dumpExecute($console);
108
109                 self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
110         }
111
112         /**
113          * Test blockedservers add command with the default reason
114          */
115         public function testAddBlockedServerWithDefaultReason()
116         {
117                 $this->configMock
118                         ->shouldReceive('get')
119                         ->with('system', 'blocklist', [])
120                         ->andReturn($this->defaultBlockList)
121                         ->once();
122
123                 $newBlockList = $this->defaultBlockList;
124                 $newBlockList[] = [
125                         'domain' => 'testme.now',
126                         'reason' => ServerBlock::DEFAULT_REASON,
127                 ];
128
129                 $this->configMock
130                         ->shouldReceive('set')
131                         ->with('system', 'blocklist', $newBlockList)
132                         ->andReturn(true)
133                         ->once();
134
135                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
136                 $console->setArgument(0, 'add');
137                 $console->setArgument(1, 'testme.now');
138                 $txt = $this->dumpExecute($console);
139
140                 self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
141         }
142
143         /**
144          * Test blockedservers add command on existed domain
145          */
146         public function testUpdateBlockedServer()
147         {
148                 $this->configMock
149                         ->shouldReceive('get')
150                         ->with('system', 'blocklist', [])
151                         ->andReturn($this->defaultBlockList)
152                         ->once();
153
154                 $newBlockList = [
155                         [
156                                 'domain' => 'social.nobodyhasthe.biz',
157                                 'reason' => 'Illegal content',
158                         ],
159                         [
160                                 'domain' => 'pod.ordoevangelistarum.com',
161                                 'reason' => 'Other reason',
162                         ]
163                 ];
164
165                 $this->configMock
166                         ->shouldReceive('set')
167                         ->with('system', 'blocklist', $newBlockList)
168                         ->andReturn(true)
169                         ->once();
170
171                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
172                 $console->setArgument(0, 'add');
173                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
174                 $console->setArgument(2, 'Other reason');
175                 $txt = $this->dumpExecute($console);
176
177                 self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
178         }
179
180         /**
181          * Test blockedservers remove command
182          */
183         public function testRemoveBlockedServer()
184         {
185                 $this->configMock
186                         ->shouldReceive('get')
187                         ->with('system', 'blocklist', [])
188                         ->andReturn($this->defaultBlockList)
189                         ->once();
190
191                 $newBlockList = [
192                         [
193                                 'domain' => 'social.nobodyhasthe.biz',
194                                 'reason' => 'Illegal content',
195                         ],
196                 ];
197
198                 $this->configMock
199                         ->shouldReceive('set')
200                         ->with('system', 'blocklist', $newBlockList)
201                         ->andReturn(true)
202                         ->once();
203
204                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
205                 $console->setArgument(0, 'remove');
206                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
207                 $txt = $this->dumpExecute($console);
208
209                 self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
210         }
211
212         /**
213          * Test blockedservers with a wrong command
214          */
215         public function testBlockedServersWrongCommand()
216         {
217                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
218                 $console->setArgument(0, 'wrongcommand');
219                 $txt = $this->dumpExecute($console);
220
221                 self::assertStringStartsWith('[Warning] Unknown command', $txt);
222         }
223
224         /**
225          * Test blockedservers remove with not existing domain
226          */
227         public function testRemoveBlockedServerNotExist()
228         {
229                 $this->configMock
230                         ->shouldReceive('get')
231                         ->with('system', 'blocklist', [])
232                         ->andReturn($this->defaultBlockList)
233                         ->once();
234
235                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
236                 $console->setArgument(0, 'remove');
237                 $console->setArgument(1, 'not.exiting');
238                 $txt = $this->dumpExecute($console);
239
240                 self::assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
241         }
242
243         /**
244          * Test blockedservers add command without argument
245          */
246         public function testAddBlockedServerMissingArgument()
247         {
248                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
249                 $console->setArgument(0, 'add');
250                 $txt = $this->dumpExecute($console);
251
252                 self::assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
253         }
254
255         /**
256          * Test blockedservers add command without save
257          */
258         public function testAddBlockedServerNoSave()
259         {
260                 $this->configMock
261                         ->shouldReceive('get')
262                         ->with('system', 'blocklist', [])
263                         ->andReturn($this->defaultBlockList)
264                         ->once();
265
266                 $newBlockList = $this->defaultBlockList;
267                 $newBlockList[] = [
268                         'domain' => 'testme.now',
269                         'reason' => ServerBlock::DEFAULT_REASON,
270                 ];
271
272                 $this->configMock
273                         ->shouldReceive('set')
274                         ->with('system', 'blocklist', $newBlockList)
275                         ->andReturn(false)
276                         ->once();
277
278                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
279                 $console->setArgument(0, 'add');
280                 $console->setArgument(1, 'testme.now');
281                 $txt = $this->dumpExecute($console);
282
283                 self::assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
284         }
285
286         /**
287          * Test blockedservers remove command without save
288          */
289         public function testRemoveBlockedServerNoSave()
290         {
291                 $this->configMock
292                         ->shouldReceive('get')
293                         ->with('system', 'blocklist', [])
294                         ->andReturn($this->defaultBlockList)
295                         ->once();
296
297                 $newBlockList = [
298                         [
299                                 'domain' => 'social.nobodyhasthe.biz',
300                                 'reason' => 'Illegal content',
301                         ],
302                 ];
303
304                 $this->configMock
305                         ->shouldReceive('set')
306                         ->with('system', 'blocklist', $newBlockList)
307                         ->andReturn(false)
308                         ->once();
309
310                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
311                 $console->setArgument(0, 'remove');
312                 $console->setArgument(1, 'pod.ordoevangelistarum.com');
313                 $txt = $this->dumpExecute($console);
314
315                 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
316         }
317
318         /**
319          * Test blockedservers remove command without argument
320          */
321         public function testRemoveBlockedServerMissingArgument()
322         {
323                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
324                 $console->setArgument(0, 'remove');
325                 $txt = $this->dumpExecute($console);
326
327                 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
328         }
329
330         /**
331          * Test the blockedservers help
332          */
333         public function testBlockedServersHelp()
334         {
335                 $console = new ServerBlock($this->configMock, $this->consoleArgv);
336                 $console->setOption('help', true);
337                 $txt = $this->dumpExecute($console);
338
339                 $help = <<<HELP
340 console serverblock - Manage blocked server domain patterns
341 Usage
342     bin/console serverblock [-h|--help|-?] [-v]
343     bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
344     bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
345     bin/console serverblock export <filename>
346     bin/console serverblock import <filename>
347
348 Description
349     With this tool, you can list the current blocked server domain patterns
350     or you can add / remove a blocked server domain pattern from the list.
351     Using the export and import options you can share your server blocklist
352     with other node admins by CSV files.
353
354     Patterns are case-insensitive shell wildcard comprising the following special characters:
355     - * : Any number of characters
356     - ? : Any single character
357     - [<char1><char2>...] : char1 or char2 or...
358
359 Options
360     -h|--help|-? Show help information
361     -v           Show more debug information.
362
363 HELP;
364
365                 self::assertEquals($help, $txt);
366         }
367 }