Skip to content

Commit

Permalink
Merge branch 'trunk' into issue/12401-login-error-xmlrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
develric committed Feb 25, 2022
2 parents cb4a783 + ac35815 commit 14864a0
Show file tree
Hide file tree
Showing 21 changed files with 609 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,17 +552,6 @@ class MockedStack_WCOrdersTest : MockedStack_Base() {
assertEquals(payload.error.type, OrderErrorType.INVALID_RESPONSE)
}

@Test
fun testPostSimplePayment() = runBlocking {
interceptor.respondWith("wc-fetch-order-response-success.json")
val response = orderRestClient.postSimplePayment(siteModel, "10.00", isTaxable = true)

with(response) {
assertNull(error)
assertNotNull(order)
}
}

@Suppress("unused")
@Subscribe
fun onAction(action: Action<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import org.mockito.junit.MockitoJUnitRunner
import org.wordpress.android.fluxc.Dispatcher
import org.wordpress.android.fluxc.UnitTestUtils
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.dashboard.CardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.TodaysStatsCardModel
import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
import org.wordpress.android.fluxc.network.UserAgent
Expand All @@ -31,16 +34,36 @@ import org.wordpress.android.fluxc.network.rest.wpcom.auth.AccessToken
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.CardsResponse
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.PostResponse
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.PostsResponse
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.TodaysStatsResponse
import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsErrorType
import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsPayload
import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError
import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType
import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError
import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType
import org.wordpress.android.fluxc.test

/* DATE */

private const val DATE_FORMAT_PATTERN = "yyyy-MM-dd HH:mm:ss"

/* CARD TYPES */

private val CARD_TYPES = listOf(CardModel.Type.TODAYS_STATS, CardModel.Type.POSTS)

/* ERRORS */
private const val JETPACK_DISABLED = "jetpack_disabled"
private const val UNAUTHORIZED = "unauthorized"

/* RESPONSE */

private val TODAYS_STATS_RESPONSE = TodaysStatsResponse(
views = 100,
visitors = 30,
likes = 50,
comments = 10
)

private val DRAFT_POST_RESPONSE_TWO = PostResponse(
id = 708,
title = "",
Expand Down Expand Up @@ -77,6 +100,7 @@ private val POSTS_RESPONSE = PostsResponse(
)

private val CARDS_RESPONSE = CardsResponse(
todaysStats = TODAYS_STATS_RESPONSE,
posts = POSTS_RESPONSE
)

Expand All @@ -90,13 +114,15 @@ class CardsRestClientTest {
@Mock private lateinit var site: SiteModel

private lateinit var urlCaptor: KArgumentCaptor<String>
private lateinit var paramsCaptor: KArgumentCaptor<Map<String, String>>
private lateinit var restClient: CardsRestClient

private val siteId: Long = 1

@Before
fun setUp() {
urlCaptor = argumentCaptor()
paramsCaptor = argumentCaptor()
restClient = CardsRestClient(
wpComGsonRequestBuilder,
dispatcher,
Expand All @@ -112,7 +138,7 @@ class CardsRestClientTest {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_JSON)
initFetchCards(data = getCardsResponseFromJsonString(json))

restClient.fetchCards(site)
restClient.fetchCards(site, CARD_TYPES)

assertEquals(urlCaptor.firstValue, "$API_SITE_PATH/${site.siteId}/$API_DASHBOARD_CARDS_PATH")
}
Expand All @@ -122,7 +148,7 @@ class CardsRestClientTest {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_JSON)
initFetchCards(data = getCardsResponseFromJsonString(json))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertSuccess(CARDS_RESPONSE, result)
}
Expand All @@ -131,7 +157,7 @@ class CardsRestClientTest {
fun `given timeout, when fetch cards gets triggered, then return cards timeout error`() = test {
initFetchCards(error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.TIMEOUT)))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertError(CardsErrorType.TIMEOUT, result)
}
Expand All @@ -140,7 +166,7 @@ class CardsRestClientTest {
fun `given network error, when fetch cards gets triggered, then return cards api error`() = test {
initFetchCards(error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.NETWORK_ERROR)))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertError(CardsErrorType.API_ERROR, result)
}
Expand All @@ -149,7 +175,7 @@ class CardsRestClientTest {
fun `given invalid response, when fetch cards gets triggered, then return cards invalid response error`() = test {
initFetchCards(error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.INVALID_RESPONSE)))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertError(CardsErrorType.INVALID_RESPONSE, result)
}
Expand All @@ -158,7 +184,7 @@ class CardsRestClientTest {
fun `given not authenticated, when fetch cards gets triggered, then return cards auth required error`() = test {
initFetchCards(error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.NOT_AUTHENTICATED)))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertError(CardsErrorType.AUTHORIZATION_REQUIRED, result)
}
Expand All @@ -167,11 +193,67 @@ class CardsRestClientTest {
fun `given unknown error, when fetch cards gets triggered, then return cards generic error`() = test {
initFetchCards(error = WPComGsonNetworkError(BaseNetworkError(GenericErrorType.UNKNOWN)))

val result = restClient.fetchCards(site)
val result = restClient.fetchCards(site, CARD_TYPES)

assertError(CardsErrorType.GENERIC_ERROR, result)
}

/* TODAY'S STATS CARD ERRORS */
@Test
fun `given jetpack disconn, when fetch cards triggered, then returns todays stats jetpack disconn card error`() =
test {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON)
initFetchCards(data = getCardsResponseFromJsonString(json))

val result = restClient.fetchCards(site, CARD_TYPES)

assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.JETPACK_DISCONNECTED, result)
}

