1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00

Merge pull request #8228 from ItzNotABug/add-test-for-user-id

Add Test to Validate Headers aren't Overridden
This commit is contained in:
Steven Nguyen 2024-06-17 16:09:50 -07:00 committed by GitHub
commit 120b5aee47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -716,4 +716,94 @@ class FunctionsCustomClientTest extends Scope
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 [];
}
}