forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateUploadServiceTest.php
More file actions
245 lines (210 loc) · 8.75 KB
/
TemplateUploadServiceTest.php
File metadata and controls
245 lines (210 loc) · 8.75 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Packs;
use ZipArchive;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Pack;
use Illuminate\Http\UploadedFile;
use Pterodactyl\Services\Packs\PackCreationService;
use Pterodactyl\Services\Packs\TemplateUploadService;
use Pterodactyl\Exceptions\Service\InvalidFileUploadException;
use Pterodactyl\Exceptions\Service\Pack\ZipExtractionException;
use Pterodactyl\Exceptions\Service\Pack\InvalidFileMimeTypeException;
use Pterodactyl\Exceptions\Service\Pack\UnreadableZipArchiveException;
use Pterodactyl\Exceptions\Service\Pack\InvalidPackArchiveFormatException;
class TemplateUploadServiceTest extends TestCase
{
const JSON_FILE_CONTENTS = '{"test_content": "value"}';
/**
* @var \ZipArchive|\Mockery\Mock
*/
protected $archive;
/**
* @var \Pterodactyl\Services\Packs\PackCreationService|\Mockery\Mock
*/
protected $creationService;
/**
* @var \Illuminate\Http\UploadedFile|\Mockery\Mock
*/
protected $file;
/**
* @var \Pterodactyl\Services\Packs\TemplateUploadService
*/
protected $service;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->archive = m::mock(ZipArchive::class);
$this->creationService = m::mock(PackCreationService::class);
$this->file = m::mock(UploadedFile::class);
$this->service = new TemplateUploadService($this->creationService, $this->archive);
}
/**
* Test that a JSON file can be processed and turned into a pack.
*
* @dataProvider jsonMimetypeProvider
*/
public function testJsonFileIsProcessed($mime)
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn($mime);
$this->file->shouldReceive('getSize')->withNoArgs()->once()->andReturn(128);
$this->file->shouldReceive('openFile->fread')->with(128)->once()->andReturn(self::JSON_FILE_CONTENTS);
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
->once()->andReturn(factory(Pack::class)->make());
$this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file));
}
/**
* Test that a zip file can be processed.
*/
public function testZipfileIsProcessed()
{
$model = factory(Pack::class)->make();
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/real');
$this->archive->shouldReceive('open')->with('/test/real')->once()->andReturn(true);
$this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn(true);
$this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn(true);
$this->archive->shouldReceive('getFromName')->with('import.json')->once()->andReturn(self::JSON_FILE_CONTENTS);
$this->creationService->shouldReceive('handle')->with(['test_content' => 'value', 'egg_id' => 1])
->once()->andReturn($model);
$this->archive->shouldReceive('extractTo')->with(storage_path('app/packs/' . $model->uuid), 'archive.tar.gz')
->once()->andReturn(true);
$this->archive->shouldReceive('close')->withNoArgs()->once()->andReturnNull();
$this->assertInstanceOf(Pack::class, $this->service->handle(1, $this->file));
}
/**
* Test that an exception is thrown if the file upload is invalid.
*/
public function testExceptionIsThrownIfFileUploadIsInvalid()
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(false);
try {
$this->service->handle(1, $this->file);
} catch (InvalidFileUploadException $exception) {
$this->assertEquals(trans('exceptions.packs.invalid_upload'), $exception->getMessage());
}
}
/**
* Test that an invalid mimetype throws an exception.
*
* @dataProvider invalidMimetypeProvider
*/
public function testExceptionIsThrownIfMimetypeIsInvalid($mime)
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->once()->andReturn($mime);
try {
$this->service->handle(1, $this->file);
} catch (InvalidFileMimeTypeException $exception) {
$this->assertEquals(trans('exceptions.packs.invalid_mime', [
'type' => implode(', ', TemplateUploadService::VALID_UPLOAD_TYPES),
]), $exception->getMessage());
}
}
/**
* Test that an exception is thrown if the zip is unreadable.
*/
public function testExceptionIsThrownIfZipArchiveIsUnreadable()
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/path');
$this->archive->shouldReceive('open')->with('/test/path')->once()->andReturn(false);
try {
$this->service->handle(1, $this->file);
} catch (UnreadableZipArchiveException $exception) {
$this->assertEquals(trans('exceptions.packs.unreadable'), $exception->getMessage());
}
}
/**
* Test that a zip missing the required files throws an exception.
*
* @dataProvider filenameProvider
*/
public function testExceptionIsThrownIfZipDoesNotContainProperFiles($a, $b)
{
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/path');
$this->archive->shouldReceive('open')->with('/test/path')->once()->andReturn(true);
$this->archive->shouldReceive('locateName')->with('import.json')->once()->andReturn($a);
if ($a) {
$this->archive->shouldReceive('locateName')->with('archive.tar.gz')->once()->andReturn($b);
}
try {
$this->service->handle(1, $this->file);
} catch (InvalidPackArchiveFormatException $exception) {
$this->assertEquals(trans('exceptions.packs.invalid_archive_exception'), $exception->getMessage());
}
}
/**
* Test that an exception is thrown if an archive cannot be extracted from the zip file.
*/
public function testExceptionIsThrownIfArchiveCannotBeExtractedFromZip()
{
$model = factory(Pack::class)->make();
$this->file->shouldReceive('isValid')->withNoArgs()->once()->andReturn(true);
$this->file->shouldReceive('getMimeType')->withNoArgs()->twice()->andReturn('application/zip');
$this->file->shouldReceive('getRealPath')->withNoArgs()->once()->andReturn('/test/real');
$this->archive->shouldReceive('open')->once()->andReturn(true);
$this->archive->shouldReceive('locateName')->twice()->andReturn(true);
$this->archive->shouldReceive('getFromName')->once()->andReturn(self::JSON_FILE_CONTENTS);
$this->creationService->shouldReceive('handle')->once()->andReturn($model);
$this->archive->shouldReceive('extractTo')->once()->andReturn(false);
try {
$this->service->handle(1, $this->file);
} catch (ZipExtractionException $exception) {
$this->assertEquals(trans('exceptions.packs.zip_extraction'), $exception->getMessage());
}
}
/**
* Provide valid JSON mimetypes to use in tests.
*
* @return array
*/
public function jsonMimetypeProvider()
{
return [
['text/plain'],
['application/json'],
];
}
/**
* Return invalid mimetypes for testing.
*
* @return array
*/
public function invalidMimetypeProvider()
{
return [
['application/gzip'],
['application/x-gzip'],
['image/jpeg'],
];
}
/**
* Return values for archive->locateName function, import.json and archive.tar.gz respectively.
*
* @return array
*/
public function filenameProvider()
{
return [
[true, false],
[false, true],
[false, false],
];
}
}