Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test to Validate Headers aren't Overridden #8228

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add: test to validate user-id isn't overridden.
  • Loading branch information
ItzNotABug committed Jun 1, 2024
commit 1a42d118f64b6af90ffa6787e58079e1dcd34e05
88 changes: 88 additions & 0 deletions tests/e2e/Services/Functions/FunctionsCustomClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,92 @@ public function testSynchronousExecution(): array

return [];
}

public function testExecutionWithUserId(): 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-user-id' => "665b0df20031bdf527fb",
]);

$output = json_decode($execution['body']['responseBody'], true);
$this->assertNotEquals('665b0df20031bdf527fb', $this->getUser()['$id']);
$this->assertEquals($this->getUser()['$id'], $output['APPWRITE_FUNCTION_USER_ID']);
ItzNotABug marked this conversation as resolved.
Show resolved Hide resolved
// Client should never see logs and errors
$this->assertEmpty($execution['body']['logs']);
$this->assertEmpty($execution['body']['errors']);

// 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 [];
}
}