Skip to content

Commit

Permalink
Update fluxc to enable authenticated rest api request to self-hosted
Browse files Browse the repository at this point in the history
sites
  • Loading branch information
mchowning committed Apr 8, 2020
1 parent 80cc5c3 commit d91beb3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import javax.inject.Named

class ReactNativeRequestHandler @Inject constructor(
private val reactNativeStore: ReactNativeStore,
private val urlUtil: ReactNativeUrlUtil,
@Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher
) : CoroutineScope {
override val coroutineContext = bgDispatcher + Job()
Expand All @@ -31,11 +30,8 @@ class ReactNativeRequestHandler @Inject constructor(
onError: Consumer<Bundle>
) {
launch {
if (mSite.isUsingWpComRestApi) {
performGetRequestForWPComSite(pathWithParams, mSite.siteId, onSuccess::accept, onError::accept)
} else {
performGetRequestForSelfHostedSite(pathWithParams, mSite.url, onSuccess::accept, onError::accept)
}
val response = reactNativeStore.executeRequest(mSite, pathWithParams)
handleResponse(response, onSuccess::accept, onError::accept)
}
}

Expand All @@ -49,30 +45,6 @@ class ReactNativeRequestHandler @Inject constructor(
coroutineContext[Job]!!.cancel()
}

private suspend fun performGetRequestForWPComSite(
pathWithParams: String,
wpComSiteId: Long,
onSuccess: (String) -> Unit,
onError: (Bundle) -> Unit
) {
urlUtil.parseUrlAndParamsForWPCom(pathWithParams, wpComSiteId)?.let { (url, params) ->
val response = reactNativeStore.performWPComRequest(url, params)
handleResponse(response, onSuccess, onError)
}
}

private suspend fun performGetRequestForSelfHostedSite(
pathWithParams: String,
siteUrl: String,
onSuccess: (String) -> Unit,
onError: (Bundle) -> Unit
) {
urlUtil.parseUrlAndParamsForWPOrg(pathWithParams, siteUrl)?.let { (url, params) ->
val response = reactNativeStore.performWPAPIRequest(url, params)
handleResponse(response, onSuccess, onError)
}
}

private fun handleResponse(
response: ReactNativeFetchResponse,
onSuccess: (String) -> Unit,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,20 @@ import org.wordpress.android.util.NoDelayCoroutineDispatcher
@RunWith(RobolectricTestRunner::class)
class ReactNativeRequestHandlerTest {
private val reactNativeStore = mock<ReactNativeStore>()
private val reactNativeUrlUtil = mock<ReactNativeUrlUtil>()
private val site = mock<SiteModel>()

private val pathWithParams = "/wp/v2/media?context=edit"
private val siteId: Long = 1111
private val siteUrl = "a_site_url"
private val parsedPath = "/wp/v2/media"
private val paramsMap = mapOf("context" to "edit")

private lateinit var subject: ReactNativeRequestHandler

@Before
fun setUp() {
subject = ReactNativeRequestHandler(
reactNativeStore,
reactNativeUrlUtil,
NoDelayCoroutineDispatcher()
)

whenever(site.siteId).thenReturn(siteId)
whenever(site.url).thenReturn(siteUrl)
}

@Test
fun `WPcom get request that succeeds`() = test {
fun `successful request`() = test {
whenever(site.isUsingWpComRestApi).thenReturn(true)

var calledSuccess = false
Expand All @@ -67,22 +56,19 @@ class ReactNativeRequestHandlerTest {
fail("Error handler should not be called")
}

whenever(reactNativeUrlUtil.parseUrlAndParamsForWPCom(pathWithParams, siteId)).thenReturn(
Pair(parsedPath, paramsMap)
)

val successfulResponseJson = mock<JsonElement>()
whenever(successfulResponseJson.toString()).thenReturn(jsonAsString)

val fetchResponse = ReactNativeFetchResponse.Success(successfulResponseJson)
whenever(reactNativeStore.performWPComRequest(parsedPath, paramsMap)).thenReturn(fetchResponse)
whenever(reactNativeStore.executeRequest(site, pathWithParams)).thenReturn(fetchResponse)

subject.performGetRequest(pathWithParams, site, successHandler, errorHandler)

assertTrue(calledSuccess)
}

@Test
fun `WPcom get request that fails`() = test {
fun `failed request`() = test {
whenever(site.isUsingWpComRestApi).thenReturn(true)

var calledError = false
Expand All @@ -98,80 +84,15 @@ class ReactNativeRequestHandlerTest {
fail("Success handler should not be called")
}

whenever(reactNativeUrlUtil.parseUrlAndParamsForWPCom(pathWithParams, siteId)).thenReturn(
Pair(parsedPath, paramsMap)
)

val fetchResponse = getFetchResponseError(errorMessage, statusCode)
whenever(reactNativeStore.performWPComRequest(parsedPath, paramsMap)).thenReturn(fetchResponse)

subject.performGetRequest(pathWithParams, site, successHandler, errorHandler)

assertTrue(calledError)
}

@Test
fun `WPorg get request that succeeds`() = test {
whenever(site.isUsingWpComRestApi).thenReturn(false)

var calledSuccess = false
val jsonAsString = "json_as_string"
val successHandler = Consumer<String> {
if (it != jsonAsString) { fail("expected json was not returned: $it") }
calledSuccess = true
}

val errorHandler = Consumer<Bundle> {
fail("Error handler should not be called")
}

whenever(reactNativeUrlUtil.parseUrlAndParamsForWPOrg(pathWithParams, siteUrl)).thenReturn(
Pair(parsedPath, paramsMap)
)

val successfulResponseJson = mock<JsonElement>()
whenever(successfulResponseJson.toString()).thenReturn(jsonAsString)
val fetchResponse = ReactNativeFetchResponse.Success(successfulResponseJson)
whenever(reactNativeStore.performWPAPIRequest(parsedPath, paramsMap)).thenReturn(fetchResponse)

subject.performGetRequest(pathWithParams, site, successHandler, errorHandler)

assertTrue(calledSuccess)
}

@Test
fun `WPorg get request that fails`() = test {
whenever(site.isUsingWpComRestApi).thenReturn(false)

var calledError = false
val statusCode = 505
val errorMessage = "error_message"
val errorHandler = Consumer<Bundle> {
assertEquals(statusCode, it.getInt("code"))
assertEquals(errorMessage, it.getString("message"))
calledError = true
}

val successHandler = Consumer<String> {
fail("Success handler should not be called")
}

whenever(reactNativeUrlUtil.parseUrlAndParamsForWPOrg(pathWithParams, siteUrl)).thenReturn(
Pair(parsedPath, paramsMap)
)

val fetchResponse = getFetchResponseError(errorMessage, statusCode)
whenever(reactNativeStore.performWPAPIRequest(parsedPath, paramsMap)).thenReturn(fetchResponse)
whenever(reactNativeStore.executeRequest(site, pathWithParams)).thenReturn(fetchResponse)

subject.performGetRequest(pathWithParams, site, successHandler, errorHandler)

assertTrue(calledError)
}

private fun getFetchResponseError(
errorMessage: String,
statusCode: Int
): Error {
private fun getFetchResponseError(errorMessage: String, statusCode: Int): Error {
val volleyNetworkResponse = NetworkResponse(statusCode, null, false, 0L, null)
val volleyError = VolleyError(volleyNetworkResponse)

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ ext {
targetSdkVersion = 28

daggerVersion = '2.22.1'
fluxCVersion = '1.6.9'
fluxCVersion = '1.6.10-beta-1'

appCompatVersion = '1.0.2'
constraintLayoutVersion = '1.1.3'
Expand Down

0 comments on commit d91beb3

Please sign in to comment.