Skip to content

Commit

Permalink
Merge branch 'trunk' into local-disability
Browse files Browse the repository at this point in the history
  • Loading branch information
jostnes committed Sep 28, 2023
2 parents 19848d3 + b300dea commit 8e3a383
Show file tree
Hide file tree
Showing 26 changed files with 1,774 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ class MockedStack_WCProductsTest : MockedStack_Base() {
assertEquals(25, payload.reviews.size)
assertNull(payload.filterProductIds)
assertNull(payload.filterByStatus)
assertFalse(payload.loadedMore)
assertTrue(payload.canLoadMore)

// Save product reviews to the database
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ class WooProductsFragment : StoreSelectingFragment() {
FetchProductReviewsPayload(
site,
productIds = listOf(remoteProductId)
)
),
deletePreviouslyCachedReviews = false
)
prependToLog("Fetched ${result.rowsAffected} product reviews")
}
Expand All @@ -321,7 +322,10 @@ class WooProductsFragment : StoreSelectingFragment() {
coroutineScope.launch {
prependToLog("Submitting request to fetch product reviews for site ${site.id}")
val payload = FetchProductReviewsPayload(site)
val result = wcProductStore.fetchProductReviews(payload)
val result = wcProductStore.fetchProductReviews(
payload,
deletePreviouslyCachedReviews = false
)
prependToLog("Fetched ${result.rowsAffected} product reviews")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SiteRestClientTest {
}

@Test
fun `returns fetched sites`() = test {
fun `returns fetched sites using filter`() = test {
val response = SiteWPComRestResponse()
response.ID = siteId
val name = "Updated name"
Expand All @@ -123,15 +123,18 @@ class SiteRestClientTest {
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)
initSitesFeaturesResponse(data = SitesFeaturesRestResponse(emptyMap()))

val responseModel = restClient.fetchSites(listOf(WPCOM), false)
assertThat(responseModel.sites).hasSize(1)
assertThat(responseModel.sites[0].name).isEqualTo(name)
assertThat(responseModel.sites[0].siteId).isEqualTo(siteId)

assertThat(urlCaptor.lastValue)
assertThat(urlCaptor.firstValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.2/me/sites/")
assertThat(paramsCaptor.lastValue).isEqualTo(
assertThat(urlCaptor.lastValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.1/me/sites/features/")
assertThat(paramsCaptor.firstValue).isEqualTo(
mapOf(
"filters" to "wpcom",
"fields" to "ID,URL,name,description,jetpack,jetpack_connection," +
Expand All @@ -141,6 +144,35 @@ class SiteRestClientTest {
)
}

@Test
fun `returns fetched sites when not filtering`() = test {
val response = SiteWPComRestResponse()
response.ID = siteId
val name = "Updated name"
response.name = name
response.URL = "site.com"

val sitesResponse = SitesResponse()
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)

val responseModel = restClient.fetchSites(emptyList(), false)
assertThat(responseModel.sites).hasSize(1)
assertThat(responseModel.sites[0].name).isEqualTo(name)
assertThat(responseModel.sites[0].siteId).isEqualTo(siteId)

assertThat(urlCaptor.firstValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.1/me/sites/")
assertThat(paramsCaptor.firstValue).isEqualTo(
mapOf(
"fields" to "ID,URL,name,description,jetpack,jetpack_connection," +
"visible,is_private,options,plan,capabilities,quota,icon,meta,zendesk_site_meta," +
"organization_id,was_ecommerce_trial"
)
)
}

@Test
fun `fetched sites can filter JP connected package sites`() = test {
val response = SiteWPComRestResponse()
Expand All @@ -155,6 +187,7 @@ class SiteRestClientTest {
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)
initSitesFeaturesResponse(data = SitesFeaturesRestResponse(features = emptyMap()))

val responseModel = restClient.fetchSites(listOf(WPCOM), true)

Expand Down Expand Up @@ -521,6 +554,13 @@ class SiteRestClientTest {
return initGetResponse(PostFormatsResponse::class.java, data ?: mock(), error)
}

private suspend fun initSitesFeaturesResponse(
data: SitesFeaturesRestResponse? = null,
error: WPComGsonNetworkError? = null
): Response<SitesFeaturesRestResponse> {
return initGetResponse(SitesFeaturesRestResponse::class.java, data ?: mock(), error)
}

private suspend fun <T> initGetResponse(
clazz: Class<T>,
data: T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package org.wordpress.android.fluxc.wc.taxes

import org.wordpress.android.fluxc.JsonLoaderUtils.jsonFileAs
import org.wordpress.android.fluxc.model.taxes.WCTaxClassModel
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.TaxRateDto
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.WCTaxRestClient.TaxClassApiResponse
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.WCTaxRestClient.TaxRateModel

object TaxTestUtils {
private fun generateSampleTaxClass(
Expand Down Expand Up @@ -36,6 +36,6 @@ object TaxTestUtils {

fun generateSampleTaxRateApiResponse() =
"wc/tax-rate-response.json"
.jsonFileAs(Array<TaxRateModel>::class.java)
.jsonFileAs(Array<TaxRateDto>::class.java)
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.WCTaxRestClient
import org.wordpress.android.fluxc.persistence.SiteSqlUtils
import org.wordpress.android.fluxc.persistence.WellSqlConfig
import org.wordpress.android.fluxc.persistence.dao.TaxRateDao
import org.wordpress.android.fluxc.store.WCTaxStore
import org.wordpress.android.fluxc.test
import org.wordpress.android.fluxc.tools.initCoroutineEngine
Expand All @@ -35,6 +36,7 @@ class WCTaxStoreTest {
private val errorSite = SiteModel().apply { id = 123 }
private val mapper = WCTaxClassMapper()
private lateinit var store: WCTaxStore
private val taxRateDao = mock<TaxRateDao>()

private val sampleTaxClassList = TaxTestUtils.generateSampleTaxClassApiResponse()
private val error = WooError(INVALID_RESPONSE, NETWORK_ERROR, "Invalid site ID")
Expand All @@ -53,7 +55,8 @@ class WCTaxStoreTest {
store = WCTaxStore(
restClient,
initCoroutineEngine(),
mapper
mapper,
taxRateDao
)

// Insert the site into the db so it's available later when fetching tax classes
Expand Down Expand Up @@ -105,7 +108,7 @@ class WCTaxStoreTest {
val result = store.fetchTaxRateList(site, 1, 100)
assertThat(this).isNotNull
assertThat(result.isError).isFalse
assertThat(result.model).isEqualTo(taxRateApiResponse?.toList())
assertThat(result).isEqualTo(WooResult(false))
}
}

Expand Down
1 change: 1 addition & 0 deletions fluxc-processor/src/main/resources/wp-com-endpoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/me/domain-contact-information/
/me/settings/
/me/sites/
/me/sites/features
/me/send-verification-email/
/me/social-login/connect/
/me/username/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SiteWPAPIRestClient @Inject constructor(
}

wpApiRestUrl = discoveredWpApiUrl
this.url = response?.url ?: cleanedUrl.replaceBefore("://", urlScheme)
this.url = cleanedUrl.replaceBefore("://", urlScheme)
this.username = payload.username
this.password = payload.password
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,51 @@ class SiteRestClient @Inject constructor(
val site: SiteModel? = null
) : Payload<SiteError>()

/**
* Fetches the user's sites from WPCom.
* Since the V1.2 endpoint doesn't return the plan features, we will handle the fetch by following two
* different approaches:
* 1. If we don't need any filtering, then we'll simply use the v1.1 endpoint which includes the features.
* 2. If we have some filters, then we'll send two requests: the first one to the v1.2 endpoint to fetch sites
* And the second one to the /me/sites/features to fetch the features separately, the combine the results.
*/
@Suppress("ComplexMethod")
suspend fun fetchSites(filters: List<SiteFilter?>, filterJetpackConnectedPackageSite: Boolean): SitesModel {
val useV2Endpoint = filters.isNotEmpty()
val params = getFetchSitesParams(filters)
val url = WPCOMREST.me.sites.urlV1_2
val url = WPCOMREST.me.sites.let { if (useV2Endpoint) it.urlV1_2 else it.urlV1_1 }
val response = wpComGsonRequestBuilder.syncGetRequest(this, url, params, SitesResponse::class.java)

val siteFeatures = if (useV2Endpoint) {
fetchSitesFeatures().let {
if (it is Error) {
val result = SitesModel()
result.error = it.error
return result
}
(it as Success).data
}
} else null

return when (response) {
is Success -> {
val siteArray = mutableListOf<SiteModel>()
val jetpackCPSiteArray = mutableListOf<SiteModel>()
for (siteResponse in response.data.sites) {
val siteModel = siteResponseToSiteModel(siteResponse)

siteFeatures?.get(siteModel.siteId)?.let {
siteModel.planActiveFeatures = it.joinToString(",")
}

if (siteModel.isJetpackCPConnected) jetpackCPSiteArray.add(siteModel)
// see https://github.com/wordpress-mobile/WordPress-Android/issues/15540#issuecomment-993752880
if (filterJetpackConnectedPackageSite && siteModel.isJetpackCPConnected) continue
siteArray.add(siteModel)
}
SitesModel(siteArray, jetpackCPSiteArray)
}

is Error -> {
val payload = SitesModel(emptyList())
payload.error = response.error
Expand All @@ -157,6 +185,21 @@ class SiteRestClient @Inject constructor(
}
}

private suspend fun fetchSitesFeatures(): Response<Map<Long, List<String>>> {
val url = WPCOMREST.me.sites.features.urlV1_1
return wpComGsonRequestBuilder.syncGetRequest(
restClient = this,
url = url,
params = emptyMap(),
clazz = SitesFeaturesRestResponse::class.java
).let {
when (it) {
is Success -> Success(it.data.features.mapValues { it.value.active })
is Error -> Error(it.error)
}
}
}

private fun getFetchSitesParams(filters: List<SiteFilter?>): Map<String, String> {
val params = mutableMapOf<String, String>()
if (filters.isNotEmpty()) params[FILTERS] = TextUtils.join(",", filters)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.wordpress.android.fluxc.network.rest.wpcom.site

data class SitesFeaturesRestResponse(
val features: Map<Long, SiteFeatures>
)

data class SiteFeatures(
val active: List<String>
)
Loading

0 comments on commit 8e3a383

Please sign in to comment.