Skip to content

Commit 1b022d7

Browse files
authored
Merge pull request #9888 from woocommerce/feat/9866-blaze-status-networking-yosemite
Site Blaze status - Networking & Yosemite layer changes
2 parents b2afcc4 + e469b3f commit 1b022d7

File tree

9 files changed

+164
-1
lines changed

9 files changed

+164
-1
lines changed

Networking/Networking.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
023930632918FF5400B2632F /* DomainRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 023930622918FF5400B2632F /* DomainRemote.swift */; };
3434
0239306B291A96F800B2632F /* DomainRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0239306A291A96F800B2632F /* DomainRemoteTests.swift */; };
3535
0239306D291A973F00B2632F /* domain-suggestions.json in Resources */ = {isa = PBXBuildFile; fileRef = 0239306C291A973F00B2632F /* domain-suggestions.json */; };
36+
0259B0822A304E8D0050D7D7 /* blaze-status-failure.json in Resources */ = {isa = PBXBuildFile; fileRef = 0259B0802A304E8D0050D7D7 /* blaze-status-failure.json */; };
37+
0259B0832A304E8D0050D7D7 /* blaze-status-success.json in Resources */ = {isa = PBXBuildFile; fileRef = 0259B0812A304E8D0050D7D7 /* blaze-status-success.json */; };
3638
025CA2C0238EB8CB00B05C81 /* ProductShippingClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025CA2BF238EB8CB00B05C81 /* ProductShippingClass.swift */; };
3739
025CA2C2238EBBAA00B05C81 /* ProductShippingClassListMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025CA2C1238EBBAA00B05C81 /* ProductShippingClassListMapper.swift */; };
3840
025CA2C4238EBC4300B05C81 /* ProductShippingClassRemote.swift in Sources */ = {isa = PBXBuildFile; fileRef = 025CA2C3238EBC4300B05C81 /* ProductShippingClassRemote.swift */; };
@@ -972,6 +974,8 @@
972974
023930622918FF5400B2632F /* DomainRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainRemote.swift; sourceTree = "<group>"; };
973975
0239306A291A96F800B2632F /* DomainRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DomainRemoteTests.swift; sourceTree = "<group>"; };
974976
0239306C291A973F00B2632F /* domain-suggestions.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "domain-suggestions.json"; sourceTree = "<group>"; };
977+
0259B0802A304E8D0050D7D7 /* blaze-status-failure.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "blaze-status-failure.json"; sourceTree = "<group>"; };
978+
0259B0812A304E8D0050D7D7 /* blaze-status-success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "blaze-status-success.json"; sourceTree = "<group>"; };
975979
025CA2BF238EB8CB00B05C81 /* ProductShippingClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductShippingClass.swift; sourceTree = "<group>"; };
976980
025CA2C1238EBBAA00B05C81 /* ProductShippingClassListMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductShippingClassListMapper.swift; sourceTree = "<group>"; };
977981
025CA2C3238EBC4300B05C81 /* ProductShippingClassRemote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductShippingClassRemote.swift; sourceTree = "<group>"; };
@@ -2412,6 +2416,8 @@
24122416
isa = PBXGroup;
24132417
children = (
24142418
DE4D23BD29B6E10F003A4B5D /* announcements.json */,
2419+
0259B0802A304E8D0050D7D7 /* blaze-status-failure.json */,
2420+
0259B0812A304E8D0050D7D7 /* blaze-status-success.json */,
24152421
021D741B2987B1550035687E /* checkout-doman-cart-with-domain-credit-success.json */,
24162422
DEC2B092297AA60D003923FB /* category-without-data.json */,
24172423
DEC2B090297AA5A6003923FB /* categories-all-without-data.json */,
@@ -3506,6 +3512,7 @@
35063512
DEF13C5E296686AB0024A02B /* orders-load-all-without-data.json in Resources */,
35073513
4515282B257A8C010076B03C /* product-attribute-delete.json in Resources */,
35083514
020C907B24C6E108001E2BEB /* product-variation-update.json in Resources */,
3515+
0259B0822A304E8D0050D7D7 /* blaze-status-failure.json in Resources */,
35093516
D8FBFF1822D4DDB9006E3336 /* order-stats-v4-hour.json in Resources */,
35103517
CCA1D6082943804C00B40560 /* site-summary-stats.json in Resources */,
35113518
B554FA8D2180B59700C54DFF /* notifications-load-hashes.json in Resources */,
@@ -3531,6 +3538,7 @@
35313538
93D8BBFF226BC1DA00AD2EB3 /* me-settings.json in Resources */,
35323539
74C947842193A6C70024CB60 /* comment-moderate-approved.json in Resources */,
35333540
DEC51AED2768A0AD009F3DF4 /* systemStatusWithPluginsOnly.json in Resources */,
3541+
0259B0832A304E8D0050D7D7 /* blaze-status-success.json in Resources */,
35343542
0286A1B62A0CBAC50099EF94 /* feature-flags-load-all-with-missing-values.json in Resources */,
35353543
DE66C5552976662700DAA978 /* just-in-time-message-list-without-data.json in Resources */,
35363544
D8C11A5C22DFCF8100D4A88D /* order-stats-v4-year-alt.json in Resources */,

