Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ PODS:
- OHHTTPStubs/Swift (6.1.0):
- OHHTTPStubs/Default
- UIDeviceIdentifier (1.1.4)
- WordPressShared (1.7.0):
- WordPressShared (1.7.2):
- CocoaLumberjack (~> 3.4)
- FormatterKit/TimeIntervalFormatter (= 1.8.2)
- wpxmlrpc (0.8.4)
Expand Down Expand Up @@ -63,7 +63,7 @@ SPEC CHECKSUMS:
OCMock: 43565190abc78977ad44a61c0d20d7f0784d35ab
OHHTTPStubs: 1e21c7d2c084b8153fc53d48400d8919d2d432d0
UIDeviceIdentifier: 8f8a24b257a4d978c8d40ad1e7355b944ffbfa8c
WordPressShared: cfbda56868419842dd7a106a4e807069a0c17aa9
WordPressShared: 63d57a4a07ad9f9a1ee5e8a7162e48fbb5192014
wpxmlrpc: 6ba55c773cfa27083ae4a2173e69b19f46da98e2

PODFILE CHECKSUM: 34d4f957f37c097c360d2863370ce2e5e06511cc
Expand Down
2 changes: 1 addition & 1 deletion WordPressKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "WordPressKit"
s.version = "3.1.0"
s.version = "3.2.0-beta.1"
s.summary = "WordPressKit offers a clean and simple WordPress.com and WordPress.org API."

s.description = <<-DESC
Expand Down
56 changes: 56 additions & 0 deletions WordPressKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions WordPressKit/BlogServiceRemoteREST+Jetpack.swift
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 WordPressKitTests/BlogServiceRemoteRESTTests+Jetpack.swift
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "ACTIVATION_FAILURE",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "ACTIVATION_ON_INSTALL_FAILURE",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "ACTIVATION_RESPONSE_ERROR",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "FORBIDDEN",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "INSTALL_FAILURE",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "INSTALL_RESPONSE_ERROR",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "INVALID_CREDENTIALS",
"message": "bad password"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "LOGIN_FAILURE",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "SITE_IS_JETPACK",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"error": "UNKNOWN_ERROR",
"message": "message"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": true
}