Skip to content

Commit 02e2f2e

Browse files
committed
Merge branch 'test-app-connection-ios-430'
2 parents 6b7c1fc + 3a4acfb commit 02e2f2e

File tree

3 files changed

+75
-13
lines changed

3 files changed

+75
-13
lines changed

ios/MullvadVPNUITests/Networking/Networking.swift

+55-12
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ class Networking {
6868
throw NetworkingError.internalError(reason: "Failed to determine device's IP address")
6969
}
7070

71-
/// Get configured ad serving domain as URL object
72-
private static func getAdServingDomainURL() -> URL? {
73-
guard let adServingDomain = Bundle(for: BaseUITestCase.self)
74-
.infoDictionary?["AdServingDomain"] as? String,
75-
let adServingDomainURL = URL(string: adServingDomain) else {
76-
XCTFail("Ad serving domain not configured")
77-
return nil
78-
}
79-
80-
return adServingDomainURL
81-
}
82-
8371
/// Get configured ad serving domain
8472
private static func getAdServingDomain() throws -> String {
8573
guard let adServingDomain = Bundle(for: Networking.self)
@@ -226,4 +214,59 @@ class Networking {
226214
XCTFail("Failed to verify DNS server provider - couldn't serialize JSON")
227215
}
228216
}
217+
218+
public static func verifyConnectedThroughMullvad() {
219+
let mullvadConnectionJsonEndpoint = "https://am.i.mullvad.net/json"
220+
guard let url = URL(string: mullvadConnectionJsonEndpoint) else {
221+
XCTFail("Failed to unwrap URL")
222+
return
223+
}
224+
225+
let request = URLRequest(url: url)
226+
let completionHandlerInvokedExpectation = XCTestExpectation(
227+
description: "Completion handler for the request is invoked"
228+
)
229+
230+
let dataTask = URLSession.shared.dataTask(with: request) { data, response, error in
231+
if let response = response as? HTTPURLResponse {
232+
if response.statusCode != 200 {
233+
XCTFail("Request to connection check API failed - unexpected server response")
234+
}
235+
}
236+
237+
if let error = error {
238+
XCTFail("Request to connection check API failed - encountered error \(error.localizedDescription)")
239+
}
240+
241+
guard let data = data else {
242+
XCTFail("Didn't receive any data")
243+
return
244+
}
245+
246+
do {
247+
let jsonObject = try JSONSerialization.jsonObject(with: data)
248+
249+
if let dictionary = jsonObject as? [String: Any] {
250+
guard let isConnectedThroughMullvad = dictionary["mullvad_exit_ip"] as? Bool else {
251+
XCTFail("Unexpected JSON format")
252+
return
253+
}
254+
255+
XCTAssertTrue(isConnectedThroughMullvad)
256+
}
257+
} catch {
258+
XCTFail("Failed to verify whether connected through Mullvad or not")
259+
}
260+
261+
completionHandlerInvokedExpectation.fulfill()
262+
}
263+
264+
dataTask.resume()
265+
266+
let waitResult = XCTWaiter.wait(for: [completionHandlerInvokedExpectation], timeout: 30)
267+
268+
if waitResult != .completed {
269+
XCTFail("Request to connection check API failed - timeout")
270+
}
271+
}
229272
}

ios/MullvadVPNUITests/RelayTests.swift

+19-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ class RelayTests: LoggedInWithTimeUITestCase {
2626
}
2727
}
2828

29+
func testAppConnection() throws {
30+
TunnelControlPage(app)
31+
.tapSecureConnectionButton()
32+
33+
allowAddVPNConfigurationsIfAsked()
34+
35+
TunnelControlPage(app)
36+
.waitForSecureConnectionLabel()
37+
38+
try Networking.verifyCanAccessInternet()
39+
Networking.verifyConnectedThroughMullvad()
40+
}
41+
2942
func testAdBlockingViaDNS() throws {
3043
HeaderBar(app)
3144
.tapSettingsButton()
@@ -205,7 +218,12 @@ class RelayTests: LoggedInWithTimeUITestCase {
205218
TunnelControlPage(app)
206219
.tapSecureConnectionButton()
207220

208-
allowAddVPNConfigurations()
221+
allowAddVPNConfigurationsIfAsked()
222+
223+
TunnelControlPage(app)
224+
.waitForSecureConnectionLabel()
225+
226+
try Networking.verifyCanAccessInternet()
209227

210228
HeaderBar(app)
211229
.tapSettingsButton()

ios/MullvadVPNUITests/Test base classes/BaseUITestCase.swift

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class BaseUITestCase: XCTestCase {
129129

130130
func logoutIfLoggedIn() {
131131
if isLoggedIn() {
132+
// First dismiss settings modal if presented
132133
if isPresentingSettings() {
133134
SettingsPage(app)
134135
.swipeDownToDismissModal()

0 commit comments

Comments
 (0)