Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Task/14355 add submission unit tests #4933

Merged
merged 10 commits into from
Dec 15, 2022
16 changes: 16 additions & 0 deletions src/xcode/ENA/ENA.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,8 @@
8F8B4A332934CD18007ED94E /* DMSRSOptionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F8B4A322934CD18007ED94E /* DMSRSOptionsViewController.swift */; };
8F8B4A352934CD30007ED94E /* DMSRSOptionsViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F8B4A342934CD30007ED94E /* DMSRSOptionsViewModel.swift */; };
8F95A1B225EFC5BE00506CF2 /* SendErrorLogsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F95A1B125EFC5BE00506CF2 /* SendErrorLogsViewController.swift */; };
8FA1E1032948463D00C08D82 /* SRSKeySubmissionResourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FA1E1022948463D00C08D82 /* SRSKeySubmissionResourceTests.swift */; };
8FA1E10729484EC600C08D82 /* OTPAuthorizationForSRSResourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FA1E10429484DE800C08D82 /* OTPAuthorizationForSRSResourceTests.swift */; };
8FA3E9C62913F334000512FC /* SRSConsentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FA3E9C52913F334000512FC /* SRSConsentViewController.swift */; };
8FA3E9C82913F34A000512FC /* SRSConsentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FA3E9C72913F34A000512FC /* SRSConsentViewModel.swift */; };
8FA88F672613325700FD36DA /* QRCodePayload+extention.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FA88F662613325700FD36DA /* QRCodePayload+extention.swift */; };
Expand Down Expand Up @@ -2942,6 +2944,8 @@
8F8B4A342934CD30007ED94E /* DMSRSOptionsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DMSRSOptionsViewModel.swift; sourceTree = "<group>"; };
8F8B4A362934DA10007ED94E /* ENATest.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ENATest.entitlements; sourceTree = "<group>"; };
8F95A1B125EFC5BE00506CF2 /* SendErrorLogsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendErrorLogsViewController.swift; sourceTree = "<group>"; };
8FA1E1022948463D00C08D82 /* SRSKeySubmissionResourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SRSKeySubmissionResourceTests.swift; sourceTree = "<group>"; };
8FA1E10429484DE800C08D82 /* OTPAuthorizationForSRSResourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OTPAuthorizationForSRSResourceTests.swift; sourceTree = "<group>"; };
8FA3E9C52913F334000512FC /* SRSConsentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SRSConsentViewController.swift; sourceTree = "<group>"; };
8FA3E9C72913F34A000512FC /* SRSConsentViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SRSConsentViewModel.swift; sourceTree = "<group>"; };
8FA88F662613325700FD36DA /* QRCodePayload+extention.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "QRCodePayload+extention.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -7388,6 +7392,7 @@
8F2FA2E3292B2D5C00C6286A /* OTPAuthorizationForSRS */ = {
isa = PBXGroup;
children = (
8FA1E10629484E1500C08D82 /* __tests__ */,
8F2FA2E4292B2D8400C6286A /* OTPAuthorizationForSRSResource.swift */,
DC29CBA929376D6800BEAA79 /* OTPForSRSResponsePropertiesReceiveModel.swift */,
);
Expand Down Expand Up @@ -7574,6 +7579,14 @@
path = DMSRSPrechecks;
sourceTree = "<group>";
};
8FA1E10629484E1500C08D82 /* __tests__ */ = {
isa = PBXGroup;
children = (
8FA1E10429484DE800C08D82 /* OTPAuthorizationForSRSResourceTests.swift */,
);
path = __tests__;
sourceTree = "<group>";
};
8FA3E9C42913F2E8000512FC /* SRSConsent */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -8703,6 +8716,7 @@
isa = PBXGroup;
children = (
ABFDBEFC281AD7C000A11FA8 /* KeySubmissionResourceTests.swift */,
8FA1E1022948463D00C08D82 /* SRSKeySubmissionResourceTests.swift */,
);
path = __tests__;
sourceTree = "<group>";
Expand Down Expand Up @@ -12695,6 +12709,7 @@
AB34787C269C93C20016A8AB /* CertLogicEngineTestData.swift in Sources */,
010A57A427185E4D001F5A09 /* RecycleBinItemCellModelTests.swift in Sources */,
50681F8B27E3C83C008056E3 /* RetryingTests.swift in Sources */,
8FA1E10729484EC600C08D82 /* OTPAuthorizationForSRSResourceTests.swift in Sources */,
BA015159279B11A900C93B02 /* RestServiceProviderTests.swift in Sources */,
506D881427A2D40D006297FE /* RegistrationTokenResourceTests.swift in Sources */,
35BDAB2E25F1148E004DFE32 /* ELSServiceTests.swift in Sources */,
Expand Down Expand Up @@ -12914,6 +12929,7 @@
BADDD83F2603956600337223 /* TraceLocationCellModelTests.swift in Sources */,
01D6816327C3D18200FF6D18 /* MockCoronaTestService.swift in Sources */,
71176E2F248922B0004B0C9F /* ENAColorTests.swift in Sources */,
8FA1E1032948463D00C08D82 /* SRSKeySubmissionResourceTests.swift in Sources */,
35C0C88B2614946F004BF509 /* CheckinSubmissionPreparationTests.swift in Sources */,
014086B82588F9FD00E9E5B2 /* DiaryEditEntriesViewModelTest.swift in Sources */,
01BA06D3261F41E100237DD8 /* ExposureSubmissionServiceTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
//
// 🦠 Corona-Warn-App
//

import Foundation
import XCTest
@testable import ENA

class SRSKeySubmissionResourceTests: XCTestCase {

let mockUrl = URL(staticString: "http://example.com")
let tan = "1234"

private var keys: [SAP_External_Exposurenotification_TemporaryExposureKey] {
var key = SAP_External_Exposurenotification_TemporaryExposureKey()
key.keyData = Data(bytes: [1, 2, 3], count: 3)
key.rollingPeriod = 1337
key.rollingStartIntervalNumber = 42
key.transmissionRiskLevel = 8

return [key]
}

func testSubmit_Success() {
let stack = MockNetworkStack(
httpStatus: 200,
// cannot be nil since this is not a a completion handler can be in (response + nil body)
responseData: Data()
)

let expectation = self.expectation(description: "completion handler is called without an error")

// Act
let payload = SubmissionPayload(
exposureKeys: keys,
visitedCountries: [],
checkins: [],
checkinProtectedReports: [],
tan: nil,
submissionType: .srsSelfTest
)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { result in
switch result {
case .success:
break
case let .failure(error):
XCTFail("Test should not fail with error: \(error)")
}
expectation.fulfill()
}

// THEN
waitForExpectations(timeout: .short)
}

func testSubmit_Request_SubmissionType() throws {
let payload = SubmissionPayload(exposureKeys: keys, visitedCountries: [], checkins: [], checkinProtectedReports: [], tan: nil, submissionType: .srsSelfTest)

let expectation = self.expectation(description: "completion handler is called without an error")

let stack = MockNetworkStack(
httpStatus: 200,
// cannot be nil since this is not a a completion handler can be in (response + nil body)
responseData: Data(),
requestObserver: { request in
print(request)

guard let protoPayload = try? SAP_Internal_SubmissionPayload(serializedData: request.httpBody ?? Data()) else {
XCTFail("Request data expected to be serializable to protobuf.")
return
}

XCTAssertEqual(protoPayload.submissionType, payload.submissionType)

expectation.fulfill()
}
)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { _ in }

// THEN
waitForExpectations(timeout: .short)
}

func testSubmit_SpecificError() {
let stack = MockNetworkStack(
mockSession: MockUrlSession(
data: nil,
nextResponse: nil,
error: TestError.error
)
)
let expectation = self.expectation(description: "SpecificError")

let payload = SubmissionPayload(exposureKeys: keys, visitedCountries: [], checkins: [], checkinProtectedReports: [], tan: nil, submissionType: .srsSelfTest)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { result in
switch result {
case .success:
XCTFail("expected an error")
case let .failure(error):
switch error {
case ServiceError.transportationError(let underLyingError):
XCTAssertNotNil(underLyingError)
default:
XCTFail("We expect error to be of type other")
}
}
expectation.fulfill()
}

waitForExpectations(timeout: .short)
}

func testSubmit_ResponseNil() {
let mockURLSession = MockUrlSession(data: nil, nextResponse: nil, error: nil)
let stack = MockNetworkStack(
mockSession: mockURLSession
)
let expectation = self.expectation(description: "ResponseNil")

let payload = SubmissionPayload(exposureKeys: keys, visitedCountries: [], checkins: [], checkinProtectedReports: [], tan: nil, submissionType: .srsSelfTest)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { result in
switch result {
case .success:
XCTFail("expected an error")
case let .failure(error):
switch error {
case .invalidResponseType:
break
default:
XCTFail("We expect error to be of type other")
}
}
expectation.fulfill()
}

waitForExpectations(timeout: .short)
}

func testSubmit_Response400() {
let stack = MockNetworkStack(
httpStatus: 400,
responseData: Data()
)

let expectation = self.expectation(description: "Response400")

let payload = SubmissionPayload(exposureKeys: keys, visitedCountries: [], checkins: [], checkinProtectedReports: [], tan: nil, submissionType: .srsSelfTest)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { result in
switch result {
case .success:
XCTFail("error expected")
case let .failure(error):
guard case ServiceError.receivedResourceError(.invalidPayloadOrHeader) = error else {
XCTFail("We expect error to be of type invalidPayloadOrHeaders")
return
}
}
expectation.fulfill()
}

waitForExpectations(timeout: .short)
}

func testSubmit_Response403() {
let stack = MockNetworkStack(
httpStatus: 403,
responseData: Data()
)

let expectation = self.expectation(description: "Response403")

let payload = SubmissionPayload(exposureKeys: keys, visitedCountries: [], checkins: [], checkinProtectedReports: [], tan: nil, submissionType: .srsSelfTest)

let restServiceProvider = RestServiceProvider(session: stack.urlSession, cache: KeyValueCacheFake())

let resource = SRSKeySubmissionResource(payload: payload, srsOtp: "Test")

restServiceProvider.load(resource) { result in
switch result {
case .success:
XCTFail("error expected")
case let .failure(error):
guard case ServiceError.receivedResourceError(.invalidOtp) = error else {
XCTFail("We expect error to be of type invalidPayloadOrHeaders")
return
}
}
expectation.fulfill()
}

waitForExpectations(timeout: .short)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct OTPAuthorizationForSRSResource: Resource {
return .otherServerError
}
default:
return .otherServerError
return .invalidResponseError
}
}

Expand All @@ -91,9 +91,8 @@ struct OTPAuthorizationForSRSResource: Resource {

do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let decodedResponse = try decoder.decode(
OTPResponseProperties.self,
OTPForSRSResponsePropertiesReceiveModel.self,
from: responseBody
)
guard let errorCode = decodedResponse.errorCode else {
Expand Down Expand Up @@ -121,7 +120,7 @@ struct OTPAuthorizationForSRSResource: Resource {
}
} catch {
Log.error("Failed to get errorCode because json could not be decoded", log: .api, error: error)
return .otherServerError
return .invalidResponseError
}
}
}
Loading