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

Fix Event not being triggered when a new account is created through OAuth #2421

Closed
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions app/controllers/api/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Appwrite\Database\Validator\UID;
use Appwrite\Database\Validator\Authorization;
use Appwrite\Detector\Detector;
use Appwrite\Event\Event;
use Appwrite\Template\Template;
use Appwrite\OpenSSL\OpenSSL;
use Appwrite\URL\URL as URLParser;
Expand Down Expand Up @@ -524,7 +525,41 @@
'registration' => \time(),
'reset' => false,
'name' => $name,
'prefs' => [],
], ['email' => $email]);

$createUserEvent = clone $audits;
$eventData = $response->output($user, Response::MODEL_USER);

$createUserEvent
->setParam('eventData', $eventData)
->setParam('event', 'account.create')
->setParam('userId', $user->getId())
->setParam('resource', 'users/'.$user->getId())
->setParam('data', ['provider' => $provider])
->trigger();

$functionsEvent = clone $events;

$functionsEvent
->setQueue('v1-functions')
->setClass('FunctionsV1')
->setParam('event', 'account.create')
->setParam('eventData', $eventData)
->setParam('userId', $user->getId())
->setParam('resource', 'users/'.$user->getId())
->trigger();

$webhookEvent = clone $events;

$webhookEvent
->setQueue('v1-webhooks')
->setClass('WebhooksV1')
->setParam('event', 'account.create')
->setParam('eventData', $eventData)
->setParam('userId', $user->getId())
->setParam('resource', 'users/'.$user->getId())
->trigger();
} catch (Duplicate $th) {
throw new Exception('Account already exists', 409);
}
Expand Down
55 changes: 55 additions & 0 deletions tests/e2e/Services/Functions/FunctionsCustomServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function testUpdate($data):array
'events' => [
'account.update.name',
'account.update.email',
'account.create'
],
'schedule' => '0 0 1 1 *',
'timeout' => 5,
Expand All @@ -159,6 +160,7 @@ public function testUpdate($data):array
$this->assertEquals([
'account.update.name',
'account.update.email',
'account.create'
], $response1['body']['events']);
$this->assertEquals('0 0 1 1 *', $response1['body']['schedule']);
$this->assertEquals(5, $response1['body']['timeout']);
Expand Down Expand Up @@ -391,6 +393,59 @@ public function testGetExecution(array $data):array
return $data;
}

/**
* @depends testGetExecution
*/
public function testOAuthCreateEvent($data):array
{
// Fire Mock OAuth Creation
$provider = 'mock';
$appId = '1';
$secret = '123456';

$response = $this->client->call(Client::METHOD_PATCH, '/projects/'.$this->getProject()['$id'].'/oauth2', array_merge([
'origin' => 'http:https://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
]), [
'provider' => $provider,
'appId' => $appId,
'secret' => $secret,
]);

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

$response = $this->client->call(Client::METHOD_GET, '/account/sessions/oauth2/'.$provider, array_merge([
'origin' => 'http:https://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'success' => 'http:https://localhost/v1/mock/tests/general/oauth2/success',
'failure' => 'http:https://localhost/v1/mock/tests/general/oauth2/failure',
]);

$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('success', $response['body']['result']);

sleep(5);

// Check if function got executed.

// Get latest execution logs
$executions = $this->client->call(Client::METHOD_GET, '/functions/'.$data['functionId'].'/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));

$latestExecution = $executions['body']['executions'][1];

$this->assertEquals('event', $latestExecution['trigger']);
PineappleIOnic marked this conversation as resolved.
Show resolved Hide resolved
$this->assertStringContainsString('account.create', $latestExecution['stdout']);

return [];
}

/**
* @depends testGetExecution
*/
Expand Down