sentryApplicationDetails.spec.tsx 16 KB

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