3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Console;
24 use Friendica\Console\ServerBlock;
25 use Friendica\Core\Config\IConfig;
27 class ServerBlockConsoleTest extends ConsoleTest
29 protected $defaultBlockList = [
31 'domain' => 'social.nobodyhasthe.biz',
32 'reason' => 'Illegal content',
35 'domain' => 'pod.ordoevangelistarum.com',
36 'reason' => 'Illegal content',
40 protected function setUp()
44 $this->configMock = \Mockery::mock(IConfig::class);
48 * Test to list the default blocked servers
50 public function testBlockedServersList()
53 ->shouldReceive('get')
54 ->with('system', 'blocklist', [])
55 ->andReturn($this->defaultBlockList)
58 $console = new ServerBlock($this->configMock, $this->consoleArgv);
59 $txt = $this->dumpExecute($console);
62 +----------------------------+-----------------+
64 +----------------------------+-----------------+
65 | social.nobodyhasthe.biz | Illegal content |
66 | pod.ordoevangelistarum.com | Illegal content |
67 +----------------------------+-----------------+
72 $this->assertEquals($output, $txt);
76 * Test blockedservers add command
78 public function testAddBlockedServer()
81 ->shouldReceive('get')
82 ->with('system', 'blocklist', [])
83 ->andReturn($this->defaultBlockList)
86 $newBlockList = $this->defaultBlockList;
88 'domain' => 'testme.now',
89 'reason' => 'I like it!',
93 ->shouldReceive('set')
94 ->with('system', 'blocklist', $newBlockList)
98 $console = new ServerBlock($this->configMock, $this->consoleArgv);
99 $console->setArgument(0, 'add');
100 $console->setArgument(1, 'testme.now');
101 $console->setArgument(2, 'I like it!');
102 $txt = $this->dumpExecute($console);
104 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
108 * Test blockedservers add command with the default reason
110 public function testAddBlockedServerWithDefaultReason()
113 ->shouldReceive('get')
114 ->with('system', 'blocklist', [])
115 ->andReturn($this->defaultBlockList)
118 $newBlockList = $this->defaultBlockList;
120 'domain' => 'testme.now',
121 'reason' => ServerBlock::DEFAULT_REASON,
125 ->shouldReceive('set')
126 ->with('system', 'blocklist', $newBlockList)
130 $console = new ServerBlock($this->configMock, $this->consoleArgv);
131 $console->setArgument(0, 'add');
132 $console->setArgument(1, 'testme.now');
133 $txt = $this->dumpExecute($console);
135 $this->assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
139 * Test blockedservers add command on existed domain
141 public function testUpdateBlockedServer()
144 ->shouldReceive('get')
145 ->with('system', 'blocklist', [])
146 ->andReturn($this->defaultBlockList)
151 'domain' => 'social.nobodyhasthe.biz',
152 'reason' => 'Illegal content',
155 'domain' => 'pod.ordoevangelistarum.com',
156 'reason' => 'Other reason',
161 ->shouldReceive('set')
162 ->with('system', 'blocklist', $newBlockList)
166 $console = new ServerBlock($this->configMock, $this->consoleArgv);
167 $console->setArgument(0, 'add');
168 $console->setArgument(1, 'pod.ordoevangelistarum.com');
169 $console->setArgument(2, 'Other reason');
170 $txt = $this->dumpExecute($console);
172 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
176 * Test blockedservers remove command
178 public function testRemoveBlockedServer()
181 ->shouldReceive('get')
182 ->with('system', 'blocklist', [])
183 ->andReturn($this->defaultBlockList)
188 'domain' => 'social.nobodyhasthe.biz',
189 'reason' => 'Illegal content',
194 ->shouldReceive('set')
195 ->with('system', 'blocklist', $newBlockList)
199 $console = new ServerBlock($this->configMock, $this->consoleArgv);
200 $console->setArgument(0, 'remove');
201 $console->setArgument(1, 'pod.ordoevangelistarum.com');
202 $txt = $this->dumpExecute($console);
204 $this->assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
208 * Test blockedservers with a wrong command
210 public function testBlockedServersWrongCommand()
212 $console = new ServerBlock($this->configMock, $this->consoleArgv);
213 $console->setArgument(0, 'wrongcommand');
214 $txt = $this->dumpExecute($console);
216 $this->assertStringStartsWith('[Warning] Unknown command', $txt);
220 * Test blockedservers remove with not existing domain
222 public function testRemoveBlockedServerNotExist()
225 ->shouldReceive('get')
226 ->with('system', 'blocklist', [])
227 ->andReturn($this->defaultBlockList)
230 $console = new ServerBlock($this->configMock, $this->consoleArgv);
231 $console->setArgument(0, 'remove');
232 $console->setArgument(1, 'not.exiting');
233 $txt = $this->dumpExecute($console);
235 $this->assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
239 * Test blockedservers add command without argument
241 public function testAddBlockedServerMissingArgument()
243 $console = new ServerBlock($this->configMock, $this->consoleArgv);
244 $console->setArgument(0, 'add');
245 $txt = $this->dumpExecute($console);
247 $this->assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
251 * Test blockedservers add command without save
253 public function testAddBlockedServerNoSave()
256 ->shouldReceive('get')
257 ->with('system', 'blocklist', [])
258 ->andReturn($this->defaultBlockList)
261 $newBlockList = $this->defaultBlockList;
263 'domain' => 'testme.now',
264 'reason' => ServerBlock::DEFAULT_REASON,
268 ->shouldReceive('set')
269 ->with('system', 'blocklist', $newBlockList)
273 $console = new ServerBlock($this->configMock, $this->consoleArgv);
274 $console->setArgument(0, 'add');
275 $console->setArgument(1, 'testme.now');
276 $txt = $this->dumpExecute($console);
278 $this->assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
282 * Test blockedservers remove command without save
284 public function testRemoveBlockedServerNoSave()
287 ->shouldReceive('get')
288 ->with('system', 'blocklist', [])
289 ->andReturn($this->defaultBlockList)
294 'domain' => 'social.nobodyhasthe.biz',
295 'reason' => 'Illegal content',
300 ->shouldReceive('set')
301 ->with('system', 'blocklist', $newBlockList)
305 $console = new ServerBlock($this->configMock, $this->consoleArgv);
306 $console->setArgument(0, 'remove');
307 $console->setArgument(1, 'pod.ordoevangelistarum.com');
308 $txt = $this->dumpExecute($console);
310 $this->assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
314 * Test blockedservers remove command without argument
316 public function testRemoveBlockedServerMissingArgument()
318 $console = new ServerBlock($this->configMock, $this->consoleArgv);
319 $console->setArgument(0, 'remove');
320 $txt = $this->dumpExecute($console);
322 $this->assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
326 * Test the blockedservers help
328 public function testBlockedServersHelp()
330 $console = new ServerBlock($this->configMock, $this->consoleArgv);
331 $console->setOption('help', true);
332 $txt = $this->dumpExecute($console);
335 console serverblock - Manage blocked server domain patterns
337 bin/console serverblock [-h|--help|-?] [-v]
338 bin/console serverblock add <pattern> <reason> [-h|--help|-?] [-v]
339 bin/console serverblock remove <pattern> [-h|--help|-?] [-v]
340 bin/console serverblock export <filename>
341 bin/console serverblock import <filename>
344 With this tool, you can list the current blocked server domain patterns
345 or you can add / remove a blocked server domain pattern from the list.
346 Using the export and import options you can share your server blocklist
347 with other node admins by CSV files.
349 Patterns are case-insensitive shell wildcard comprising the following special characters:
350 - * : Any number of characters
351 - ? : Any single character
352 - [<char1><char2>...] : char1 or char2 or...
355 -h|--help|-? Show help information
356 -v Show more debug information.
360 $this->assertEquals($help, $txt);