From 5015c7f72ca552ce788938a773659b89ef34575b Mon Sep 17 00:00:00 2001 From: acke Date: Mon, 5 Feb 2024 11:32:34 +0100 Subject: [PATCH] fix: updating unit test cases for the SenryErrorReporter --- .../kotlin/snyk/common/CustomEndpoints.kt | 2 +- .../kotlin/snyk/common/CustomEndpointsTest.kt | 1 + .../errorHandler/SentryErrorReporterTest.kt | 26 ++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/snyk/common/CustomEndpoints.kt b/src/main/kotlin/snyk/common/CustomEndpoints.kt index 67c2d0720..8cd369979 100644 --- a/src/main/kotlin/snyk/common/CustomEndpoints.kt +++ b/src/main/kotlin/snyk/common/CustomEndpoints.kt @@ -127,7 +127,7 @@ fun URI.isOauth() = isSnykGov() fun URI.isDev() = isSnykDomain() && host.lowercase().startsWith("dev.") fun URI.isAnalyticsPermitted() = host != null && - (host.lowercase() == "app.snyk.io" || host.lowercase() == "app.us.snyk.io") + (host.lowercase() == "app.snyk.io" || host.lowercase() == "app.us.snyk.io" || host.lowercase() == "snyk.io" ) fun isAnalyticsPermitted(): Boolean { val settings = pluginSettings() diff --git a/src/test/kotlin/snyk/common/CustomEndpointsTest.kt b/src/test/kotlin/snyk/common/CustomEndpointsTest.kt index cf81b5058..06c807d62 100644 --- a/src/test/kotlin/snyk/common/CustomEndpointsTest.kt +++ b/src/test/kotlin/snyk/common/CustomEndpointsTest.kt @@ -220,6 +220,7 @@ class CustomEndpointsTest { @Test fun `isAnalyticsPermitted true for the right URIs`() { val uris = listOf( + "https://snyk.io/api", "https://app.snyk.io", "https://app.us.snyk.io", "https://app.snyk.io/api", diff --git a/src/test/kotlin/snyk/errorHandler/SentryErrorReporterTest.kt b/src/test/kotlin/snyk/errorHandler/SentryErrorReporterTest.kt index f228ded91..f7ceb9c8f 100644 --- a/src/test/kotlin/snyk/errorHandler/SentryErrorReporterTest.kt +++ b/src/test/kotlin/snyk/errorHandler/SentryErrorReporterTest.kt @@ -11,11 +11,14 @@ import io.sentry.Sentry import io.sentry.protocol.SentryId import io.snyk.plugin.pluginSettings import io.snyk.plugin.services.SnykApplicationSettingsStateService +import junit.framework.TestCase import org.junit.After import org.junit.Before import org.junit.Test import snyk.PluginInformation +import snyk.common.isAnalyticsPermitted import snyk.pluginInfo +import java.net.URI class SentryErrorReporterTest { @Before @@ -43,26 +46,35 @@ class SentryErrorReporterTest { } @Test - fun `captureException should not send exceptions to Sentry when crashReportingEnabled is true`() { + fun `captureException should only report if permitted env`() { val settings = mockPluginInformation() setUnitTesting(false) settings.crashReportingEnabled = true - settings.customEndpointUrl = "https://api.fedramp.snykgov.io" - SentryErrorReporter.captureException(RuntimeException("test")) + val uris = listOf( + "https://app.fedramp.snykgov.io", + "https://app.eu.snyk.io/api", + "https://app.au.snyk.io/api" + ) - verify(exactly = 0) { Sentry.captureException(any()) } + uris.forEach { uri -> + settings.customEndpointUrl = uri + + SentryErrorReporter.captureException(RuntimeException("test")) + + verify(exactly = 0) { Sentry.captureException(any()) } + } } @Test - fun `captureException should not send exceptions to Sentry when URL is not a permitted environment`() { + fun `captureException should send exceptions to Sentry when URL is a permitted environment`() { val settings = mockPluginInformation() setUnitTesting(false) settings.crashReportingEnabled = true - settings.customEndpointUrl = "https://app.eu.snyk.io" + settings.customEndpointUrl = "https://app.snyk.io" SentryErrorReporter.captureException(RuntimeException("test")) - verify(exactly = 0) { Sentry.captureException(any()) } + verify(exactly = 1) { Sentry.captureException(any()) } } @Test