Skip to content

Commit 1013a26

Browse files
committed
Add RequestAuthenticator tests
1 parent b66136b commit 1013a26

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

WooCommerce/WooCommerce.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,7 @@
11641164
AEB4DB9D290AE94600AE4340 /* WKCookieJarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB4DB9C290AE94600AE4340 /* WKCookieJarTests.swift */; };
11651165
AEB4DB9F290AEA6100AE4340 /* AuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB4DB9E290AEA6100AE4340 /* AuthenticationService.swift */; };
11661166
AEB4DBA1290AEC0D00AE4340 /* RequestAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB4DBA0290AEC0D00AE4340 /* RequestAuthenticator.swift */; };
1167+
AEB4DBA3290AEDB900AE4340 /* RequestAuthenticatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB4DBA2290AEDB900AE4340 /* RequestAuthenticatorTests.swift */; };
11671168
AEB73C0C25CD734200A8454A /* AttributePickerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB73C0B25CD734200A8454A /* AttributePickerViewModel.swift */; };
11681169
AEB73C1725CD8E5800A8454A /* AttributePickerViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEB73C1625CD8E5800A8454A /* AttributePickerViewModelTests.swift */; };
11691170
AEBFD13F28E7655F00F598C6 /* StoreInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEBFD13E28E7655F00F598C6 /* StoreInfoView.swift */; };
@@ -3079,6 +3080,7 @@
30793080
AEB4DB9C290AE94600AE4340 /* WKCookieJarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKCookieJarTests.swift; sourceTree = "<group>"; };
30803081
AEB4DB9E290AEA6100AE4340 /* AuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationService.swift; sourceTree = "<group>"; };
30813082
AEB4DBA0290AEC0D00AE4340 /* RequestAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestAuthenticator.swift; sourceTree = "<group>"; };
3083+
AEB4DBA2290AEDB900AE4340 /* RequestAuthenticatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestAuthenticatorTests.swift; sourceTree = "<group>"; };
30823084
AEB73C0B25CD734200A8454A /* AttributePickerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributePickerViewModel.swift; sourceTree = "<group>"; };
30833085
AEB73C1625CD8E5800A8454A /* AttributePickerViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributePickerViewModelTests.swift; sourceTree = "<group>"; };
30843086
AEBFD13E28E7655F00F598C6 /* StoreInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoreInfoView.swift; sourceTree = "<group>"; };
@@ -6656,6 +6658,7 @@
66566658
EEADF625281A65A9001B40F1 /* DefaultShippingValueLocalizerTests.swift */,
66576659
AEB4DB9A290AE92800AE4340 /* CookieJarTests.swift */,
66586660
AEB4DB9C290AE94600AE4340 /* WKCookieJarTests.swift */,
6661+
AEB4DBA2290AEDB900AE4340 /* RequestAuthenticatorTests.swift */,
66596662
);
66606663
path = Tools;
66616664
sourceTree = "<group>";
@@ -10617,6 +10620,7 @@
1061710620
02AB40822784297C00929CF3 /* ProductTableViewCellViewModelTests.swift in Sources */,
1061810621
02829BAA288FA8B300951E1E /* MockUserNotification.swift in Sources */,
1061910622
D85B8336222FCDA1002168F3 /* StatusListTableViewCellTests.swift in Sources */,
10623+
AEB4DBA3290AEDB900AE4340 /* RequestAuthenticatorTests.swift in Sources */,
1062010624
3198A1E82694DC7200597213 /* MockKnownReadersProvider.swift in Sources */,
1062110625
DEC51B04276B30F6009F3DF4 /* SystemStatusReportViewModelTests.swift in Sources */,
1062210626
26B119C224D1CD3500FED5C7 /* WooConstantsTests.swift in Sources */,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import XCTest
2+
@testable import WooCommerce
3+
4+
private extension URLRequest {
5+
var httpBodyString: String? {
6+
return httpBody.flatMap({ data in
7+
return String(data: data, encoding: .utf8)
8+
})
9+
}
10+
}
11+
12+
class RequestAuthenticatorTests: XCTestCase {
13+
let dotComLoginURL = URL(string: "https://wordpress.com/wp-login.php")!
14+
let dotComUser = "comuser"
15+
let dotComToken = "comtoken"
16+
let siteLoginURL = URL(string: "https://example.com/wp-login.php")!
17+
let siteUser = "siteuser"
18+
let sitePassword = "x>73R9&9;r&ju9$J499FmZ?2*Nii/?$8"
19+
let sitePasswordEncoded = "x%3E73R9%269;r%26ju9$J499FmZ?2*Nii/?$8"
20+
21+
var dotComAuthenticator: RequestAuthenticator {
22+
return RequestAuthenticator(credentials: .dotCom(username: dotComUser, authToken: dotComToken, authenticationType: .regular))
23+
}
24+
25+
var siteAuthenticator: RequestAuthenticator {
26+
return RequestAuthenticator(
27+
credentials: .siteLogin(loginURL: siteLoginURL, username: siteUser, password: sitePassword))
28+
}
29+
30+
func testUnauthenticatedDotComRequestWithCookie() {
31+
let url = URL(string: "https://example.wordpress.com/some-page/")!
32+
let authenticator = dotComAuthenticator
33+
34+
let cookieJar = MockCookieJar()
35+
cookieJar.setWordPressComCookie(username: dotComUser)
36+
var authenticatedRequest: URLRequest? = nil
37+
authenticator.request(url: url, cookieJar: cookieJar) {
38+
authenticatedRequest = $0
39+
}
40+
guard let request = authenticatedRequest else {
41+
XCTFail("The authenticator should return a valid request")
42+
return
43+
}
44+
XCTAssertEqual(request.url, url)
45+
XCTAssertNil(request.value(forHTTPHeaderField: "Authorization"))
46+
}
47+
48+
func testDecideActionForNavigationResponse() {
49+
let url = URL(string: "https://example.wordpress.com/some-page/")!
50+
let authenticator = dotComAuthenticator
51+
let cookieJar = MockCookieJar()
52+
cookieJar.setWordPressComCookie(username: dotComUser)
53+
54+
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
55+
56+
let expectation = self.expectation(description: "Action Should be decided")
57+
authenticator.decideActionFor(response: response, cookieJar: cookieJar) { action in
58+
XCTAssertEqual(action, RequestAuthenticator.WPNavigationActionType.allow)
59+
expectation.fulfill()
60+
}
61+
62+
waitForExpectations(timeout: 0.2)
63+
}
64+
65+
func testDecideActionForNavigationResponse_RemoteLoginError() {
66+
let url = URL(string: "https://r-login.wordpress.com/remote-login.php?action=auth")!
67+
let authenticator = dotComAuthenticator
68+
let cookieJar = MockCookieJar()
69+
cookieJar.setWordPressComCookie(username: dotComUser)
70+
71+
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
72+
73+
let expectation = self.expectation(description: "Action Should be decided")
74+
authenticator.decideActionFor(response: response, cookieJar: cookieJar) { action in
75+
XCTAssertEqual(action, RequestAuthenticator.WPNavigationActionType.reload)
76+
expectation.fulfill()
77+
}
78+
79+
waitForExpectations(timeout: 0.2)
80+
}
81+
82+
func testDecideActionForNavigationResponse_ClientError() {
83+
let url = URL(string: "https://example.wordpress.com/some-page/")!
84+
let authenticator = dotComAuthenticator
85+
let cookieJar = MockCookieJar()
86+
cookieJar.setWordPressComCookie(username: dotComUser)
87+
88+
let response = HTTPURLResponse(url: url, statusCode: 404, httpVersion: nil, headerFields: nil)!
89+
90+
let expectation = self.expectation(description: "Action Should be decided")
91+
authenticator.decideActionFor(response: response, cookieJar: cookieJar) { action in
92+
XCTAssertEqual(action, RequestAuthenticator.WPNavigationActionType.reload)
93+
expectation.fulfill()
94+
}
95+
96+
waitForExpectations(timeout: 0.2)
97+
}
98+
}

0 commit comments

Comments
 (0)