3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\Capability\IManageConfigValues;
28 class ServerBlockConsoleTest extends ConsoleTest
30 protected $defaultBlockList = [
32 'domain' => 'social.nobodyhasthe.biz',
33 'reason' => 'Illegal content',
36 'domain' => 'pod.ordoevangelistarum.com',
37 'reason' => 'Illegal content',
41 * @var IManageConfigValues|Mockery\LegacyMockInterface|Mockery\MockInterface
45 protected function setUp() : void
49 $this->configMock = Mockery::mock(IManageConfigValues::class);
53 * Test to list the default blocked servers
55 public function testBlockedServersList()
58 ->shouldReceive('get')
59 ->with('system', 'blocklist', [])
60 ->andReturn($this->defaultBlockList)
63 $console = new ServerBlock($this->configMock, $this->consoleArgv);
64 $txt = $this->dumpExecute($console);
67 +----------------------------+-----------------+
69 +----------------------------+-----------------+
70 | social.nobodyhasthe.biz | Illegal content |
71 | pod.ordoevangelistarum.com | Illegal content |
72 +----------------------------+-----------------+
77 self::assertEquals($output, $txt);
81 * Test blockedservers add command
83 public function testAddBlockedServer()
86 ->shouldReceive('get')
87 ->with('system', 'blocklist', [])
88 ->andReturn($this->defaultBlockList)
91 $newBlockList = $this->defaultBlockList;
93 'domain' => 'testme.now',
94 'reason' => 'I like it!',
98 ->shouldReceive('set')
99 ->with('system', 'blocklist', $newBlockList)
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);
109 self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'I like it!\')' . PHP_EOL, $txt);
113 * Test blockedservers add command with the default reason
115 public function testAddBlockedServerWithDefaultReason()
118 ->shouldReceive('get')
119 ->with('system', 'blocklist', [])
120 ->andReturn($this->defaultBlockList)
123 $newBlockList = $this->defaultBlockList;
125 'domain' => 'testme.now',
126 'reason' => ServerBlock::DEFAULT_REASON,
130 ->shouldReceive('set')
131 ->with('system', 'blocklist', $newBlockList)
135 $console = new ServerBlock($this->configMock, $this->consoleArgv);
136 $console->setArgument(0, 'add');
137 $console->setArgument(1, 'testme.now');
138 $txt = $this->dumpExecute($console);
140 self::assertEquals('The domain \'testme.now\' is now blocked. (Reason: \'' . ServerBlock::DEFAULT_REASON . '\')' . PHP_EOL, $txt);
144 * Test blockedservers add command on existed domain
146 public function testUpdateBlockedServer()
149 ->shouldReceive('get')
150 ->with('system', 'blocklist', [])
151 ->andReturn($this->defaultBlockList)
156 'domain' => 'social.nobodyhasthe.biz',
157 'reason' => 'Illegal content',
160 'domain' => 'pod.ordoevangelistarum.com',
161 'reason' => 'Other reason',
166 ->shouldReceive('set')
167 ->with('system', 'blocklist', $newBlockList)
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);
177 self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is now updated. (Reason: \'Other reason\')' . PHP_EOL, $txt);
181 * Test blockedservers remove command
183 public function testRemoveBlockedServer()
186 ->shouldReceive('get')
187 ->with('system', 'blocklist', [])
188 ->andReturn($this->defaultBlockList)
193 'domain' => 'social.nobodyhasthe.biz',
194 'reason' => 'Illegal content',
199 ->shouldReceive('set')
200 ->with('system', 'blocklist', $newBlockList)
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);
209 self::assertEquals('The domain \'pod.ordoevangelistarum.com\' is not more blocked' . PHP_EOL, $txt);
213 * Test blockedservers with a wrong command
215 public function testBlockedServersWrongCommand()
217 $console = new ServerBlock($this->configMock, $this->consoleArgv);
218 $console->setArgument(0, 'wrongcommand');
219 $txt = $this->dumpExecute($console);
221 self::assertStringStartsWith('[Warning] Unknown command', $txt);
225 * Test blockedservers remove with not existing domain
227 public function testRemoveBlockedServerNotExist()
230 ->shouldReceive('get')
231 ->with('system', 'blocklist', [])
232 ->andReturn($this->defaultBlockList)
235 $console = new ServerBlock($this->configMock, $this->consoleArgv);
236 $console->setArgument(0, 'remove');
237 $console->setArgument(1, 'not.exiting');
238 $txt = $this->dumpExecute($console);
240 self::assertEquals('The domain \'not.exiting\' is not blocked.' . PHP_EOL, $txt);
244 * Test blockedservers add command without argument
246 public function testAddBlockedServerMissingArgument()
248 $console = new ServerBlock($this->configMock, $this->consoleArgv);
249 $console->setArgument(0, 'add');
250 $txt = $this->dumpExecute($console);
252 self::assertStringStartsWith('[Warning] Add needs a domain and optional a reason.', $txt);
256 * Test blockedservers add command without save
258 public function testAddBlockedServerNoSave()
261 ->shouldReceive('get')
262 ->with('system', 'blocklist', [])
263 ->andReturn($this->defaultBlockList)
266 $newBlockList = $this->defaultBlockList;
268 'domain' => 'testme.now',
269 'reason' => ServerBlock::DEFAULT_REASON,
273 ->shouldReceive('set')
274 ->with('system', 'blocklist', $newBlockList)
278 $console = new ServerBlock($this->configMock, $this->consoleArgv);
279 $console->setArgument(0, 'add');
280 $console->setArgument(1, 'testme.now');
281 $txt = $this->dumpExecute($console);
283 self::assertEquals('Couldn\'t save \'testme.now\' as blocked server' . PHP_EOL, $txt);
287 * Test blockedservers remove command without save
289 public function testRemoveBlockedServerNoSave()
292 ->shouldReceive('get')
293 ->with('system', 'blocklist', [])
294 ->andReturn($this->defaultBlockList)
299 'domain' => 'social.nobodyhasthe.biz',
300 'reason' => 'Illegal content',
305 ->shouldReceive('set')
306 ->with('system', 'blocklist', $newBlockList)
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);
315 self::assertEquals('Couldn\'t remove \'pod.ordoevangelistarum.com\' from blocked servers' . PHP_EOL, $txt);
319 * Test blockedservers remove command without argument
321 public function testRemoveBlockedServerMissingArgument()
323 $console = new ServerBlock($this->configMock, $this->consoleArgv);
324 $console->setArgument(0, 'remove');
325 $txt = $this->dumpExecute($console);
327 self::assertStringStartsWith('[Warning] Remove needs a second parameter.', $txt);
331 * Test the blockedservers help
333 public function testBlockedServersHelp()
335 $console = new ServerBlock($this->configMock, $this->consoleArgv);
336 $console->setOption('help', true);
337 $txt = $this->dumpExecute($console);
340 console serverblock - Manage blocked server domain patterns
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>
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.
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...
360 -h|--help|-? Show help information
361 -v Show more debug information.
365 self::assertEquals($help, $txt);