-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathRequestFileUploadTest.php
More file actions
166 lines (148 loc) · 5.63 KB
/
Copy pathRequestFileUploadTest.php
File metadata and controls
166 lines (148 loc) · 5.63 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
<?php
namespace Tests\Feature\Api;
use Illuminate\Http\Testing\File;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\User;
use ProcessMaker\Nayra\Storage\BpmnDocument;
use ProcessMaker\Providers\WorkflowServiceProvider;
use Tests\Feature\Api\TestProcessExecutionTrait;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class RequestFileUploadTest extends TestCase
{
use RequestHelper;
use TestProcessExecutionTrait;
/**
* @var Process
*/
protected $process;
/**
* @var \ProcessMaker\Nayra\Contracts\Bpmn\ActivityInterface
*/
protected $task;
/**
* @var \ProcessMaker\Models\User[]
*/
protected $assigned = [];
/**
* Test a user that participate from the request can
* upload a file.
*/
public function testUploadRequestFile()
{
$this->loadTestProcess(
file_get_contents(__DIR__ . '/processes/FileUpload.bpmn'),
[
'2' => User::factory()->create([
'status' => 'ACTIVE',
'is_administrator' => false,
]),
]
);
// Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'node_1']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
$requestJson = $response->json();
$request = ProcessRequest::find($requestJson['id']);
// Upload file from the first task
$uploadTask = $request->tokens()->where('status', 'ACTIVE')->first();
$route = route('api.requests.files.store', [$request->id, 'event' => 'node_1']);
$response = $this->actingAs($uploadTask->user, 'api')
->json('POST', $route, [
'file' => File::image('photo.jpg'),
'data_name' => 'photo',
]);
// Check the user has access to upload a file
$response->assertStatus(200);
// Check the file was uploaded
$this->assertEquals($request->getMedia()[0]->file_name, 'photo.jpg');
}
/**
* Test a user that does not participate from the request can
* not upload a file.
*/
public function testCanNotUploadRequestFile()
{
// Load the FileUpload.bpmn process
$this->loadTestProcess(
file_get_contents(__DIR__ . '/processes/FileUpload.bpmn'),
[
'2' => User::factory()->create([
'status' => 'ACTIVE',
'is_administrator' => false,
]),
]
);
// Create an external user
$doesNotParticipateUser = User::factory()->create([
'status' => 'ACTIVE',
'is_administrator' => false,
]);
// Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'node_1']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
$requestJson = $response->json();
$request = ProcessRequest::find($requestJson['id']);
// Upload file with a user that does not participate in the request
$route = route('api.requests.files.store', [$request->id, 'event' => 'node_1']);
$response = $this->actingAs($doesNotParticipateUser, 'api')
->json('POST', $route, [
'file' => File::image('photo.jpg'),
]);
// Check the user does not have access to upload a file
$response->assertStatus(403);
// Check the file was not uploaded
$this->assertEquals(0, $request->getMedia()->count());
}
/**
* Test a user that can claim a self task can view the requests uploaded files.
*/
public function testViewUploadedRequestFile()
{
$this->loadTestProcess(
file_get_contents(__DIR__ . '/processes/ViewFileUpload.bpmn'),
[
'2' => User::factory()->create([
'status' => 'ACTIVE',
'is_administrator' => false,
]),
]
);
// Create the user assigned to the task (as self service)
$selfServiceUser = User::factory()->create([
'id' => 15,
'status' => 'ACTIVE',
'is_administrator' => false,
]);
$anotherUser = User::factory()->create([
'status' => 'ACTIVE',
'is_administrator' => false,
]);
// Start a process request
$route = route('api.process_events.trigger', [$this->process->id, 'event' => 'node_1']);
$data = [];
$response = $this->apiCall('POST', $route, $data);
$requestJson = $response->json();
$request = ProcessRequest::find($requestJson['id']);
// Upload a file to the request
$route = route('api.requests.files.store', [$request->id, 'event' => 'node_1']);
$response = $this->actingAs($request->user, 'api')
->json('POST', $route, [
'file' => File::image('photo.jpg'),
'data_name' => 'photo',
]);
// User assigned to the self service task can view the file
$route = route('api.requests.files.index', [$request->id, 'name' => 'photo']);
$response = $this->actingAs($selfServiceUser, 'api')
->json('GET', $route);
$response->assertStatus(200);
// User NOT assigned to the self service task can not view the file
$route = route('api.requests.files.index', [$request->id, 'name' => 'photo']);
$response = $this->actingAs($anotherUser, 'api')
->json('GET', $route);
$response->assertStatus(403);
}
}