diff options
Diffstat (limited to 'tests/unit/Module')
-rw-r--r-- | tests/unit/Module/AdminAccountsTest.php | 173 | ||||
-rw-r--r-- | tests/unit/Module/HelpTest.php | 4 | ||||
-rw-r--r-- | tests/unit/Module/MagicTest.php | 8 | ||||
-rw-r--r-- | tests/unit/Module/OwaTest.php | 64 | ||||
-rw-r--r-- | tests/unit/Module/TestCase.php | 2 |
5 files changed, 245 insertions, 6 deletions
diff --git a/tests/unit/Module/AdminAccountsTest.php b/tests/unit/Module/AdminAccountsTest.php new file mode 100644 index 000000000..2c76f2779 --- /dev/null +++ b/tests/unit/Module/AdminAccountsTest.php @@ -0,0 +1,173 @@ +<?php +/* + * SPDX-FileCopyrightText: 2024 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Module; + +use PHPUnit\Framework\Attributes\Before; + +class AdminAccountsTest extends TestCase { + + protected $stub_check_security; + protected $stub_is_site_admin; + protected $stub_goaway; + protected $stub_notice; + + protected array $notice; + + /** + * Set up the stubs common for the tests. + */ + #[Before] + public function setup_stubs(): void { + $this->stub_check_form_security(); + $this->stub_is_site_admin(); + $this->stub_goaway(); + $this->stub_notice(); + } + + public function test_blocking_accounts_marks_selected_accounts_as_blocked(): void { + $params = [ + 'user' => [ 42 ], + 'blocked' => [ false ], + 'page_accounts_block' => true, + ]; + + try { + $this->post('admin/accounts', [], $params); + } catch (RedirectException $redirect) { + $this->assertEquals(z_root() . '/admin/accounts', $redirect->getMessage()); + } + + $account = get_account_by_id(42); + $this->assertEquals(ACCOUNT_BLOCKED, $account['account_flags'] & ACCOUNT_BLOCKED); + + $this->assertEquals('1 account blocked/unblocked', $this->notice[0]); + } + + public function test_unblocking_accounts_clears_the_blocked_flag(): void { + // Pass two users to the module, one that is not blocked, + // and one that is. + $params = [ + 'user' => [ 42, 44 ], + 'blocked' => [ false, true ], + 'page_accounts_block' => true, + ]; + + try { + $this->post('admin/accounts', [], $params); + } catch (RedirectException $redirect) { + $this->assertEquals(z_root() . '/admin/accounts', $redirect->getMessage()); + } + + // We expect the previously unblocked account to be blocked. + $account = get_account_by_id(42); + $this->assertEquals(ACCOUNT_BLOCKED, $account['account_flags'] & ACCOUNT_BLOCKED); + + // We expect the previously blocked account to be unblocked. + $blocked_account = get_account_by_id(44); + $this->assertEquals(0, $blocked_account['account_flags'] & ACCOUNT_BLOCKED); + + $this->assertEquals('2 account blocked/unblocked', $this->notice[0]); + } + + public function test_deleting_accouns_remove_them_from_db(): void { + $params = [ + 'user' => [ 42, 44 ], + 'page_accounts_delete' => true, + ]; + + try { + $this->post('admin/accounts', [], $params); + } catch (RedirectException $redirect) { + $this->assertEquals(z_root() . '/admin/accounts', $redirect->getMessage()); + } + + $this->assertEquals(null, get_account_by_id(42)); + $this->assertEquals(null, get_account_by_id(44)); + } + + public function test_approving_pending_accounts_clears_pending_flag(): void { + + // Catch calls to the php mail function + // + // This is just to get it out of the way, we don't care about + // how many times it's called, or with what args here. + $this->getFunctionMock('Zotlabs\Lib', 'mail') + ->expects($this->any()) + ->willReturn(true); + + $params = [ + 'pending' => [ + $this->fixtures['register'][0]['reg_hash'], + $this->fixtures['register'][1]['reg_hash'] + ], + 'page_accounts_approve' => true, + ]; + + try { + $this->post('admin/accounts', [], $params); + } catch (RedirectException $redirect) { + $this->assertEquals(z_root() . '/admin/accounts', $redirect->getMessage()); + } + + foreach ([45, 46] as $id) { + $account = get_account_by_id($id); + $this->assertEquals(0, $account['account_flags'] & ACCOUNT_PENDING); + } + } + + /** + * Stub the check_form_security_token_ForbiddenOnErr. + */ + protected function stub_check_form_security(): void { + $this->stub_check_security = + $this->getFunctionMock('Zotlabs\Module\Admin', 'check_form_security_token_redirectOnErr') + ->expects($this->once()) + ->with( + $this->identicalTo('/admin/accounts'), + $this->identicalTo('admin_accounts')) + ->willReturn(true); + } + + /** + * Stub the call to is_site_admin in the Admin main module. + */ + protected function stub_is_site_admin(): void { + $this->stub_is_site_admin = + $this->getFunctionMock('Zotlabs\Module', 'is_site_admin') + ->expects($this->once()) + ->willReturn(true); + } + + /** + * Stub the goaway function. + * + * Will throw an RedirectException with the URL being redirected to + * as the exception message. + * + * @throws RedirectException + */ + protected function stub_goaway(): void { + $this->stub_goaway = + $this->getFunctionMock('Zotlabs\Module\Admin', 'goaway') + ->expects($this->once()) + ->willReturnCallback(function (string $uri) { + throw new RedirectException($uri); + }); + } + + protected function stub_notice(): void { + $this->notice = []; + $this->stub_notice = + $this->getFunctionMock('Zotlabs\Module\Admin', 'notice') + ->expects($this->any()) + ->willReturnCallback(function (string $arg) { + $this->notice[] = $arg; + }); + } +} diff --git a/tests/unit/Module/HelpTest.php b/tests/unit/Module/HelpTest.php index 1feef26a8..e1aea1a06 100644 --- a/tests/unit/Module/HelpTest.php +++ b/tests/unit/Module/HelpTest.php @@ -76,13 +76,13 @@ class HelpTest extends \Zotlabs\Tests\Unit\Module\TestCase { public function test_get_request_without_args_redirects_to_about_page(): void { $this->stub_goaway(); $this->expectException(\Zotlabs\Tests\Unit\Module\RedirectException::class); - $this->expectExceptionMessage('about/about'); + $this->expectExceptionMessage('about'); $this->get('help'); } public function test_getting_locale_with_no_topic_should_redirect_to_about_page(): void { - $this->expectRedirectTo('help/about/about'); + $this->expectRedirectTo('help/about'); $this->get('help/de'); } diff --git a/tests/unit/Module/MagicTest.php b/tests/unit/Module/MagicTest.php index 4a03d9d57..2c426bf76 100644 --- a/tests/unit/Module/MagicTest.php +++ b/tests/unit/Module/MagicTest.php @@ -46,9 +46,9 @@ class MagicTest extends TestCase { App::set_baseurl($baseurl); - App::$observer = [ + App::set_observer([ 'xchan_hash' => 'the hash', - ]; + ]); // We pass a local URL, and have a valid observer, but as the // delegate param is not passed, nothing will be done except @@ -72,9 +72,9 @@ class MagicTest extends TestCase { App::$timezone = 'UTC'; // Simulate a foreign (to this hub) observer, - App::$observer = [ + App::set_observer([ 'xchan_hash' => 'foreign hash', - ]; + ]); // Create the channel the foreign observer wants to access $result = create_identity([ diff --git a/tests/unit/Module/OwaTest.php b/tests/unit/Module/OwaTest.php new file mode 100644 index 000000000..dbb25c0b5 --- /dev/null +++ b/tests/unit/Module/OwaTest.php @@ -0,0 +1,64 @@ +<?php +/* + * SPDX-FileCopyrightText: 2025 Hubzilla Community + * SPDX-FileContributor: Harald Eilertsen + * + * SPDX-License-Identifier: MIT + */ + +namespace Zotlabs\Tests\Unit\Module; + +class OwaTest extends TestCase +{ + public function testShouldReturnErrorIfNoAuthorizationHeader(): void + { + // Expect the call to return error + $this->expectJsonResponse([ + 'success' => false, + 'message' => 'Missing or invalid authorization header.' + ]); + + $this->get('owa'); + } + + public function testShouldReturnErrorIfWrongAuthorizationHeader(): void + { + // Expect the call to return error + $this->expectJsonResponse([ + 'success' => false, + 'message' => 'Missing or invalid authorization header.' + ]); + + $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer kjkjhkjhkjh'; + $this->get('owa'); + } + + public function testShouldReturnErrorIfInvalidAuthorizationHeader(): void + { + // Expect the call to return error + $this->expectJsonResponse(['success' => false]); + + $_SERVER['HTTP_AUTHORIZATION'] = 'Signature kjkjhkjhkjh'; + $this->get('owa'); + } + + /** + * Expect the request to be terminated and return a json response. + */ + private function expectJsonResponse(array $data): void + { + $this->getFunctionMock('Zotlabs\Module', 'json_return_and_die') + ->expects($this->once()) + ->with( + $this->identicalTo($data), + $this->identicalTo('application/x-zot+json') + ) + ->willReturnCallback( + function() { + throw new KillmeException(); + } + ); + + $this->expectException(KillmeException::class); + } +} diff --git a/tests/unit/Module/TestCase.php b/tests/unit/Module/TestCase.php index 1a4cf52fc..dd88a5a3b 100644 --- a/tests/unit/Module/TestCase.php +++ b/tests/unit/Module/TestCase.php @@ -43,6 +43,8 @@ class TestCase extends UnitTestCase { $_SERVER['REQUEST_METHOD'] = $method; $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $_SERVER['QUERY_STRING'] = "q={$uri}"; + $_SERVER['REQUEST_URI'] = $uri; + // phpcs:disable Generic.PHP.DisallowRequestSuperglobal.Found $_REQUEST = array_merge($_GET, $_POST); // phpcs::enable |