11import XCTest
2+ import OHHTTPStubs
23
34@testable import WordPressKit
45
@@ -13,12 +14,37 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
1314 // Requests the correct set of cards
1415 //
1516 func testRequestCardsParam( ) {
16- let expect = expectation ( description: " Get cards successfully " )
17- stubRemoteResponse ( " wpcom/v2/sites/165243437/dashboard/cards-data/?cards=posts,todays_stats " , filename: " dashboard-200-with-drafts-and-scheduled-posts.json " , contentType: . ApplicationJSON)
18-
19- dashboardServiceRemote. fetch ( cards: [ " posts " , " todays_stats " ] , forBlogID: 165243437 ) { _ in
17+ let expect = expectation ( description: " Dashboard endpoint should contain query params " )
18+ let expectedPath = " /wpcom/v2/sites/165243437/dashboard/cards-data "
19+ let expectedQueryParams : Set < String > = [
20+ " identifier " ,
21+ " platform " ,
22+ " build_number " ,
23+ " marketing_version " ,
24+ " device_id " ,
25+ " cards " ,
26+ " locale "
27+ ]
28+
29+ stubRemoteResponse ( { req in
30+ let url = req. url? . absoluteString ?? " "
31+ let containsQueryParams = self . queryParams ( expectedQueryParams, containedInRequest: req)
32+ let matchesPath = isPath ( expectedPath) ( req)
33+ XCTAssertTrue ( matchesPath, " The URL ' \( url) ' doesn't match the expected path. " )
34+ XCTAssertTrue ( containsQueryParams, " The URL ' \( url) ' doesn't contain the expected query params. " )
35+ return containsQueryParams && matchesPath
36+ } , filename: " dashboard-200-with-drafts-and-scheduled-posts.json " , contentType: . ApplicationJSON)
37+
38+ dashboardServiceRemote. fetch (
39+ cards: [ " posts " , " todays_stats " ] ,
40+ forBlogID: 165243437 ,
41+ deviceId: " Test "
42+ ) { _ in
2043 expect. fulfill ( )
21- } failure: { _ in }
44+ } failure: { error in
45+ XCTFail ( " Dashboard cards request failed: \( error. localizedDescription) " )
46+ expect. fulfill ( )
47+ }
2248
2349 waitForExpectations ( timeout: timeout, handler: nil )
2450 }
@@ -27,9 +53,18 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
2753 //
2854 func testRequestCards( ) {
2955 let expect = expectation ( description: " Get cards successfully " )
30- stubRemoteResponse ( " wpcom/v2/sites/165243437/dashboard/cards-data/?cards=posts,todays_stats " , filename: " dashboard-200-with-drafts-and-scheduled-posts.json " , contentType: . ApplicationJSON)
3156
32- dashboardServiceRemote. fetch ( cards: [ " posts " , " todays_stats " ] , forBlogID: 165243437 ) { cards in
57+ stubRemoteResponse (
58+ isPath ( " /wpcom/v2/sites/165243437/dashboard/cards-data " ) ,
59+ filename: " dashboard-200-with-drafts-and-scheduled-posts.json " ,
60+ contentType: . ApplicationJSON
61+ )
62+
63+ dashboardServiceRemote. fetch (
64+ cards: [ " posts " , " todays_stats " ] ,
65+ forBlogID: 165243437 ,
66+ deviceId: " Test "
67+ ) { cards in
3368 XCTAssertTrue ( ( cards [ " posts " ] as! NSDictionary ) [ " has_published " ] as! Bool )
3469 XCTAssertEqual ( ( cards [ " todays_stats " ] as! NSDictionary ) [ " views " ] as! Int , 0 )
3570 expect. fulfill ( )
@@ -44,7 +79,11 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
4479 let expect = expectation ( description: " Get cards successfully " )
4580 stubRemoteResponse ( " wpcom/v2/sites/165243437/dashboard/cards-data/?cards=posts,todays_stats " , filename: " dashboard-200-with-drafts-and-scheduled-posts.json " , contentType: . ApplicationJSON, status: 503 )
4681
47- dashboardServiceRemote. fetch ( cards: [ " posts " , " todays_stats " ] , forBlogID: 165243437 ) { _ in
82+ dashboardServiceRemote. fetch (
83+ cards: [ " posts " , " todays_stats " ] ,
84+ forBlogID: 165243437 ,
85+ deviceId: " Test "
86+ ) { _ in
4887 XCTFail ( " This call should not suceed " )
4988 } failure: { error in
5089 expect. fulfill ( )
@@ -59,7 +98,11 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
5998 let expect = expectation ( description: " Get cards successfully " )
6099 stubRemoteResponse ( " wpcom/v2/sites/165243437/dashboard/cards-data/?cards=invalid_card " , filename: " dashboard-400-invalid-card.json " , contentType: . ApplicationJSON, status: 400 )
61100
62- dashboardServiceRemote. fetch ( cards: [ " invalid_card " ] , forBlogID: 165243437 ) { _ in
101+ dashboardServiceRemote. fetch (
102+ cards: [ " invalid_card " ] ,
103+ forBlogID: 165243437 ,
104+ deviceId: " Test "
105+ ) { _ in
63106 XCTFail ( " This call should not suceed " )
64107 } failure: { error in
65108 expect. fulfill ( )
@@ -74,7 +117,11 @@ class DashboardServiceRemoteTests: RemoteTestCase, RESTTestable {
74117 let expect = expectation ( description: " Get cards successfully " )
75118 stubRemoteResponse ( " wpcom/v2/sites/165243437/dashboard/cards-data/?cards=posts,todays_stats " , data: " foo " . data ( using: . utf8) !, contentType: . ApplicationJSON)
76119
77- dashboardServiceRemote. fetch ( cards: [ " posts " , " todays_stats " ] , forBlogID: 165243437 ) { _ in
120+ dashboardServiceRemote. fetch (
121+ cards: [ " posts " , " todays_stats " ] ,
122+ forBlogID: 165243437 ,
123+ deviceId: " Test "
124+ ) { _ in
78125 XCTFail ( " This call should not suceed " )
79126 } failure: { error in
80127 expect. fulfill ( )
0 commit comments