Skip to content

Commit

Permalink
CID-2776: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlajmileanix committed Aug 6, 2024
1 parent 158679c commit 918f68c
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.leanix.githubagent.controllers

import com.ninjasquad.springmockk.MockkBean
import io.mockk.verify
import net.leanix.githubagent.services.WebhookService
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.anyString
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers

@WebMvcTest(GitHubWebhookController::class)
class GitHubWebhookControllerTest {

@Autowired
private lateinit var mockMvc: MockMvc

@MockkBean
private lateinit var webhookService: WebhookService

@Test
fun `should not process not supported events`() {
val eventType = "UNSUPPORTED_EVENT"
val payload = "{}"

mockMvc.perform(
MockMvcRequestBuilders.post("/github/webhook")
.header("X-GitHub-Event", eventType)
.content(payload)
)
.andExpect(MockMvcResultMatchers.status().isAccepted)

verify(exactly = 0) { webhookService.consumeWebhookEvent(anyString(), anyString()) }
}
}
121 changes: 121 additions & 0 deletions src/test/kotlin/net/leanix/githubagent/services/WebhookServiceTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package net.leanix.githubagent.services

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.verify
import net.leanix.githubagent.dto.ManifestFileAction
import net.leanix.githubagent.dto.ManifestFileUpdateDto
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles

@SpringBootTest
@ActiveProfiles("test")
class WebhookServiceTest {

@MockkBean
private lateinit var webSocketService: WebSocketService

@MockkBean
private lateinit var gitHubGraphQLService: GitHubGraphQLService

@MockkBean
private lateinit var cachingService: CachingService

@MockkBean
private lateinit var gitHubAuthenticationService: GitHubAuthenticationService

@Autowired
private lateinit var webhookService: WebhookService

@BeforeEach
fun setUp() {
every { gitHubAuthenticationService.refreshTokens() } returns Unit
every { webSocketService.sendMessage(any(), any()) } returns Unit
}

@Test
fun `should refresh tokens if expired`() {
val payload = """{
"repository": {
"name": "repo",
"full_name": "owner/repo",
"owner": {"name": "owner"},
"default_branch": "main"
},
"head_commit": {
"added": [],
"modified": [],
"removed": []
},
"installation": {"id": 1},
"ref": "refs/heads/main"
}"""

every { cachingService.get("installationToken:1") } returns null andThen "token"

webhookService.consumeWebhookEvent("PUSH", payload)

verify(exactly = 1) { gitHubAuthenticationService.refreshTokens() }
}

@Test
fun `should process push event`() {
every { cachingService.get(any()) } returns "token"
every { gitHubGraphQLService.getFileContent(any(), any(), any(), any()) } returns "content"

val payload = """{
"repository": {
"name": "repo",
"full_name": "owner/repo",
"owner": {"name": "owner"},
"default_branch": "main"
},
"head_commit": {
"added": [],
"modified": ["leanix.yaml"],
"removed": []
},
"installation": {"id": 1},
"ref": "refs/heads/main"
}"""

webhookService.consumeWebhookEvent("PUSH", payload)

verify(exactly = 1) {
webSocketService.sendMessage(
"/events/manifestFile",
ManifestFileUpdateDto(
"owner/repo",
ManifestFileAction.MODIFIED,
"content"
)
)
}
}

@Test
fun `should send all events of type other than push to backend without processing`() {
val payload = """{
"repository": {
"name": "repo",
"full_name": "owner/repo",
"owner": {"name": "owner"},
"default_branch": "main"
},
"head_commit": {
"added": [],
"modified": [],
"removed": []
},
"installation": {"id": 1},
"ref": "refs/heads/main"
}"""

webhookService.consumeWebhookEvent("OTHER", payload)

verify(exactly = 1) { webSocketService.sendMessage("/events/other", payload) }
}
}

0 comments on commit 918f68c

Please sign in to comment.