aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Module/OwaTest.php
blob: dbb25c0b5b5c5f7e39cb05c42200287520ba337d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
	}
}