From 63cfe9a023084067884039819a5b76aba56f1562 Mon Sep 17 00:00:00 2001 From: Sai Sankeerth Date: Thu, 28 Sep 2023 20:37:11 +0530 Subject: [PATCH] fix: add auth status inactive category handling to GA in userDeletion flow Signed-off-by: Sai Sankeerth --- src/v0/destinations/ga/networkHandler.js | 17 +++++++- .../destinations/ga/deleteUsers/data.ts | 42 +++++++++++++++++++ test/integrations/destinations/ga/network.ts | 34 +++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/src/v0/destinations/ga/networkHandler.js b/src/v0/destinations/ga/networkHandler.js index ac244fb4a7..a12f9594cd 100644 --- a/src/v0/destinations/ga/networkHandler.js +++ b/src/v0/destinations/ga/networkHandler.js @@ -1,4 +1,7 @@ -const { REFRESH_TOKEN } = require('../../../adapters/networkhandler/authConstants'); +const { + REFRESH_TOKEN, + AUTH_STATUS_INACTIVE, +} = require('../../../adapters/networkhandler/authConstants'); const { processAxiosResponse, getDynamicErrorType, @@ -29,6 +32,18 @@ const gaResponseHandler = (gaResponse) => { if (isInvalidCredsError || response?.error?.status === 'UNAUTHENTICATED') { throw new InvalidAuthTokenError('invalid credentials', 500, response, REFRESH_TOKEN); } + const isInvalidGrantError = + response?.error.code === 403 && + response.error?.errors?.some((errObj) => errObj.reason === 'insufficientPermissions'); + if (isInvalidGrantError) { + throw new InvalidAuthTokenError( + response?.error?.message || 'insufficent permissions', + 400, + response, + AUTH_STATUS_INACTIVE, + ); + } + throw new NetworkError( `Error occurred while completing deletion request: ${response.error?.message}`, status, diff --git a/test/integrations/destinations/ga/deleteUsers/data.ts b/test/integrations/destinations/ga/deleteUsers/data.ts index d11881e8fe..6cbc5d5cfe 100644 --- a/test/integrations/destinations/ga/deleteUsers/data.ts +++ b/test/integrations/destinations/ga/deleteUsers/data.ts @@ -1,3 +1,5 @@ +import { AUTH_STATUS_INACTIVE } from '../../../../../src/adapters/networkhandler/authConstants'; + export const data = [ { name: 'ga', @@ -136,4 +138,44 @@ export const data = [ }, }, }, + { + name: 'ga', + description: 'Test 3', + feature: 'userDeletion', + module: 'destination', + version: 'v0', + input: { + request: { + headers: { + 'x-rudder-dest-info': '{"secret": { "access_token": "no_permissions_token" }}', + }, + body: [ + { + destType: 'GA', + userAttributes: [ + { + userId: 'test_user_20', + }, + ], + config: { + trackingID: 'UA-123456789-7', + useNativeSDK: false, + }, + }, + ], + }, + }, + output: { + response: { + status: 400, + body: [ + { + statusCode: 400, + authErrorCategory: AUTH_STATUS_INACTIVE, + error: 'User does not have sufficient permissions', + }, + ], + }, + }, + }, ]; diff --git a/test/integrations/destinations/ga/network.ts b/test/integrations/destinations/ga/network.ts index bef0b7a047..acfe5db430 100644 --- a/test/integrations/destinations/ga/network.ts +++ b/test/integrations/destinations/ga/network.ts @@ -289,5 +289,39 @@ const deleteNwData = [ statusText: 'OK', }, }, + { + httpReq: { + method: 'post', + url: 'https://www.googleapis.com/analytics/v3/userDeletion/userDeletionRequests:upsert', + data: { + kind: 'analytics#userDeletionRequest', + id: { + type: 'USER_ID', + userId: 'test_user_20', + }, + webPropertyId: 'UA-123456789-7', + }, + headers: { + Authorization: 'Bearer no_permissions_token', + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + }, + httpRes: { + data: { + error: { + errors: [ + { + reason: 'insufficientPermissions', + }, + ], + code: 403, + message: 'User does not have sufficient permissions', + }, + }, + status: 200, + statusText: 'OK', + }, + }, ]; export const networkCallsData = [...deleteNwData];