Skip to content

Commit d8fc6e8

Browse files
committed
test: cover the persisted OAuth client authentication method
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent e25a7ea commit d8fc6e8

2 files changed

Lines changed: 205 additions & 0 deletions

File tree

‎owncloudData/src/androidTest/java/com/owncloud/android/data/authentication/datasources/implementation/OCLocalAuthenticationDataSourceTest.kt‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ import androidx.test.platform.app.InstrumentationRegistry
2727
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE
2828
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_ID
2929
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_CLIENT_SECRET
30+
import com.owncloud.android.data.authentication.KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD
3031
import com.owncloud.android.data.authentication.KEY_OAUTH2_REFRESH_TOKEN
3132
import com.owncloud.android.data.authentication.KEY_OAUTH2_SCOPE
3233
import com.owncloud.android.data.authentication.SELECTED_ACCOUNT
3334
import com.owncloud.android.data.providers.SharedPreferencesProvider
3435
import com.owncloud.android.domain.authentication.oauth.model.ClientRegistrationInfo
36+
import com.owncloud.android.domain.authentication.oauth.model.OIDCServerConfiguration
3537
import com.owncloud.android.domain.exceptions.AccountNotFoundException
3638
import com.owncloud.android.domain.exceptions.AccountNotNewException
3739
import com.owncloud.android.domain.exceptions.AccountNotTheSameException
@@ -290,6 +292,159 @@ class OCLocalAuthenticationDataSourceTest {
290292
}
291293
}
292294