Networking/Networking/Remote/SiteRemote.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public protocol SiteRemoteProtocol {
2121
/// - Parameter siteID: Remote ID of the site to load.
2222
/// - Returns: The site that matches the site ID.
2323
func loadSite(siteID: Int64) async throws -> Site
24+
25+
/// Loads the Blaze status of a site.
26+
/// - Parameter siteID: Remote ID of the site to load the Blaze status.
27+
/// - Returns: A boolean that indicates whether Blaze is approved for the site.
28+
func loadBlazeStatus(siteID: Int64) async throws -> Bool
2429
}
2530

2631
/// Site: Remote Endpoints
@@ -110,6 +115,13 @@ public class SiteRemote: Remote, SiteRemoteProtocol {
110115
let request = DotcomRequest(wordpressApiVersion: .mark1_1, method: .get, path: path, parameters: parameters)
111116
return try await enqueue(request)
112117
}
118+
119+
public func loadBlazeStatus(siteID: Int64) async throws -> Bool {
120+
let path = Path.loadBlazeStatus(siteID: siteID)
121+
let request = DotcomRequest(wordpressApiVersion: .wpcomMark2, method: .get, path: path)
122+
let response: BlazeStatusResponse = try await enqueue(request)
123+
return response.isApproved
124+
}
113125
}
114126

115127
/// Possible Site Creation Flows
@@ -221,6 +233,15 @@ public struct SiteProfilerData {
221233
}
222234
}
223235

236+
/// Site Blaze status response.
237+
private struct BlazeStatusResponse: Decodable {
238+
let isApproved: Bool
239+
240+
private enum CodingKeys: String, CodingKey {
241+
case isApproved = "approved"
242+
}
243+
}
244+
224245
extension SiteRemote {
225246
enum SiteParameter {
226247
enum Fields {
@@ -253,5 +274,9 @@ private extension SiteRemote {
253274
static func loadSite(siteID: Int64) -> String {
254275
"sites/\(siteID)"
255276
}
277+
278+
static func loadBlazeStatus(siteID: Int64) -> String {
279+
"sites/\(siteID)/blaze/status"
280+
}
256281
}
257282
}

Networking/NetworkingTests/Remote/SiteRemoteTests.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,41 @@ final class SiteRemoteTests: XCTestCase {
193193
message: "You cannot add WordPress.com eCommerce Trial when you already have paid upgrades")
194194
})
195195
}
196+
197+
// MARK: - `loadBlazeStatus`
198+
199+
func test_loadBlazeStatus_returns_true_on_success() async throws {
200+
// Given
201+
network.simulateResponse(requestUrlSuffix: "blaze/status", filename: "blaze-status-success")
202+
203+
// When
204+
let isApproved = try await remote.loadBlazeStatus(siteID: 134)
205+
206+
// Then
207+
XCTAssertTrue(isApproved)
208+
}
209+
210+
func test_loadBlazeStatus_returns_false_on_failure() async throws {
211+
// Given
212+
network.simulateResponse(requestUrlSuffix: "blaze/status", filename: "blaze-status-failure")
213+
214+
// When
215+
let isApproved = try await remote.loadBlazeStatus(siteID: 134)
216+
217+
// Then
218+
XCTAssertFalse(isApproved)
219+
}
220+
221+
func test_loadBlazeStatus_throws_decoding_error_on_generic_error() async throws {
222+
// Given
223+
network.simulateResponse(requestUrlSuffix: "blaze/status", filename: "generic_error")
224+
225+
await assertThrowsError({
226+
// When
227+
_ = try await remote.loadBlazeStatus(siteID: 134)
228+
}, errorAssert: { error in
229+
// Unexpected response format.
230+
error is DecodingError
231+
})
232+
}
196233
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"approved": false
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"approved": true
3+
}

Yosemite/Yosemite/Actions/SiteAction.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public enum SiteAction: Action {
2828
/// - siteID: ID of the site to load.
2929
/// - completion: Called when the result of the synced site is available.
3030
case syncSite(siteID: Int64, completion: (Result<Site, Error>) -> Void)
31+
32+
/// Loads the Blaze status of a site.
33+
/// - Parameter:
34+
/// - siteID: ID of the site to load.
35+
/// - completion: Called when the result of the Blaze status is available.
36+
case loadBlazeStatus(siteID: Int64, completion: (Result<Bool, Error>) -> Void)
3137
}
3238

3339
/// The result of site creation including necessary site information.

