123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- import {Members} from 'sentry-fixture/members';
- import {Organization} from 'sentry-fixture/organization';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import {
- openInviteMembersModal,
- openTeamAccessRequestModal,
- } from 'sentry/actionCreators/modal';
- import TeamMembers from 'sentry/views/settings/organizationTeams/teamMembers';
- jest.mock('sentry/actionCreators/modal', () => ({
- openInviteMembersModal: jest.fn(),
- openTeamAccessRequestModal: jest.fn(),
- }));
- describe('TeamMembers', function () {
- let createMock;
- const organization = Organization();
- const team = TestStubs.Team();
- const managerTeam = TestStubs.Team({orgRole: 'manager'});
- const members = Members();
- const member = TestStubs.Member({
- id: '9',
- email: 'sentry9@test.com',
- name: 'Sentry 9 Name',
- });
- const router = TestStubs.router();
- const routerProps = {
- router,
- routes: router.routes,
- params: router.params,
- routeParams: router.params,
- route: router.routes[0],
- location: router.location,
- };
- beforeEach(function () {
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/`,
- method: 'GET',
- body: [member],
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team.slug}/members/`,
- method: 'GET',
- body: members,
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team.slug}/`,
- method: 'GET',
- body: team,
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${managerTeam.slug}/`,
- method: 'GET',
- body: managerTeam,
- });
- createMock = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${member.id}/teams/${team.slug}/`,
- method: 'POST',
- });
- });
- it('can add member to team with open membership', async function () {
- const org = Organization({access: [], openMembership: true});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- expect(createMock).toHaveBeenCalled();
- });
- it('can add multiple members with one click on dropdown', async function () {
- const org = Organization({access: [], openMembership: true});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- expect(createMock).toHaveBeenCalled();
- expect(screen.getAllByTestId('add-member-menu')[0]).toBeVisible();
- });
- it('can add member to team with team:admin permission', async function () {
- const org = Organization({access: ['team:admin'], openMembership: false});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- expect(createMock).toHaveBeenCalled();
- });
- it('can add member to team with org:write permission', async function () {
- const org = Organization({access: ['org:write'], openMembership: false});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- expect(createMock).toHaveBeenCalled();
- });
- it('can request access to add member to team without permission', async function () {
- const org = Organization({access: [], openMembership: false});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- expect(openTeamAccessRequestModal).toHaveBeenCalled();
- });
- it('can invite member from team dropdown with access', async function () {
- const {organization: org, routerContext} = initializeOrg({
- organization: Organization({
- access: ['team:admin'],
- openMembership: false,
- }),
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />,
- {context: routerContext}
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getByTestId('invite-member'));
- expect(openInviteMembersModal).toHaveBeenCalled();
- });
- it('can invite member from team dropdown with access and `Open Membership` enabled', async function () {
- const {organization: org, routerContext} = initializeOrg({
- organization: Organization({
- access: ['team:admin'],
- openMembership: true,
- }),
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />,
- {context: routerContext}
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getByTestId('invite-member'));
- expect(openInviteMembersModal).toHaveBeenCalled();
- });
- it('can invite member from team dropdown without access and `Open Membership` enabled', async function () {
- const {organization: org, routerContext} = initializeOrg({
- organization: Organization({access: [], openMembership: true}),
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />,
- {context: routerContext}
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getByTestId('invite-member'));
- expect(openInviteMembersModal).toHaveBeenCalled();
- });
- it('can invite member from team dropdown without access and `Open Membership` disabled', async function () {
- const {organization: org, routerContext} = initializeOrg({
- organization: Organization({access: [], openMembership: false}),
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={org}
- team={team}
- />,
- {context: routerContext}
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getByTestId('invite-member'));
- expect(openInviteMembersModal).toHaveBeenCalled();
- });
- it('can remove member from team', async function () {
- const deleteMock = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${members[0].id}/teams/${team.slug}/`,
- method: 'DELETE',
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={organization}
- team={team}
- />
- );
- await screen.findAllByRole('button', {name: 'Add Member'});
- expect(deleteMock).not.toHaveBeenCalled();
- await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
- expect(deleteMock).toHaveBeenCalled();
- });
- it('can only remove self from team', async function () {
- const me = TestStubs.Member({
- id: '123',
- email: 'foo@example.com',
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team.slug}/members/`,
- method: 'GET',
- body: [...members, me],
- });
- const deleteMock = MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/${me.id}/teams/${team.slug}/`,
- method: 'DELETE',
- });
- const organizationMember = Organization({access: []});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={organizationMember}
- team={team}
- />
- );
- await screen.findAllByRole('button', {name: 'Add Member'});
- expect(deleteMock).not.toHaveBeenCalled();
- expect(screen.getAllByTestId('letter_avatar-avatar')).toHaveLength(
- members.length + 1
- );
- // Can only remove self
- expect(screen.getByRole('button', {name: 'Leave'})).toBeInTheDocument();
- await userEvent.click(screen.getByRole('button', {name: 'Leave'}));
- expect(deleteMock).toHaveBeenCalled();
- });
- it('renders team-level roles without flag', async function () {
- const owner = TestStubs.Member({
- id: '123',
- email: 'foo@example.com',
- orgRole: 'owner',
- role: 'owner',
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team.slug}/members/`,
- method: 'GET',
- body: [...members, owner],
- });
- await render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={organization}
- team={team}
- />
- );
- const admins = screen.queryAllByText('Team Admin');
- expect(admins).toHaveLength(3);
- const contributors = screen.queryAllByText('Contributor');
- expect(contributors).toHaveLength(2);
- });
- it('renders team-level roles with flag', async function () {
- const manager = TestStubs.Member({
- id: '123',
- email: 'foo@example.com',
- orgRole: 'manager',
- role: 'manager',
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team.slug}/members/`,
- method: 'GET',
- body: [...members, manager],
- });
- const orgWithTeamRoles = Organization({features: ['team-roles']});
- await render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team.slug}}
- organization={orgWithTeamRoles}
- team={team}
- />
- );
- const admins = screen.queryAllByText('Team Admin');
- expect(admins).toHaveLength(3);
- const contributors = screen.queryAllByText('Contributor');
- expect(contributors).toHaveLength(2);
- });
- it('adding member to manager team makes them team admin', async function () {
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${managerTeam.slug}/members/`,
- method: 'GET',
- body: [],
- });
- const orgWithTeamRoles = Organization({features: ['team-roles']});
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: managerTeam.slug}}
- organization={orgWithTeamRoles}
- team={managerTeam}
- />
- );
- await userEvent.click(
- (await screen.findAllByRole('button', {name: 'Add Member'}))[0]
- );
- await userEvent.click(screen.getAllByTestId('letter_avatar-avatar')[0]);
- const admin = screen.queryByText('Team Admin');
- expect(admin).toBeInTheDocument();
- });
- it('cannot add or remove members if team is idp:provisioned', function () {
- const team2 = TestStubs.Team({
- flags: {
- 'idp:provisioned': true,
- },
- });
- const me = TestStubs.Member({
- id: '123',
- email: 'foo@example.com',
- role: 'owner',
- flags: {
- 'idp:provisioned': true,
- },
- });
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/`,
- method: 'GET',
- body: [...members, me],
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team2.slug}/members/`,
- method: 'GET',
- body: members,
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team2.slug}/`,
- method: 'GET',
- body: team2,
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team2.slug}}
- organization={organization}
- team={team2}
- />
- );
- waitFor(() => {
- expect(screen.findByRole('button', {name: 'Add Member'})).toBeDisabled();
- expect(screen.findByRole('button', {name: 'Remove'})).toBeDisabled();
- });
- });
- it('cannot add or remove members or leave if team has org role and no access', function () {
- const team2 = TestStubs.Team({orgRole: 'manager'});
- const me = TestStubs.Member({
- id: '123',
- email: 'foo@example.com',
- role: 'member',
- });
- MockApiClient.clearMockResponses();
- MockApiClient.addMockResponse({
- url: `/organizations/${organization.slug}/members/`,
- method: 'GET',
- body: [...members, me],
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team2.slug}/members/`,
- method: 'GET',
- body: members,
- });
- MockApiClient.addMockResponse({
- url: `/teams/${organization.slug}/${team2.slug}/`,
- method: 'GET',
- body: team2,
- });
- render(
- <TeamMembers
- {...routerProps}
- params={{teamId: team2.slug}}
- organization={organization}
- team={team2}
- />
- );
- waitFor(() => {
- expect(screen.findByRole('button', {name: 'Add Member'})).toBeDisabled();
- expect(screen.findByRole('button', {name: 'Remove'})).toBeDisabled();
- expect(screen.findByRole('button', {name: 'Leave'})).toBeDisabled();
- });
- });
- });
|