Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upcoming: [M3-7660] - Cleanup files to use profile to get user_type #10102

Merged
merged 9 commits into from
Feb 1, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Upcoming Features
---

Cleanup files to use profile to get user_type ([#10102](https://github.com/linode/manager/pull/10102))
6 changes: 2 additions & 4 deletions packages/manager/src/features/Account/AccountLanding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { TabPanels } from 'src/components/Tabs/TabPanels';
import { Tabs } from 'src/components/Tabs/Tabs';
import { useFlags } from 'src/hooks/useFlags';
import { useAccount } from 'src/queries/account';
import { useAccountUser } from 'src/queries/accountUsers';
import { useGrants, useProfile } from 'src/queries/profile';

import AccountLogins from './AccountLogins';
Expand Down Expand Up @@ -48,7 +47,6 @@ const AccountLanding = () => {
const { data: account } = useAccount();
const { data: grants } = useGrants();
const { data: profile } = useProfile();
const { data: user } = useAccountUser(profile?.username ?? '');

const flags = useFlags();
const [isDrawerOpen, setIsDrawerOpen] = React.useState<boolean>(false);
Expand Down Expand Up @@ -118,7 +116,7 @@ const AccountLanding = () => {
const isBillingTabSelected = location.pathname.match(/billing/);
const canSwitchBetweenParentOrProxyAccount =
flags.parentChildAccountAccess &&
(user?.user_type === 'parent' || user?.user_type === 'proxy');
(profile?.user_type === 'parent' || profile?.user_type === 'proxy');

const landingHeaderProps: LandingHeaderProps = {
breadcrumbProps: {
Expand Down Expand Up @@ -174,7 +172,7 @@ const AccountLanding = () => {
</React.Suspense>
</Tabs>
<SwitchAccountDrawer
isProxyUser={user?.user_type === 'proxy'}
isProxyUser={profile?.user_type === 'proxy'}
jaalah-akamai marked this conversation as resolved.
Show resolved Hide resolved
onClose={() => setIsDrawerOpen(false)}
open={isDrawerOpen}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent } from '@testing-library/react';
import * as React from 'react';

import { accountUserFactory } from 'src/factories/accountUsers';
import { profileFactory } from 'src/factories/profile';
import { rest, server } from 'src/mocks/testServer';
import { renderWithTheme } from 'src/utilities/testHelpers';

Expand Down Expand Up @@ -32,8 +32,8 @@ describe('SwitchAccountDrawer', () => {

it('should include a link to switch back to the parent account if the active user is a proxy user', async () => {
server.use(
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'proxy' })));
rest.get('*/account/profile/*', (req, res, ctx) => {
jaalah-akamai marked this conversation as resolved.
Show resolved Hide resolved
return res(ctx.json(profileFactory.build({ user_type: 'proxy' })));
jaalah-akamai marked this conversation as resolved.
Show resolved Hide resolved
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { waitFor, within } from '@testing-library/react';
import * as React from 'react';

import { accountFactory } from 'src/factories/account';
import { accountUserFactory } from 'src/factories/accountUsers';
import { profileFactory } from 'src/factories/profile';
import { ChildAccountList } from 'src/features/Account/SwitchAccounts/ChildAccountList';
import { makeResourcePage } from 'src/mocks/serverHandlers';
import { rest, server } from 'src/mocks/testServer';
Expand All @@ -18,7 +18,7 @@ const props = {
it('should display a list of child accounts', async () => {
server.use(
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'parent' })));
return res(ctx.json(profileFactory.build({ user_type: 'parent' })));
jaalah-akamai marked this conversation as resolved.
Show resolved Hide resolved
}),
rest.get('*/account/child-accounts', (req, res, ctx) => {
return res(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import userEvent from '@testing-library/user-event';
import * as React from 'react';

import { appTokenFactory } from 'src/factories';
import { accountUserFactory } from 'src/factories/accountUsers';
import { profileFactory } from 'src/factories/profile';
import { rest, server } from 'src/mocks/testServer';
import { renderWithTheme } from 'src/utilities/testHelpers';

import { CreateAPITokenDrawer } from './CreateAPITokenDrawer';

// Mock the useAccountUser hooks to immediately return the expected data, circumventing the HTTP request and loading state.
// Mock the useProfile hooks to immediately return the expected data, circumventing the HTTP request and loading state.
const queryMocks = vi.hoisted(() => ({
useAccountUser: vi.fn().mockReturnValue({}),
useProfile: vi.fn().mockReturnValue({}),
}));

vi.mock('src/queries/accountUsers', async () => {
const actual = await vi.importActual<any>('src/queries/accountUsers');
vi.mock('src/queries/profile', async () => {
const actual = await vi.importActual<any>('src/queries/profile');
return {
...actual,
useAccountUser: queryMocks.useAccountUser,
useProfile: queryMocks.useProfile,
};
});

Expand Down Expand Up @@ -88,8 +88,8 @@ describe('Create API Token Drawer', () => {
});

it('Should show the Child Account Access scope for a parent user account with the parent/child feature flag on', () => {
queryMocks.useAccountUser.mockReturnValue({
data: accountUserFactory.build({ user_type: 'parent' }),
queryMocks.useProfile.mockReturnValue({
data: profileFactory.build({ user_type: 'parent' }),
});

const { getByText } = renderWithTheme(<CreateAPITokenDrawer {...props} />, {
Expand All @@ -100,8 +100,8 @@ describe('Create API Token Drawer', () => {
});

it('Should not show the Child Account Access scope for a non-parent user account with the parent/child feature flag on', () => {
queryMocks.useAccountUser.mockReturnValue({
data: accountUserFactory.build({ user_type: null }),
queryMocks.useProfile.mockReturnValue({
data: profileFactory.build({ user_type: null }),
});

const { queryByText } = renderWithTheme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { AccessCell } from 'src/features/ObjectStorage/AccessKeyLanding/AccessCe
import { VPC_READ_ONLY_TOOLTIP } from 'src/features/VPCs/constants';
import { useFlags } from 'src/hooks/useFlags';
import { useAccount } from 'src/queries/account';
import { useAccountUser } from 'src/queries/accountUsers';
import { useProfile } from 'src/queries/profile';
import { useCreatePersonalAccessTokenMutation } from 'src/queries/tokens';
import { isFeatureEnabled } from 'src/utilities/accountCapabilities';
Expand Down Expand Up @@ -99,7 +98,6 @@ export const CreateAPITokenDrawer = (props: Props) => {

const { data: profile } = useProfile();
const { data: account } = useAccount();
const { data: user } = useAccountUser(profile?.username ?? '');

const {
error,
Expand Down Expand Up @@ -190,7 +188,7 @@ export const CreateAPITokenDrawer = (props: Props) => {
const allPermissions = form.values.scopes;

const showFilteredPermissions =
(flags.parentChildAccountAccess && user?.user_type !== 'parent') ||
(flags.parentChildAccountAccess && profile?.user_type !== 'parent') ||
Boolean(!flags.parentChildAccountAccess);

const filteredPermissions = allPermissions.filter(
Expand Down
49 changes: 28 additions & 21 deletions packages/manager/src/features/TopMenu/UserMenu/UserMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { fireEvent, within } from '@testing-library/react';
import * as React from 'react';

import { accountFactory, profileFactory } from 'src/factories';
import { accountUserFactory } from 'src/factories/accountUsers';
import { rest, server } from 'src/mocks/testServer';
import { mockMatchMedia, renderWithTheme } from 'src/utilities/testHelpers';

Expand All @@ -25,10 +24,14 @@ describe('UserMenu', () => {
);
}),
rest.get('*/profile', (req, res, ctx) => {
return res(ctx.json(profileFactory.build({ username: 'parent-user' })));
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'parent' })));
return res(
ctx.json(
profileFactory.build({
user_type: 'parent',
username: 'parent-user',
})
)
);
})
);

Expand All @@ -48,10 +51,14 @@ describe('UserMenu', () => {
);
}),
rest.get('*/profile', (req, res, ctx) => {
return res(ctx.json(profileFactory.build({ username: 'parent-user' })));
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'proxy' })));
return res(
ctx.json(
profileFactory.build({
user_type: 'proxy',
username: 'parent-user',
})
)
);
})
);

Expand All @@ -71,10 +78,11 @@ describe('UserMenu', () => {
);
}),
rest.get('*/profile', (req, res, ctx) => {
return res(ctx.json(profileFactory.build({ username: 'child-user' })));
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'child' })));
return res(
ctx.json(
profileFactory.build({ user_type: 'child', username: 'child-user' })
)
);
})
);

Expand All @@ -93,11 +101,10 @@ describe('UserMenu', () => {
}),
rest.get('*/profile', (req, res, ctx) => {
return res(
ctx.json(profileFactory.build({ username: 'regular-user' }))
ctx.json(
profileFactory.build({ user_type: null, username: 'regular-user' })
)
);
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: null })));
})
);

Expand All @@ -116,8 +123,8 @@ describe('UserMenu', () => {
ctx.json(accountFactory.build({ company: 'Parent Company' }))
);
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'parent' })));
rest.get('*/profile', (req, res, ctx) => {
return res(ctx.json(profileFactory.build({ user_type: 'parent' })));
})
);

