11package net.leanix.githubagent.services
22
33import io.mockk.every
4- import io.mockk.just
54import io.mockk.mockk
6- import io.mockk.runs
75import io.mockk.slot
86import io.mockk.verify
97import net.leanix.githubagent.client.GitHubClient
108import net.leanix.githubagent.dto.Account
9+ import net.leanix.githubagent.dto.GitHubAppResponse
1110import net.leanix.githubagent.dto.GitHubSearchResponse
1211import net.leanix.githubagent.dto.Installation
1312import net.leanix.githubagent.dto.InstallationTokenResponse
@@ -36,7 +35,7 @@ class GitHubScanningServiceTest {
3635 private val gitHubAuthenticationService = mockk<GitHubAuthenticationService >()
3736 private val syncLogService = mockk<SyncLogService >(relaxUnitFun = true )
3837 private val rateLimitHandler = mockk<RateLimitHandler >(relaxUnitFun = true )
39- private val gitHubEnterpriseService = mockk< GitHubEnterpriseService >(relaxUnitFun = true )
38+ private val gitHubEnterpriseService = GitHubEnterpriseService (gitHubClient, syncLogService )
4039 private val gitHubScanningService = GitHubScanningService (
4140 gitHubClient,
4241 cachingService,
@@ -49,11 +48,14 @@ class GitHubScanningServiceTest {
4948 )
5049 private val runId = UUID .randomUUID()
5150
51+ private val permissions = mapOf (" administration" to " read" , " contents" to " read" , " metadata" to " read" )
52+ private val events = listOf (" label" , " public" , " repository" , " push" )
53+
5254 @BeforeEach
5355 fun setup () {
5456 every { cachingService.get(any()) } returns " value"
5557 every { gitHubClient.getInstallations(any()) } returns listOf (
56- Installation (1 , Account (" testInstallation" ), mapOf (), listOf () )
58+ Installation (1 , Account (" testInstallation" ), permissions, events )
5759 )
5860 every { gitHubClient.createInstallationToken(1 , any()) } returns
5961 InstallationTokenResponse (" testToken" , " 2024-01-01T00:00:00Z" , mapOf (), " all" )
@@ -70,7 +72,7 @@ class GitHubScanningServiceTest {
7072 every { syncLogService.sendInfoLog(any()) } returns Unit
7173 every { rateLimitHandler.executeWithRateLimitHandler(any< () -> Any > ()) } answers
7274 { firstArg< () -> Any > ().invoke() }
73- every { gitHubEnterpriseService.validateEnabledPermissionsAndEvents (any(), any(), any()) } just runs
75+ every { gitHubClient.getApp (any()) } returns GitHubAppResponse ( " testApp " , permissions, events)
7476 }
7577
7678 @Test
@@ -230,4 +232,21 @@ class GitHubScanningServiceTest {
230232 verify { webSocketService.sendMessage(eq(" $runId /manifestFiles" ), capture(fileSlot)) }
231233 assertEquals(fileSlot.captured.manifestFiles[0 ].path, " " )
232234 }
235+
236+ @Test
237+ fun `scanGitHubResources should skip organizations without correct permissions and events` () {
238+ every { cachingService.get(" runId" ) } returns runId
239+ every { gitHubClient.getInstallations(any()) } returns listOf (
240+ Installation (1 , Account (" testInstallation1" ), mapOf (), listOf ()),
241+ Installation (2 , Account (" testInstallation2" ), permissions, events),
242+ Installation (3 , Account (" testInstallation3" ), permissions, events)
243+ )
244+ gitHubScanningService.scanGitHubResources()
245+ verify { webSocketService.sendMessage(eq(" $runId /organizations" ), any()) }
246+ verify { syncLogService.sendErrorLog(" Failed to scan organization testInstallation1. Installation missing " +
247+ " the following permissions: [administration, contents, metadata], " +
248+ " and the following events: [label, public, repository, push]" ) }
249+ verify { syncLogService.sendInfoLog(" Finished initial full scan for organization testInstallation2." ) }
250+ verify { syncLogService.sendInfoLog(" Finished initial full scan for organization testInstallation2." ) }
251+ }
233252}
0 commit comments