Skip to content

Commit

Permalink
Merge pull request #8228 from ItzNotABug/add-test-for-user-id
Browse files Browse the repository at this point in the history
Add Test to Validate Headers aren't Overridden
  • Loading branch information
stnguyen90 committed Jun 17, 2024
2 parents f91a8a9 + 37da4a0 commit 120b5ae
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions tests/e2e/Services/Functions/FunctionsCustomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,94 @@ public function testSynchronousExecution(): array

return [];
}

public function testNonOverrideOfHeaders(): array
{
/**
* Test for SUCCESS
*/
$projectId = $this->getProject()['$id'];
$apikey = $this->getProject()['apiKey'];

$function = $this->client->call(Client::METHOD_POST, '/functions', [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'functionId' => ID::unique(),
'name' => 'Test',
'execute' => [Role::any()->toString()],
'runtime' => 'node-18.0',
'entrypoint' => 'index.js'
]);

$functionId = $function['body']['$id'] ?? '';

$this->assertEquals(201, $function['headers']['status-code']);

$folder = 'node';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);

$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'entrypoint' => 'index.js',
'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), //different tarball names intentional
'activate' => true
]);

$deploymentId = $deployment['body']['$id'] ?? '';

$this->assertEquals(202, $deployment['headers']['status-code']);

// Poll until deployment is built
while (true) {
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);

if (
$deployment['headers']['status-code'] >= 400
|| \in_array($deployment['body']['status'], ['ready', 'failed'])
) {
break;
}

\sleep(1);
}

$this->assertEquals('ready', $deployment['body']['status']);

$execution = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'x-appwrite-event' => "OVERRIDDEN",
'x-appwrite-trigger' => "OVERRIDDEN",
'x-appwrite-user-id' => "OVERRIDDEN",
'x-appwrite-user-jwt' => "OVERRIDDEN",
]);

$output = json_decode($execution['body']['responseBody'], true);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_JWT']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_EVENT']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_TRIGGER']);
$this->assertNotEquals('OVERRIDDEN', $output['APPWRITE_FUNCTION_USER_ID']);

// Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], []);

$this->assertEquals(204, $response['headers']['status-code']);

return [];
}
}

0 comments on commit 120b5ae

Please sign in to comment.