inviteRequestRow.spec.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import selectEvent from 'react-select-event';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {Team} from 'sentry-fixture/team';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import {OrgRole} from 'sentry/types';
  12. import InviteRequestRow from 'sentry/views/settings/organizationMembers/inviteRequestRow';
  13. const roles: OrgRole[] = [
  14. {
  15. id: 'admin',
  16. name: 'Admin',
  17. desc: 'This is the admin role',
  18. minimumTeamRole: '',
  19. allowed: true,
  20. },
  21. {
  22. id: 'member',
  23. name: 'Member',
  24. desc: 'This is the member role',
  25. minimumTeamRole: '',
  26. allowed: true,
  27. },
  28. {
  29. id: 'owner',
  30. name: 'Owner',
  31. desc: 'This is the owner role',
  32. minimumTeamRole: '',
  33. allowed: false,
  34. },
  35. ];
  36. describe('InviteRequestRow', function () {
  37. const orgWithoutAdminAccess = Organization({
  38. access: [],
  39. });
  40. const orgWithAdminAccess = Organization({
  41. access: ['member:admin'],
  42. });
  43. const inviteRequestBusy: Record<string, boolean> = {};
  44. const inviteRequest = TestStubs.Member({
  45. user: null,
  46. inviterName: TestStubs.User().name,
  47. inviterId: TestStubs.User().id,
  48. inviteStatus: 'requested_to_be_invited',
  49. role: 'member',
  50. teams: ['myteam'],
  51. });
  52. const joinRequest = TestStubs.Member({
  53. user: null,
  54. inviteStatus: 'requested_to_join',
  55. role: 'member',
  56. teams: ['myteam'],
  57. });
  58. it('renders request to be invited', function () {
  59. render(
  60. <InviteRequestRow
  61. organization={orgWithoutAdminAccess}
  62. inviteRequest={inviteRequest}
  63. inviteRequestBusy={inviteRequestBusy}
  64. onApprove={() => {}}
  65. onDeny={() => {}}
  66. onUpdate={() => {}}
  67. allRoles={roles}
  68. />
  69. );
  70. expect(screen.getByText(inviteRequest.email)).toBeInTheDocument();
  71. expect(
  72. screen.getByText(`Requested by ${inviteRequest.inviterName}`)
  73. ).toBeInTheDocument();
  74. });
  75. it('renders request to join', function () {
  76. render(
  77. <InviteRequestRow
  78. organization={orgWithoutAdminAccess}
  79. inviteRequest={joinRequest}
  80. inviteRequestBusy={inviteRequestBusy}
  81. onApprove={() => {}}
  82. onDeny={() => {}}
  83. onUpdate={() => {}}
  84. allRoles={roles}
  85. />
  86. );
  87. expect(screen.getByText(joinRequest.email)).toBeInTheDocument();
  88. expect(screen.getByRole('button', {name: 'Approve'})).toBeInTheDocument();
  89. expect(screen.getByRole('button', {name: 'Deny'})).toBeInTheDocument();
  90. });
  91. it('admin can approve invite request', async function () {
  92. const mockApprove = jest.fn();
  93. const mockDeny = jest.fn();
  94. render(
  95. <InviteRequestRow
  96. organization={orgWithAdminAccess}
  97. inviteRequest={inviteRequest}
  98. inviteRequestBusy={inviteRequestBusy}
  99. onApprove={mockApprove}
  100. onDeny={mockDeny}
  101. onUpdate={() => {}}
  102. allRoles={roles}
  103. />
  104. );
  105. await userEvent.click(screen.getByRole('button', {name: 'Approve'}));
  106. renderGlobalModal();
  107. await userEvent.click(screen.getByTestId('confirm-button'));
  108. expect(mockApprove).toHaveBeenCalledWith(inviteRequest);
  109. expect(mockDeny).not.toHaveBeenCalled();
  110. });
  111. it('admin can deny invite request', async function () {
  112. const mockApprove = jest.fn();
  113. const mockDeny = jest.fn();
  114. render(
  115. <InviteRequestRow
  116. organization={orgWithAdminAccess}
  117. inviteRequest={inviteRequest}
  118. inviteRequestBusy={inviteRequestBusy}
  119. onApprove={mockApprove}
  120. onDeny={mockDeny}
  121. onUpdate={() => {}}
  122. allRoles={roles}
  123. />
  124. );
  125. await userEvent.click(screen.getByRole('button', {name: 'Deny'}));
  126. expect(mockDeny).toHaveBeenCalledWith(inviteRequest);
  127. expect(mockApprove).not.toHaveBeenCalled();
  128. });
  129. it('non-admin can not approve or deny invite request', function () {
  130. render(
  131. <InviteRequestRow
  132. organization={orgWithoutAdminAccess}
  133. inviteRequest={inviteRequest}
  134. inviteRequestBusy={inviteRequestBusy}
  135. onApprove={() => {}}
  136. onDeny={() => {}}
  137. onUpdate={() => {}}
  138. allRoles={roles}
  139. />
  140. );
  141. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  142. expect(screen.getByRole('button', {name: 'Deny'})).toBeDisabled();
  143. });
  144. it('admin can change role and teams', async function () {
  145. const adminInviteRequest = TestStubs.Member({
  146. user: null,
  147. inviterName: TestStubs.User().name,
  148. inviterId: TestStubs.User().id,
  149. inviteStatus: 'requested_to_be_invited',
  150. role: 'admin',
  151. teams: ['myteam'],
  152. });
  153. void TeamStore.loadInitialData([
  154. Team({id: '1', slug: 'one'}),
  155. Team({id: '2', slug: 'two'}),
  156. ]);
  157. const mockUpdate = jest.fn();
  158. render(
  159. <InviteRequestRow
  160. organization={orgWithAdminAccess}
  161. inviteRequest={adminInviteRequest}
  162. inviteRequestBusy={inviteRequestBusy}
  163. onApprove={() => {}}
  164. onDeny={() => {}}
  165. onUpdate={mockUpdate}
  166. allRoles={roles}
  167. />
  168. );
  169. // Select role from first select input
  170. await selectEvent.select(screen.getAllByRole('textbox')[0], 'Member');
  171. expect(mockUpdate).toHaveBeenCalledWith({role: 'member'});
  172. // Select teams from first select input
  173. await selectEvent.select(screen.getAllByRole('textbox')[1], ['#one']);
  174. expect(mockUpdate).toHaveBeenCalledWith({teams: ['one']});
  175. TeamStore.reset();
  176. });
  177. it('cannot be approved when invitee role is not allowed', function () {
  178. const ownerInviteRequest = TestStubs.Member({
  179. user: null,
  180. inviterName: TestStubs.User().name,
  181. inviterId: TestStubs.User().id,
  182. inviteStatus: 'requested_to_be_invited',
  183. role: 'owner',
  184. teams: ['myteam'],
  185. });
  186. const mockUpdate = jest.fn();
  187. render(
  188. <InviteRequestRow
  189. organization={orgWithoutAdminAccess}
  190. inviteRequest={ownerInviteRequest}
  191. inviteRequestBusy={inviteRequestBusy}
  192. onApprove={() => {}}
  193. onDeny={() => {}}
  194. onUpdate={mockUpdate}
  195. allRoles={roles}
  196. />
  197. );
  198. expect(screen.getByRole('button', {name: 'Approve'})).toBeDisabled();
  199. });
  200. });