generated from leanix/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
158679c
commit 918f68c
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/test/kotlin/net/leanix/githubagent/controllers/GitHubWebhookControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
121
src/test/kotlin/net/leanix/githubagent/services/WebhookServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |