@@ -9,10 +9,11 @@ final class BlazeServiceRemoteTests: RemoteTestCase, RESTTestable {
99
1010 // MARK: - Properties
1111
12- var statusEndpoint : String { return " sites/ \( siteId) /blaze/status " }
12+ var statusEndpoint : String { " sites/ \( siteId) /blaze/status " }
13+ var searchEndpoint : String { " sites/ \( siteId) /wordads/dsp/api/v1/search/campaigns/site/ \( siteId) " }
1314 var service : BlazeServiceRemote !
1415
15- // MARK: - Tests
16+ // MARK: - Get Status
1617
1718 func testGetStatusReturnsSuccess( ) throws {
1819 // Given
@@ -79,15 +80,18 @@ final class BlazeServiceRemoteTests: RemoteTestCase, RESTTestable {
7980
8081 // MARK: - Campaigns
8182
82- func testDecodeCampaignsSearchResponse ( ) throws {
83+ func testGetCampagnsSuccess ( ) throws {
8384 // Given
84- let url = try XCTUnwrap ( Bundle ( for: BlazeServiceRemoteTests . self) . url ( forResource: " blaze-campaigns-search " , withExtension: " json " ) )
85- let data = try Data ( contentsOf: url)
85+ let bundle = Bundle ( for: BlazeServiceRemoteTests . self)
86+ let url = try XCTUnwrap ( bundle. url ( forResource: " blaze-campaigns-search " , withExtension: " json " ) )
87+ stubRemoteResponse ( searchEndpoint, data: try Data ( contentsOf: url) , contentType: . ApplicationJSON)
8688
8789 // When
88- let response = try JSONDecoder . apiDecoder . decode ( BlazeCampaignsSearchResponse . self , from : data )
90+ let result = try getSearchCampaignsResult ( )
8991
9092 // Then
93+ let response = try result. get ( )
94+
9195 XCTAssertEqual ( response. totalItems, 1 )
9296 XCTAssertEqual ( response. totalPages, 1 )
9397 XCTAssertEqual ( response. page, 1 )
@@ -110,4 +114,48 @@ final class BlazeServiceRemoteTests: RemoteTestCase, RESTTestable {
110114 XCTAssertEqual ( stats. impressionsTotal, 1000 )
111115 XCTAssertEqual ( stats. clicksTotal, 235 )
112116 }
117+
118+ func testGetCampagnsSuccessFailureInvalidJSON( ) throws {
119+ // Given
120+ let data = #"{ "campaigns": "XXXX" }"# . data ( using: . utf8) !
121+ stubRemoteResponse ( searchEndpoint, data: data, contentType: . ApplicationJSON)
122+
123+ // When
124+ let result = try getSearchCampaignsResult ( )
125+
126+ // Then
127+ switch result {
128+ case . success:
129+ XCTFail ( " Expected failure " )
130+ case . failure:
131+ break // OK
132+ }
133+ }
134+
135+ func testGetCampagnsSuccessFailureUnauthorized( ) throws {
136+ // Given
137+ stubRemoteResponse ( searchEndpoint, data: Data ( ) , contentType: . NoContentType, status: 403 )
138+
139+ // When
140+ let result = try getSearchCampaignsResult ( )
141+
142+ // Then
143+ switch result {
144+ case . success:
145+ XCTFail ( " Expected failure " )
146+ case . failure:
147+ break // OK
148+ }
149+ }
150+
151+ private func getSearchCampaignsResult( ) throws -> Result < BlazeCampaignsSearchResponse , Error > {
152+ var result : Result < BlazeCampaignsSearchResponse , Error > ?
153+ let expectation = self . expectation ( description: " requestCompleted " )
154+ BlazeServiceRemote ( wordPressComRestApi: getRestApi ( ) ) . searchCampaigns ( forSiteId: siteId) {
155+ result = $0
156+ expectation. fulfill ( )
157+ }
158+ wait ( for: [ expectation] , timeout: 1 )
159+ return try XCTUnwrap ( result)
160+ }
113161}
0 commit comments