sentryApplicationDetails.spec.tsx 19 KB

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