|
| 1 | +package fi.hsl.jore4.timetables.config |
| 2 | + |
| 3 | +import io.mockk.every |
| 4 | +import io.mockk.mockkObject |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken |
| 7 | +import java.net.URI |
| 8 | +import java.net.http.HttpClient |
| 9 | +import java.net.http.HttpHeaders |
| 10 | +import java.net.http.HttpRequest |
| 11 | +import java.net.http.HttpResponse |
| 12 | +import java.util.Optional |
| 13 | +import javax.net.ssl.SSLSession |
| 14 | +import kotlin.jvm.optionals.getOrDefault |
| 15 | +import kotlin.test.assertTrue |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertFalse |
| 18 | + |
| 19 | +class RemoteAuthenticationProviderTest { |
| 20 | + |
| 21 | + companion object { |
| 22 | + private const val requestUrl = "http://testing:123" |
| 23 | + private const val ROLE_HEADER = "X-Hasura-Role" |
| 24 | + private const val ID_HEADER = "X-Hasura-Id" |
| 25 | + } |
| 26 | + |
| 27 | + private val authenticationProperties = AuthenticationProperties(requestUrl) |
| 28 | + private val remoteAuthenticationProvider = RemoteAuthenticationProvider(authenticationProperties) |
| 29 | + |
| 30 | + class CustomHttpResponse( |
| 31 | + private val status: Int, |
| 32 | + private val uri: URI, |
| 33 | + private val id: String, |
| 34 | + private val role: String |
| 35 | + ) : HttpResponse<String> { |
| 36 | + |
| 37 | + override fun statusCode(): Int { |
| 38 | + return status |
| 39 | + } |
| 40 | + |
| 41 | + override fun request(): HttpRequest { |
| 42 | + TODO("Not implemented") |
| 43 | + } |
| 44 | + |
| 45 | + override fun previousResponse(): Optional<HttpResponse<String>> { |
| 46 | + return Optional.empty() |
| 47 | + } |
| 48 | + |
| 49 | + override fun headers(): HttpHeaders { |
| 50 | + return HttpHeaders.of(mutableMapOf("Content-Type" to listOf("application/json"))) { _, _ -> true } |
| 51 | + } |
| 52 | + |
| 53 | + override fun body(): String { |
| 54 | + if (status == 401) { |
| 55 | + return "" |
| 56 | + } |
| 57 | + return """ |
| 58 | + { |
| 59 | + "$ID_HEADER": "$id", |
| 60 | + "$ROLE_HEADER": "$role" |
| 61 | + } |
| 62 | + """.trimIndent() |
| 63 | + } |
| 64 | + |
| 65 | + override fun sslSession(): Optional<SSLSession> { |
| 66 | + return Optional.empty() |
| 67 | + } |
| 68 | + |
| 69 | + override fun uri(): URI { |
| 70 | + return uri |
| 71 | + } |
| 72 | + |
| 73 | + override fun version(): HttpClient.Version { |
| 74 | + return HttpClient.Version.HTTP_2 |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private fun setupResponseForToken( |
| 79 | + sessionToken: String, |
| 80 | + userName: String, |
| 81 | + role: String |
| 82 | + ) { |
| 83 | + mockkObject(RemoteAuthenticationProvider) |
| 84 | + every { |
| 85 | + RemoteAuthenticationProvider.sendRequest( |
| 86 | + match { request -> |
| 87 | + request.headers() |
| 88 | + .firstValue("cookie") |
| 89 | + .map { it.substringAfter("SESSION=") } |
| 90 | + .map { it == sessionToken } |
| 91 | + .getOrDefault(false) |
| 92 | + } |
| 93 | + ) |
| 94 | + } returns CustomHttpResponse( |
| 95 | + 200, |
| 96 | + URI(requestUrl), |
| 97 | + userName, |
| 98 | + role |
| 99 | + ) |
| 100 | + |
| 101 | + every { |
| 102 | + RemoteAuthenticationProvider.sendRequest( |
| 103 | + match { request -> |
| 104 | + request.headers() |
| 105 | + .firstValue("cookie") |
| 106 | + .map { it.substringAfter("SESSION=") } |
| 107 | + .map { it != sessionToken } |
| 108 | + .getOrDefault(true) |
| 109 | + } |
| 110 | + ) |
| 111 | + } returns CustomHttpResponse( |
| 112 | + 401, |
| 113 | + URI(requestUrl), |
| 114 | + "", |
| 115 | + "" |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun `should authenticate the user`() { |
| 121 | + val requestedRole = "admin" |
| 122 | + val userName = "user123" |
| 123 | + val sessionToken = "sessionToken123" |
| 124 | + |
| 125 | + setupResponseForToken(sessionToken, userName, requestedRole) |
| 126 | + |
| 127 | + val preAuth = PreAuthenticatedAuthenticationToken(sessionToken, requestedRole) |
| 128 | + |
| 129 | + val value = remoteAuthenticationProvider.authenticate(preAuth) |
| 130 | + |
| 131 | + assertTrue(value.isAuthenticated) |
| 132 | + assertEquals(userName, value.principal) |
| 133 | + assertEquals(1, value.authorities.size) |
| 134 | + assertEquals(requestedRole, value.authorities.first().authority) |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + fun `should fail to authenticate`() { |
| 139 | + val requestedRole = "admin" |
| 140 | + val userName = "user123" |
| 141 | + val sessionToken = "sessionToken123" |
| 142 | + |
| 143 | + setupResponseForToken(sessionToken, userName, requestedRole) |
| 144 | + |
| 145 | + val preAuth = PreAuthenticatedAuthenticationToken("wrong token", requestedRole) |
| 146 | + |
| 147 | + val value = remoteAuthenticationProvider.authenticate(preAuth) |
| 148 | + |
| 149 | + assertFalse(value.isAuthenticated) |
| 150 | + assertEquals("", value.principal) |
| 151 | + assertEquals(0, value.authorities.size) |
| 152 | + } |
| 153 | +} |
0 commit comments