diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index c236509dd6b..bc024b0948c 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -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; @@ -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); } diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 4534508b9bc..99521cf21b0 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -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, @@ -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']); @@ -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://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://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ]), [ + 'success' => 'http://localhost/v1/mock/tests/general/oauth2/success', + 'failure' => 'http://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']); + $this->assertStringContainsString('account.create', $latestExecution['stdout']); + + return []; + } + /** * @depends testGetExecution */