Skip to content

Commit

Permalink
fix: updating unit test cases for the SenryErrorReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
acke committed Feb 5, 2024
1 parent 85e361b commit 5015c7f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/snyk/common/CustomEndpoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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" )

Check warning

Code scanning / detekt

Reports spaces around parentheses Warning

Unexpected spacing before ")"

fun isAnalyticsPermitted(): Boolean {
val settings = pluginSettings()
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/snyk/common/CustomEndpointsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 19 additions & 7 deletions src/test/kotlin/snyk/errorHandler/SentryErrorReporterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()) }
}

Check warning

Code scanning / detekt

Declarations and declarations with annotations should have an empty space between. Warning test

Declarations and declarations with annotations should have an empty space between.

@Test
Expand Down

0 comments on commit 5015c7f

Please sign in to comment.