295+
@Test
296+
fun addOAuthAccountStoresPostAuthMethod() {
297+
mockRegularAccountCreationFlow()
298+
mockSelectedAccountNameInPreferences()
299+
300+
val postRegistration = OC_CLIENT_REGISTRATION.copy(
301+
tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_POST
302+
)
303+
val newAccountName = ocLocalAuthenticationDataSource.addOAuthAccount(
304+
OC_ACCOUNT_ID,
305+
OC_REDIRECTION_PATH.lastPermanentLocation,
306+
OC_AUTH_TOKEN_TYPE,
307+
OC_ACCESS_TOKEN,
308+
OC_SECURE_SERVER_INFO_BASIC_AUTH,
309+
OC_USER_INFO,
310+
OC_REFRESH_TOKEN,
311+
OC_SCOPE,
312+
null,
313+
postRegistration
314+
)
315+
316+
val newAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)
317+
assertEquals(newAccount.name, newAccountName)
318+
verifyOAuthParamsAreUpdated(
319+
newAccount,
320+
OC_ACCESS_TOKEN,
321+
OC_OAUTH_SUPPORTED_TRUE,
322+
OC_REFRESH_TOKEN,
323+
OC_SCOPE,
324+
postRegistration,
325+
1
326+
)
327+
}
328+
329+
@Test
330+
fun addOAuthAccountReauthorizationReplacesAuthMethod() {
331+
mockSelectedAccountNameInPreferences()
332+
333+
val postRegistration = OC_CLIENT_REGISTRATION.copy(
334+
tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_POST
335+
)
336+
val accountName = ocLocalAuthenticationDataSource.addOAuthAccount(
337+
OC_ACCOUNT_ID,
338+
OC_REDIRECTION_PATH.lastPermanentLocation,
339+
OC_AUTH_TOKEN_TYPE,
340+
OC_ACCESS_TOKEN,
341+
OC_SECURE_SERVER_INFO_BASIC_AUTH,
342+
OC_USER_INFO.copy(id = OC_ACCOUNT_ID),
343+
OC_REFRESH_TOKEN,
344+
OC_SCOPE,
345+
OC_ACCOUNT_NAME,
346+
postRegistration
347+
)
348+
349+
assertEquals(OC_ACCOUNT_NAME, accountName)
350+
verifyOAuthParamsAreUpdated(
351+
OC_ACCOUNT,
352+
OC_ACCESS_TOKEN,
353+
OC_OAUTH_SUPPORTED_TRUE,
354+
OC_REFRESH_TOKEN,
355+
OC_SCOPE,
356+
postRegistration,
357+
1
358+
)
359+
}
360+
361+
@Test
362+
fun addOAuthAccountIsolatesAuthMethodBetweenAccounts() {
363+
mockRegularAccountCreationFlow()
364+
mockSelectedAccountNameInPreferences()
365+
366+
val postRegistration = OC_CLIENT_REGISTRATION.copy(
367+
tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_POST
368+
)
369+
ocLocalAuthenticationDataSource.addOAuthAccount(
370+
OC_ACCOUNT_ID,
371+
OC_REDIRECTION_PATH.lastPermanentLocation,
372+
OC_AUTH_TOKEN_TYPE,
373+
OC_ACCESS_TOKEN,
374+
OC_SECURE_SERVER_INFO_BASIC_AUTH,
375+
OC_USER_INFO,
376+
OC_REFRESH_TOKEN,
377+
OC_SCOPE,
378+
null,
379+
postRegistration
380+
)
381+
382+
val firstAccount = Account(OC_ACCOUNT_NAME, OC_ACCOUNT.type)
383+
val secondAccount = Account("other@demo.owncloud.com", OC_ACCOUNT.type)
384+
mockGetAccountsByType(OC_ACCOUNT.type, arrayOf(firstAccount))
385+
386+
ocLocalAuthenticationDataSource.addOAuthAccount(
387+
"other",
388+
OC_REDIRECTION_PATH.lastPermanentLocation,
389+
OC_AUTH_TOKEN_TYPE,
390+
OC_ACCESS_TOKEN,
391+
OC_SECURE_SERVER_INFO_BASIC_AUTH,
392+
OC_USER_INFO,
393+
OC_REFRESH_TOKEN,
394+
OC_SCOPE,
395+
null,
396+
OC_CLIENT_REGISTRATION
397+
)
398+
399+
verify(exactly = 1) {
400+
accountManager.setUserData(
401+
firstAccount,
402+
KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD,
403+
OIDCServerConfiguration.CLIENT_SECRET_POST
404+
)
405+
}
406+
verify(exactly = 0) {
407+
accountManager.setUserData(
408+
firstAccount,
409+
KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD,
410+
OIDCServerConfiguration.CLIENT_SECRET_BASIC
411+
)
412+
}
413+
verify(exactly = 1) {
414+
accountManager.setUserData(
415+
secondAccount,
416+
KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD,
417+
OC_CLIENT_REGISTRATION.tokenEndpointAuthMethod
418+
)
419+
}
420+
}
421+
422+
@Test
423+
fun addOAuthAccountWithoutRegistrationDoesNotStoreAuthMethod() {
424+
mockRegularAccountCreationFlow()
425+
mockSelectedAccountNameInPreferences()
426+
427+
ocLocalAuthenticationDataSource.addOAuthAccount(
428+
OC_ACCOUNT_ID,
429+
OC_REDIRECTION_PATH.lastPermanentLocation,
430+
OC_AUTH_TOKEN_TYPE,
431+
OC_ACCESS_TOKEN,
432+
OC_SECURE_SERVER_INFO_BASIC_AUTH,
433+
OC_USER_INFO,
434+
OC_REFRESH_TOKEN,
435+
OC_SCOPE,
436+
null,
437+
null
438+
)
439+
440+
verify(exactly = 0) {
441+
accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD, any())
442+
accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_ID, any())
443+
accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_SECRET, any())
444+
accountManager.setUserData(any(), KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE, any())
445+
}
446+
}
447+
293448
@Test
294449
fun supportsOAuth2Ok() {
295450

@@ -409,6 +564,11 @@ class OCLocalAuthenticationDataSourceTest {
409564
accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_SECRET, clientInfo.clientSecret)
410565
accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_ID, clientInfo.clientId)
411566
accountManager.setUserData(account, KEY_CLIENT_REGISTRATION_CLIENT_EXPIRATION_DATE, clientInfo.clientSecretExpiration.toString())
567+
accountManager.setUserData(
568+
account,
569+
KEY_CLIENT_REGISTRATION_TOKEN_ENDPOINT_AUTH_METHOD,
570+
clientInfo.tokenEndpointAuthMethod
571+
)
412572
}
413573
}
414574

