From eab6275c5955f8fb5a15f4b01f7a197c4aae261e Mon Sep 17 00:00:00 2001 From: Salim Braksa Date: Tue, 9 Jan 2024 03:10:19 +0100 Subject: [PATCH] Turn deviceId argument of DashboardServiceRemote.deviceId optional --- CHANGELOG.md | 4 +-- WordPressKit/DashboardServiceRemote.swift | 11 +++++-- .../DashboardServiceRemoteTests.swift | 30 +++++++++++++++++++ WordPressKitTests/RemoteTestCase.swift | 4 +-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9556f6f9..209b51a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,11 +34,11 @@ _None._ ### Breaking Changes -- Add `deviceId` param to `DashboardServiceRemote.fetch` method. [#674] +_None._ ### New Features -_None._ +- Add `deviceId` param to `DashboardServiceRemote.fetch` method. [#674] ### Bug Fixes diff --git a/WordPressKit/DashboardServiceRemote.swift b/WordPressKit/DashboardServiceRemote.swift index 1ef61006..5dfd2549 100644 --- a/WordPressKit/DashboardServiceRemote.swift +++ b/WordPressKit/DashboardServiceRemote.swift @@ -4,7 +4,7 @@ open class DashboardServiceRemote: ServiceRemoteWordPressComREST { open func fetch( cards: [String], forBlogID blogID: Int, - deviceId: String, + deviceId: String? = nil, success: @escaping (NSDictionary) -> Void, failure: @escaping (Error) -> Void ) { @@ -32,11 +32,16 @@ open class DashboardServiceRemote: ServiceRemoteWordPressComREST { }) } - private func makeQueryParams(cards: [String], deviceId: String) throws -> [String: AnyObject] { + private func makeQueryParams(cards: [String], deviceId: String?) throws -> [String: AnyObject] { let cardsParams: [String: AnyObject] = [ "cards": cards.joined(separator: ",") as NSString ] - let featureFlagParams = try SessionDetails(deviceId: deviceId).dictionaryRepresentation() + let featureFlagParams: [String: AnyObject]? = try { + guard let deviceId else { + return nil + } + return try SessionDetails(deviceId: deviceId).dictionaryRepresentation() + }() return cardsParams.merging(featureFlagParams ?? [:]) { first, second in return first } diff --git a/WordPressKitTests/DashboardServiceRemoteTests.swift b/WordPressKitTests/DashboardServiceRemoteTests.swift index 86d62c61..d1f6b115 100644 --- a/WordPressKitTests/DashboardServiceRemoteTests.swift +++ b/WordPressKitTests/DashboardServiceRemoteTests.swift @@ -49,6 +49,36 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable { waitForExpectations(timeout: timeout, handler: nil) } + // Validates the request's path and query items when the `deviceId` param is `nil`. + // + func testRequestCardsParamWithoutDeviceId() { + let expect = expectation(description: "Dashboard endpoint should contain query params") + let expectedPath = "/wpcom/v2/sites/165243437/dashboard/cards-data" + let expectedQueryParams: Set = ["cards", "locale"] + + stubRemoteResponse({ req in + let url = req.url?.absoluteString ?? "" + let containsQueryParams = self.queryParams(expectedQueryParams, containedInRequest: req) + let matchesPath = isPath(expectedPath)(req) + XCTAssertTrue(matchesPath, "The URL '\(url)' doesn't match the expected path.") + XCTAssertTrue(containsQueryParams, "The URL '\(url)' doesn't contain the expected query params.") + return containsQueryParams && matchesPath + }, filename: "dashboard-200-with-drafts-and-scheduled-posts.json", contentType: .ApplicationJSON) + + dashboardServiceRemote.fetch( + cards: ["posts", "todays_stats"], + forBlogID: 165243437, + deviceId: nil + ) { _ in + expect.fulfill() + } failure: { error in + XCTFail("Dashboard cards request failed: \(error.localizedDescription)") + expect.fulfill() + } + + waitForExpectations(timeout: timeout, handler: nil) + } + // Return the cards when the request succeeds // func testRequestCards() { diff --git a/WordPressKitTests/RemoteTestCase.swift b/WordPressKitTests/RemoteTestCase.swift index 52b63d20..aa04122c 100644 --- a/WordPressKitTests/RemoteTestCase.swift +++ b/WordPressKitTests/RemoteTestCase.swift @@ -197,7 +197,7 @@ extension RemoteTestCase { guard let url = request.url else { return false } - return queryParamsContained(queryParams, containedInURL: url) + return self.queryParams(queryParams, containedInURL: url) } /// Checks if the specified set of query parameter names are all present in a given `URL`. @@ -207,7 +207,7 @@ extension RemoteTestCase { /// - queryParams: A set of query parameter names to check for in the URL. /// - url: The `URL` to inspect for the presence of query parameter names. /// - Returns: A Boolean value indicating whether all specified query parameter names are present in the URL's query string. - func queryParamsContained(_ queryParams: Set, containedInURL url: URL) -> Bool { + func queryParams(_ queryParams: Set, containedInURL url: URL) -> Bool { guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true), let queryItems = components.queryItems?.map({ $0.name }) else {