projectTeams.spec.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {TeamFixture} from 'sentry-fixture/team';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import TeamStore from 'sentry/stores/teamStore';
  11. import type {Organization} from 'sentry/types/organization';
  12. import type {Project} from 'sentry/types/project';
  13. import ProjectTeams from 'sentry/views/settings/project/projectTeams';
  14. describe('ProjectTeams', function () {
  15. let org: Organization;
  16. let project: Project;
  17. const team1WithAdmin = TeamFixture({
  18. access: ['team:read', 'team:write', 'team:admin'],
  19. });
  20. const team2WithAdmin = TeamFixture({
  21. id: '2',
  22. slug: 'team-slug-2',
  23. name: 'Team Name 2',
  24. hasAccess: true,
  25. access: ['team:read', 'team:write', 'team:admin'],
  26. });
  27. const team3NoAdmin = TeamFixture({
  28. id: '3',
  29. slug: 'team-slug-3',
  30. name: 'Team Name 3',
  31. hasAccess: true,
  32. access: ['team:read'],
  33. });
  34. beforeEach(function () {
  35. const initialData = initializeOrg();
  36. org = initialData.organization;
  37. project = {
  38. ...initialData.project,
  39. access: ['project:admin', 'project:write', 'project:admin'],
  40. };
  41. TeamStore.loadInitialData([team1WithAdmin, team2WithAdmin]);
  42. MockApiClient.addMockResponse({
  43. url: `/projects/${org.slug}/${project.slug}/`,
  44. method: 'GET',
  45. body: project,
  46. });
  47. MockApiClient.addMockResponse({
  48. url: `/projects/${org.slug}/${project.slug}/teams/`,
  49. method: 'GET',
  50. body: [team1WithAdmin],
  51. });
  52. MockApiClient.addMockResponse({
  53. url: `/organizations/${org.slug}/teams/`,
  54. method: 'GET',
  55. body: [team1WithAdmin, team2WithAdmin],
  56. });
  57. });
  58. afterEach(function () {
  59. MockApiClient.clearMockResponses();
  60. });
  61. it('can remove a team from project', async function () {
  62. MockApiClient.addMockResponse({
  63. url: `/projects/${org.slug}/${project.slug}/teams/`,
  64. method: 'GET',
  65. body: [team1WithAdmin, team2WithAdmin],
  66. });
  67. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  68. const mock1 = MockApiClient.addMockResponse({
  69. url: endpoint1,
  70. method: 'DELETE',
  71. statusCode: 200,
  72. });
  73. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  74. const mock2 = MockApiClient.addMockResponse({
  75. url: endpoint2,
  76. method: 'DELETE',
  77. statusCode: 200,
  78. });
  79. render(<ProjectTeams organization={org} project={project} />);
  80. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  81. expect(mock1).not.toHaveBeenCalled();
  82. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  83. renderGlobalModal();
  84. expect(screen.getByRole('dialog')).toBeInTheDocument();
  85. await userEvent.click(screen.getByText('Remove Team'));
  86. expect(mock1).toHaveBeenCalledWith(
  87. endpoint1,
  88. expect.objectContaining({
  89. method: 'DELETE',
  90. })
  91. );
  92. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  93. // Remove second team
  94. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  95. await userEvent.click(screen.getByText('Remove Team'));
  96. expect(mock2).toHaveBeenCalledWith(
  97. endpoint2,
  98. expect.objectContaining({
  99. method: 'DELETE',
  100. })
  101. );
  102. });
  103. it('cannot remove a team without admin scopes', async function () {
  104. MockApiClient.addMockResponse({
  105. url: `/projects/${org.slug}/${project.slug}/teams/`,
  106. method: 'GET',
  107. body: [team1WithAdmin, team2WithAdmin, team3NoAdmin],
  108. });
  109. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  110. const mock1 = MockApiClient.addMockResponse({
  111. url: endpoint1,
  112. method: 'DELETE',
  113. statusCode: 200,
  114. });
  115. const endpoint3 = `/projects/${org.slug}/${project.slug}/teams/${team3NoAdmin.slug}/`;
  116. const mock3 = MockApiClient.addMockResponse({
  117. url: endpoint3,
  118. method: 'DELETE',
  119. statusCode: 200,
  120. });
  121. render(<ProjectTeams organization={org} project={project} />);
  122. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  123. // Remove first team
  124. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  125. renderGlobalModal();
  126. await userEvent.click(screen.getByText('Remove Team'));
  127. expect(mock1).toHaveBeenCalledWith(
  128. endpoint1,
  129. expect.objectContaining({
  130. method: 'DELETE',
  131. })
  132. );
  133. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  134. // Remove third team, but button should be disabled
  135. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[1]);
  136. expect(mock3).not.toHaveBeenCalled();
  137. });
  138. it('removes team from project when project team is not in org list', async function () {
  139. MockApiClient.addMockResponse({
  140. url: `/projects/${org.slug}/${project.slug}/teams/`,
  141. method: 'GET',
  142. body: [team1WithAdmin, team2WithAdmin],
  143. });
  144. const endpoint1 = `/projects/${org.slug}/${project.slug}/teams/${team1WithAdmin.slug}/`;
  145. const mock1 = MockApiClient.addMockResponse({
  146. url: endpoint1,
  147. method: 'DELETE',
  148. });
  149. const endpoint2 = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  150. const mock2 = MockApiClient.addMockResponse({
  151. url: endpoint2,
  152. method: 'DELETE',
  153. });
  154. MockApiClient.addMockResponse({
  155. url: `/organizations/${org.slug}/teams/`,
  156. method: 'GET',
  157. body: [team3NoAdmin],
  158. });
  159. render(<ProjectTeams organization={org} project={project} />);
  160. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  161. expect(mock1).not.toHaveBeenCalled();
  162. // Remove first team
  163. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  164. renderGlobalModal();
  165. await userEvent.click(screen.getByText('Remove Team'));
  166. expect(mock1).toHaveBeenCalledWith(
  167. endpoint1,
  168. expect.objectContaining({
  169. method: 'DELETE',
  170. })
  171. );
  172. expect(screen.queryByText('#team-slug')).not.toBeInTheDocument();
  173. // Remove second team
  174. await userEvent.click(screen.getAllByRole('button', {name: 'Remove'})[0]);
  175. // Modal opens because this is the last team in project
  176. expect(screen.getByRole('dialog')).toBeInTheDocument();
  177. // Click confirm
  178. await userEvent.click(screen.getByText('Remove Team'));
  179. expect(mock2).toHaveBeenCalledWith(
  180. endpoint2,
  181. expect.objectContaining({
  182. method: 'DELETE',
  183. })
  184. );
  185. });
  186. it('can associate a team with project', async function () {
  187. const endpoint = `/projects/${org.slug}/${project.slug}/teams/${team2WithAdmin.slug}/`;
  188. const mock = MockApiClient.addMockResponse({
  189. url: endpoint,
  190. method: 'POST',
  191. statusCode: 200,
  192. });
  193. render(<ProjectTeams organization={org} project={project} />);
  194. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  195. expect(mock).not.toHaveBeenCalled();
  196. // Add a team
  197. await userEvent.click(screen.getAllByRole('button', {name: 'Add Team'})[1]);
  198. await userEvent.click(screen.getByText('#team-slug-2'));
  199. expect(mock).toHaveBeenCalledWith(
  200. endpoint,
  201. expect.objectContaining({
  202. method: 'POST',
  203. })
  204. );
  205. });
  206. it('creates a new team adds it to current project using the "create team modal" in dropdown', async function () {
  207. MockApiClient.addMockResponse({
  208. url: '/assistant/',
  209. body: {},
  210. });
  211. MockApiClient.addMockResponse({
  212. url: '/organizations/',
  213. body: [org],
  214. });
  215. const addTeamToProject = MockApiClient.addMockResponse({
  216. url: `/projects/${org.slug}/${project.slug}/teams/new-team/`,
  217. method: 'POST',
  218. });
  219. const createTeam = MockApiClient.addMockResponse({
  220. url: `/organizations/${org.slug}/teams/`,
  221. method: 'POST',
  222. body: TeamFixture({slug: 'new-team'}),
  223. });
  224. render(<ProjectTeams project={project} organization={org} />);
  225. expect(await screen.findByText('Project Teams for project-slug')).toBeInTheDocument();
  226. // Add new team
  227. await userEvent.click(screen.getAllByRole('button', {name: 'Add Team'})[1]);
  228. // XXX(epurkhiser): Create Team should really be a button
  229. await userEvent.click(screen.getByRole('link', {name: 'Create Team'}));
  230. renderGlobalModal();
  231. await screen.findByRole('dialog');
  232. await userEvent.type(screen.getByRole('textbox', {name: 'Team Name'}), 'new-team');
  233. await userEvent.click(screen.getByRole('button', {name: 'Create Team'}));
  234. await waitFor(() => expect(createTeam).toHaveBeenCalledTimes(1));
  235. expect(createTeam).toHaveBeenCalledWith(
  236. '/organizations/org-slug/teams/',
  237. expect.objectContaining({
  238. data: {slug: 'new-team'},
  239. })
  240. );
  241. expect(addTeamToProject).toHaveBeenCalledTimes(1);
  242. expect(addTeamToProject).toHaveBeenCalledWith(
  243. '/projects/org-slug/project-slug/teams/new-team/',
  244. expect.anything()
  245. );
  246. });
  247. });