-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AccountService & OTPService tests
- Loading branch information
Showing
6 changed files
with
181 additions
and
5 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
106 changes: 106 additions & 0 deletions
106
OakOTPCommon/Tests/OakOTPCommonTests/AccountServiceTests.swift
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,106 @@ | ||
import XCTest | ||
import Resolver | ||
@testable import OakOTPCommon | ||
|
||
class InitalFetchSpy: AccountServiceDelegate { | ||
let expectedAccounts: [Account] | ||
|
||
var accountsChangedCalled = false | ||
|
||
init(expectedAccounts: [Account]) { | ||
self.expectedAccounts = expectedAccounts | ||
} | ||
|
||
func accountsChanged(accounts: [Account]) { | ||
accountsChangedCalled = true | ||
XCTAssertEqual(accounts.count, expectedAccounts.count) | ||
} | ||
} | ||
|
||
class FilterSpy: AccountServiceDelegate { | ||
var expectedAccounts: [Account] | ||
var hasFiltered = false | ||
|
||
init(expectedAccounts: [Account]) { | ||
self.expectedAccounts = expectedAccounts | ||
} | ||
|
||
func accountsChanged(accounts: [Account]) { | ||
XCTAssertEqual(accounts.count, expectedAccounts.count) | ||
|
||
if hasFiltered { | ||
XCTAssertEqual(accounts, expectedAccounts) | ||
} | ||
} | ||
} | ||
|
||
final class AccountServiceTests: OakOTPCommonTestCase { | ||
|
||
/** | ||
Scope for our mocked persistent store is shared | ||
so to keep the same instance between our test and our account service | ||
we need to keep a reference here | ||
*/ | ||
var persistent: PersistentStore! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
persistent = Resolver.resolve(PersistentStore.self, name: nil, args: nil) | ||
} | ||
|
||
override func tearDown() { | ||
super.tearDown() | ||
persistent = nil | ||
} | ||
|
||
private func createAccounts() throws -> [Account] { | ||
let persistent: PersistentStore = Resolver.resolve() | ||
let account1 = Account(context: persistent.viewContext) | ||
account1.issuer = "Google" | ||
account1.name = "john@doe.co.uk" | ||
account1.secret = "helloworld" | ||
account1.algorithm = .sha1 | ||
account1.type = .totp | ||
account1.createdAt = Date() | ||
account1.digits = Int16(6) | ||
account1.period = Int16(30) | ||
|
||
let account2 = Account(context: persistent.viewContext) | ||
account2.issuer = "Facebook" | ||
account2.name = "john@doe.co.uk" | ||
account2.secret = "helloworld123" | ||
account2.algorithm = .sha1 | ||
account2.type = .totp | ||
account2.createdAt = Date() | ||
account2.digits = Int16(6) | ||
account2.period = Int16(30) | ||
|
||
try persistent.save() | ||
|
||
return [account1, account2] | ||
} | ||
|
||
func testInitialFetch() throws { | ||
let createdAccounts = try createAccounts() | ||
let service = RealAccountService() | ||
|
||
let initialFetchSpy = InitalFetchSpy(expectedAccounts: createdAccounts) | ||
service.delegate = initialFetchSpy | ||
|
||
XCTAssertTrue(initialFetchSpy.accountsChangedCalled) | ||
} | ||
|
||
func testFilter() throws { | ||
let createdAccounts = try createAccounts() | ||
let service = RealAccountService() | ||
|
||
let filterSpy = FilterSpy(expectedAccounts: createdAccounts) | ||
service.delegate = filterSpy | ||
|
||
let predicate = NSPredicate(format: "name CONTAINS[cd] %@ OR issuer CONTAINS[cd] %@", "Facebook", "Facebook") | ||
filterSpy.expectedAccounts = [createdAccounts.last!] | ||
filterSpy.hasFiltered = true | ||
try service.filter(predicate: predicate) | ||
|
||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
OakOTPCommon/Tests/OakOTPCommonTests/OakOTPCommonTestCase.swift
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,12 @@ | ||
import XCTest | ||
import Resolver | ||
|
||
class OakOTPCommonTestCase: XCTestCase { | ||
override func setUp() { | ||
Resolver.resetUnitTestRegistrations() | ||
} | ||
|
||
override func tearDown() { | ||
Resolver.root = .test | ||
} | ||
} |
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,21 @@ | ||
// | ||
// Setup.swift | ||
// OakTests | ||
// | ||
// Created by Alex Catchpole on 16/05/2021. | ||
// | ||
|
||
import Foundation | ||
import OakOTPCommon | ||
import Resolver | ||
|
||
extension Resolver { | ||
static var test: Resolver! | ||
|
||
static func resetUnitTestRegistrations() { | ||
Resolver.test = Resolver(parent: .main) | ||
Resolver.root = .test | ||
|
||
Resolver.test.register { MockPersistentStore() as PersistentStore }.scope(.shared) | ||
} | ||
} |