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}
This commit is contained in:
Virginia Cepeda 2023-03-10 09:56:01 -03:00 committed by GitHub
parent 79152969f3
commit 93b32eec4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 9 deletions

View File

@ -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');
});
});

View File

@ -45,9 +45,9 @@ export function withPerformanceLogging<TFunc extends (...args: any[]) => 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;
}