|
24 | 24 | package com.microsoft.identity.common.internal.util |
25 | 25 |
|
26 | 26 | import android.app.Activity |
27 | | -import android.content.Intent |
28 | | -import android.content.IntentFilter |
29 | | -import android.content.pm.ActivityInfo |
30 | | -import android.content.pm.ApplicationInfo |
31 | | -import android.content.pm.PackageInfo |
32 | | -import android.content.pm.ResolveInfo |
33 | | -import android.content.pm.Signature |
34 | | -import android.net.Uri |
35 | 27 | import androidx.fragment.app.Fragment |
36 | 28 | import com.microsoft.identity.common.internal.ui.AndroidAuthorizationStrategyFactory |
37 | 29 | import com.microsoft.identity.common.internal.ui.browser.DefaultBrowserAuthorizationStrategy |
38 | 30 | import com.microsoft.identity.common.internal.ui.webview.EmbeddedWebViewAuthorizationStrategy |
39 | | -import com.microsoft.identity.common.java.commands.parameters.InteractiveTokenCommandParameters |
| 31 | +import com.microsoft.identity.common.java.browser.Browser |
40 | 32 | import com.microsoft.identity.common.java.providers.oauth2.IAuthorizationStrategy |
41 | 33 | import com.microsoft.identity.common.java.ui.AuthorizationAgent |
42 | | -import com.microsoft.identity.common.java.ui.BrowserDescriptor |
43 | 34 | import org.junit.Test |
44 | 35 | import org.junit.runner.RunWith |
45 | 36 | import org.mockito.Mockito.mock |
46 | | -import org.mockito.Mockito.`when` |
47 | 37 | import org.robolectric.RobolectricTestRunner |
48 | | -import org.robolectric.Shadows |
49 | | -import org.robolectric.annotation.Config |
50 | 38 |
|
51 | 39 | @RunWith(RobolectricTestRunner::class) |
52 | | -// We need this to add the PackageManager.MATCH_DEFAULT_ONLY flag on BrowserSelector.getBrowsers |
53 | | -// and to avoid a NPE on PackageHelper.getSignatures. |
54 | | -@Config(sdk = [27]) |
55 | 40 | class AndroidAuthorizationStrategyFactoryTest { |
56 | 41 |
|
57 | 42 | companion object { |
58 | | - private const val PACKAGE_NAME = "com.android.chrome" |
59 | | - private const val BROWSER_NAME = "Chrome" |
60 | | - private const val SIGNATURE_HASH_HEX = "3082010A0282010100C3B3A700D1E020302020034A7B8888" |
61 | | - private const val SIGNATURE_HASH_BASE_64 = |
62 | | - "Lu2NRuBdl7odm7sAKREJMShwDFWO7piPO_K69PxPWghQSaboLhOI2fvJt-Q17dW9NTgyPhOopWS6Cxgi9wrTew==" |
63 | | - private const val VERSION_LOWER_BOUND = "0" |
64 | | - private const val VERSION_UPPER_BOUND = "1" |
65 | | - private val fakeBrowser = BrowserDescriptor( |
66 | | - PACKAGE_NAME, |
67 | | - SIGNATURE_HASH_BASE_64, |
68 | | - VERSION_LOWER_BOUND, |
69 | | - VERSION_UPPER_BOUND |
70 | | - ) |
| 43 | + private val browser = mock(Browser::class.java) |
71 | 44 | } |
72 | 45 |
|
73 | 46 | @Test |
74 | | - fun `test getAuthorizationStrategy with authorization agent WEBVIEW, empty browser safe list`() { |
| 47 | + fun `test getAuthorizationStrategy with authorization agent WEBVIEW`() { |
75 | 48 | val strategy = getAuthorizationStrategy( |
76 | 49 | authorizationAgent = AuthorizationAgent.WEBVIEW, |
77 | | - browserSafeList = emptyList() |
| 50 | + browser = browser, |
78 | 51 | ) |
79 | 52 | assert(strategy is EmbeddedWebViewAuthorizationStrategy) |
80 | 53 | } |
81 | 54 |
|
82 | 55 | @Test |
83 | | - fun `test getAuthorizationStrategy with authorization agent BROWSER, empty browser safe list`() { |
| 56 | + fun `test getAuthorizationStrategy with authorization agent WEBVIEW, browser null`() { |
84 | 57 | val strategy = getAuthorizationStrategy( |
85 | | - authorizationAgent = AuthorizationAgent.BROWSER, |
86 | | - browserSafeList = emptyList() |
| 58 | + authorizationAgent = AuthorizationAgent.WEBVIEW, |
| 59 | + browser = null, |
87 | 60 | ) |
88 | 61 | assert(strategy is EmbeddedWebViewAuthorizationStrategy) |
89 | 62 | } |
90 | 63 |
|
91 | 64 | @Test |
92 | | - fun `test getAuthorizationStrategy with authorization agent WEBVIEW, fakeBrowser in safe list`() { |
| 65 | + fun `test getAuthorizationStrategy with authorization agent BROWSER`() { |
93 | 66 | val strategy = getAuthorizationStrategy( |
94 | | - authorizationAgent = AuthorizationAgent.WEBVIEW, |
95 | | - browserSafeList = listOf(fakeBrowser) |
| 67 | + authorizationAgent = AuthorizationAgent.BROWSER, |
| 68 | + browser = browser, |
96 | 69 | ) |
97 | | - assert(strategy is EmbeddedWebViewAuthorizationStrategy) |
| 70 | + assert(strategy is DefaultBrowserAuthorizationStrategy) |
98 | 71 | } |
99 | 72 |
|
| 73 | + |
100 | 74 | @Test |
101 | | - fun `test getAuthorizationStrategy with authorization agent BROWSER, fakeBrowser in safe list`() { |
| 75 | + fun `test getAuthorizationStrategy with authorization agent BROWSER, browser null`() { |
102 | 76 | val strategy = getAuthorizationStrategy( |
103 | 77 | authorizationAgent = AuthorizationAgent.BROWSER, |
104 | | - browserSafeList = listOf(fakeBrowser) |
| 78 | + browser = null, |
105 | 79 | ) |
106 | | - assert(strategy is DefaultBrowserAuthorizationStrategy) |
| 80 | + assert(strategy is EmbeddedWebViewAuthorizationStrategy) |
107 | 81 | } |
108 | 82 |
|
109 | 83 | private fun getAuthorizationStrategy( |
110 | | - browserSafeList: List<BrowserDescriptor>, |
111 | | - authorizationAgent: AuthorizationAgent |
| 84 | + browser: Browser?, |
| 85 | + authorizationAgent: AuthorizationAgent, |
112 | 86 | ): IAuthorizationStrategy<*, *> { |
113 | | - |
114 | | - val context = org.robolectric.RuntimeEnvironment.getApplication() |
115 | | - val shadowPackageManager = Shadows.shadowOf(context.packageManager) |
116 | | - |
117 | | - // Define a mock browser package |
118 | | - val browserPackageInfo = PackageInfo().apply { |
119 | | - packageName = PACKAGE_NAME |
120 | | - versionName = "1.0" |
121 | | - // Add signatures |
122 | | - signatures = arrayOf( |
123 | | - Signature(SIGNATURE_HASH_HEX) |
124 | | - ) |
125 | | - applicationInfo = ApplicationInfo().apply { |
126 | | - this.packageName = PACKAGE_NAME |
127 | | - this.name = BROWSER_NAME |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - // Add the browser package to the shadow PackageManager |
132 | | - shadowPackageManager.installPackage(browserPackageInfo) |
133 | | - |
134 | | - // Create a mock ResolveInfo for browser activities |
135 | | - val browserResolveInfo = ResolveInfo().apply { |
136 | | - activityInfo = ActivityInfo().apply { |
137 | | - packageName = PACKAGE_NAME |
138 | | - name = BROWSER_NAME |
139 | | - } |
140 | | - isDefault = true |
141 | | - |
142 | | - // Add a filter for handling specific intents (e.g., http/https URLs) |
143 | | - filter = IntentFilter().apply { |
144 | | - addAction(Intent.ACTION_VIEW) // Action to view content |
145 | | - addCategory(Intent.CATEGORY_BROWSABLE) // Default category |
146 | | - addDataScheme("http") // Filter for http URLs |
147 | | - addDataScheme("https") // Filter for https URLs |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - // Add the browser to handle VIEW intents with http/https schemes |
152 | | - shadowPackageManager.addResolveInfoForIntent( |
153 | | - Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")), |
154 | | - browserResolveInfo |
155 | | - ) |
156 | | - |
157 | 87 | // Construct the factory |
158 | 88 | val strategyFactory = AndroidAuthorizationStrategyFactory.builder() |
159 | | - .context(context) |
| 89 | + .context(org.robolectric.RuntimeEnvironment.getApplication()) |
160 | 90 | .activity(mock(Activity::class.java)) |
161 | 91 | .fragment(mock(Fragment::class.java)) |
162 | 92 | .build() |
163 | 93 |
|
164 | | - // Mock the parameters |
165 | | - val params: InteractiveTokenCommandParameters = |
166 | | - mock(InteractiveTokenCommandParameters::class.java) |
167 | | - `when`(params.browserSafeList).thenReturn(browserSafeList) |
168 | | - `when`(params.preferredBrowser).thenReturn(null) |
169 | | - `when`(params.authorizationAgent).thenReturn(authorizationAgent) |
170 | | - |
171 | | - return strategyFactory.getAuthorizationStrategy(params) |
| 94 | + return strategyFactory.getAuthorizationStrategy(authorizationAgent, browser, true) |
172 | 95 | } |
173 | 96 | } |
0 commit comments