@Test
fun `given jetpack disabled, when fetch cards triggered, then returns todays stats jetpack disabled card error`() =
test {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON)
val data = getCardsResponseFromJsonString(json)
.copy(todaysStats = TodaysStatsResponse(error = JETPACK_DISABLED))
initFetchCards(data = data)

val result = restClient.fetchCards(site, CARD_TYPES)

assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.JETPACK_DISABLED, result)
}

@Test
fun `given stats unauthorized, when fetch cards triggered, then returns todays stats unauthorized card error`() =
test {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON)
val data = getCardsResponseFromJsonString(json)
.copy(todaysStats = TodaysStatsResponse(error = UNAUTHORIZED))
initFetchCards(data = data)

val result = restClient.fetchCards(site, CARD_TYPES)

assertSuccessWithTodaysStatsError(TodaysStatsCardErrorType.UNAUTHORIZED, result)
}

/* POST CARD ERROR */
@Test
fun `given posts unauthorized error, when fetch cards triggered, then returns post card card error`() =
test {
val json = UnitTestUtils.getStringFromResourceFile(javaClass, DASHBOARD_CARDS_WITH_ERRORS_JSON)
initFetchCards(data = getCardsResponseFromJsonString(json))

val result = restClient.fetchCards(site, CARD_TYPES)

assertSuccessWithPostCardError(result)
}

private fun CardsPayload<CardsResponse>.findTodaysStatsCardError(): TodaysStatsCardError? =
this.response?.toCards()?.filterIsInstance(TodaysStatsCardModel::class.java)?.firstOrNull()?.error

private fun CardsPayload<CardsResponse>.findPostCardError(): PostCardError? =
this.response?.toCards()?.filterIsInstance(PostsCardModel::class.java)?.firstOrNull()?.error

private fun getCardsResponseFromJsonString(json: String): CardsResponse {
val responseType = object : TypeToken<CardsResponse>() {}.type
return GsonBuilder().setDateFormat(DATE_FORMAT_PATTERN)
Expand All @@ -188,7 +270,7 @@ class CardsRestClientTest {
wpComGsonRequestBuilder.syncGetRequest(
eq(restClient),
urlCaptor.capture(),
eq(mapOf()),
paramsCaptor.capture(),
eq(CardsResponse::class.java),
eq(false),
any(),
Expand Down Expand Up @@ -223,11 +305,33 @@ class CardsRestClientTest {
}
}

private fun assertSuccessWithTodaysStatsError(
expected: TodaysStatsCardErrorType,
actual: CardsPayload<CardsResponse>
) {
with(actual) {
assertEquals(site, this@CardsRestClientTest.site)
assertFalse(isError)
assertEquals(expected, findTodaysStatsCardError()?.type)
}
}

private fun assertSuccessWithPostCardError(
actual: CardsPayload<CardsResponse>
) {
with(actual) {
assertEquals(site, this@CardsRestClientTest.site)
assertFalse(isError)
assertEquals(PostCardErrorType.UNAUTHORIZED, findPostCardError()?.type)
}
}

companion object {
private const val API_BASE_PATH = "https://public-api.wordpress.com/wpcom/v2"
private const val API_SITE_PATH = "$API_BASE_PATH/sites"
private const val API_DASHBOARD_CARDS_PATH = "dashboard/cards/"
private const val API_DASHBOARD_CARDS_PATH = "dashboard/cards-data/"

private const val DASHBOARD_CARDS_JSON = "wp/dashboard/cards.json"
private const val DASHBOARD_CARDS_WITH_ERRORS_JSON = "wp/dashboard/cards_with_errors.json"
}
}
Loading

0 comments on commit 14864a0

Please sign in to comment.