Yosemite/Yosemite/Stores/SiteStore.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public final class SiteStore: Store {
5454
launchSite(siteID: siteID, completion: completion)
5555
case let .enableFreeTrial(siteID, profilerData, completion):
5656
enableFreeTrial(siteID: siteID, profilerData: profilerData, completion: completion)
57-
case let.syncSite(siteID, completion):
57+
case let .syncSite(siteID, completion):
5858
syncSite(siteID: siteID, completion: completion)
59+
case let .loadBlazeStatus(siteID, completion):
60+
loadBlazeStatus(siteID: siteID, completion: completion)
5961
}
6062
}
6163
}
@@ -120,6 +122,17 @@ private extension SiteStore {
120122
}
121123
}
122124
}
125+
126+
func loadBlazeStatus(siteID: Int64, completion: @escaping (Result<Bool, Error>) -> Void) {
127+
Task { @MainActor in
128+
do {
129+
let isApproved = try await remote.loadBlazeStatus(siteID: siteID)
130+
completion(.success(isApproved))
131+
} catch {
132+
completion(.failure(error))
133+
}
134+
}
135+
}
123136
}
124137

125138
private extension SiteStore {

Yosemite/YosemiteTests/Mocks/Networking/Remote/MockSiteRemote.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ final class MockSiteRemote {
1616
/// The results to return in `loadSite`.
1717
private var loadSiteResult: Result<Site, Error>?
1818

19+
/// The results to return in `loadBlazeStatus`.
20+
private var loadBlazeStatusResult: Result<Bool, Error>?
21+
1922
/// Returns the value when `createSite` is called.
2023
func whenCreatingSite(thenReturn result: Result<SiteCreationResponse, Error>) {
2124
createSiteResult = result
@@ -35,6 +38,11 @@ final class MockSiteRemote {
3538
func whenLoadingSite(thenReturn result: Result<Site, Error>) {
3639
loadSiteResult = result
3740
}
41+
42+
/// Returns the value when `loadBlazeStatus` is called.
43+
func whenLoadingBlazeStatus(thenReturn result: Result<Bool, Error>) {
44+
loadBlazeStatusResult = result
45+
}
3846
}
3947

4048
extension MockSiteRemote: SiteRemoteProtocol {
@@ -73,4 +81,12 @@ extension MockSiteRemote: SiteRemoteProtocol {
7381

7482
return try result.get()
7583
}
84+
85+
func loadBlazeStatus(siteID: Int64) async throws -> Bool {
86+
guard let result = loadBlazeStatusResult else {
87+
XCTFail("Could not find result for loading a site's Blaze status.")
88+
throw NetworkError.notFound
89+
}
90+
return try result.get()
91+
}
7692
}

Yosemite/YosemiteTests/Stores/SiteStoreTests.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,56 @@ final class SiteStoreTests: XCTestCase {
239239
let error = try XCTUnwrap(result.failure)
240240
XCTAssertEqual(error as? DotcomError, .unknown(code: "error", message: nil))
241241
}
242+
243+
// MARK: - `loadBlazeStatus`
244+
245+
func test_loadBlazeStatus_returns_true_on_success_with_approved_status() throws {
246+
// Given
247+
remote.whenLoadingBlazeStatus(thenReturn: .success(true))
248+
249+
// When
250+
let result = waitFor { promise in
251+
self.store.onAction(SiteAction.loadBlazeStatus(siteID: 134) { result in
252+
promise(result)
253+
})
254+
}
255+
256+
// Then
257+
XCTAssertTrue(result.isSuccess)
258+
let isApproved = try XCTUnwrap(result.get())
259+
XCTAssertTrue(isApproved)
260+
}
261+
262+
func test_loadBlazeStatus_returns_false_on_success_without_approved_status() throws {
263+
// Given
264+
remote.whenLoadingBlazeStatus(thenReturn: .success(false))
265+
266+
// When
267+
let result = waitFor { promise in
268+
self.store.onAction(SiteAction.loadBlazeStatus(siteID: 134) { result in
269+
promise(result)
270+
})
271+
}
272+
273+
// Then
274+
XCTAssertTrue(result.isSuccess)
275+
let isApproved = try XCTUnwrap(result.get())
276+
XCTAssertFalse(isApproved)
277+
}
278+
279+
func test_loadBlazeStatus_returns_error_on_failure() throws {
280+
// Given
281+
remote.whenLoadingBlazeStatus(thenReturn: .failure(DotcomError.unknown(code: "error", message: nil)))
282+
283+
// When
284+
let result = waitFor { promise in
285+
self.store.onAction(SiteAction.loadBlazeStatus(siteID: 134) { result in
286+
promise(result)
287+
})
288+
}
289+
290+
// Then
291+
let error = try XCTUnwrap(result.failure)
292+
XCTAssertEqual(error as? DotcomError, .unknown(code: "error", message: nil))
293+
}
242294
}

0 commit comments

Comments
 (0)