aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/App/AppStore/Fetcher/AppDiscoverFetcherTest.php
blob: 82f40043971bb8d762d3eee8a75bc66ad1a61d39 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\App\AppStore\Fetcher;

use OC\App\AppStore\Fetcher\AppDiscoverFetcher;
use OC\App\CompareVersion;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use OCP\Files\SimpleFS\ISimpleFolder;
use PHPUnit\Framework\MockObject\MockObject;

class AppDiscoverFetcherTest extends FetcherBase {
	protected CompareVersion|MockObject $compareVersion;

	protected function setUp(): void {
		parent::setUp();
		$this->fileName = 'discover.json';
		$this->endpoint = 'https://apps.nextcloud.com/api/v1/discover.json';

		$this->compareVersion = $this->createMock(CompareVersion::class);

		$this->fetcher = new AppDiscoverFetcher(
			$this->appDataFactory,
			$this->clientService,
			$this->timeFactory,
			$this->config,
			$this->logger,
			$this->registry,
			$this->compareVersion,
		);
	}

	public function testAppstoreDisabled() {
		$this->config
			->method('getSystemValueBool')
			->willReturnCallback(function ($var, $default) {
				if ($var === 'appstoreenabled') {
					return false;
				}
				return $default;
			});
		$this->appData
			->expects($this->never())
			->method('getFolder');

		$this->assertEquals([], $this->fetcher->get());
	}

	public function testNoInternet() {
		$this->config
			->method('getSystemValueBool')
			->willReturnCallback(function ($var, $default) {
				if ($var === 'has_internet_connection') {
					return false;
				}
				return $default;
			});
		$this->config
			->method('getSystemValueString')
			->willReturnCallback(function ($var, $default) {
				return $default;
			});
		$this->appData
			->expects($this->never())
			->method('getFolder');

		$this->assertEquals([], $this->fetcher->get());
	}

	/**
	 * @dataProvider dataGetETag
	 */
	public function testGetEtag(string|null $expected, bool $throws, string $content = '') {
		$folder = $this->createMock(ISimpleFolder::class);
		if (!$throws) {
			$file = $this->createMock(ISimpleFile::class);
			$file->expects($this->once())
				->method('getContent')
				->willReturn($content);
			$folder->expects($this->once())
				->method('getFile')
				->with('discover.json')
				->willReturn($file);
		} else {
			$folder->expects($this->once())
				->method('getFile')
				->with('discover.json')
				->willThrowException(new NotFoundException(''));
		}

		$this->appData->expects($this->once())
			->method('getFolder')
			->with('/')
			->willReturn($folder);

		$etag = $this->fetcher->getETag();
		$this->assertEquals($expected, $etag);
		if ($expected !== null) {
			$this->assertTrue(gettype($etag) === 'string');
		}
	}

	public function dataGetETag(): array {
		return [
			'file not found' => [null, true],
			'empty file' => [null, false, ''],
			'missing etag' => [null, false, '{ "foo": "bar" }'],
			'valid etag' => ['test', false, '{ "ETag": "test" }'],
			'numeric etag' => ['132', false, '{ "ETag": 132 }'],
		];
	}
}