From 0e39ca5cfcf865c82d372c6386bc495a8f4293c3 Mon Sep 17 00:00:00 2001 From: Lauren Zugai Date: Fri, 31 May 2024 17:01:40 -0500 Subject: [PATCH] feat(metrics): Add reg_complete backend glean event for third party auth new account creation Because: * We are emitting this event for successful signup, but not for successful third party auth new account signup This commit: * Adds the event closes FXA-9752 --- packages/fxa-auth-server/lib/routes/index.js | 3 ++- .../fxa-auth-server/lib/routes/linked-accounts.ts | 11 ++++++++--- .../test/local/routes/linked-accounts.js | 13 ++++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/fxa-auth-server/lib/routes/index.js b/packages/fxa-auth-server/lib/routes/index.js index 77900f3e4f1..c43e92ad6de 100644 --- a/packages/fxa-auth-server/lib/routes/index.js +++ b/packages/fxa-auth-server/lib/routes/index.js @@ -200,7 +200,8 @@ module.exports = function ( config, mailer, profile, - statsd + statsd, + glean ); const { cloudTaskRoutes } = require('./cloud-tasks'); diff --git a/packages/fxa-auth-server/lib/routes/linked-accounts.ts b/packages/fxa-auth-server/lib/routes/linked-accounts.ts index 4510105e8f1..8abd9896772 100644 --- a/packages/fxa-auth-server/lib/routes/linked-accounts.ts +++ b/packages/fxa-auth-server/lib/routes/linked-accounts.ts @@ -31,6 +31,7 @@ import { appleEventHandlers, AppleSETEvent, } from './utils/third-party-events'; +import { gleanMetrics } from '../metrics/glean'; const HEX_STRING = validators.HEX_STRING; @@ -48,7 +49,8 @@ export class LinkedAccountHandler { private config: ConfigType, private mailer: any, private profile: ProfileClient, - private statsd: { increment: (value: string) => void } + private statsd: { increment: (value: string) => void }, + private glean: ReturnType ) { if (config.googleAuthConfig && config.googleAuthConfig.clientId) { this.googleAuthClient = new OAuth2Client( @@ -416,6 +418,7 @@ export class LinkedAccountHandler { flowBeginTime, service, }); + this.glean.registration.complete(request, { uid: accountRecord.uid }); } } else { // This is an existing user and existing FxA user @@ -492,7 +495,8 @@ export const linkedAccountRoutes = ( config: ConfigType, mailer: any, profile: ProfileClient, - statsd: any + statsd: any, + glean: ReturnType ) => { const handler = new LinkedAccountHandler( log, @@ -500,7 +504,8 @@ export const linkedAccountRoutes = ( config, mailer, profile, - statsd + statsd, + glean ); return [ diff --git a/packages/fxa-auth-server/test/local/routes/linked-accounts.js b/packages/fxa-auth-server/test/local/routes/linked-accounts.js index 607ae7d54ed..2bd43f0de40 100644 --- a/packages/fxa-auth-server/test/local/routes/linked-accounts.js +++ b/packages/fxa-auth-server/test/local/routes/linked-accounts.js @@ -10,6 +10,7 @@ const getRoute = require('../../routes_helpers').getRoute; const mocks = require('../../mocks'); const error = require('../../../lib/error'); const proxyquire = require('proxyquire'); +const glean = mocks.mockGlean(); const GOOGLE_PROVIDER = 'google'; const APPLE_PROVIDER = 'apple'; @@ -29,7 +30,7 @@ const makeRoutes = function (options = {}, requireMocks) { requireMocks || {} ); - return linkedAccountRoutes(log, db, config, mailer, profile, statsd); + return linkedAccountRoutes(log, db, config, mailer, profile, statsd, glean); }; function runTest(route, request, assertions) { @@ -110,6 +111,7 @@ describe('/linked_account', function () { ), '/linked_account/login' ); + glean.registration.complete.reset(); }); it('fails if no google config', async () => { @@ -161,7 +163,7 @@ describe('/linked_account', function () { assert.ok(result.sessionToken); }); - it('should create new fxa account from new google account and return session', async () => { + it('should create new fxa account from new google account, return session, emit Glean events', async () => { mockDB.accountRecord = sinon.spy(() => Promise.reject(new error.unknownAccount(mockGoogleUser.email)) ); @@ -201,6 +203,7 @@ describe('/linked_account', function () { assert.equal(result.uid, UID); assert.ok(result.sessionToken); + assert.calledOnce(glean.registration.complete); }); it('should linking existing fxa account and new google account and return session', async () => { @@ -224,6 +227,8 @@ describe('/linked_account', function () { assert.isTrue(mockDB.createSessionToken.calledOnce); assert.equal(result.uid, UID); assert.ok(result.sessionToken); + // should not be called for existing account + assert.notCalled(glean.registration.complete); }); it('should return session with valid google id token', async () => { @@ -336,6 +341,7 @@ describe('/linked_account', function () { ), '/linked_account/login' ); + glean.registration.complete.reset(); }); it('fails if no apple config', async () => { @@ -390,7 +396,7 @@ describe('/linked_account', function () { assert.ok(result.sessionToken); }); - it('should create new fxa account from new apple account and return session', async () => { + it('should create new fxa account from new apple account, return session, emit Glean events', async () => { mockDB.accountRecord = sinon.spy(() => Promise.reject(new error.unknownAccount(mockAppleUser.email)) ); @@ -414,6 +420,7 @@ describe('/linked_account', function () { assert.isTrue(mockDB.createSessionToken.calledOnce); assert.equal(result.uid, UID); assert.ok(result.sessionToken); + assert.calledOnce(glean.registration.complete); }); it('should link existing fxa account and new apple account and return session', async () => {