sentryApplicationDetails.spec.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. import selectEvent from 'react-select-event';
  2. import {SentryApp} from 'sentry-fixture/sentryApp';
  3. import {SentryAppToken} from 'sentry-fixture/sentryAppToken';
  4. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  5. import SentryApplicationDetails from 'sentry/views/settings/organizationDeveloperSettings/sentryApplicationDetails';
  6. describe('Sentry Application Details', function () {
  7. let org;
  8. let sentryApp;
  9. let token;
  10. let createAppRequest;
  11. let editAppRequest;
  12. const maskedValue = '*'.repeat(64);
  13. const router = TestStubs.router();
  14. beforeEach(() => {
  15. MockApiClient.clearMockResponses();
  16. org = TestStubs.Organization({features: ['sentry-app-logo-upload']});
  17. });
  18. describe('Creating a new public Sentry App', () => {
  19. function renderComponent() {
  20. return render(
  21. <SentryApplicationDetails
  22. router={router}
  23. location={router.location}
  24. routes={router.routes}
  25. routeParams={{}}
  26. route={{path: 'new-public/'}}
  27. params={{}}
  28. />,
  29. {context: TestStubs.routerContext([{organization: org}])}
  30. );
  31. }
  32. beforeEach(() => {
  33. createAppRequest = MockApiClient.addMockResponse({
  34. url: '/sentry-apps/',
  35. method: 'POST',
  36. body: [],
  37. });
  38. });
  39. it('has inputs for redirectUrl and verifyInstall', () => {
  40. renderComponent();
  41. expect(
  42. screen.getByRole('checkbox', {name: 'Verify Installation'})
  43. ).toBeInTheDocument();
  44. expect(screen.getByRole('textbox', {name: 'Redirect URL'})).toBeInTheDocument();
  45. });
  46. it('shows empty scopes and no credentials', function () {
  47. renderComponent();
  48. expect(screen.getByText('Permissions')).toBeInTheDocument();
  49. // new app starts off with no scopes selected
  50. expect(screen.getByRole('checkbox', {name: 'issue'})).not.toBeChecked();
  51. expect(screen.getByRole('checkbox', {name: 'error'})).not.toBeChecked();
  52. expect(screen.getByRole('checkbox', {name: 'comment'})).not.toBeChecked();
  53. });
  54. it('does not show logo upload fields', function () {
  55. renderComponent();
  56. expect(screen.queryByText('Logo')).not.toBeInTheDocument();
  57. expect(screen.queryByText('Small Icon')).not.toBeInTheDocument();
  58. });
  59. it('saves', async function () {
  60. renderComponent();
  61. await userEvent.type(screen.getByRole('textbox', {name: 'Name'}), 'Test App');
  62. await userEvent.type(screen.getByRole('textbox', {name: 'Author'}), 'Sentry');
  63. await userEvent.type(
  64. screen.getByRole('textbox', {name: 'Webhook URL'}),
  65. 'https://webhook.com'
  66. );
  67. await userEvent.type(
  68. screen.getByRole('textbox', {name: 'Redirect URL'}),
  69. 'https://webhook.com/setup'
  70. );
  71. await userEvent.click(screen.getByRole('textbox', {name: 'Schema'}));
  72. await userEvent.paste('{}');
  73. await userEvent.click(screen.getByRole('checkbox', {name: 'Alert Rule Action'}));
  74. await selectEvent.select(screen.getByRole('textbox', {name: 'Member'}), 'Admin');
  75. await selectEvent.select(
  76. screen.getByRole('textbox', {name: 'Issue & Event'}),
  77. 'Admin'
  78. );
  79. await userEvent.click(screen.getByRole('checkbox', {name: 'issue'}));
  80. await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
  81. const data = {
  82. name: 'Test App',
  83. author: 'Sentry',
  84. organization: org.slug,
  85. redirectUrl: 'https://webhook.com/setup',
  86. webhookUrl: 'https://webhook.com',
  87. scopes: expect.arrayContaining([
  88. 'member:read',
  89. 'member:admin',
  90. 'event:read',
  91. 'event:admin',
  92. ]),
  93. events: ['issue'],
  94. isInternal: false,
  95. verifyInstall: true,
  96. isAlertable: true,
  97. allowedOrigins: [],
  98. schema: {},
  99. };
  100. expect(createAppRequest).toHaveBeenCalledWith(
  101. '/sentry-apps/',
  102. expect.objectContaining({
  103. data,
  104. method: 'POST',
  105. })
  106. );
  107. });
  108. });
  109. describe('Creating a new internal Sentry App', () => {
  110. function renderComponent() {
  111. return render(
  112. <SentryApplicationDetails
  113. router={router}
  114. location={router.location}
  115. routes={router.routes}
  116. routeParams={{}}
  117. route={{path: 'new-internal/'}}
  118. params={{}}
  119. />,
  120. {context: TestStubs.routerContext([{organization: org}])}
  121. );
  122. }
  123. it('does not show logo upload fields', function () {
  124. renderComponent();
  125. expect(screen.queryByText('Logo')).not.toBeInTheDocument();
  126. expect(screen.queryByText('Small Icon')).not.toBeInTheDocument();
  127. });
  128. it('no inputs for redirectUrl and verifyInstall', () => {
  129. renderComponent();
  130. expect(
  131. screen.queryByRole('checkbox', {name: 'Verify Installation'})
  132. ).not.toBeInTheDocument();
  133. expect(
  134. screen.queryByRole('textbox', {name: 'Redirect URL'})
  135. ).not.toBeInTheDocument();
  136. });
  137. });
  138. describe('Renders public app', function () {
  139. function renderComponent() {
  140. return render(
  141. <SentryApplicationDetails
  142. router={router}
  143. location={router.location}
  144. routes={router.routes}
  145. routeParams={{}}
  146. route={router.routes[0]}
  147. params={{appSlug: sentryApp.slug}}
  148. />,
  149. {
  150. context: TestStubs.routerContext([{organization: org}]),
  151. }
  152. );
  153. }
  154. beforeEach(() => {
  155. sentryApp = SentryApp();
  156. sentryApp.events = ['issue'];
  157. MockApiClient.addMockResponse({
  158. url: `/sentry-apps/${sentryApp.slug}/`,
  159. body: sentryApp,
  160. });
  161. MockApiClient.addMockResponse({
  162. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  163. body: [],
  164. });
  165. });
  166. it('shows logo upload fields', function () {
  167. renderComponent();
  168. expect(screen.getByText('Logo')).toBeInTheDocument();
  169. expect(screen.getByText('Small Icon')).toBeInTheDocument();
  170. });
  171. it('has inputs for redirectUrl and verifyInstall', () => {
  172. renderComponent();
  173. expect(
  174. screen.getByRole('checkbox', {name: 'Verify Installation'})
  175. ).toBeInTheDocument();
  176. expect(screen.getByRole('textbox', {name: 'Redirect URL'})).toBeInTheDocument();
  177. });
  178. it('shows application data', function () {
  179. renderComponent();
  180. selectEvent.openMenu(screen.getByRole('textbox', {name: 'Project'}));
  181. expect(screen.getByRole('menuitemradio', {name: 'Read'})).toBeChecked();
  182. });
  183. it('renders clientId and clientSecret for public apps', function () {
  184. renderComponent();
  185. expect(screen.getByRole('textbox', {name: 'Client ID'})).toBeInTheDocument();
  186. expect(screen.getByRole('textbox', {name: 'Client Secret'})).toBeInTheDocument();
  187. });
  188. });
  189. describe('Renders for internal apps', () => {
  190. function renderComponent() {
  191. return render(
  192. <SentryApplicationDetails
  193. router={router}
  194. location={router.location}
  195. routes={router.routes}
  196. routeParams={{}}
  197. route={router.routes[0]}
  198. params={{appSlug: sentryApp.slug}}
  199. />,
  200. {
  201. context: TestStubs.routerContext([{organization: org}]),
  202. }
  203. );
  204. }
  205. beforeEach(() => {
  206. sentryApp = SentryApp({
  207. status: 'internal',
  208. });
  209. token = SentryAppToken();
  210. sentryApp.events = ['issue'];
  211. MockApiClient.addMockResponse({
  212. url: `/sentry-apps/${sentryApp.slug}/`,
  213. body: sentryApp,
  214. });
  215. MockApiClient.addMockResponse({
  216. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  217. body: [token],
  218. });
  219. });
  220. it('no inputs for redirectUrl and verifyInstall', () => {
  221. renderComponent();
  222. expect(
  223. screen.queryByRole('checkbox', {name: 'Verify Installation'})
  224. ).not.toBeInTheDocument();
  225. expect(
  226. screen.queryByRole('textbox', {name: 'Redirect URL'})
  227. ).not.toBeInTheDocument();
  228. });
  229. it('shows logo upload fields', function () {
  230. renderComponent();
  231. expect(screen.getByText('Logo')).toBeInTheDocument();
  232. expect(screen.getByText('Small Icon')).toBeInTheDocument();
  233. });
  234. it('shows tokens', function () {
  235. renderComponent();
  236. expect(screen.getByText('Tokens')).toBeInTheDocument();
  237. expect(screen.getByRole('textbox', {name: 'Token value'})).toHaveValue(
  238. '123456123456123456123456-token'
  239. );
  240. });
  241. it('shows just clientSecret', function () {
  242. renderComponent();
  243. expect(screen.queryByRole('textbox', {name: 'Client ID'})).not.toBeInTheDocument();
  244. expect(screen.getByRole('textbox', {name: 'Client Secret'})).toBeInTheDocument();
  245. });
  246. });
  247. describe('Renders masked values', () => {
  248. function renderComponent() {
  249. return render(
  250. <SentryApplicationDetails
  251. router={router}
  252. location={router.location}
  253. routes={router.routes}
  254. routeParams={{}}
  255. route={router.routes[0]}
  256. params={{appSlug: sentryApp.slug}}
  257. />,
  258. {
  259. context: TestStubs.routerContext([{organization: org}]),
  260. }
  261. );
  262. }
  263. beforeEach(() => {
  264. sentryApp = SentryApp({
  265. status: 'internal',
  266. clientSecret: maskedValue,
  267. });
  268. token = SentryAppToken({token: maskedValue, refreshToken: maskedValue});
  269. sentryApp.events = ['issue'];
  270. MockApiClient.addMockResponse({
  271. url: `/sentry-apps/${sentryApp.slug}/`,
  272. body: sentryApp,
  273. });
  274. MockApiClient.addMockResponse({
  275. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  276. body: [token],
  277. });
  278. });
  279. it('shows masked tokens', function () {
  280. renderComponent();
  281. expect(screen.getByRole('textbox', {name: 'Token value'})).toHaveValue(maskedValue);
  282. });
  283. it('shows masked clientSecret', function () {
  284. renderComponent();
  285. expect(screen.getByRole('textbox', {name: 'Client Secret'})).toHaveValue(
  286. maskedValue
  287. );
  288. });
  289. });
  290. describe('Editing internal app tokens', () => {
  291. function renderComponent() {
  292. return render(
  293. <SentryApplicationDetails
  294. router={router}
  295. location={router.location}
  296. routes={router.routes}
  297. routeParams={{}}
  298. route={router.routes[0]}
  299. params={{appSlug: sentryApp.slug}}
  300. />,
  301. {
  302. context: TestStubs.routerContext([{organization: org}]),
  303. }
  304. );
  305. }
  306. beforeEach(() => {
  307. sentryApp = SentryApp({
  308. status: 'internal',
  309. isAlertable: true,
  310. });
  311. token = SentryAppToken();
  312. sentryApp.events = ['issue'];
  313. MockApiClient.addMockResponse({
  314. url: `/sentry-apps/${sentryApp.slug}/`,
  315. body: sentryApp,
  316. });
  317. MockApiClient.addMockResponse({
  318. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  319. body: [token],
  320. });
  321. });
  322. it('adding token to list', async function () {
  323. MockApiClient.addMockResponse({
  324. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  325. method: 'POST',
  326. body: [
  327. SentryAppToken({
  328. token: '392847329',
  329. dateCreated: '2018-03-02T18:30:26Z',
  330. }),
  331. ],
  332. });
  333. renderComponent();
  334. await userEvent.click(screen.getByRole('button', {name: 'New Token'}));
  335. await waitFor(() => {
  336. expect(screen.getAllByRole('textbox', {name: 'Token value'})).toHaveLength(2);
  337. });
  338. });
  339. it('removing token from list', async function () {
  340. MockApiClient.addMockResponse({
  341. url: `/sentry-apps/${sentryApp.slug}/api-tokens/${token.token}/`,
  342. method: 'DELETE',
  343. body: {},
  344. });
  345. renderComponent();
  346. await userEvent.click(screen.getByRole('button', {name: 'Revoke'}));
  347. expect(await screen.findByText('No tokens created yet.')).toBeInTheDocument();
  348. });
  349. it('removing webhookURL unsets isAlertable and changes webhookDisabled to true', async () => {
  350. renderComponent();
  351. expect(screen.getByRole('checkbox', {name: 'Alert Rule Action'})).toBeChecked();
  352. await userEvent.clear(screen.getByRole('textbox', {name: 'Webhook URL'}));
  353. expect(screen.getByRole('checkbox', {name: 'Alert Rule Action'})).not.toBeChecked();
  354. });
  355. });
  356. describe('Editing an existing public Sentry App', () => {
  357. function renderComponent() {
  358. return render(
  359. <SentryApplicationDetails
  360. router={router}
  361. location={router.location}
  362. routes={router.routes}
  363. routeParams={{}}
  364. route={router.routes[0]}
  365. params={{appSlug: sentryApp.slug}}
  366. />,
  367. {
  368. context: TestStubs.routerContext([{organization: org}]),
  369. }
  370. );
  371. }
  372. beforeEach(() => {
  373. sentryApp = SentryApp();
  374. sentryApp.events = ['issue'];
  375. sentryApp.scopes = ['project:read', 'event:read'];
  376. editAppRequest = MockApiClient.addMockResponse({
  377. url: `/sentry-apps/${sentryApp.slug}/`,
  378. method: 'PUT',
  379. body: [],
  380. });
  381. MockApiClient.addMockResponse({
  382. url: `/sentry-apps/${sentryApp.slug}/`,
  383. body: sentryApp,
  384. });
  385. MockApiClient.addMockResponse({
  386. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  387. body: [],
  388. });
  389. });
  390. it('updates app with correct data', async function () {
  391. renderComponent();
  392. await userEvent.clear(screen.getByRole('textbox', {name: 'Redirect URL'}));
  393. await userEvent.type(
  394. screen.getByRole('textbox', {name: 'Redirect URL'}),
  395. 'https://hello.com/'
  396. );
  397. await userEvent.click(screen.getByRole('textbox', {name: 'Schema'}));
  398. await userEvent.paste('{}');
  399. await userEvent.click(screen.getByRole('checkbox', {name: 'issue'}));
  400. await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
  401. expect(editAppRequest).toHaveBeenCalledWith(
  402. `/sentry-apps/${sentryApp.slug}/`,
  403. expect.objectContaining({
  404. data: expect.objectContaining({
  405. redirectUrl: 'https://hello.com/',
  406. events: [],
  407. }),
  408. method: 'PUT',
  409. })
  410. );
  411. });
  412. it('submits with no-access for event subscription when permission is revoked', async () => {
  413. renderComponent();
  414. await userEvent.click(screen.getByRole('checkbox', {name: 'issue'}));
  415. await userEvent.click(screen.getByRole('textbox', {name: 'Schema'}));
  416. await userEvent.paste('{}');
  417. await selectEvent.select(
  418. screen.getByRole('textbox', {name: 'Issue & Event'}),
  419. 'No Access'
  420. );
  421. await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
  422. expect(editAppRequest).toHaveBeenCalledWith(
  423. `/sentry-apps/${sentryApp.slug}/`,
  424. expect.objectContaining({
  425. data: expect.objectContaining({
  426. events: [],
  427. }),
  428. method: 'PUT',
  429. })
  430. );
  431. });
  432. });
  433. describe('Editing an existing public Sentry App with a scope error', () => {
  434. function renderComponent() {
  435. render(
  436. <SentryApplicationDetails
  437. router={router}
  438. location={router.location}
  439. routes={router.routes}
  440. routeParams={{}}
  441. route={router.routes[0]}
  442. params={{appSlug: sentryApp.slug}}
  443. />,
  444. {
  445. context: TestStubs.routerContext([{organization: org}]),
  446. }
  447. );
  448. }
  449. beforeEach(() => {
  450. sentryApp = SentryApp();
  451. editAppRequest = MockApiClient.addMockResponse({
  452. url: `/sentry-apps/${sentryApp.slug}/`,
  453. method: 'PUT',
  454. statusCode: 400,
  455. body: {
  456. scopes: [
  457. "Requested permission of member:write exceeds requester's permission. Please contact an administrator to make the requested change.",
  458. "Requested permission of member:admin exceeds requester's permission. Please contact an administrator to make the requested change.",
  459. ],
  460. },
  461. });
  462. MockApiClient.addMockResponse({
  463. url: `/sentry-apps/${sentryApp.slug}/`,
  464. body: sentryApp,
  465. });
  466. MockApiClient.addMockResponse({
  467. url: `/sentry-apps/${sentryApp.slug}/api-tokens/`,
  468. body: [],
  469. });
  470. });
  471. it('renders the error', async () => {
  472. renderComponent();
  473. await userEvent.click(screen.getByRole('button', {name: 'Save Changes'}));
  474. expect(
  475. await screen.findByText(
  476. "Requested permission of member:admin exceeds requester's permission. Please contact an administrator to make the requested change."
  477. )
  478. ).toBeInTheDocument();
  479. });
  480. });
  481. });