From 29877348327b320ff5ab253cbea47243f654fd03 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Wed, 19 Oct 2022 14:24:52 +0100 Subject: [PATCH] only get organizations if user is signed in (#57279) --- .../OrganizationSwitcher.test.tsx | 20 +++++++++++++++++++ .../Organization/OrganizationSwitcher.tsx | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.test.tsx b/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.test.tsx index 5044ecc4023..dcc7a6b3d77 100644 --- a/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.test.tsx +++ b/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.test.tsx @@ -3,6 +3,8 @@ import React from 'react'; import { Provider } from 'react-redux'; import { OrgRole } from '@grafana/data'; +import { ContextSrv, setContextSrv } from 'app/core/services/context_srv'; +import { getUserOrganizations } from 'app/features/org/state/actions'; import { configureStore } from 'app/store/configureStore'; import * as appTypes from 'app/types'; @@ -92,4 +94,22 @@ describe('OrganisationSwitcher', () => { expect(screen.getByRole('button', { name: /change organization/i })).toBeInTheDocument(); }); + + it('should not render and not try to get user organizations if not signed in', () => { + const contextSrv = new ContextSrv(); + contextSrv.user.isSignedIn = false; + setContextSrv(contextSrv); + + renderWithProvider({ + initialState: { + organization: { + organization: { name: 'test', id: 1 }, + userOrgs: [], + }, + }, + }); + + expect(screen.queryByRole('combobox', { name: 'Change organization' })).not.toBeInTheDocument(); + expect(getUserOrganizations).not.toHaveBeenCalled(); + }); }); diff --git a/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.tsx b/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.tsx index faf1621bd20..3727b903a6b 100644 --- a/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.tsx +++ b/public/app/core/components/AppChrome/Organization/OrganizationSwitcher.tsx @@ -4,6 +4,7 @@ import { SelectableValue } from '@grafana/data'; import { locationService } from '@grafana/runtime'; import { useTheme2 } from '@grafana/ui'; import { useMediaQueryChange } from 'app/core/hooks/useMediaQueryChange'; +import { contextSrv } from 'app/core/services/context_srv'; import { getUserOrganizations, setUserOrganization } from 'app/features/org/state/actions'; import { useDispatch, useSelector, UserOrg } from 'app/types'; @@ -23,7 +24,9 @@ export function OrganizationSwitcher() { } }; useEffect(() => { - dispatch(getUserOrganizations()); + if (contextSrv.isSignedIn) { + dispatch(getUserOrganizations()); + } }, [dispatch]); const breakpoint = theme.breakpoints.values.sm;