|
| 1 | +package io.github.typesafegithub.workflows.jitbindingserver |
| 2 | + |
| 3 | +import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords |
| 4 | +import io.kotest.core.spec.style.FunSpec |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | +import io.ktor.client.request.get |
| 7 | +import io.ktor.client.statement.bodyAsText |
| 8 | +import io.ktor.http.HttpStatusCode |
| 9 | +import io.ktor.server.testing.testApplication |
| 10 | +import io.mockk.every |
| 11 | +import io.mockk.mockk |
| 12 | +import io.mockk.verify |
| 13 | + |
| 14 | +class MetadataRoutesTest : |
| 15 | + FunSpec({ |
| 16 | + context("artifacts for a given package") { |
| 17 | + test("when some artifacts were generated") { |
| 18 | + testApplication { |
| 19 | + // Given |
| 20 | + application { |
| 21 | + appModule( |
| 22 | + buildPackageArtifacts = { _, _, _ -> |
| 23 | + mapOf("maven-metadata.xml" to "Some XML contents") |
| 24 | + }, |
| 25 | + getGithubAuthToken = { "some-token" }, |
| 26 | + // Irrelevant for these tests. |
| 27 | + buildVersionArtifacts = { emptyMap() }, |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + // When |
| 32 | + val response = client.get("some-owner/some-action/maven-metadata.xml") |
| 33 | + |
| 34 | + // Then |
| 35 | + response.status shouldBe HttpStatusCode.OK |
| 36 | + response.bodyAsText() shouldBe "Some XML contents" |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + test("when no artifacts could be generated") { |
| 41 | + testApplication { |
| 42 | + // Given |
| 43 | + application { |
| 44 | + appModule( |
| 45 | + buildPackageArtifacts = { _, _, _ -> |
| 46 | + emptyMap() |
| 47 | + }, |
| 48 | + getGithubAuthToken = { "some-token" }, |
| 49 | + // Irrelevant for these tests. |
| 50 | + buildVersionArtifacts = { emptyMap() }, |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + // When |
| 55 | + val response = client.get("some-owner/some-action/maven-metadata.xml") |
| 56 | + |
| 57 | + // Then |
| 58 | + response.status shouldBe HttpStatusCode.NotFound |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + test("when artifact generation fails") { |
| 63 | + testApplication { |
| 64 | + // Given |
| 65 | + application { |
| 66 | + appModule( |
| 67 | + buildPackageArtifacts = { _, _, _ -> |
| 68 | + error("An internal error occurred!") |
| 69 | + }, |
| 70 | + getGithubAuthToken = { "some-token" }, |
| 71 | + // Irrelevant for these tests. |
| 72 | + buildVersionArtifacts = { emptyMap() }, |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + // When |
| 77 | + val response = client.get("some-owner/some-action/maven-metadata.xml") |
| 78 | + |
| 79 | + // Then |
| 80 | + response.status shouldBe HttpStatusCode.InternalServerError |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + test("when binding generation fails and then succeeds, and two requests are made") { |
| 85 | + testApplication { |
| 86 | + // Given |
| 87 | + val mockBuildPackageArtifacts = |
| 88 | + mockk< |
| 89 | + ( |
| 90 | + ActionCoords, |
| 91 | + String, |
| 92 | + (Collection<ActionCoords>) -> Unit, |
| 93 | + ) -> Map<String, String>, |
| 94 | + >() |
| 95 | + every { mockBuildPackageArtifacts(any(), any(), any()) } throws |
| 96 | + Exception("An internal error occurred!") andThen |
| 97 | + mapOf("maven-metadata.xml" to "Some XML contents") |
| 98 | + application { |
| 99 | + appModule( |
| 100 | + buildPackageArtifacts = mockBuildPackageArtifacts, |
| 101 | + getGithubAuthToken = { "some-token" }, |
| 102 | + // Irrelevant for these tests. |
| 103 | + buildVersionArtifacts = { emptyMap() }, |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + // When |
| 108 | + val response = client.get("some-owner/some-action/maven-metadata.xml") |
| 109 | + // Then |
| 110 | + response.status shouldBe HttpStatusCode.InternalServerError |
| 111 | + |
| 112 | + // When |
| 113 | + val response2 = client.get("some-owner/some-action/maven-metadata.xml") |
| 114 | + // Then |
| 115 | + // This assertion represents an undesired behavior - it should retry generating the artifact |
| 116 | + // and return the actual POM. |
| 117 | + // TODO fix in https://github.com/typesafegithub/github-workflows-kt/issues/1938 |
| 118 | + response2.status shouldBe HttpStatusCode.InternalServerError |
| 119 | + |
| 120 | + // This assertion represents an undesired behavior - it should call the artifact generation twice, |
| 121 | + // first with exception, and then when it's successful. |
| 122 | + // TODO fix in https://github.com/typesafegithub/github-workflows-kt/issues/1938 |
| 123 | + verify(exactly = 1) { mockBuildPackageArtifacts(any(), any(), any()) } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }) |
0 commit comments