1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

Fix old function tests

This commit is contained in:
Bradley Schofield 2022-08-03 13:32:50 +00:00
parent 67fa3f728c
commit f62ad19ecd
6 changed files with 220 additions and 83 deletions

View file

@ -71,7 +71,6 @@ App::post('/v1/functions')
'name' => $name,
'runtime' => $runtime,
'deployment' => '',
'vars' => null,
'events' => $events,
'schedule' => $schedule,
'schedulePrevious' => 0,
@ -909,8 +908,11 @@ App::post('/v1/functions/:functionId/executions')
return $response->dynamic($execution, Response::MODEL_EXECUTION);
}
/** Collect environment variables */
$vars = \array_merge($function->getAttribute('vars', []), [
$variables = $dbForProject->find('variables', [
new Query('functionInternalId', Query::TYPE_EQUAL, [$function->getInternalId()]),
], 5000);
$vars = \array_merge($variables, [
'APPWRITE_FUNCTION_ID' => $function->getId(),
'APPWRITE_FUNCTION_NAME' => $function->getAttribute('name', ''),
'APPWRITE_FUNCTION_DEPLOYMENT' => $deployment->getId(),
@ -1119,7 +1121,7 @@ App::post('/v1/functions/variables/:functionId')
->desc('Create Variable')
->groups(['api', 'functions'])
->label('scope', 'functions.write')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'createVariable')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
@ -1163,7 +1165,7 @@ App::get('/v1/functions/variables/:functionId')
->desc('List Variables')
->groups(['api', 'functions'])
->label('scope', 'functions.read')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'listVariables')
->label('sdk.response.code', Response::STATUS_CODE_OK)
@ -1194,7 +1196,7 @@ App::get('/v1/functions/variables/:functionId/:variableId')
->desc('Get Variable')
->groups(['api', 'functions'])
->label('scope', 'functions.read')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'getVariable')
->label('sdk.response.code', Response::STATUS_CODE_OK)
@ -1227,7 +1229,7 @@ App::put('/v1/functions/variables/:functionId/:variableId')
->desc('Update Variable')
->groups(['api', 'functions'])
->label('scope', 'functions.write')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'updateVariable')
->label('sdk.response.code', Response::STATUS_CODE_OK)
@ -1279,7 +1281,7 @@ App::delete('/v1/functions/variables/:functionId/:variableId')
->desc('Delete Variable')
->groups(['api', 'functions'])
->label('scope', 'functions.write')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
->label('sdk.namespace', 'functions')
->label('sdk.method', 'deleteVariable')
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)

View file