‎owncloudData/src/test/java/com/owncloud/android/data/oauth/datasources/implementation/OCRemoteOAuthDataSourceTest.kt‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import com.owncloud.android.data.ClientManager
2525
import com.owncloud.android.data.oauth.OC_REMOTE_CLIENT_REGISTRATION_RESPONSE
2626
import com.owncloud.android.data.oauth.OC_REMOTE_OIDC_DISCOVERY_RESPONSE
2727
import com.owncloud.android.data.oauth.OC_REMOTE_TOKEN_RESPONSE
28+
import com.owncloud.android.domain.authentication.oauth.model.OIDCServerConfiguration
29+
import com.owncloud.android.domain.exceptions.ServerNotReachableException
2830
import com.owncloud.android.lib.common.OwnCloudClient
2931
import com.owncloud.android.lib.common.operations.RemoteOperationResult
32+
import com.owncloud.android.lib.resources.oauth.params.ClientRegistrationParams
3033
import com.owncloud.android.lib.resources.oauth.responses.ClientRegistrationResponse
3134
import com.owncloud.android.lib.resources.oauth.responses.OIDCDiscoveryResponse
3235
import com.owncloud.android.lib.resources.oauth.responses.TokenResponse
@@ -152,4 +155,46 @@ class OCRemoteOAuthDataSourceTest {
152155
oidcService.registerClientWithRegistrationEndpoint(ocClientMocked, any())
153156
}
154157
}
158+
159+
@Test
160+
fun `registerClient returns a ClientRegistrationInfo with the requested auth method`() {
161+
val clientRegistrationResponse: RemoteOperationResult<ClientRegistrationResponse> =
162+
createRemoteOperationResultMock(data = OC_REMOTE_CLIENT_REGISTRATION_RESPONSE, isSuccess = true)
163+
val capturedParams = mutableListOf<ClientRegistrationParams>()
164+
165+
every {
166+
oidcService.registerClientWithRegistrationEndpoint(ocClientMocked, capture(capturedParams))
167+
} returns clientRegistrationResponse
168+
169+
val postRequest = OC_CLIENT_REGISTRATION_REQUEST.copy(
170+
tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_POST
171+
)
172+
val basicInfo = remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST)
173+
val postInfo = remoteOAuthDataSource.registerClient(postRequest)
174+
175+
assertEquals(OC_CLIENT_REGISTRATION_REQUEST.tokenEndpointAuthMethod, capturedParams[0].tokenEndpointAuthMethod)
176+
assertEquals(OIDCServerConfiguration.CLIENT_SECRET_POST, capturedParams[1].tokenEndpointAuthMethod)
177+
assertEquals(OC_CLIENT_REGISTRATION.copy(tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_BASIC), basicInfo)
178+
assertEquals(OC_CLIENT_REGISTRATION.copy(tokenEndpointAuthMethod = OIDCServerConfiguration.CLIENT_SECRET_POST), postInfo)
179+
180+
verify(exactly = 2) {
181+
oidcService.registerClientWithRegistrationEndpoint(ocClientMocked, any())
182+
}
183+
}
184+
185+
@Test(expected = ServerNotReachableException::class)
186+
fun `registerClient returns a ServerNotReachableException when the server is not available`() {
187+
val clientRegistrationResponse: RemoteOperationResult<ClientRegistrationResponse> =
188+
createRemoteOperationResultMock(
189+
data = OC_REMOTE_CLIENT_REGISTRATION_RESPONSE,
190+
isSuccess = false,
191+
resultCode = RemoteOperationResult.ResultCode.HOST_NOT_AVAILABLE,
192+
)
193+
194+
every {
195+
oidcService.registerClientWithRegistrationEndpoint(ocClientMocked, any())
196+
} returns clientRegistrationResponse
197+
198+
remoteOAuthDataSource.registerClient(OC_CLIENT_REGISTRATION_REQUEST)
199+
}
155200
}

0 commit comments

Comments
 (0)