-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
640feaf
commit 7992792
Showing
19 changed files
with
573 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import DeltaDNA | ||
|
||
class DDNAConsentTrackerTests: XCTestCase { | ||
var consentTracker: DDNAConsentTracker! | ||
var userDefaults: UserDefaults! | ||
var geoIPMock: GeoIpNetworkClientMock! | ||
|
||
override func setUp() { | ||
let userDefaultDomain = "com.deltadna.test.consenttracker" | ||
UserDefaults().removePersistentDomain(forName: userDefaultDomain) | ||
userDefaults = UserDefaults(suiteName: userDefaultDomain) | ||
|
||
geoIPMock = GeoIpNetworkClientMock() | ||
|
||
consentTracker = DDNAConsentTracker(userDefaults: userDefaults, geoIpNetworkClient: geoIPMock) | ||
} | ||
|
||
// MARK: isPiplConsentRequired | ||
|
||
func test_whenCheckingIfConsentIsRequired_noPreviousResponse_andGeoIpReturnsResponseNotInChina_consentIsNotRequired() { | ||
let expectationToCheck = expectation(description: "Waiting for callback to complete") | ||
|
||
geoIPMock.responseToReturn = GeoIpResponse(identifier: "gdpr", country: "uk", region: "scotland", ageGateLimit: 13) | ||
|
||
consentTracker.isPiplConsentRequired(callback: { isRequired, error in | ||
XCTAssertNil(error) | ||
XCTAssertFalse(isRequired) | ||
expectationToCheck.fulfill() | ||
}) | ||
|
||
waitForExpectations(timeout: 3, handler: { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
}) | ||
} | ||
|
||
func test_whenCheckingIfConsentIsRequired_noPreviousResponse_andGeoIpReturnsResponseInChina_consentIsRequired() { | ||
let expectationToCheck = expectation(description: "Waiting for callback to complete") | ||
geoIPMock.responseToReturn = GeoIpResponse(identifier: "pipl", country: "cn", region: "beijing", ageGateLimit: 13) | ||
|
||
consentTracker.isPiplConsentRequired(callback: { isRequired, error in | ||
XCTAssertNil(error) | ||
XCTAssertTrue(isRequired) | ||
expectationToCheck.fulfill() | ||
}) | ||
|
||
waitForExpectations(timeout: 3, handler: { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
}) | ||
} | ||
|
||
func test_whenCheckingIfConsentIsRequired_andNotBothPreviousResponseIsRecorded_andGeoIpReturnsResponseInChina_consentCheckIsRequired() { | ||
let expectationToCheck = expectation(description: "Waiting for callback to complete") | ||
geoIPMock.responseToReturn = GeoIpResponse(identifier: "pipl", country: "cn", region: "beijing", ageGateLimit: 13) | ||
userDefaults.set(ConsentStatus.consentGiven.rawValue, forKey: "ddnaPiplUseStatus") | ||
|
||
consentTracker.isPiplConsentRequired(callback: { isRequired, error in | ||
XCTAssertNil(error) | ||
XCTAssertTrue(isRequired) | ||
expectationToCheck.fulfill() | ||
}) | ||
|
||
waitForExpectations(timeout: 3, handler: { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
}) | ||
} | ||
|
||
func test_whenCheckingIfConsentIsRequired_andBothPreviousResponseIsRecorded_consentCheckIsNotRequired() { | ||
let expectationToCheck = expectation(description: "Waiting for callback to complete") | ||
geoIPMock.responseToReturn = GeoIpResponse(identifier: "pipl", country: "cn", region: "beijing", ageGateLimit: 13) | ||
userDefaults.set(ConsentStatus.consentGiven.rawValue, forKey: "ddnaPiplUseStatus") | ||
userDefaults.set(ConsentStatus.consentGiven.rawValue, forKey: "ddnaPiplExportStatus") | ||
|
||
consentTracker.isPiplConsentRequired(callback: { isRequired, error in | ||
XCTAssertNil(error) | ||
XCTAssertTrue(isRequired) | ||
expectationToCheck.fulfill() | ||
}) | ||
|
||
waitForExpectations(timeout: 3, handler: { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
}) | ||
} | ||
|
||
// MARK: hasCheckedForConsent | ||
|
||
func test_whenCheckingIfConsentHasBeenChecked_andNoConsentsAreChecked_returnsFalse() { | ||
consentTracker.piplUseStatus = .unknown | ||
consentTracker.piplExportStatus = .unknown | ||
|
||
XCTAssertFalse(consentTracker.hasCheckedForConsent()) | ||
} | ||
|
||
func test_whenCheckingIfConsentHasBeenChecked_andExportIsNotChecked_returnsFalse() { | ||
consentTracker.piplUseStatus = .notRequired | ||
consentTracker.piplExportStatus = .unknown | ||
|
||
XCTAssertFalse(consentTracker.hasCheckedForConsent()) | ||
} | ||
|
||
func test_whenCheckingIfConsentHasBeenChecked_andStatusIsNotChecked_returnsFalse() { | ||
consentTracker.piplUseStatus = .unknown | ||
consentTracker.piplExportStatus = .notRequired | ||
|
||
XCTAssertFalse(consentTracker.hasCheckedForConsent()) | ||
} | ||
|
||
func test_whenCheckingIfConsentHasBeenChecked_andBothHaveBeenChecked_returnsTrue() { | ||
consentTracker.piplUseStatus = .consentGiven | ||
consentTracker.piplExportStatus = .consentDenied | ||
|
||
XCTAssertTrue(consentTracker.hasCheckedForConsent()) | ||
} | ||
|
||
// MARK: setPiplUseConsent | ||
|
||
func test_whenSettingUserConsent_andConsentIsProvided_theCorrectConsentIsSaved() { | ||
consentTracker.setPiplUseConsent(true) | ||
|
||
XCTAssertEqual(ConsentStatus.consentGiven.rawValue, userDefaults.integer(forKey: "ddnaPiplUseStatus")) | ||
XCTAssertEqual(ConsentStatus.consentGiven, consentTracker.piplUseStatus) | ||
} | ||
|
||
func test_whenSettingUserConsent_andConsentIsNotProvided_theCorrectConsentIsSaved() { | ||
consentTracker.setPiplUseConsent(false) | ||
|
||
XCTAssertEqual(ConsentStatus.consentDenied.rawValue, userDefaults.integer(forKey: "ddnaPiplUseStatus")) | ||
XCTAssertEqual(ConsentStatus.consentDenied, consentTracker.piplUseStatus) | ||
} | ||
|
||
// MARK: setPiplExportConsent | ||
|
||
func test_whenSettingUserExportConsent_andConsentIsProvided_theCorrectConsentIsSaved() { | ||
consentTracker.setPiplExportConsent(true) | ||
|
||
XCTAssertEqual(ConsentStatus.consentGiven.rawValue, userDefaults.integer(forKey: "ddnaPiplExportStatus")) | ||
XCTAssertEqual(ConsentStatus.consentGiven, consentTracker.piplExportStatus) | ||
} | ||
|
||
func test_whenSettingUserExportConsent_andConsentIsNotProvided_theCorrectConsentIsSaved() { | ||
consentTracker.setPiplExportConsent(false) | ||
|
||
XCTAssertEqual(ConsentStatus.consentDenied.rawValue, userDefaults.integer(forKey: "ddnaPiplExportStatus")) | ||
XCTAssertEqual(ConsentStatus.consentDenied, consentTracker.piplExportStatus) | ||
} | ||
|
||
// MARK: allConsentsAreMet | ||
|
||
func test_whenCheckingAllConsentsAreMet_IfNoneAreMet_returnsFalse() { | ||
consentTracker.piplExportStatus = .consentDenied | ||
consentTracker.piplUseStatus = .unknown | ||
|
||
XCTAssertFalse(consentTracker.allConsentsAreMet()) | ||
} | ||
|
||
func test_whenCheckingAllConsentsAreMet_IfSomeAreMet_returnsFalse() { | ||
consentTracker.piplExportStatus = .consentGiven | ||
consentTracker.piplUseStatus = .unknown | ||
|
||
XCTAssertFalse(consentTracker.allConsentsAreMet()) | ||
} | ||
|
||
func test_whenCheckingAllConsentsAreMet_IfAllAreMet_returnsTrue() { | ||
consentTracker.piplExportStatus = .consentGiven | ||
consentTracker.piplUseStatus = .consentGiven | ||
|
||
XCTAssertTrue(consentTracker.allConsentsAreMet()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import Foundation | ||
import XCTest | ||
@testable import DeltaDNA | ||
|
||
class URLSessionDataTaskMock: URLSessionDataTask { | ||
override func resume() { | ||
// Do nothing | ||
} | ||
} | ||
|
||
class URLSessionWithDataTaskMock: URLSessionDataTaskProtocol { | ||
var dataToReturn: Data? | ||
var error: Error? | ||
|
||
func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask { | ||
completionHandler(dataToReturn, nil, error) | ||
return URLSessionDataTaskMock() // Deprecated, but doesn't really matter as we don't use this return value | ||
} | ||
} | ||
|
||
class GeoIpNetworkClientTests: XCTestCase { | ||
var geoIpClient: GeoIpNetworkClient! | ||
var urlSessionMock: URLSessionWithDataTaskMock! | ||
|
||
override func setUp() { | ||
urlSessionMock = URLSessionWithDataTaskMock() | ||
geoIpClient = GeoIpNetworkClient(urlSession: urlSessionMock) | ||
} | ||
|
||
func test_ifErrorIsReturned_correctCallbackParamsAreReturned() { | ||
let expectationToCheck = expectation(description: "Ensure callback is called") | ||
|
||
urlSessionMock.error = URLError(.notConnectedToInternet) | ||
|
||
geoIpClient.fetchGeoIpResponse {(response, error) in | ||
XCTAssertNil(response) | ||
XCTAssertEqual((error as? URLError)?.code, URLError.Code.notConnectedToInternet) | ||
expectationToCheck.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 3) { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
} | ||
} | ||
|
||
func test_ifResponseIsReturned_correctCallbackParamsAreReturned() { | ||
let expectationToCheck = expectation(description: "Ensure callback is called") | ||
|
||
let responseObject = GeoIpResponse(identifier: "pipl", country: "cn", region: "beijing", ageGateLimit: 13) | ||
urlSessionMock.dataToReturn = try! JSONEncoder().encode(responseObject) | ||
|
||
geoIpClient.fetchGeoIpResponse {(response, error) in | ||
XCTAssertEqual(responseObject.identifier, response?.identifier) | ||
XCTAssertNil(error) | ||
expectationToCheck.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 3) { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
} | ||
} | ||
|
||
func test_ifInvalidResponseIsReturned_correctCallbackParamsAreReturned() { | ||
let expectationToCheck = expectation(description: "Ensure callback is called") | ||
|
||
let responseObject = ["notAValidObject": 1234] | ||
urlSessionMock.dataToReturn = try! JSONEncoder().encode(responseObject) | ||
|
||
geoIpClient.fetchGeoIpResponse {(response, error) in | ||
XCTAssertNil(response) | ||
XCTAssertNotNil(error) | ||
expectationToCheck.fulfill() | ||
} | ||
|
||
waitForExpectations(timeout: 3) { error in | ||
if let error = error { | ||
XCTFail("\(error)") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.