From 93b32eec4b435005cb881e9008131f60782b8ec0 Mon Sep 17 00:00:00 2001 From: Virginia Cepeda Date: Fri, 10 Mar 2023 09:56:01 -0300 Subject: [PATCH] Alerting: fix users call 403 by calling /user instead of /users/{id} (#64544) Fetch user data with calling /user endpoint This avoids a permission error we were getting by calling /users/{id} --- public/app/features/alerting/unified/Analytics.test.ts | 8 ++++---- public/app/features/alerting/unified/Analytics.ts | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/public/app/features/alerting/unified/Analytics.test.ts b/public/app/features/alerting/unified/Analytics.test.ts index f884c8d40eb..bea69a3ade4 100644 --- a/public/app/features/alerting/unified/Analytics.test.ts +++ b/public/app/features/alerting/unified/Analytics.test.ts @@ -18,10 +18,10 @@ describe('isNewUser', function () { getBackendSrv().get = jest.fn().mockResolvedValue(newUser); - const isNew = await isNewUser(1); + const isNew = await isNewUser(); expect(isNew).toBe(true); expect(getBackendSrv().get).toHaveBeenCalledTimes(1); - expect(getBackendSrv().get).toHaveBeenCalledWith('/api/users/1'); + expect(getBackendSrv().get).toHaveBeenCalledWith('/api/user'); }); it('should return false if the user has been created prior to the last two weeks', async () => { @@ -32,9 +32,9 @@ describe('isNewUser', function () { getBackendSrv().get = jest.fn().mockResolvedValue(oldUser); - const isNew = await isNewUser(2); + const isNew = await isNewUser(); expect(isNew).toBe(false); expect(getBackendSrv().get).toHaveBeenCalledTimes(1); - expect(getBackendSrv().get).toHaveBeenCalledWith('/api/users/2'); + expect(getBackendSrv().get).toHaveBeenCalledWith('/api/user'); }); }); diff --git a/public/app/features/alerting/unified/Analytics.ts b/public/app/features/alerting/unified/Analytics.ts index ca8481e5136..da31b83ebf1 100644 --- a/public/app/features/alerting/unified/Analytics.ts +++ b/public/app/features/alerting/unified/Analytics.ts @@ -45,9 +45,9 @@ export function withPerformanceLogging Promise }; } -export async function isNewUser(userId: number) { +export async function isNewUser() { try { - const { createdAt } = await getBackendSrv().get(`/api/users/${userId}`); + const { createdAt } = await getBackendSrv().get(`/api/user`); const limitDateForNewUser = dateTime().subtract(USER_CREATION_MIN_DAYS, 'days'); const userCreationDate = dateTime(createdAt); @@ -61,7 +61,7 @@ export async function isNewUser(userId: number) { } export const trackNewAlerRuleFormSaved = async (props: AlertRuleTrackingProps) => { - const isNew = await isNewUser(props.user_id); + const isNew = await isNewUser(); if (isNew) { return; } @@ -69,7 +69,7 @@ export const trackNewAlerRuleFormSaved = async (props: AlertRuleTrackingProps) = }; export const trackNewAlerRuleFormCancelled = async (props: AlertRuleTrackingProps) => { - const isNew = await isNewUser(props.user_id); + const isNew = await isNewUser(); if (isNew) { return; } @@ -77,7 +77,7 @@ export const trackNewAlerRuleFormCancelled = async (props: AlertRuleTrackingProp }; export const trackNewAlerRuleFormError = async (props: AlertRuleTrackingProps & { error: string }) => { - const isNew = await isNewUser(props.user_id); + const isNew = await isNewUser(); if (isNew) { return; }