From d0125398f1d6974a6dcdfa17a5662228d102a30b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 26 Jun 2024 15:46:12 +0100 Subject: [PATCH] fix: test --- app/config/locale/translations/en.json | 2 +- app/controllers/api/account.php | 19 +++++++------------ app/controllers/api/projects.php | 2 +- .../Account/AccountCustomClientTest.php | 5 +++-- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/app/config/locale/translations/en.json b/app/config/locale/translations/en.json index 7d6bad9989..953888013a 100644 --- a/app/config/locale/translations/en.json +++ b/app/config/locale/translations/en.json @@ -21,7 +21,7 @@ "emails.sessionAlert.subject": "New session alert for {{project}}", "emails.sessionAlert.hello":"Hello {{user}}", "emails.sessionAlert.body": "We're writing to inform you that a new session has been initiated on your {{b}}{{project}}{{/b}} account, on {{b}}{{dateTime}}{{/b}}. \nHere are the details of the new session: ", - "emails.sessionAlert.listDevice": "Device: {{b}}{{agentDevice}}{{/b}}", + "emails.sessionAlert.listDevice": "Device: {{b}}{{device}}{{/b}}", "emails.sessionAlert.listIpAddress": "IP Address: {{b}}{{ipAddress}}{{/b}}", "emails.sessionAlert.listCountry": "Country: {{b}}{{country}}{{/b}}", "emails.sessionAlert.footer": "If you didn't request the sign in, you can safely ignore this email. If you suspect unauthorized activity, please secure your account immediately.", diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 52db2cbd53..8f46ca3c62 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -58,17 +58,11 @@ use Utopia\Validator\WhiteList; $oauthDefaultSuccess = '/auth/oauth2/success'; $oauthDefaultFailure = '/auth/oauth2/failure'; -function sendSessionAlert(Request $request, Locale $locale, Document $user, Document $project, Reader $geodb, Mail $queueForMails) +function sendSessionAlert(Locale $locale, Document $user, Document $project, Document $session, Mail $queueForMails) { $subject = $locale->getText("emails.sessionAlert.subject"); $customTemplate = $project->getAttribute('templates', [])['email.sessionAlert-' . $locale->default] ?? []; - $detector = new Detector($request->getUserAgent('UNKNOWN')); - $agentDevice = $detector->getDevice(); - - $record = $geodb->get($request->getIP()); - $countryCode = $record['country']['iso_code'] ?? ''; - $message = Template::fromFile(__DIR__ . '/../../config/locale/templates/email-session-alert.tpl'); $message ->setParam('{{hello}}', $locale->getText("emails.sessionAlert.hello")) @@ -77,6 +71,7 @@ function sendSessionAlert(Request $request, Locale $locale, Document $user, Docu ->setParam('{{listIpAddress}}', $locale->getText("emails.sessionAlert.listIpAddress")) ->setParam('{{listCountry}}', $locale->getText("emails.sessionAlert.listCountry")) ->setParam('{{footer}}', $locale->getText("emails.sessionAlert.footer")) + ->setParam('{{thanks}}', $locale->getText("emails.sessionAlert.thanks")) ->setParam('{{signature}}', $locale->getText("emails.sessionAlert.signature")); $body = $message->render(); @@ -132,9 +127,9 @@ function sendSessionAlert(Request $request, Locale $locale, Document $user, Docu 'dateTime' => DateTime::format(new \DateTime(), 'Y-m-d H:i:s'), 'user' => $user->getAttribute('name'), 'project' => $project->getAttribute('name'), - 'agentDevice' => $agentDevice['deviceBrand'] ?? $agentDevice['deviceBrand'] ?? 'UNKNOWN', - 'ipAddress' => $request->getIP(), - 'country' => $locale->getText('countries.' . strtolower($countryCode), $locale->getText('locale.country.unknown')), + 'device' => $session->getAttribute('clientName'), + 'ipAddress' => $session->getAttribute('ip'), + 'country' => $locale->getText('countries.' . $session->getAttribute('countryCode'), $locale->getText('locale.country.unknown')), ]; $email = $user->getAttribute('email'); @@ -229,7 +224,7 @@ $createSession = function (string $userId, string $secret, Request $request, Res } if ($project->getAttribute('auths', [])['sessionAlerts'] ?? false) { - sendSessionAlert($request, $locale, $user, $project, $geodb, $queueForMails); + sendSessionAlert($locale, $user, $project, $session, $queueForMails); } $queueForEvents @@ -909,7 +904,7 @@ App::post('/v1/account/sessions/email') ; if ($project->getAttribute('auths', [])['sessionAlerts'] ?? false) { - sendSessionAlert($request, $locale, $user, $project, $geodb, $queueForMails); + sendSessionAlert($locale, $user, $project, $session, $queueForMails); } $response->dynamic($session, Response::MODEL_SESSION); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index dd97657448..1533456f03 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -101,7 +101,7 @@ App::post('/v1/projects') $auths = [ 'limit' => 0, 'maxSessions' => APP_LIMIT_USER_SESSIONS_DEFAULT, - 'passwordHistory' => 0, + 'passwordHistory' => 0, 'passwordDictionary' => false, 'duration' => Auth::TOKEN_EXPIRATION_LOGIN_LONG, 'personalDataCheck' => false, diff --git a/tests/e2e/Services/Account/AccountCustomClientTest.php b/tests/e2e/Services/Account/AccountCustomClientTest.php index 0405714f0e..7a6b469d9b 100644 --- a/tests/e2e/Services/Account/AccountCustomClientTest.php +++ b/tests/e2e/Services/Account/AccountCustomClientTest.php @@ -1230,6 +1230,7 @@ class AccountCustomClientTest extends Scope 'origin' => 'http://localhost', 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], + 'user-agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', ]), [ 'email' => $email, 'password' => $password, @@ -1243,8 +1244,8 @@ class AccountCustomClientTest extends Scope $this->assertEquals($email, $lastEmail['to'][0]['address']); $this->assertStringContainsString('New session alert', $lastEmail['subject']); $this->assertStringContainsString($response['body']['ip'], $lastEmail['text']); // IP Address - $this->assertStringContainsString($response['body']['osName'], $lastEmail['text']); // OS Name - $this->assertStringContainsString($response['body']['clientType'], $lastEmail['text']); // Client Type + $this->assertStringContainsString('Unknown', $lastEmail['text']); // Country + $this->assertStringContainsString($response['body']['clientName'], $lastEmail['text']); // Client name } /**