-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathEnvironmentVariablesTest.php
More file actions
258 lines (219 loc) · 8.46 KB
/
Copy pathEnvironmentVariablesTest.php
File metadata and controls
258 lines (219 loc) · 8.46 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
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace Tests\Feature\Api;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Hash;
use ProcessMaker\Models\EnvironmentVariable;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;
class EnvironmentVariablesTest extends TestCase
{
use RequestHelper;
const API_TEST_VARIABLES = '/environment_variables';
/** @test */
public function test_it_should_create_an_environment_variable()
{
$data = [
'name' => 'testvariable',
'description' => 'test description',
'value' => 'testsecret',
];
$response = $this->apiCall('POST', self::API_TEST_VARIABLES, $data);
// Check for created status code
$response->assertStatus(201);
// Ensure there is a record in the database that matches
unset($data['value']);
$this->assertDatabaseHas('environment_variables', $data);
}
/** @test */
public function test_it_should_store_values_as_encrypted()
{
$variable = EnvironmentVariable::factory()->create([
'value' => 'testvalue',
]);
$this->assertDatabaseMissing('environment_variables', ['value' => 'testvalue']);
// Now fetch record
$variable->refresh();
$this->assertEquals('testvalue', $variable->value);
}
/** @test */
public function test_it_should_have_validation_errors_on_name_uniqueness_during_create()
{
// Create an environment variable with a set name
EnvironmentVariable::factory()->create([
'name' => 'testname',
]);
// Data with a duplicate name
$data = [
'name' => 'testname',
'description' => 'test',
'value' => 'testvalue',
];
$response = $this->apiCall('POST', self::API_TEST_VARIABLES, $data);
// Check for validation error status code
$response->assertStatus(422);
// Ensure the record does NOT exist
unset($data['value']);
$this->assertDatabaseMissing('environment_variables', $data);
}
/** @test */
public function test_it_should_not_allow_whitespace_in_variable_name()
{
// Data with a name with a space
$data = [
'name' => 'test name',
'description' => 'test',
'value' => 'testvalue',
];
$response = $this->apiCall('POST', self::API_TEST_VARIABLES, $data);
// Check for validation error status code
$response->assertStatus(422);
}
/** @test */
public function test_it_should_successfully_return_an_environment_variable()
{
// Create an environment variable with a set name
$variable = EnvironmentVariable::factory()->create([
'name' => 'testname',
'value' => 'testvalue',
]);
$variable->fresh();
// Is now fetch the variable and see if success
$response = $this->apiCall('get', self::API_TEST_VARIABLES . '/' . $variable->id);
$response->assertStatus(200);
$response->assertJsonStructure([
'id',
'name',
'description',
]);
// Ensure the JSON response does NOT have value attribute, as this should be hidden
}
/** @test */
public function test_it_should_have_validation_errors_on_name_uniqueness_during_update()
{
// Create an environment variable with a set name for the update
$variable = EnvironmentVariable::factory()->create([
'name' => 'testname',
'value' => 'testvalue',
]);
// Create a variable with another name that will clash with the uniqueness rule
EnvironmentVariable::factory()->create([
'name' => 'anothername',
'value' => 'testvalue',
]);
$variable->fresh();
$data = [
'name' => 'anothername',
'description' => 'testdescription',
'value' => 'differentvalue',
];
$response = $this->apiCall('PUT', self::API_TEST_VARIABLES . '/' . $variable->id, $data);
// Check for validation error status code
$response->assertStatus(422);
}
/** @test */
public function test_it_should_successfully_update_an_environment_variable()
{
// Create an environment variable with a set name
$variable = EnvironmentVariable::factory()->create([
'name' => 'testname',
'value' => 'testvalue',
]);
$variable->fresh();
$data = [
'name' => 'newname',
'description' => 'newdescription',
'value' => 'newvalue',
];
$response = $this->apiCall('PUT', self::API_TEST_VARIABLES . '/' . $variable->id, $data);
$response->assertStatus(200);
$data['id'] = $variable->id;
unset($data['value']);
$this->assertDatabaseHas('environment_variables', $data);
$variable = EnvironmentVariable::where('name', 'newname')->first();
$this->assertEquals('newvalue', $variable->value);
}
/** @test */
public function test_it_should_return_paginated_environment_variables_during_index()
{
// Can't truncate because of DatabaseTransactions
EnvironmentVariable::whereNotNull('id')->delete();
$this->withoutExceptionHandling();
EnvironmentVariable::factory()->count(50)->create();
// Fetch from index
$response = $this->apiCall('GET', self::API_TEST_VARIABLES);
// Verify 200 status code
$response->assertStatus(200);
// Grab users
$data = json_decode($response->getContent(), true);
// Verify we have a total of 9 results (our 5 plus admin plus our created user)
$this->assertCount(10, $data['data']);
$this->assertEquals(50, $data['meta']['total']);
}
/** @test */
public function test_it_should_return_filtered_environment_variables()
{
EnvironmentVariable::factory()->count(50)->create();
// Put in a match
EnvironmentVariable::factory()->create([
'name' => 'matchingfield',
]);
// Fetch from index
$response = $this->apiCall('GET', self::API_TEST_VARIABLES . '?filter=' . urlencode('matchingfield'));
$response->assertStatus(200);
$data = json_decode($response->getContent(), true);
// Ensure we have empty results
$this->assertCount(1, $data['data']);
$this->assertEquals(1, $data['meta']['total']);
$this->assertEquals('matchingfield', $data['meta']['filter']);
}
/** @test */
public function test_it_should_successfully_remove_environment_variable()
{
// Create an environment variable with a set name
$variable = EnvironmentVariable::factory()->create([
'name' => 'testname',
'value' => 'testvalue',
]);
$variable->fresh();
$response = $this->apiCall('DELETE', self::API_TEST_VARIABLES . '/' . $variable->id);
$response->assertStatus(200);
$this->assertDatabaseMissing('environment_variables', [
'name' => 'testname',
'value' => 'testvalue',
]);
}
/** @test */
public function test_it_value_does_not_change_if_value_is_null()
{
// Create an environment variable with a set name
$variable = EnvironmentVariable::factory()->create([
'name' => 'testname',
'value' => 'testvalue',
]);
$variable->fresh();
$data = [
'name' => 'newname',
'description' => 'newdescription',
'value' => '',
];
$response = $this->apiCall('PUT', self::API_TEST_VARIABLES . '/' . $variable->id, $data);
$response->assertStatus(200);
$data['id'] = $variable->id;
unset($data['value']);
$this->assertDatabaseHas('environment_variables', $data);
$variable = EnvironmentVariable::where('name', 'newname')->first();
$this->assertEquals('testvalue', $variable->value);
}
public function testCreationOfMetricVariable()
{
Artisan::call('db:seed', ['class' => 'MetricsApiEnvironmentVariableSeeder']);
// Assert only one record exists
$this->assertCount(1, EnvironmentVariable::where('name', 'METRICS_API_ENDPOINT')->get());
// Assert the original values were preserved
$this->assertDatabaseHas('environment_variables', [
'name' => 'METRICS_API_ENDPOINT',
]);
}
}