|
1 | 1 | package net.leanix.githubagent.services |
2 | 2 |
|
3 | 3 | import com.ninjasquad.springmockk.MockkBean |
| 4 | +import com.ninjasquad.springmockk.SpykBean |
4 | 5 | import io.mockk.every |
5 | 6 | import io.mockk.just |
6 | 7 | import io.mockk.runs |
7 | 8 | import io.mockk.verify |
8 | 9 | import net.leanix.githubagent.client.GitHubClient |
9 | 10 | import net.leanix.githubagent.dto.Account |
| 11 | +import net.leanix.githubagent.dto.GitHubSearchResponse |
10 | 12 | import net.leanix.githubagent.dto.Installation |
| 13 | +import net.leanix.githubagent.dto.ItemResponse |
11 | 14 | import net.leanix.githubagent.dto.ManifestFileAction |
12 | 15 | import net.leanix.githubagent.dto.ManifestFileUpdateDto |
13 | 16 | 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 |
14 | 21 | import net.leanix.githubagent.shared.MANIFEST_FILE_NAME |
15 | 22 | import org.junit.jupiter.api.BeforeEach |
16 | 23 | import org.junit.jupiter.api.Test |
@@ -49,6 +56,9 @@ class WebhookEventServiceTest { |
49 | 56 | @MockkBean |
50 | 57 | private lateinit var workflowRunService: WorkflowRunService |
51 | 58 |
|
| 59 | + @SpykBean |
| 60 | + private lateinit var syncLogService: SyncLogService |
| 61 | + |
52 | 62 | private val permissions = mapOf("administration" to "read", "contents" to "read", "metadata" to "read") |
53 | 63 | private val events = listOf("label", "public", "repository", "push") |
54 | 64 |
|
@@ -507,4 +517,109 @@ class WebhookEventServiceTest { |
507 | 517 |
|
508 | 518 | verify { workflowRunService.consumeWebhookPayload(payload) } |
509 | 519 | } |
| 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 | + } |
510 | 625 | } |
0 commit comments