Skip to content

Commit bd7fe4c

Browse files
CID-3627: Add test
1 parent 5cff314 commit bd7fe4c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

src/test/kotlin/net/leanix/githubagent/services/WebhookEventServiceTest.kt

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package net.leanix.githubagent.services
22

33
import com.ninjasquad.springmockk.MockkBean
4+
import com.ninjasquad.springmockk.SpykBean
45
import io.mockk.every
56
import io.mockk.just
67
import io.mockk.runs
78
import io.mockk.verify
89
import net.leanix.githubagent.client.GitHubClient
910
import net.leanix.githubagent.dto.Account
11+
import net.leanix.githubagent.dto.GitHubSearchResponse
1012
import net.leanix.githubagent.dto.Installation
13+
import net.leanix.githubagent.dto.ItemResponse
1114
import net.leanix.githubagent.dto.ManifestFileAction
1215
import net.leanix.githubagent.dto.ManifestFileUpdateDto
1316
import net.leanix.githubagent.dto.Organization
17+
import net.leanix.githubagent.dto.RepositoryDto
18+
import net.leanix.githubagent.dto.RepositoryItemResponse
19+
import net.leanix.githubagent.graphql.data.enums.RepositoryVisibility
20+
import net.leanix.githubagent.shared.INSTALLATION_REPOSITORIES
1421
import net.leanix.githubagent.shared.MANIFEST_FILE_NAME
1522
import org.junit.jupiter.api.BeforeEach
1623
import org.junit.jupiter.api.Test
@@ -49,6 +56,9 @@ class WebhookEventServiceTest {
4956
@MockkBean
5057
private lateinit var workflowRunService: WorkflowRunService
5158

59+
@SpykBean
60+
private lateinit var syncLogService: SyncLogService
61+
5262
private val permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read")
5363
private val events = listOf("label", "public", "repository", "push")
5464

@@ -507,4 +517,109 @@ class WebhookEventServiceTest {
507517

508518
verify { workflowRunService.consumeWebhookPayload(payload) }
509519
}
520+
521+
@Test
522+
fun `should handle installation repositories event`() {
523+
every { syncLogService.sendSyncLog(any(), any(), any(), any()) } just runs
524+
every { gitHubGraphQLService.getRepository(any(), any(), any()) } returns RepositoryDto(
525+
id = "1",
526+
name = "repo",
527+
organizationName = "owner/repo",
528+
description = "main",
529+
url = "content",
530+
defaultBranch = "main",
531+
archived = false,
532+
visibility = RepositoryVisibility.PRIVATE,
533+
languages = emptyList(),
534+
topics = emptyList(),
535+
updatedAt = "2021-09-01"
536+
)
537+
every { gitHubClient.searchManifestFiles(any(), any()) } returnsMany listOf(
538+
GitHubSearchResponse(
539+
2,
540+
listOf(
541+
createItemResponse("repo-test", "cider-org-3"),
542+
createItemResponse("repo-test", "cider-org-3")
543+
)
544+
),
545+
GitHubSearchResponse(
546+
3,
547+
listOf(
548+
createItemResponse("demo-repo-1", "cider-org-3"),
549+
createItemResponse("demo-repo-1", "cider-org-3")
550+
)
551+
)
552+
)
553+
554+
val payload = """
555+
{
556+
"action": "added",
557+
"installation": {
558+
"id": 150,
559+
"account": {
560+
"login": "cider-org-3",
561+
"id": 47
562+
},
563+
"repository_selection": "selected",
564+
"access_tokens_url": "https://cider-gh-enterprise.northeurope.cloudapp.azure.com/api/v3/app/installations/150/access_tokens",
565+
"repositories_url": "https://cider-gh-enterprise.northeurope.cloudapp.azure.com/api/v3/installation/repositories",
566+
"html_url": "https://cider-gh-enterprise.northeurope.cloudapp.azure.com/organizations/cider-org-3/settings/installations/150",
567+
"app_id": 3,
568+
"app_slug": "test-github-app",
569+
"target_id": 47,
570+
"target_type": "Organization",
571+
"permissions": {
572+
"checks": "write",
573+
"members": "read",
574+
"contents": "read",
575+
"metadata": "read",
576+
"administration": "read"
577+
},
578+
"events": [
579+
"check_run",
580+
"check_suite",
581+
"label",
582+
"organization",
583+
"public",
584+
"push",
585+
"repository"
586+
]
587+
},
588+
"repository_selection": "selected",
589+
"repositories_added": [
590+
{
591+
"id": 30,
592+
"node_id": "MDEwOlJlcG9zaXRvcnkzMA==",
593+
"name": "repo-test",
594+
"full_name": "cider-org-3/kostas-test",
595+
"private": true
596+
},
597+
{
598+
"id": 1648,
599+
"node_id": "MDEwOlJlcG9zaXRvcnkxNjQ4",
600+
"name": "demo-repo-1",
601+
"full_name": "cider-org-3/demo-repo-1",
602+
"private": true
603+
},
604+
{
605+
"id": 1649,
606+
"node_id": "MDEwOlJlcG9zaXRvcnkxNjQ5",
607+
"name": "demo-repo-2",
608+
"full_name": "cider-org-3/demo-repo-2",
609+
"private": true
610+
}
611+
],
612+
"repositories_removed": []
613+
}
614+
"""
615+
616+
webhookEventService.consumeWebhookEvent(INSTALLATION_REPOSITORIES, payload)
617+
618+
verify(exactly = 3) { webSocketService.sendMessage("events/repository", any()) }
619+
verify(exactly = 6) { webSocketService.sendMessage("/events/manifestFile", any()) }
620+
}
621+
622+
fun createItemResponse(repoName: String, organization: String): ItemResponse {
623+
return ItemResponse(MANIFEST_FILE_NAME, "$organization/$repoName/$MANIFEST_FILE_NAME", RepositoryItemResponse(repoName, repoName), "url")
624+
}
510625
}

0 commit comments

Comments
 (0)