Skip to content

Commit

Permalink
fix(clerk-js): Update to new permission names
Browse files Browse the repository at this point in the history
  • Loading branch information
panteliselef committed Oct 13, 2023
1 parent b6abe36 commit 41e7308
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,6 @@ export const DomainList = withGate(
);
},
{
permission: 'org:domains:manage',
permission: 'org:sys_domains:manage',
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const OrganizationProfileRoutes = (props: PropsOfComponent<typeof Profile
flowStart
>
<Gate
permission={'org:profile:manage'}
permission={'org:sys_profile:manage'}
redirectTo='../'
>
<ProfileSettingsPage />
Expand All @@ -35,31 +35,31 @@ export const OrganizationProfileRoutes = (props: PropsOfComponent<typeof Profile
<Switch>
<Route path=':id/verify'>
<Gate
permission={'org:domains:manage'}
permission={'org:sys_domains:manage'}
redirectTo='../../'
>
<VerifyDomainPage />
</Gate>
</Route>
<Route path=':id/remove'>
<Gate
permission={'org:domains:delete'}
permission={'org:sys_domains:delete'}
redirectTo='../../'
>
<RemoveDomainPage />
</Gate>
</Route>
<Route path=':id'>
<Gate
permission={'org:domains:manage'}
permission={'org:sys_domains:manage'}
redirectTo='../../'
>
<VerifiedDomainPage />
</Gate>
</Route>
<Route index>
<Gate
permission={'org:domains:manage'}
permission={'org:sys_domains:manage'}
redirectTo='../'
>
<AddDomainPage />
Expand All @@ -78,7 +78,7 @@ export const OrganizationProfileRoutes = (props: PropsOfComponent<typeof Profile
flowStart
>
<Gate
permission={'org:profile:delete'}
permission={'org:sys_profile:delete'}
redirectTo='../'
>
<DeleteOrganizationPage />
Expand All @@ -96,7 +96,7 @@ export const OrganizationProfileRoutes = (props: PropsOfComponent<typeof Profile
flowStart
>
<Gate
permission={'org:memberships:manage'}
permission={'org:sys_memberships:manage'}
redirectTo='../'
>
<InviteMembersPage />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const OrganizationSettings = () => {
<Header.Subtitle localizationKey={localizationKeys('organizationProfile.start.headerSubtitle__settings')} />
</Header.Root>
<OrganizationProfileSection />
<Gate permission='org:domains:manage'>
<Gate permission='org:sys_domains:manage'>
<OrganizationDomainsSection />
</Gate>
<OrganizationDangerSection />
Expand Down Expand Up @@ -56,7 +56,7 @@ const OrganizationProfileSection = () => {
id='organizationProfile'
>
<Gate
permission={'org:profile:manage'}
permission={'org:sys_profile:manage'}
fallback={<>{profile}</>}
>
<BlockButton onClick={() => navigate('profile')}>{profile}</BlockButton>
Expand Down Expand Up @@ -97,21 +97,15 @@ const OrganizationDomainsSection = () => {
};

const OrganizationDangerSection = () => {
// TODO: update this in order to filter by permissions
const { organization, memberships: adminMembers } = useCoreOrganization({
memberships: {
role: ['admin'],
},
});
const { organization } = useCoreOrganization();
const { navigate } = useRouter();
const { isAuthorizedUser: canDeleteOrganization } = useGate({ permission: 'org:profile:delete' });
const { isAuthorizedUser: canDeleteOrganization } = useGate({ permission: 'org:sys_profile:delete' });

if (!organization) {
return null;
}

const adminDeleteEnabled = organization.adminDeleteEnabled;
const hasMoreThanOneAdmin = (adminMembers?.count || 0) > 1;

return (
<ProfileSection
Expand All @@ -133,8 +127,6 @@ const OrganizationDangerSection = () => {
colorScheme='danger'
textVariant='buttonExtraSmallBold'
onClick={() => navigate('leave')}
// TODO: rewrite to check if user has all clerk permissions and there are more than 1 admins
isDisabled={!!canDeleteOrganization && !hasMoreThanOneAdmin}
localizationKey={localizationKeys('organizationProfile.profilePage.dangerSection.leaveOrganization.title')}
/>
{canDeleteOrganization && adminDeleteEnabled && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ describe('OrganizationMembers', () => {
expect(queryByText('Member')).toBeInTheDocument();
});

// TODO: remove skip once OrganizationMembers uses permission based checks
it.skip('changes tab and renders pending requests', async () => {
it('changes tab and renders pending requests', async () => {
const requests = {
data: [
createFakeOrganizationMembershipRequest({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createFakeDomain, createFakeMember } from './utils';
const { createFixtures } = bindCreateFixtures('OrganizationProfile');

describe('OrganizationSettings', () => {
it('enables organization profile button and disables leave when user is the only admin', async () => {
it.skip('enables organization profile button and disables leave when user is the only admin', async () => {
const adminsList: OrganizationMembershipResource[] = [createFakeMember({ id: '1', orgId: '1', role: 'admin' })];
const domainList: OrganizationDomainResource[] = [
createFakeDomain({ id: '1', organizationId: '1', name: 'clerk.dev' }),
Expand Down Expand Up @@ -39,10 +39,6 @@ describe('OrganizationSettings', () => {
});

it('enables organization profile button and enables leave when user is admin and there is more', async () => {
const adminsList: OrganizationMembershipResource[] = [
createFakeMember({ id: '1', orgId: '1', role: 'admin' }),
createFakeMember({ id: '2', orgId: '1', role: 'admin' }),
];
const domainList: OrganizationDomainResource[] = [
createFakeDomain({ id: '1', organizationId: '1', name: 'clerk.dev' }),
];
Expand All @@ -52,7 +48,6 @@ describe('OrganizationSettings', () => {
f.withUser({ email_addresses: ['[email protected]'], organization_memberships: [{ name: 'Org1' }] });
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve({ data: adminsList, total_count: 2 }));
fixtures.clerk.organization?.getDomains.mockReturnValue(
Promise.resolve({
data: domainList,
Expand All @@ -62,14 +57,13 @@ describe('OrganizationSettings', () => {
fixtures.clerk.session?.isAuthorized.mockResolvedValue(true);
const { getByText } = render(<OrganizationSettings />, { wrapper });
await waitFor(() => {
expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
expect(getByText('Settings')).toBeDefined();
expect(getByText('Org1', { exact: false }).closest('button')).not.toBeNull();
expect(getByText(/leave organization/i, { exact: false }).closest('button')).not.toHaveAttribute('disabled');
});
});

it('disables organization profile button and enables leave when user is not admin', async () => {
it.skip('disables organization profile button and enables leave when user is not admin', async () => {
const adminsList: OrganizationMembershipResource[] = [createFakeMember({ id: '1', orgId: '1', role: 'admin' })];

const { wrapper, fixtures } = await createFixtures(f => {
Expand Down Expand Up @@ -101,31 +95,16 @@ describe('OrganizationSettings', () => {
});
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(Promise.resolve([]));
fixtures.clerk.session?.isAuthorized.mockResolvedValue(false);
const { getByText, queryByRole } = render(<OrganizationSettings />, { wrapper });
await waitFor(() => {
expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
expect(getByText('Danger')).toBeDefined();
expect(getByText(/leave organization/i).closest('button')).toBeInTheDocument();
expect(queryByRole('button', { name: /delete organization/i })).not.toBeInTheDocument();
});
});

it('enabled leave organization button with delete organization button', async () => {
const adminsList: OrganizationMembershipResource[] = [
createFakeMember({
id: '1',
orgId: '1',
role: 'admin',
}),
createFakeMember({
id: '2',
orgId: '1',
role: 'admin',
}),
];

const { wrapper, fixtures } = await createFixtures(f => {
f.withOrganizations();
f.withUser({
Expand All @@ -134,21 +113,16 @@ describe('OrganizationSettings', () => {
});
});

fixtures.clerk.organization?.getMemberships.mockReturnValue(
Promise.resolve({ data: adminsList, total_count: 2 }),
);

fixtures.clerk.session?.isAuthorized.mockResolvedValue(true);
const { getByText } = render(<OrganizationSettings />, { wrapper });
await waitFor(() => {
expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
expect(getByText('Danger')).toBeDefined();
expect(getByText(/leave organization/i).closest('button')).not.toHaveAttribute('disabled');
expect(getByText(/delete organization/i).closest('button')).toBeInTheDocument();
});
});

it('disabled leave organization button with delete organization button', async () => {
it.skip('disabled leave organization button with delete organization button', async () => {
const adminsList: OrganizationMembershipResource[] = [
createFakeMember({
id: '1',
Expand Down Expand Up @@ -222,7 +196,7 @@ describe('OrganizationSettings', () => {
fixtures.clerk.session?.isAuthorized.mockResolvedValue(false);
const { findByText } = render(<OrganizationSettings />, { wrapper });
await waitFor(async () => {
expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
// expect(fixtures.clerk.organization?.getMemberships).toHaveBeenCalled();
await userEvent.click(await findByText(/leave organization/i, { exact: false }));
});
expect(fixtures.router.navigate).toHaveBeenCalledWith('leave');
Expand Down

0 comments on commit 41e7308

Please sign in to comment.