Expand All @@ -141,8 +148,8 @@ describe('UserMenu', () => {
ctx.json(accountFactory.build({ company: 'Child Company' }))
);
}),
rest.get('*/account/users/*', (req, res, ctx) => {
return res(ctx.json(accountUserFactory.build({ user_type: 'proxy' })));
rest.get('*/profile', (req, res, ctx) => {
return res(ctx.json(profileFactory.build({ user_type: 'proxy' })));
})
);

Expand Down
8 changes: 3 additions & 5 deletions packages/manager/src/features/TopMenu/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { SwitchAccountButton } from 'src/features/Account/SwitchAccountButton';
import { SwitchAccountDrawer } from 'src/features/Account/SwitchAccountDrawer';
import { useFlags } from 'src/hooks/useFlags';
import { useAccount } from 'src/queries/account';
import { useAccountUser } from 'src/queries/accountUsers';
import { useGrants, useProfile } from 'src/queries/profile';
import { getStorage } from 'src/utilities/storage';

Expand Down Expand Up @@ -56,7 +55,6 @@ export const UserMenu = React.memo(() => {

const { data: account } = useAccount();
const { data: profile } = useProfile();
const { data: user } = useAccountUser(profile?.username ?? '');
const { data: grants } = useGrants();
const { enqueueSnackbar } = useSnackbar();
const flags = useFlags();
Expand All @@ -67,13 +65,13 @@ export const UserMenu = React.memo(() => {
const hasAccountAccess = !isRestrictedUser || hasGrant('account_access');
const hasReadWriteAccountAccess = hasGrant('account_access') === 'read_write';
const hasParentChildAccountAccess = Boolean(flags.parentChildAccountAccess);
const isParentUser = user?.user_type === 'parent';
const isProxyUser = user?.user_type === 'proxy';
const isParentUser = profile?.user_type === 'parent';
const isProxyUser = profile?.user_type === 'proxy';
jaalah-akamai marked this conversation as resolved.
Show resolved Hide resolved
const canSwitchBetweenParentOrProxyAccount =
hasParentChildAccountAccess && (isParentUser || isProxyUser);
const open = Boolean(anchorEl);
const id = open ? 'user-menu-popover' : undefined;
const companyName = (user?.user_type && account?.company) ?? '';
const companyName = (profile?.user_type && account?.company) ?? '';
const showCompanyName = hasParentChildAccountAccess && companyName;

// Used for fetching parent profile and account data by making a request with the parent's token.
Expand Down
7 changes: 2 additions & 5 deletions packages/manager/src/queries/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { useMutation, useQuery, useQueryClient } from 'react-query';

import { useGrants, useProfile } from 'src/queries/profile';

import { useAccountUser } from './accountUsers';
import { queryPresets } from './base';

import type {
Expand Down Expand Up @@ -54,7 +53,6 @@ export const useChildAccounts = ({
params,
}: RequestOptions) => {
const { data: profile } = useProfile();
const { data: user } = useAccountUser(profile?.username ?? '');
const { data: grants } = useGrants();
const hasExplicitAuthToken = Boolean(headers?.Authorization);

Expand All @@ -63,7 +61,7 @@ export const useChildAccounts = ({
() => getChildAccounts({ filter, headers, params }),
{
enabled:
(Boolean(user?.user_type === 'parent') && !profile?.restricted) ||
(Boolean(profile?.user_type === 'parent') && !profile?.restricted) ||
Boolean(grants?.global?.child_account_access) ||
hasExplicitAuthToken,
keepPreviousData: true,
Expand All @@ -73,7 +71,6 @@ export const useChildAccounts = ({

export const useChildAccount = ({ euuid, headers }: ChildAccountPayload) => {
const { data: profile } = useProfile();
const { data: user } = useAccountUser(profile?.username ?? '');
const { data: grants } = useGrants();
const hasExplicitAuthToken = Boolean(headers?.Authorization);

Expand All @@ -82,7 +79,7 @@ export const useChildAccount = ({ euuid, headers }: ChildAccountPayload) => {
() => getChildAccount({ euuid }),
{
enabled:
(Boolean(user?.user_type === 'parent') && !profile?.restricted) ||
(Boolean(profile?.user_type === 'parent') && !profile?.restricted) ||
Boolean(grants?.global?.child_account_access) ||
hasExplicitAuthToken,
}
Expand Down
Loading