This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
[Jetpack Remote Install] New Blog service API #124
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d55667
Pod install
danielebogo c52f2bc
Bump podspec
danielebogo 30b1da5
Add new extension
danielebogo 499770f
Fix method signature
danielebogo df3b328
Add unit tests
danielebogo d9ae027
Fix test
danielebogo bd18946
Lint
danielebogo f0716af
Fix init method removing the guard let statement
danielebogo 35c2112
Remove locale from the path
danielebogo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| public enum JetpackInstallError: String, Error { | ||
| case invalidCredentials = "INVALID_CREDENTIALS" | ||
| case forbidden = "FORBIDDEN" | ||
| case installFailure = "INSTALL_FAILURE" | ||
| case installResponseError = "INSTALL_RESPONSE_ERROR" | ||
| case loginFailure = "LOGIN_FAILURE" | ||
| case siteIsJetpack = "SITE_IS_JETPACK" | ||
| case activationOnInstallFailure = "ACTIVATION_ON_INSTALL_FAILURE" | ||
| case activationResponseError = "ACTIVATION_RESPONSE_ERROR" | ||
| case activationFailure = "ACTIVATION_FAILURE" | ||
| case unknown | ||
|
|
||
| init(error key: String) { | ||
| self = JetpackInstallError(rawValue: key) ?? .unknown | ||
| } | ||
| } | ||
|
|
||
| public extension BlogServiceRemoteREST { | ||
| public func installJetpack(url: String, | ||
| username: String, | ||
| password: String, | ||
| completion: @escaping (Bool, JetpackInstallError?) -> Void) { | ||
| guard let escapedURL = url.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else { | ||
| completion(false, .unknown) | ||
| return | ||
| } | ||
| let path = String(format: "jetpack-install/%@/", escapedURL) | ||
| let requestUrl = self.path(forEndpoint: path, withVersion: ._1_0) | ||
| let parameters = ["user": username, | ||
| "password": password] | ||
|
|
||
| wordPressComRestApi.POST(requestUrl, | ||
| parameters: parameters as [String : AnyObject], | ||
| success: { (response: AnyObject, httpResponse: HTTPURLResponse?) in | ||
| if let response = response as? [String: Bool], | ||
| let success = response[Constants.status] { | ||
| completion(success, nil) | ||
| } else { | ||
| completion(false, .installResponseError) | ||
| } | ||
| }) { (error: NSError, httpResponse: HTTPURLResponse?) in | ||
| if let key = error.userInfo[WordPressComRestApi.ErrorKeyErrorCode] as? String { | ||
| completion(false, JetpackInstallError(error: key)) | ||
| } else { | ||
| completion(false, .unknown) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private enum Constants { | ||
| static let status = "status" | ||
| } | ||
| } | ||
197 changes: 197 additions & 0 deletions
197
WordPressKitTests/BlogServiceRemoteRESTTests+Jetpack.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import Foundation | ||
| import XCTest | ||
| @testable import WordPressKit | ||
|
|
||
| class BlogServiceRemoteRESTTests_Jetpack: RemoteTestCase, RESTTestable { | ||
| let siteId = 12345 | ||
| let url = "http://www.wordpress.com" | ||
| let encodedURL = "http%3A%2F%2Fwww.wordpress.com" | ||
| let username = "username" | ||
| let password = "qwertyuiop" | ||
|
|
||
| let jetpackRemoteSuccessMockFilename = "blog-service-jetpack-remote-success.json" | ||
| let jetpackRemoteFailureMockFilename = "blog-service-jetpack-remote-failure.json" | ||
|
|
||
| let jetpackRemoteErrorUnknownMockFilename = "blog-service-jetpack-remote-error-unknown.json" | ||
| let jetpackRemoteErrorInvalidCredentialsMockFilename = "blog-service-jetpack-remote-error-invalid-credentials.json" | ||
| let jetpackRemoteErrorForbiddenMockFilename = "blog-service-jetpack-remote-error-forbidden.json" | ||
| let jetpackRemoteErrorInstallFailureMockFilename = "blog-service-jetpack-remote-error-install-failure.json" | ||
| let jetpackRemoteErrorInstallResponseMockFilename = "blog-service-jetpack-remote-error-install-response.json" | ||
| let jetpackRemoteErrorLoginFailureMockFilename = "blog-service-jetpack-remote-error-login-failure.json" | ||
| let jetpackRemoteErrorSiteIsJetpackMockFilename = "blog-service-jetpack-remote-error-site-is-jetpack.json" | ||
| let jetpackRemoteErrorActivationInstallMockFilename = "blog-service-jetpack-remote-error-activation-install.json" | ||
| let jetpackRemoteErrorActivationResponseMockFilename = "blog-service-jetpack-remote-error-activation-response.json" | ||
| let jetpackRemoteErrorActivationFailureMockFilename = "blog-service-jetpack-remote-error-activation-failure.json" | ||
|
|
||
| var endpoint: String { return "jetpack-install/\(encodedURL)/" } | ||
|
|
||
| var remote: BlogServiceRemoteREST! | ||
|
|
||
| // MARK: - Overridden Methods | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
|
|
||
| remote = BlogServiceRemoteREST(wordPressComRestApi: getRestApi(), siteID: NSNumber(value: siteId)) | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| super.tearDown() | ||
|
|
||
| remote = nil | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationSuccess() { | ||
| let expect = expectation(description: "Install Jetpack success") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteSuccessMockFilename, contentType: .ApplicationJSON, status: 200) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertTrue(success, "Success should be true") | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationFailure() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteFailureMockFilename, contentType: .ApplicationJSON, status: 200) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationErrorInvalidCredentials() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInvalidCredentialsMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .invalidCredentials) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationErrorUnknown() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorUnknownMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .unknown) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationErrorForbidden() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorForbiddenMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .forbidden) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationInstallFailure() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallFailureMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .installFailure) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationInstallResponse() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorInstallResponseMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .installResponseError) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationLoginFailure() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorLoginFailureMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .loginFailure) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationSiteIsJetpack() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorSiteIsJetpackMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .siteIsJetpack) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationActivationInstall() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationInstallMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .activationOnInstallFailure) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationActivationResponse() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationResponseMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .activationResponseError) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
|
|
||
| func testJetpackRemoteInstallationActivationFailure() { | ||
| let expect = expectation(description: "Install Jetpack failure") | ||
|
|
||
| stubRemoteResponse(endpoint, filename: jetpackRemoteErrorActivationFailureMockFilename, contentType: .ApplicationJSON, status: 400) | ||
| remote.installJetpack(url: url, username: username, password: password) { (success, error) in | ||
| XCTAssertFalse(success, "Success should be false") | ||
| XCTAssertEqual(error, .activationFailure) | ||
| expect.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: timeout, handler: nil) | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-activation-failure.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "ACTIVATION_FAILURE", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-activation-install.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "ACTIVATION_ON_INSTALL_FAILURE", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-activation-response.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "ACTIVATION_RESPONSE_ERROR", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-forbidden.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "FORBIDDEN", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-install-failure.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "INSTALL_FAILURE", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-install-response.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "INSTALL_RESPONSE_ERROR", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-invalid-credentials.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "INVALID_CREDENTIALS", | ||
| "message": "bad password" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-login-failure.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "LOGIN_FAILURE", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-site-is-jetpack.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "SITE_IS_JETPACK", | ||
| "message": "message" | ||
| } |
4 changes: 4 additions & 0 deletions
4
WordPressKitTests/Mock Data/blog-service-jetpack-remote-error-unknown.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "error": "UNKNOWN_ERROR", | ||
| "message": "message" | ||
| } |
3 changes: 3 additions & 0 deletions
3
WordPressKitTests/Mock Data/blog-service-jetpack-remote-failure.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "status": false | ||
| } |
3 changes: 3 additions & 0 deletions
3
WordPressKitTests/Mock Data/blog-service-jetpack-remote-success.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "status": true | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.