Adding some API tests

This commit is contained in:
Selim Mustafaev 2021-07-14 20:17:55 +03:00
parent 2ad43928db
commit d83fc71810
7 changed files with 89 additions and 32 deletions

View File

@ -35,6 +35,9 @@
7A40D60826998DCF009B0BC4 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60726998DCF009B0BC4 /* Alert.swift */; }; 7A40D60826998DCF009B0BC4 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60726998DCF009B0BC4 /* Alert.swift */; };
7A40D60926998DCF009B0BC4 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60726998DCF009B0BC4 /* Alert.swift */; }; 7A40D60926998DCF009B0BC4 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60726998DCF009B0BC4 /* Alert.swift */; };
7A40D60C2699A070009B0BC4 /* MockURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60B2699A070009B0BC4 /* MockURLProtocol.swift */; }; 7A40D60C2699A070009B0BC4 /* MockURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A40D60B2699A070009B0BC4 /* MockURLProtocol.swift */; };
7A503C03269F382F002C1A0D /* login_success.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A503C02269F382F002C1A0D /* login_success.json */; };
7A503C05269F494C002C1A0D /* login_invalid_params.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A503C04269F494C002C1A0D /* login_invalid_params.json */; };
7A503C07269F49F4002C1A0D /* login_wrong_credentials.json in Resources */ = {isa = PBXBuildFile; fileRef = 7A503C06269F49F4002C1A0D /* login_wrong_credentials.json */; };
7A683999269612EA00B2188A /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A683998269612EA00B2188A /* Response.swift */; }; 7A683999269612EA00B2188A /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A683998269612EA00B2188A /* Response.swift */; };
7A68399A269612EA00B2188A /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A683998269612EA00B2188A /* Response.swift */; }; 7A68399A269612EA00B2188A /* Response.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A683998269612EA00B2188A /* Response.swift */; };
7ACD05D72695C08A00557667 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ACD05D62695C08A00557667 /* Constants.swift */; }; 7ACD05D72695C08A00557667 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7ACD05D62695C08A00557667 /* Constants.swift */; };
@ -113,6 +116,9 @@
7A40D6012694FF5D009B0BC4 /* Api.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Api.swift; sourceTree = "<group>"; }; 7A40D6012694FF5D009B0BC4 /* Api.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Api.swift; sourceTree = "<group>"; };
7A40D60726998DCF009B0BC4 /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; }; 7A40D60726998DCF009B0BC4 /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = "<group>"; };
7A40D60B2699A070009B0BC4 /* MockURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockURLProtocol.swift; sourceTree = "<group>"; }; 7A40D60B2699A070009B0BC4 /* MockURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockURLProtocol.swift; sourceTree = "<group>"; };
7A503C02269F382F002C1A0D /* login_success.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = login_success.json; sourceTree = "<group>"; };
7A503C04269F494C002C1A0D /* login_invalid_params.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = login_invalid_params.json; sourceTree = "<group>"; };
7A503C06269F49F4002C1A0D /* login_wrong_credentials.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = login_wrong_credentials.json; sourceTree = "<group>"; };
7A683998269612EA00B2188A /* Response.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; }; 7A683998269612EA00B2188A /* Response.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Response.swift; sourceTree = "<group>"; };
7ACD05D62695C08A00557667 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; }; 7ACD05D62695C08A00557667 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
7AEFAEEC26985A3400ED2C85 /* ACProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ACProgressView.swift; sourceTree = "<group>"; }; 7AEFAEEC26985A3400ED2C85 /* ACProgressView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ACProgressView.swift; sourceTree = "<group>"; };
@ -260,6 +266,7 @@
7A40D5F42693A63A009B0BC4 /* AutoCat2Tests */ = { 7A40D5F42693A63A009B0BC4 /* AutoCat2Tests */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
7A503C00269F370A002C1A0D /* Responses */,
7A40D60A2699A04F009B0BC4 /* Mocks */, 7A40D60A2699A04F009B0BC4 /* Mocks */,
7A40D5F52693A63A009B0BC4 /* SettingsTests.swift */, 7A40D5F52693A63A009B0BC4 /* SettingsTests.swift */,
7AF552D82696E5C100578083 /* ApiTests.swift */, 7AF552D82696E5C100578083 /* ApiTests.swift */,
@ -293,6 +300,24 @@
path = Mocks; path = Mocks;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
7A503C00269F370A002C1A0D /* Responses */ = {
isa = PBXGroup;
children = (
7A503C01269F3797002C1A0D /* Login */,
);
path = Responses;
sourceTree = "<group>";
};
7A503C01269F3797002C1A0D /* Login */ = {
isa = PBXGroup;
children = (
7A503C06269F49F4002C1A0D /* login_wrong_credentials.json */,
7A503C04269F494C002C1A0D /* login_invalid_params.json */,
7A503C02269F382F002C1A0D /* login_success.json */,
);
path = Login;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
@ -473,6 +498,9 @@
isa = PBXResourcesBuildPhase; isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
7A503C07269F49F4002C1A0D /* login_wrong_credentials.json in Resources */,
7A503C03269F382F002C1A0D /* login_success.json in Resources */,
7A503C05269F494C002C1A0D /* login_invalid_params.json in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -16,8 +16,21 @@ class ApiTests: XCTestCase {
// Put teardown code here. This method is called after the invocation of each test method in the class. // Put teardown code here. This method is called after the invocation of each test method in the class.
} }
func testLogin() async throws { func testLoginSuccess() async throws {
MockURLProtocol.responseType = MockResponseType.loginSuccess
let user = try await self.api.login(email: "", password: "") let user = try await self.api.login(email: "", password: "")
XCTAssertTrue(!user.token.isEmpty)
}
func testLoginInvalidParams() async throws {
MockURLProtocol.responseType = MockResponseType.loginWrongCredentials
do {
_ = try await self.api.login(email: "", password: "")
} catch {
return
}
XCTFail("Exception expected")
} }
// func testPerformanceExample() throws { // func testPerformanceExample() throws {

View File

@ -1,7 +1,15 @@
import Foundation import Foundation
enum MockResponseType: String {
case loginSuccess = "login_success"
case loginWrongCredentials = "login_wrong_credentials"
case
}
class MockURLProtocol: URLProtocol { class MockURLProtocol: URLProtocol {
static var responseType: MockResponseType?
override class func canInit(with request: URLRequest) -> Bool { override class func canInit(with request: URLRequest) -> Bool {
// To check if this protocol can handle the given request. // To check if this protocol can handle the given request.
return true return true
@ -13,28 +21,22 @@ class MockURLProtocol: URLProtocol {
} }
override func startLoading() { override func startLoading() {
guard let handler = MockURLProtocol.requestHandler else {
fatalError("Handler is unavailable.")
}
do { guard let respType = MockURLProtocol.responseType else { return }
// 2. Call handler with received request and capture the tuple of response and data. guard let jsonUrl = Bundle(for: type(of: self)).url(forResource: respType.rawValue, withExtension: "json") else { return }
let (response, data) = try handler(request) guard let response = HTTPURLResponse(url: jsonUrl, statusCode: 200, httpVersion: "HTTP/2", headerFields: [:]) else { return }
let data = try? Data(contentsOf: jsonUrl)
// 3. Send received response to the client.
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
if let data = data { if let data = data {
// 4. Send received data to the client.
client?.urlProtocol(self, didLoad: data) client?.urlProtocol(self, didLoad: data)
} }
// 5. Notify request has been finished.
client?.urlProtocolDidFinishLoading(self) client?.urlProtocolDidFinishLoading(self)
} catch {
// 6. Notify received error. //client?.urlProtocol(self, didFailWithError: error)
client?.urlProtocol(self, didFailWithError: error)
}
} }
override func stopLoading() { override func stopLoading() {

View File

@ -0,0 +1,4 @@
{
"success": false,
"error": "Invalid parameters"
}

View File

@ -0,0 +1,8 @@
{
"success": true,
"data": {
"_id": "832c1bd4-5caa-4c9d-b24c-4c000cd8a793",
"email": "selim@fastmail.fm",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InNlbGltQGZhc3RtYWlsLmZtIiwiaWF0IjoxNjI2MjgwNDgxLCJleHAiOjE2NTc4MTY0ODF9.eU6wpacgCSnhM4EiyMY2lUptsfSfHz9guuvOsAw4X90"
}
}

View File

@ -0,0 +1,4 @@
{
"success": false,
"error": "Incorrect email or password"
}

View File

@ -26,9 +26,7 @@ struct AuthView: View {
} }
} }
} }
.alert(item: $alert) { id in .alert(item: $alert, content: Alert.init)
Alert(id)
}
Spacer() Spacer()
} }
.buttonStyle(.bordered) .buttonStyle(.bordered)