sentryApplicationDetails.spec.tsx 18 KB

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