Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Remote/ServerTransferControllerTest.php
14044 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Remote;
4
5
use Pterodactyl\Models\Node;
6
use Pterodactyl\Models\Location;
7
use Pterodactyl\Models\Allocation;
8
use Pterodactyl\Models\ServerTransfer;
9
use Pterodactyl\Tests\Integration\IntegrationTestCase;
10
11
class ServerTransferControllerTest extends IntegrationTestCase
12
{
13
protected ServerTransfer $transfer;
14
15
public function setup(): void
16
{
17
parent::setUp();
18
19
$server = $this->createServerModel();
20
21
$new = Node::factory()
22
->for(Location::factory())
23
->has(Allocation::factory())
24
->create();
25
26
$this->transfer = ServerTransfer::factory()->for($server)->create([
27
'old_allocation' => $server->allocation_id,
28
'new_allocation' => $new->allocations->first()->id,
29
'new_node' => $new->id,
30
'old_node' => $server->node_id,
31
]);
32
}
33
34
public function testSuccessStatusUpdateCanBeSentFromNewNode(): void
35
{
36
$server = $this->transfer->server;
37
$newNode = $this->transfer->newNode;
38
39
$this
40
->withHeader('Authorization', "Bearer $newNode->daemon_token_id." . $newNode->getDecryptedKey())
41
->postJson("/api/remote/servers/{$server->uuid}/transfer/success")
42
->assertNoContent();
43
44
$this->assertTrue($this->transfer->refresh()->successful);
45
}
46
47
public function testFailureStatusUpdateCanBeSentFromOldNode(): void
48
{
49
$server = $this->transfer->server;
50
$oldNode = $this->transfer->oldNode;
51
52
$this->withHeader('Authorization', "Bearer $oldNode->daemon_token_id." . $oldNode->getDecryptedKey())
53
->postJson("/api/remote/servers/{$server->uuid}/transfer/failure")
54
->assertNoContent();
55
56
$this->assertFalse($this->transfer->refresh()->successful);
57
}
58
59
public function testFailureStatusUpdateCanBeSentFromNewNode(): void
60
{
61
$server = $this->transfer->server;
62
$newNode = $this->transfer->newNode;
63
64
$this->withHeader('Authorization', "Bearer $newNode->daemon_token_id." . $newNode->getDecryptedKey())
65
->postJson("/api/remote/servers/{$server->uuid}/transfer/failure")
66
->assertNoContent();
67
68
$this->assertFalse($this->transfer->refresh()->successful);
69
}
70
71
public function testSuccessStatusUpdateCannotBeSentFromOldNode(): void
72
{
73
$server = $this->transfer->server;
74
$oldNode = $this->transfer->oldNode;
75
76
$this->withHeader('Authorization', "Bearer $oldNode->daemon_token_id." . $oldNode->getDecryptedKey())
77
->postJson("/api/remote/servers/{$server->uuid}/transfer/success")
78
->assertForbidden()
79
->assertJsonPath('errors.0.code', 'HttpForbiddenException')
80
->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.');
81
82
$this->assertNull($this->transfer->refresh()->successful);
83
}
84
85
public function testSuccessStatusUpdateCannotBeSentFromUnauthorizedNode(): void
86
{
87
$server = $this->transfer->server;
88
$node = Node::factory()->for(Location::factory())->create();
89
90
$this->withHeader('Authorization', "Bearer $node->daemon_token_id." . $node->getDecryptedKey())
91
->postJson("/api/remote/servers/$server->uuid/transfer/success")
92
->assertForbidden()
93
->assertJsonPath('errors.0.code', 'HttpForbiddenException')
94
->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.');
95
96
$this->assertNull($this->transfer->refresh()->successful);
97
}
98
99
public function testFailureStatusUpdateCannotBeSentFromUnauthorizedNode(): void
100
{
101
$server = $this->transfer->server;
102
$node = Node::factory()->for(Location::factory())->create();
103
104
$this->withHeader('Authorization', "Bearer $node->daemon_token_id." . $node->getDecryptedKey())
105
->postJson("/api/remote/servers/$server->uuid/transfer/failure")->assertForbidden()
106
->assertJsonPath('errors.0.code', 'HttpForbiddenException')
107
->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.');
108
109
$this->assertNull($this->transfer->refresh()->successful);
110
}
111
}
112
113