@ -61,12 +61,6 @@ class Func extends Model
'default' => '',
'example' => '5e5ea5c16897e',
])
->addRule('vars', [
'type' => self::TYPE_JSON,
'description' => 'Function environment variables.',
'default' => new \stdClass(),
'example' => ['key' => 'value'],
])
->addRule('events', [
'type' => self::TYPE_STRING,
'description' => 'Function trigger events.',

View file

@ -22,11 +22,6 @@ class FunctionsConsoleClientTest extends Scope
'name' => 'Test',
'execute' => ['user:' . $this->getUser()['$id']],
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',

View file

@ -27,11 +27,6 @@ class FunctionsCustomClientTest extends Scope
], $this->getHeaders()), [
'functionId' => 'unique()',
'name' => 'Test',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
@ -59,11 +54,6 @@ class FunctionsCustomClientTest extends Scope
'name' => 'Test',
'execute' => ['user:' . $this->getUser()['$id']],
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
@ -74,6 +64,38 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $function['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $function['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $function['body']['$id'], [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
$folder = 'php';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);
@ -149,17 +171,44 @@ class FunctionsCustomClientTest extends Scope
'name' => 'Test',
'execute' => ['role:all'],
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
$functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
$folder = 'php-fn';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
@ -334,11 +383,6 @@ class FunctionsCustomClientTest extends Scope
'name' => 'Test',
'execute' => ['role:all'],
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'timeout' => 10,
]);
@ -346,6 +390,38 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apikey,
], [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
$folder = 'php-fn';
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
$this->packageCode($folder);

View file

@ -28,11 +28,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test',
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
@ -50,11 +45,6 @@ class FunctionsCustomServerTest extends Scope
$this->assertIsInt($response1['body']['$createdAt']);
$this->assertIsInt($response1['body']['$updatedAt']);
$this->assertEquals('', $response1['body']['deployment']);
$this->assertEquals([
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
], $response1['body']['vars']);
$this->assertEquals([
'users.*.create',
'users.*.delete',
@ -62,6 +52,35 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals('0 0 1 1 *', $response1['body']['schedule']);
$this->assertEquals(10, $response1['body']['timeout']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
/**
* Test for FAILURE
*/
@ -126,11 +145,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test 2',
'runtime' => 'php-8.0',
'vars' => [
'funcKey1' => 'funcValue1',
'funcKey2' => 'funcValue2',
'funcKey3' => 'funcValue3',
],
'events' => [
'users.*.create',
'users.*.delete',
@ -140,6 +154,35 @@ class FunctionsCustomServerTest extends Scope
]);
$this->assertNotEmpty($response['body']['$id']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $response['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey1',
'value' => 'funcValue1',
]);
$variable2 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $response['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey2',
'value' => 'funcValue2',
]);
$variable3 = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $response['body']['$id'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'funcKey3',
'value' => 'funcValue3',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$this->assertEquals(201, $variable2['headers']['status-code']);
$this->assertEquals(201, $variable3['headers']['status-code']);
$functions = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -232,11 +275,6 @@ class FunctionsCustomServerTest extends Scope
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'name' => 'Test1',
'vars' => [
'key4' => 'value4',
'key5' => 'value5',
'key6' => 'value6',
],
'events' => [
'users.*.update.name',
'users.*.update.email',
@ -251,11 +289,6 @@ class FunctionsCustomServerTest extends Scope
$this->assertIsInt($response1['body']['$createdAt']);
$this->assertIsInt($response1['body']['$updatedAt']);
$this->assertEquals('', $response1['body']['deployment']);
$this->assertEquals([
'key4' => 'value4',
'key5' => 'value5',
'key6' => 'value6',
], $response1['body']['vars']);
$this->assertEquals([
'users.*.update.name',
'users.*.update.email',
@ -714,7 +747,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -799,7 +831,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -908,9 +939,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [
'CUSTOM_VARIABLE' => 'variable',
],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -920,6 +948,17 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
// Create variable
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'CUSTOM_VARIABLE',
'value' => 'variable',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
@ -1013,9 +1052,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [
'CUSTOM_VARIABLE' => 'variable',
],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -1025,6 +1061,17 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'CUSTOM_VARIABLE',
'value' => 'variable',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
@ -1118,9 +1165,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [
'CUSTOM_VARIABLE' => 'variable',
],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -1130,6 +1174,17 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'CUSTOM_VARIABLE',
'value' => 'variable',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
@ -1223,9 +1278,6 @@ class FunctionsCustomServerTest extends Scope
'functionId' => 'unique()',
'name' => 'Test ' . $name,
'runtime' => $name,
'vars' => [
'CUSTOM_VARIABLE' => 'variable',
],
'events' => [],
'schedule' => '',
'timeout' => $timeout,
@ -1235,6 +1287,17 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals(201, $function['headers']['status-code']);
/** Create Variables */
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'CUSTOM_VARIABLE',
'value' => 'variable',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],

View file

@ -443,15 +443,22 @@ class WebhooksCustomServerTest extends Scope
], $this->getHeaders()), [
'name' => 'Test',
'runtime' => 'php-8.0',
'execute' => ['role:all'],
'vars' => [
'key1' => 'value1',
]
'execute' => ['role:all']
]);
$this->assertEquals($function['headers']['status-code'], 200);
$this->assertEquals($function['body']['$id'], $data['functionId']);
$this->assertEquals($function['body']['vars'], ['key1' => 'value1']);
// Create variable
$variable = $this->client->call(Client::METHOD_POST, '/functions/variables/' . $data['functionId'], array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => 'key1',
'value' => 'value1',
]);
$this->assertEquals(201, $variable['headers']['status-code']);
$webhook = $this->getLastRequest();
$signatureExpected = self::getWebhookSignature($webhook, $this->getProject()['signatureKey']);