about summary refs log tree commit diff stats
path: root/tests_integration/DownloadHandlerTest.php
blob: edf9e46f38517269f69ee7ff33db3d5c581ef727 (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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
use PHPUnit\Framework\TestCase;

final class DownloadHandlerTest extends ApiTestCase
{
	// ── Authentication ───────────────────────────────────────────────────────

	public function test_download_requires_authentication(): void
	{
		[$status, $body] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 6,
		]);

		$this->assertEquals(401, $status);
		$this->assertArrayHasKey('error', $body);
	}

	// ── Valid downloads ──────────────────────────────────────────────────────

	public function test_download_returns_200_for_existing_file(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		// data.id 6 = Huckleberry Finn EPUB
		[$status, $body, $headers] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 6,
		], false);

		$this->assertEquals(200, $status);
		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
		$this->assertStringContainsString('epub', $headers['Content-Disposition'] ?? '');
		$this->assertNotEmpty($body);
	}

	public function test_download_original_epub_format(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		// data.id 5 = Huckleberry Finn ORIGINAL_EPUB
		[$status, $body, $headers] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 5,
		], false);

		$this->assertEquals(200, $status);
		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
		$this->assertNotEmpty($body);
	}

	public function test_download_different_book(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		// data.id 9 = Sherlock Holmes EPUB
		[$status, $body, $headers] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 9,
		], false);

		$this->assertEquals(200, $status);
		$this->assertStringContainsString('application/epub+zip', $headers['Content-type'] ?? '');
		$this->assertStringContainsString('attachment', $headers['Content-Disposition'] ?? '');
		$this->assertNotEmpty($body);
	}

	// ── Missing/invalid ID ───────────────────────────────────────────────────

	public function test_download_without_id(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		[$status, $body] = $this->post($this->apiUrl, [
			'op' => 'download',
		]);

		$this->assertEquals(404, $status);
		$this->assertEquals('Not found.', $body['error']);
	}

	public function test_download_with_zero_id(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		[$status, $body] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 0,
		]);

		$this->assertEquals(404, $status);
		$this->assertEquals('Not found.', $body['error']);
	}

	public function test_download_with_negative_id(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		[$status, $body] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => -1,
		]);

		$this->assertEquals(404, $status);
		$this->assertEquals('Not found.', $body['error']);
	}

	public function test_download_with_nonexistent_data_id(): void
	{
		// Login
		[$status, $body] = $this->post($this->apiUrl, [
			'op'       => 'perform-login',
			'user'     => 'test',
			'password' => 'test',
		]);
		$this->assertEquals(200, $status);

		// data.id 99999 doesn't exist in the DB
		[$status, $body] = $this->post($this->apiUrl, [
			'op' => 'download',
			'id' => 99999,
		]);

		$this->assertEquals(404, $status);
		$this->assertEquals('Not found.', $body['error']);
	}
}