|
22 | 22 | * THE SOFTWARE. |
23 | 23 | */ |
24 | 24 |
|
| 25 | +import com.google.api.client.http.HttpTransport |
| 26 | +import com.google.api.client.http.LowLevelHttpRequest |
| 27 | +import com.google.api.client.http.LowLevelHttpResponse |
25 | 28 | import com.looker.rtl.AuthSession |
26 | 29 | import com.looker.rtl.OAuthSession |
27 | 30 | import com.looker.rtl.Transport |
| 31 | +import com.looker.rtl.TransportOptions |
28 | 32 | import org.junit.Test |
| 33 | +import java.io.ByteArrayInputStream |
29 | 34 | import java.io.File |
| 35 | +import java.io.InputStream |
30 | 36 | import kotlin.test.assertEquals |
31 | 37 | import kotlin.test.assertFalse |
32 | 38 | import kotlin.test.assertTrue |
33 | 39 |
|
34 | 40 | @ExperimentalUnsignedTypes |
35 | 41 | class TestAuthSession { |
36 | | - val config = TestConfig() |
37 | | - val settings = config.settings |
38 | | - val testSettings = config.testSettings(settings) |
| 42 | + val config by lazy { TestConfig() } |
| 43 | + val settings by lazy { config.settings } |
| 44 | + val testSettings by lazy { config.testSettings(settings) } |
39 | 45 |
|
40 | 46 | @Test |
41 | 47 | fun testTestFiles() { |
@@ -73,6 +79,62 @@ class TestAuthSession { |
73 | 79 | assertFalse(session.isAuthenticated()) |
74 | 80 | } |
75 | 81 |
|
| 82 | + @Test |
| 83 | + fun testSudoLoginUsesApiPrefixAndAuthHeader() { |
| 84 | + val mockSettings = MockSettings(bareMinimum) |
| 85 | + val requests = mutableListOf<Pair<String, String>>() |
| 86 | + val requestHeaders = mutableListOf<Map<String, String>>() |
| 87 | + |
| 88 | + val mockHttpTransport = object : HttpTransport() { |
| 89 | + override fun buildRequest(method: String, url: String): LowLevelHttpRequest { |
| 90 | + return object : LowLevelHttpRequest() { |
| 91 | + private val headers = mutableMapOf<String, String>() |
| 92 | + |
| 93 | + override fun addHeader(name: String, value: String) { |
| 94 | + headers[name] = value |
| 95 | + } |
| 96 | + |
| 97 | + override fun execute(): LowLevelHttpResponse { |
| 98 | + requests.add(method to url) |
| 99 | + requestHeaders.add(headers.toMap()) |
| 100 | + val responseJson = if (url.endsWith("/login")) { |
| 101 | + """{"access_token":"admin_token","token_type":"Bearer","expires_in":3600}""" |
| 102 | + } else { |
| 103 | + """{"access_token":"sudo_token","token_type":"Bearer","expires_in":3600}""" |
| 104 | + } |
| 105 | + return object : LowLevelHttpResponse() { |
| 106 | + override fun getContent(): InputStream = |
| 107 | + ByteArrayInputStream(responseJson.toByteArray(Charsets.UTF_8)) |
| 108 | + override fun getContentEncoding(): String? = null |
| 109 | + override fun getContentLength(): Long = responseJson.length.toLong() |
| 110 | + override fun getContentType(): String = "application/json" |
| 111 | + override fun getStatusLine(): String = "HTTP/1.1 200 OK" |
| 112 | + override fun getStatusCode(): Int = 200 |
| 113 | + override fun getReasonPhrase(): String = "OK" |
| 114 | + override fun getHeaderCount(): Int = 0 |
| 115 | + override fun getHeaderName(index: Int): String? = null |
| 116 | + override fun getHeaderValue(index: Int): String? = null |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + val mockTransport = object : Transport(mockSettings) { |
| 124 | + override fun initTransport(options: TransportOptions): HttpTransport = mockHttpTransport |
| 125 | + } |
| 126 | + |
| 127 | + val session = AuthSession(mockSettings, mockTransport) |
| 128 | + val token = session.login("user_123") |
| 129 | + |
| 130 | + assertEquals(2, requests.size) |
| 131 | + assertEquals("POST" to "https://my.looker.com:19999/api/4.0/login", requests[0]) |
| 132 | + assertEquals("POST" to "https://my.looker.com:19999/api/4.0/login/user_123", requests[1]) |
| 133 | + assertEquals("token admin_token", requestHeaders[1]["Authorization"]) |
| 134 | + assertTrue(session.isSudo()) |
| 135 | + assertEquals("sudo_token", token.accessToken) |
| 136 | + } |
| 137 | + |
76 | 138 | @Test |
77 | 139 | fun testSha256() { |
78 | 140 | val session = OAuthSession(settings, Transport(testSettings)) |
|
0 commit comments