|
| 1 | +// All rights reserved. |
| 2 | +// |
| 3 | +// This code is licensed under the MIT License. |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files(the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions : |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | +package com.microsoft.identity.common.internal.ui.webview.challengehandlers |
| 23 | + |
| 24 | +import org.junit.runner.RunWith |
| 25 | +import org.robolectric.RobolectricTestRunner |
| 26 | +import android.webkit.WebView |
| 27 | +import com.microsoft.identity.common.java.broker.CommonRefreshTokenCredentialProvider |
| 28 | +import com.microsoft.identity.common.java.opentelemetry.AttributeName |
| 29 | +import io.mockk.every |
| 30 | +import io.mockk.mockkObject |
| 31 | +import io.mockk.unmockkAll |
| 32 | +import io.opentelemetry.api.trace.Span |
| 33 | +import org.junit.Before |
| 34 | +import org.junit.Test |
| 35 | +import org.mockito.Mockito.* |
| 36 | + |
| 37 | +@RunWith(RobolectricTestRunner::class) |
| 38 | +class CrossCloudChallengeHandlerTest { |
| 39 | + |
| 40 | + private lateinit var webView: WebView |
| 41 | + private lateinit var headers: HashMap<String, String> |
| 42 | + private lateinit var span: Span |
| 43 | + private lateinit var crossCloudChallengeHandler: CrossCloudChallengeHandler |
| 44 | + |
| 45 | + @Before |
| 46 | + fun setUp() { |
| 47 | + webView = mock(WebView::class.java) |
| 48 | + headers = HashMap() |
| 49 | + span = mock(Span::class.java) |
| 50 | + crossCloudChallengeHandler = CrossCloudChallengeHandler(webView, headers, span) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `testProcessChallenge success`() { |
| 55 | + val testUrl = "https://example.com?login_hint=testuser" |
| 56 | + crossCloudChallengeHandler.processChallenge(testUrl) |
| 57 | + verify(webView).loadUrl(eq(testUrl), eq(headers)) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `testProcessChallenge when exception is thrown`() { |
| 62 | + val testUrl = "https://example.com?login_hint=testuser" |
| 63 | + |
| 64 | + mockkObject(CommonRefreshTokenCredentialProvider) |
| 65 | + every { |
| 66 | + CommonRefreshTokenCredentialProvider.getRefreshTokenCredential( |
| 67 | + testUrl, |
| 68 | + "testuser" |
| 69 | + ) |
| 70 | + } throws Exception() |
| 71 | + |
| 72 | + try { |
| 73 | + crossCloudChallengeHandler.processChallenge(testUrl) |
| 74 | + } catch (e: Exception) { |
| 75 | + verify(webView, never()).loadUrl(eq(testUrl), eq(headers)) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `modifyHeadersWithRefreshTokenCredential should update headers when prt is available`() { |
| 81 | + val url = "https://login.microsoftonline.com?login_hint=testuser" |
| 82 | + val username = "testuser" |
| 83 | + val refreshTokenCredential = "refreshTokenCredential" |
| 84 | + |
| 85 | + mockkObject(CommonRefreshTokenCredentialProvider) |
| 86 | + every { |
| 87 | + CommonRefreshTokenCredentialProvider.getRefreshTokenCredential( |
| 88 | + url, |
| 89 | + username |
| 90 | + ) |
| 91 | + } returns refreshTokenCredential |
| 92 | + |
| 93 | + // Call the method |
| 94 | + crossCloudChallengeHandler.modifyHeadersWithRefreshTokenCredential(url) |
| 95 | + verify(span).setAttribute( |
| 96 | + AttributeName.is_new_refresh_token_cred_header_attached.name, |
| 97 | + true |
| 98 | + ) |
| 99 | + unmockkAll() |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `modifyHeadersWithRefreshTokenCredential should not update headers when login_hint is missing`() { |
| 104 | + val url = "https://login.microsoftonline.com" |
| 105 | + crossCloudChallengeHandler.modifyHeadersWithRefreshTokenCredential(url) |
| 106 | + verify(span, never()).setAttribute( |
| 107 | + AttributeName.is_new_refresh_token_cred_header_attached.name, |
| 108 | + true |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + fun `modifyHeadersWithRefreshTokenCredential null refresh token credential`() { |
| 114 | + val url = "https://login.microsoftonline.com?login_hint=testuser" |
| 115 | + crossCloudChallengeHandler.modifyHeadersWithRefreshTokenCredential(url) |
| 116 | + verify(span, never()).setAttribute( |
| 117 | + AttributeName.is_new_refresh_token_cred_header_attached.name, |
| 118 | + true |
| 119 | + ) |
| 120 | + } |
| 121 | +} |
0 commit comments