From c64843e9523f10948d6f3a5e73cb680ec83afae7 Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 13 Jan 2021 23:34:30 +0100 Subject: [PATCH] migrate BaseAuthenticationRequestTest --- .../BaseAuthenticationRequestTest.java | 165 ------------------ .../internal/BaseAuthenticationRequestTest.kt | 141 +++++++++++++++ 2 files changed, 141 insertions(+), 165 deletions(-) delete mode 100644 auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java create mode 100644 auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.kt diff --git a/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java b/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java deleted file mode 100644 index 5133a6f2f..000000000 --- a/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.auth0.android.request.internal; - -import com.auth0.android.authentication.AuthenticationException; -import com.auth0.android.request.AuthenticationRequest; -import com.auth0.android.request.ErrorAdapter; -import com.auth0.android.request.HttpMethod; -import com.auth0.android.request.JsonAdapter; -import com.auth0.android.request.NetworkingClient; -import com.auth0.android.request.Request; -import com.auth0.android.request.RequestOptions; -import com.auth0.android.request.ServerResponse; -import com.auth0.android.result.Credentials; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; - -import java.io.InputStream; -import java.io.Reader; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsMapContaining.hasEntry; -import static org.hamcrest.collection.IsMapWithSize.aMapWithSize; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@RunWith(RobolectricTestRunner.class) -public class BaseAuthenticationRequestTest { - - @Rule - public ExpectedException exception = ExpectedException.none(); - private static final String BASE_URL = "https://auth0.com/oauth/token"; - - @Mock - private NetworkingClient client; - @Mock - private JsonAdapter resultAdapter; - @Mock - private ErrorAdapter errorAdapter; - @Captor - private ArgumentCaptor optionsCaptor; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - private AuthenticationRequest createRequest(String url) { - Request baseRequest = new BaseRequest<>(HttpMethod.POST.INSTANCE, url, client, resultAdapter, errorAdapter); - AuthenticationRequest request = new BaseAuthenticationRequest(baseRequest); - return spy(request); - } - - @Test - public void shouldSetGrantType() throws Exception { - mockSuccessfulServerResponse(); - - createRequest(BASE_URL) - .setGrantType("grantType") - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(1)); - assertThat(values, hasEntry("grant_type", "grantType")); - } - - @Test - public void shouldSetConnection() throws Exception { - mockSuccessfulServerResponse(); - - createRequest(BASE_URL) - .setConnection("my-connection") - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(1)); - assertThat(values, hasEntry("connection", "my-connection")); - } - - @Test - public void shouldSetRealm() throws Exception { - mockSuccessfulServerResponse(); - - createRequest(BASE_URL) - .setRealm("my-realm") - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(1)); - assertThat(values, hasEntry("realm", "my-realm")); - } - - @Test - public void shouldSetScope() throws Exception { - mockSuccessfulServerResponse(); - - createRequest(BASE_URL) - .setScope("email profile") - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(1)); - assertThat(values, hasEntry("scope", "email profile")); - } - - @Test - public void shouldSetAudience() throws Exception { - mockSuccessfulServerResponse(); - - createRequest(BASE_URL) - .setAudience("my-api") - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(1)); - assertThat(values, hasEntry("audience", "my-api")); - } - - @Test - public void shouldAddAuthenticationParameters() throws Exception { - mockSuccessfulServerResponse(); - - HashMap parameters = new HashMap<>(); - parameters.put("extra", "value"); - parameters.put("123", "890"); - - createRequest(BASE_URL) - .addParameters(parameters) - .execute(); - - verify(client).load(eq(BASE_URL), optionsCaptor.capture()); - Map values = optionsCaptor.getValue().getParameters(); - assertThat(values, aMapWithSize(2)); - assertThat(values, hasEntry("extra", "value")); - assertThat(values, hasEntry("123", "890")); - } - - private void mockSuccessfulServerResponse() throws Exception { - InputStream inputStream = mock(InputStream.class); - Credentials credentials = mock(Credentials.class); - when(inputStream.read()).thenReturn(123); - when(resultAdapter.fromJson(any(Reader.class))).thenReturn(credentials); - ServerResponse response = new ServerResponse(200, inputStream, Collections.emptyMap()); - when(client.load(eq(BASE_URL), any(RequestOptions.class))).thenReturn(response); - } -} \ No newline at end of file diff --git a/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.kt b/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.kt new file mode 100644 index 000000000..cb6ed2f5c --- /dev/null +++ b/auth0/src/test/java/com/auth0/android/request/internal/BaseAuthenticationRequestTest.kt @@ -0,0 +1,141 @@ +package com.auth0.android.request.internal + +import com.auth0.android.authentication.AuthenticationException +import com.auth0.android.request.* +import com.auth0.android.result.Credentials +import com.nhaarman.mockitokotlin2.* +import org.hamcrest.MatcherAssert +import org.hamcrest.collection.IsMapContaining +import org.hamcrest.collection.IsMapWithSize +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.MockitoAnnotations +import org.robolectric.RobolectricTestRunner +import java.io.InputStream +import java.util.* + +@RunWith(RobolectricTestRunner::class) +public class BaseAuthenticationRequestTest { + + @Mock + private lateinit var client: NetworkingClient + + @Mock + private lateinit var resultAdapter: JsonAdapter + + @Mock + private lateinit var errorAdapter: ErrorAdapter + + private val optionsCaptor: KArgumentCaptor = argumentCaptor() + + @Before + public fun setUp() { + MockitoAnnotations.openMocks(this) + } + + private fun createRequest(url: String): AuthenticationRequest { + val baseRequest: Request = + BaseRequest(HttpMethod.POST, url, client, resultAdapter, errorAdapter) + val request: AuthenticationRequest = BaseAuthenticationRequest(baseRequest) + return Mockito.spy(request) + } + + @Test + @Throws(Exception::class) + public fun shouldSetGrantType() { + mockSuccessfulServerResponse() + createRequest(BASE_URL) + .setGrantType("grantType") + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(1)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("grant_type", "grantType")) + } + + @Test + @Throws(Exception::class) + public fun shouldSetConnection() { + mockSuccessfulServerResponse() + createRequest(BASE_URL) + .setConnection("my-connection") + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(1)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("connection", "my-connection")) + } + + @Test + @Throws(Exception::class) + public fun shouldSetRealm() { + mockSuccessfulServerResponse() + createRequest(BASE_URL) + .setRealm("my-realm") + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(1)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("realm", "my-realm")) + } + + @Test + @Throws(Exception::class) + public fun shouldSetScope() { + mockSuccessfulServerResponse() + createRequest(BASE_URL) + .setScope("email profile") + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(1)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("scope", "email profile")) + } + + @Test + @Throws(Exception::class) + public fun shouldSetAudience() { + mockSuccessfulServerResponse() + createRequest(BASE_URL) + .setAudience("my-api") + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(1)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("audience", "my-api")) + } + + @Test + @Throws(Exception::class) + public fun shouldAddAuthenticationParameters() { + mockSuccessfulServerResponse() + val parameters = HashMap() + parameters["extra"] = "value" + parameters["123"] = "890" + createRequest(BASE_URL) + .addParameters(parameters) + .execute() + verify(client).load(eq(BASE_URL), optionsCaptor.capture()) + val values: Map = optionsCaptor.firstValue.parameters + MatcherAssert.assertThat(values, IsMapWithSize.aMapWithSize(2)) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("extra", "value")) + MatcherAssert.assertThat(values, IsMapContaining.hasEntry("123", "890")) + } + + @Throws(Exception::class) + private fun mockSuccessfulServerResponse() { + val inputStream: InputStream = mock() + val credentials: Credentials = mock() + whenever(inputStream.read()).thenReturn(123) + whenever(resultAdapter.fromJson(any())).thenReturn(credentials) + val response = ServerResponse(200, inputStream, emptyMap()) + whenever(client.load(eq(BASE_URL), any())).thenReturn(response) + } + + private companion object { + private const val BASE_URL = "https://auth0.com/oauth/token" + } +} \ No newline at end of file