|
| 1 | +import XCTest |
| 2 | +@testable import WordPressKit |
| 3 | + |
| 4 | +class QRLoginServiceRemoteTests: RemoteTestCase, RESTTestable { |
| 5 | + let mockRemoteApi = MockWordPressComRestApi() |
| 6 | + var qrLoginServiceRemote: QRLoginServiceRemote! |
| 7 | + |
| 8 | + override func setUp() { |
| 9 | + qrLoginServiceRemote = QRLoginServiceRemote(wordPressComRestApi: getRestApi()) |
| 10 | + } |
| 11 | + |
| 12 | + // MARK: - Validate Tests |
| 13 | + |
| 14 | + // Calls the success block with valid data when the request succeeds |
| 15 | + // |
| 16 | + func testValidResponseObject() { |
| 17 | + let expect = expectation(description: "Validate the response object successfully") |
| 18 | + stubRemoteResponse("wpcom/v2/auth/qr-code/validate", filename: "qrlogin-validate-200.json", contentType: .ApplicationJSON) |
| 19 | + |
| 20 | + let browser = "Chrome" |
| 21 | + let location = "Mount Laurel, New Jersey" |
| 22 | + |
| 23 | + qrLoginServiceRemote.validate(token: "", data: "") { response in |
| 24 | + XCTAssertEqual(browser, response.browser) |
| 25 | + XCTAssertEqual(location, response.location) |
| 26 | + expect.fulfill() |
| 27 | + } failure: { _, _ in } |
| 28 | + |
| 29 | + waitForExpectations(timeout: timeout, handler: nil) |
| 30 | + } |
| 31 | + |
| 32 | + // Calls the failure block with invalidData when providing invalid token/data |
| 33 | + // |
| 34 | + func testValidateInvalidData() { |
| 35 | + let expect = expectation(description: "Validate the invalid data error is being handled") |
| 36 | + stubRemoteResponse("wpcom/v2/auth/qr-code/validate", filename: "qrlogin-validate-400.json", contentType: .ApplicationJSON, status: 400) |
| 37 | + |
| 38 | + qrLoginServiceRemote.validate(token: "invalid_token", data: "invalid_data") { _ in |
| 39 | + XCTFail("This request should not succeed") |
| 40 | + } failure: { _, qrLoginError in |
| 41 | + XCTAssertEqual(qrLoginError!, .invalidData) |
| 42 | + expect.fulfill() |
| 43 | + } |
| 44 | + |
| 45 | + waitForExpectations(timeout: timeout, handler: nil) |
| 46 | + } |
| 47 | + |
| 48 | + // Calls the failure block with expired when providing expired token/data |
| 49 | + // |
| 50 | + func testValidateExpired() { |
| 51 | + let expect = expectation(description: "Validate the expired data error is being handled") |
| 52 | + stubRemoteResponse("wpcom/v2/auth/qr-code/validate", filename: "qrlogin-validate-expired-401.json", contentType: .ApplicationJSON, status: 401) |
| 53 | + |
| 54 | + qrLoginServiceRemote.validate(token: "expired_token", data: "expired_data") { _ in |
| 55 | + XCTFail("This request should not succeed") |
| 56 | + } failure: { _, qrLoginError in |
| 57 | + XCTAssertEqual(qrLoginError!, .expired) |
| 58 | + expect.fulfill() |
| 59 | + } |
| 60 | + |
| 61 | + waitForExpectations(timeout: timeout, handler: nil) |
| 62 | + } |
| 63 | + |
| 64 | + // Calls the failure block with invalidData when parsing an invalid response |
| 65 | + // |
| 66 | + func testInvalidJSON() { |
| 67 | + let expect = expectation(description: "Validate the failure object is being returned") |
| 68 | + stubRemoteResponse("wpcom/v2/auth/qr-code/validate", data: "foo".data(using: .utf8)!, contentType: .ApplicationJSON) |
| 69 | + |
| 70 | + qrLoginServiceRemote.validate(token: "expired_token", data: "expired_data") { _ in |
| 71 | + XCTFail("This request should not succeed") |
| 72 | + } failure: { _, qrLoginError in |
| 73 | + XCTAssertEqual(qrLoginError!, .invalidData) |
| 74 | + expect.fulfill() |
| 75 | + } |
| 76 | + |
| 77 | + waitForExpectations(timeout: timeout, handler: nil) |
| 78 | + } |
| 79 | + |
| 80 | + // MARK: - Authenticate Tests |
| 81 | + |
| 82 | + // Calls the success block when authenticating valid data |
| 83 | + // |
| 84 | + func testAuthenticateSuccess() { |
| 85 | + let expect = expectation(description: "Successful Authentication") |
| 86 | + stubRemoteResponse("wpcom/v2/auth/qr-code/authenticate", filename: "qrlogin-authenticate-200.json", contentType: .ApplicationJSON) |
| 87 | + |
| 88 | + qrLoginServiceRemote.authenticate(token: "valid_token", data: "valid_data") { authenticated in |
| 89 | + XCTAssertTrue(authenticated) |
| 90 | + expect.fulfill() |
| 91 | + } failure: { _ in } |
| 92 | + |
| 93 | + waitForExpectations(timeout: timeout, handler: nil) |
| 94 | + } |
| 95 | + |
| 96 | + // Calls the failure block when providing invalid data |
| 97 | + // |
| 98 | + func testAuthenticateFailure() { |
| 99 | + let expect = expectation(description: "Failed Authentication") |
| 100 | + stubRemoteResponse("wpcom/v2/auth/qr-code/authenticate", filename: "qrlogin-authenticate-failed-400.json", contentType: .ApplicationJSON, status: 400) |
| 101 | + |
| 102 | + qrLoginServiceRemote.authenticate(token: "valid_token", data: "valid_data") { authenticated in |
| 103 | + XCTFail("This request should not succeed") |
| 104 | + } failure: { error in |
| 105 | + expect.fulfill() |
| 106 | + } |
| 107 | + |
| 108 | + waitForExpectations(timeout: timeout, handler: nil) |
| 109 | + } |
| 110 | + |
| 111 | + // Calls the failure block when parsing invalid JSON |
| 112 | + func testAuthenticateInvalidJSON() { |
| 113 | + let expect = expectation(description: "Failed Authentication") |
| 114 | + stubRemoteResponse("wpcom/v2/auth/qr-code/authenticate", data: "foo".data(using: .utf8)!, contentType: .ApplicationJSON) |
| 115 | + |
| 116 | + qrLoginServiceRemote.authenticate(token: "valid_token", data: "valid_data") { authenticated in |
| 117 | + XCTFail("This request should not succeed") |
| 118 | + } failure: { error in |
| 119 | + expect.fulfill() |
| 120 | + } |
| 121 | + |
| 122 | + waitForExpectations(timeout: timeout, handler: nil) |
| 123 | + } |
| 124 | +} |
0 commit comments