| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  | const INVALID_DSN = 'an_invalid_dsn'; | 
					
						
							|  |  |  | const VALID_DSN = 'a_valid_dsn'; | 
					
						
							| 
									
										
										
										
											2021-09-30 15:34:11 +02:00
										 |  |  | const mockCaptureException = jest.fn(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-23 20:18:13 +02:00
										 |  |  | // FIXME
 | 
					
						
							|  |  |  | /* eslint-disable import/extensions */ | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | jest.mock('@sentry/node', () => { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     init(options = {}) { | 
					
						
							|  |  |  |       if (options.dsn !== VALID_DSN) { | 
					
						
							|  |  |  |         throw Error(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-09-30 15:34:11 +02:00
										 |  |  |     captureException: mockCaptureException, | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     withScope(configureScope) { | 
					
						
							|  |  |  |       configureScope(); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  | const sentryServiceLoader = require('../sentry'); | 
					
						
							| 
									
										
										
										
											2021-07-29 16:39:26 +02:00
										 |  |  | const defaultConfig = require('../../config').default; | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 13:51:12 +02:00
										 |  |  | describe('Sentry service', () => { | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     // Reset Strapi state
 | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  |     global.strapi = { | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |       config: { | 
					
						
							|  |  |  |         get: () => defaultConfig, | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |       log: { | 
					
						
							|  |  |  |         warn: jest.fn(), | 
					
						
							|  |  |  |         info: jest.fn(), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-01-26 10:58:28 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterEach(() => { | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     // Reset the plugin resource state
 | 
					
						
							|  |  |  |     jest.resetModules(); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   it('disables Sentry when no DSN is provided', () => { | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |     const sentryService = sentryServiceLoader({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     sentryService.init(); | 
					
						
							|  |  |  |     expect(strapi.log.info).toHaveBeenCalledWith(expect.stringMatching(/disabled/i)); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     const instance = sentryService.getInstance(); | 
					
						
							|  |  |  |     expect(instance).toBeNull(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   it('disables Sentry when an invalid DSN is provided', () => { | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |     global.strapi.config.get = () => ({ dsn: INVALID_DSN }); | 
					
						
							|  |  |  |     const sentryService = sentryServiceLoader({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     sentryService.init(); | 
					
						
							|  |  |  |     expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/could not set up sentry/i)); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     const instance = sentryService.getInstance(); | 
					
						
							|  |  |  |     expect(instance).toBeNull(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   it("doesn't send events before init", () => { | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |     const sentryService = sentryServiceLoader({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     sentryService.sendError(Error()); | 
					
						
							|  |  |  |     expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/cannot send event/i)); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-17 11:43:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   it('initializes and sends errors', () => { | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |     global.strapi.config.get = () => ({ dsn: VALID_DSN, sendMetadata: true }); | 
					
						
							|  |  |  |     const sentryService = sentryServiceLoader({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     sentryService.init(); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     // Saves the instance correctly
 | 
					
						
							|  |  |  |     const instance = sentryService.getInstance(); | 
					
						
							|  |  |  |     expect(instance).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     // Doesn't allow re-init
 | 
					
						
							|  |  |  |     sentryService.init(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Send error
 | 
					
						
							|  |  |  |     const error = Error('an error'); | 
					
						
							|  |  |  |     const configureScope = jest.fn(); | 
					
						
							|  |  |  |     sentryService.sendError(error, configureScope); | 
					
						
							|  |  |  |     expect(configureScope).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2021-09-30 15:34:11 +02:00
										 |  |  |     expect(mockCaptureException).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |   it('does not not send metadata when the option is disabled', () => { | 
					
						
							|  |  |  |     // Init with metadata option disabled
 | 
					
						
							| 
									
										
										
										
											2021-08-02 18:09:29 +02:00
										 |  |  |     global.strapi.config.get = () => ({ dsn: VALID_DSN, sendMetadata: false }); | 
					
						
							|  |  |  |     const sentryService = sentryServiceLoader({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     sentryService.init(); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:11:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 18:26:25 +01:00
										 |  |  |     // Send error
 | 
					
						
							|  |  |  |     const error = Error('an error'); | 
					
						
							|  |  |  |     const configureScope = jest.fn(); | 
					
						
							|  |  |  |     sentryService.sendError(error, configureScope); | 
					
						
							|  |  |  |     expect(configureScope).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |