2021-08-02 14:08:48 -04:00
|
|
|
import setWindowLocation from 'helpers/set_window_location_helper';
|
2021-05-19 20:10:36 -04:00
|
|
|
import { DEFAULT_SORT, MEMBER_TYPES } from '~/members/constants';
|
2020-11-03 13:09:22 -05:00
|
|
|
import {
|
|
|
|
generateBadges,
|
|
|
|
isGroup,
|
|
|
|
isDirectMember,
|
|
|
|
isCurrentUser,
|
|
|
|
canRemove,
|
|
|
|
canResend,
|
|
|
|
canUpdate,
|
|
|
|
canOverride,
|
2020-12-08 04:09:41 -05:00
|
|
|
parseSortParam,
|
2020-12-10 13:10:16 -05:00
|
|
|
buildSortHref,
|
2021-01-21 07:09:05 -05:00
|
|
|
parseDataAttributes,
|
|
|
|
groupLinkRequestFormatter,
|
2020-11-19 07:09:26 -05:00
|
|
|
} from '~/members/utils';
|
2021-01-28 13:09:27 -05:00
|
|
|
import {
|
|
|
|
member as memberMock,
|
|
|
|
directMember,
|
|
|
|
inheritedMember,
|
2021-02-28 19:10:57 -05:00
|
|
|
member2faEnabled,
|
2021-01-28 13:09:27 -05:00
|
|
|
group,
|
|
|
|
invite,
|
|
|
|
members,
|
2021-04-22 08:09:49 -04:00
|
|
|
pagination,
|
2021-05-11 14:10:36 -04:00
|
|
|
dataAttribute,
|
2021-01-28 13:09:27 -05:00
|
|
|
} from './mock_data';
|
2020-11-03 13:09:22 -05:00
|
|
|
|
|
|
|
const IS_CURRENT_USER_ID = 123;
|
|
|
|
const IS_NOT_CURRENT_USER_ID = 124;
|
2020-12-08 04:09:41 -05:00
|
|
|
const URL_HOST = 'https://localhost/';
|
2020-10-01 02:09:59 -04:00
|
|
|
|
|
|
|
describe('Members Utils', () => {
|
|
|
|
describe('generateBadges', () => {
|
|
|
|
it('has correct properties for each badge', () => {
|
2021-02-28 19:10:57 -05:00
|
|
|
const badges = generateBadges({
|
|
|
|
member: memberMock,
|
|
|
|
isCurrentUser: true,
|
|
|
|
canManageMembers: true,
|
|
|
|
});
|
2020-10-01 02:09:59 -04:00
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
badges.forEach((badge) => {
|
2020-10-01 02:09:59 -04:00
|
|
|
expect(badge).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
show: expect.any(Boolean),
|
|
|
|
text: expect.any(String),
|
|
|
|
variant: expect.stringMatching(/muted|neutral|info|success|danger|warning/),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
2021-02-28 19:10:57 -05:00
|
|
|
member | expected
|
|
|
|
${memberMock} | ${{ show: true, text: "It's you", variant: 'success' }}
|
|
|
|
${{ ...memberMock, user: { ...memberMock.user, blocked: true } }} | ${{ show: true, text: 'Blocked', variant: 'danger' }}
|
|
|
|
${member2faEnabled} | ${{ show: true, text: '2FA', variant: 'info' }}
|
2020-10-01 02:09:59 -04:00
|
|
|
`('returns expected output for "$expected.text" badge', ({ member, expected }) => {
|
2021-02-28 19:10:57 -05:00
|
|
|
expect(
|
|
|
|
generateBadges({ member, isCurrentUser: true, canManageMembers: true }),
|
|
|
|
).toContainEqual(expect.objectContaining(expected));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when `canManageMembers` argument is `false`', () => {
|
|
|
|
describe.each`
|
|
|
|
description | memberIsCurrentUser | expectedBadgeToBeShown
|
|
|
|
${'is not the current user'} | ${false} | ${false}
|
|
|
|
${'is the current user'} | ${true} | ${true}
|
|
|
|
`('when member is $description', ({ memberIsCurrentUser, expectedBadgeToBeShown }) => {
|
|
|
|
it(`sets 'show' to '${expectedBadgeToBeShown}' for 2FA badge`, () => {
|
|
|
|
const badges = generateBadges({
|
|
|
|
member: member2faEnabled,
|
|
|
|
isCurrentUser: memberIsCurrentUser,
|
|
|
|
canManageMembers: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(badges.find((badge) => badge.text === '2FA').show).toBe(expectedBadgeToBeShown);
|
|
|
|
});
|
|
|
|
});
|
2020-10-01 02:09:59 -04:00
|
|
|
});
|
|
|
|
});
|
2020-11-03 13:09:22 -05:00
|
|
|
|
|
|
|
describe('isGroup', () => {
|
|
|
|
test.each`
|
|
|
|
member | expected
|
|
|
|
${group} | ${true}
|
|
|
|
${memberMock} | ${false}
|
|
|
|
`('returns $expected', ({ member, expected }) => {
|
|
|
|
expect(isGroup(member)).toBe(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isDirectMember', () => {
|
|
|
|
test.each`
|
2021-01-28 13:09:27 -05:00
|
|
|
member | expected
|
|
|
|
${directMember} | ${true}
|
|
|
|
${inheritedMember} | ${false}
|
|
|
|
`('returns $expected', ({ member, expected }) => {
|
|
|
|
expect(isDirectMember(member)).toBe(expected);
|
2020-11-03 13:09:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isCurrentUser', () => {
|
|
|
|
test.each`
|
|
|
|
currentUserId | expected
|
|
|
|
${IS_CURRENT_USER_ID} | ${true}
|
|
|
|
${IS_NOT_CURRENT_USER_ID} | ${false}
|
|
|
|
`('returns $expected', ({ currentUserId, expected }) => {
|
|
|
|
expect(isCurrentUser(memberMock, currentUserId)).toBe(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('canRemove', () => {
|
|
|
|
test.each`
|
2021-01-28 13:09:27 -05:00
|
|
|
member | expected
|
|
|
|
${{ ...directMember, canRemove: true }} | ${true}
|
|
|
|
${{ ...inheritedMember, canRemove: true }} | ${false}
|
|
|
|
${{ ...memberMock, canRemove: false }} | ${false}
|
|
|
|
`('returns $expected', ({ member, expected }) => {
|
|
|
|
expect(canRemove(member)).toBe(expected);
|
2020-11-03 13:09:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('canResend', () => {
|
|
|
|
test.each`
|
|
|
|
member | expected
|
|
|
|
${invite} | ${true}
|
|
|
|
${{ ...invite, invite: { ...invite.invite, canResend: false } }} | ${false}
|
2021-01-28 13:09:27 -05:00
|
|
|
`('returns $expected', ({ member, expected }) => {
|
|
|
|
expect(canResend(member)).toBe(expected);
|
2020-11-03 13:09:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('canUpdate', () => {
|
|
|
|
test.each`
|
2021-01-28 13:09:27 -05:00
|
|
|
member | currentUserId | expected
|
|
|
|
${{ ...directMember, canUpdate: true }} | ${IS_NOT_CURRENT_USER_ID} | ${true}
|
|
|
|
${{ ...directMember, canUpdate: true }} | ${IS_CURRENT_USER_ID} | ${false}
|
|
|
|
${{ ...inheritedMember, canUpdate: true }} | ${IS_CURRENT_USER_ID} | ${false}
|
|
|
|
${{ ...directMember, canUpdate: false }} | ${IS_NOT_CURRENT_USER_ID} | ${false}
|
|
|
|
`('returns $expected', ({ member, currentUserId, expected }) => {
|
|
|
|
expect(canUpdate(member, currentUserId)).toBe(expected);
|
2020-11-03 13:09:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('canOverride', () => {
|
|
|
|
it('returns `false`', () => {
|
|
|
|
expect(canOverride(memberMock)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2020-12-08 04:09:41 -05:00
|
|
|
|
|
|
|
describe('parseSortParam', () => {
|
|
|
|
beforeEach(() => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation(URL_HOST);
|
2020-12-08 04:09:41 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when `sort` param is not present', () => {
|
|
|
|
it('returns default sort options', () => {
|
|
|
|
expect(parseSortParam(['account'])).toEqual(DEFAULT_SORT);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when field passed in `sortableFields` argument does not have `sort` key defined', () => {
|
|
|
|
it('returns default sort options', () => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation('?sort=source_asc');
|
2020-12-08 04:09:41 -05:00
|
|
|
|
|
|
|
expect(parseSortParam(['source'])).toEqual(DEFAULT_SORT);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe.each`
|
|
|
|
sortParam | expected
|
2020-12-10 13:10:16 -05:00
|
|
|
${'name_asc'} | ${{ sortByKey: 'account', sortDesc: false }}
|
|
|
|
${'name_desc'} | ${{ sortByKey: 'account', sortDesc: true }}
|
|
|
|
${'last_joined'} | ${{ sortByKey: 'granted', sortDesc: false }}
|
|
|
|
${'oldest_joined'} | ${{ sortByKey: 'granted', sortDesc: true }}
|
|
|
|
${'access_level_asc'} | ${{ sortByKey: 'maxRole', sortDesc: false }}
|
|
|
|
${'access_level_desc'} | ${{ sortByKey: 'maxRole', sortDesc: true }}
|
|
|
|
${'recent_sign_in'} | ${{ sortByKey: 'lastSignIn', sortDesc: false }}
|
|
|
|
${'oldest_sign_in'} | ${{ sortByKey: 'lastSignIn', sortDesc: true }}
|
2020-12-08 04:09:41 -05:00
|
|
|
`('when `sort` query string param is `$sortParam`', ({ sortParam, expected }) => {
|
|
|
|
it(`returns ${JSON.stringify(expected)}`, async () => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation(`?sort=${sortParam}`);
|
2020-12-08 04:09:41 -05:00
|
|
|
|
|
|
|
expect(parseSortParam(['account', 'granted', 'expires', 'maxRole', 'lastSignIn'])).toEqual(
|
|
|
|
expected,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-10 13:10:16 -05:00
|
|
|
describe('buildSortHref', () => {
|
2020-12-08 04:09:41 -05:00
|
|
|
beforeEach(() => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation(URL_HOST);
|
2020-12-08 04:09:41 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when field passed in `sortBy` argument does not have `sort` key defined', () => {
|
|
|
|
it('returns an empty string', () => {
|
|
|
|
expect(
|
2020-12-10 13:10:16 -05:00
|
|
|
buildSortHref({
|
2020-12-08 04:09:41 -05:00
|
|
|
sortBy: 'source',
|
|
|
|
sortDesc: false,
|
|
|
|
filteredSearchBarTokens: [],
|
|
|
|
filteredSearchBarSearchParam: 'search',
|
|
|
|
}),
|
|
|
|
).toBe('');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when there are no filter params set', () => {
|
|
|
|
it('sets `sort` param', () => {
|
|
|
|
expect(
|
2020-12-10 13:10:16 -05:00
|
|
|
buildSortHref({
|
2020-12-08 04:09:41 -05:00
|
|
|
sortBy: 'account',
|
|
|
|
sortDesc: false,
|
|
|
|
filteredSearchBarTokens: [],
|
|
|
|
filteredSearchBarSearchParam: 'search',
|
|
|
|
}),
|
|
|
|
).toBe(`${URL_HOST}?sort=name_asc`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when filter params are set', () => {
|
|
|
|
it('merges the `sort` param with the filter params', () => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation('?two_factor=enabled&with_inherited_permissions=exclude');
|
2020-12-08 04:09:41 -05:00
|
|
|
|
|
|
|
expect(
|
2020-12-10 13:10:16 -05:00
|
|
|
buildSortHref({
|
2020-12-08 04:09:41 -05:00
|
|
|
sortBy: 'account',
|
|
|
|
sortDesc: false,
|
|
|
|
filteredSearchBarTokens: ['two_factor', 'with_inherited_permissions'],
|
|
|
|
filteredSearchBarSearchParam: 'search',
|
|
|
|
}),
|
|
|
|
).toBe(`${URL_HOST}?two_factor=enabled&with_inherited_permissions=exclude&sort=name_asc`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when search param is set', () => {
|
|
|
|
it('merges the `sort` param with the search param', () => {
|
2021-08-02 14:08:48 -04:00
|
|
|
setWindowLocation('?search=foobar');
|
2020-12-08 04:09:41 -05:00
|
|
|
|
|
|
|
expect(
|
2020-12-10 13:10:16 -05:00
|
|
|
buildSortHref({
|
2020-12-08 04:09:41 -05:00
|
|
|
sortBy: 'account',
|
|
|
|
sortDesc: false,
|
|
|
|
filteredSearchBarTokens: ['two_factor', 'with_inherited_permissions'],
|
|
|
|
filteredSearchBarSearchParam: 'search',
|
|
|
|
}),
|
|
|
|
).toBe(`${URL_HOST}?search=foobar&sort=name_asc`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-01-21 07:09:05 -05:00
|
|
|
|
|
|
|
describe('parseDataAttributes', () => {
|
|
|
|
let el;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
el = document.createElement('div');
|
2022-06-17 11:08:29 -04:00
|
|
|
el.dataset.membersData = dataAttribute;
|
2021-01-21 07:09:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
el = null;
|
|
|
|
});
|
|
|
|
|
2021-05-11 14:10:36 -04:00
|
|
|
it('correctly parses the data attribute', () => {
|
|
|
|
expect(parseDataAttributes(el)).toMatchObject({
|
2021-05-19 20:10:36 -04:00
|
|
|
[MEMBER_TYPES.user]: {
|
|
|
|
members,
|
|
|
|
pagination,
|
|
|
|
memberPath: '/groups/foo-bar/-/group_members/:id',
|
|
|
|
},
|
2021-01-21 07:09:05 -05:00
|
|
|
sourceId: 234,
|
|
|
|
canManageMembers: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('groupLinkRequestFormatter', () => {
|
|
|
|
it('returns expected format', () => {
|
|
|
|
expect(
|
|
|
|
groupLinkRequestFormatter({
|
|
|
|
accessLevel: 50,
|
|
|
|
expires_at: '2020-10-16',
|
|
|
|
}),
|
|
|
|
).toEqual({ group_link: { group_access: 50, expires_at: '2020-10-16' } });
|
|
|
|
});
|
|
|
|
});
|
2020-10-01 02:09:59 -04:00
|
|
|
});
|