Skip to content

Commit

Permalink
Fix typo in Persistence's name
Browse files Browse the repository at this point in the history
  • Loading branch information
mohannad-hassan committed Jan 5, 2025
1 parent 8f80afa commit 4f7000a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import VLogging
final class AuthenticationClientImpl: AuthenticationClient {
// MARK: Lifecycle

init(configurations: OAuthAppConfiguration?, caller: OAuthCaller, persistance: Persistance) {
init(configurations: OAuthAppConfiguration?, caller: OAuthCaller, persistence: Persistence) {
self.caller = caller
self.persistance = persistance
self.persistence = persistence
appConfiguration = configurations
}

Expand All @@ -31,7 +31,7 @@ final class AuthenticationClientImpl: AuthenticationClient {

public func login(on viewController: UIViewController) async throws {
do {
try persistance.clear()
try persistence.clear()
logger.info("Cleared previous authentication state before login")
} catch {
// If persisting the new state works, this error should be of little concern.
Expand All @@ -54,7 +54,7 @@ final class AuthenticationClientImpl: AuthenticationClient {
logger.error("restoreState invoked without OAuth client configurations being set")
throw AuthenticationClientError.oauthClientHasNotBeenSet
}
guard let state = try persistance.retrieve() else {
guard let state = try persistence.retrieve() else {
logger.info("No previous authentication state found")
return false
}
Expand Down Expand Up @@ -84,7 +84,7 @@ final class AuthenticationClientImpl: AuthenticationClient {
// MARK: Private

private let caller: OAuthCaller
private let persistance: Persistance
private let persistence: Persistence

private var stateChangedCancellable: AnyCancellable?

Expand All @@ -101,7 +101,7 @@ final class AuthenticationClientImpl: AuthenticationClient {

private func persist(state: AuthenticationData) {
do {
try persistance.persist(state: state)
try persistence.persist(state: state)
} catch {
// If this happens, the state will not nullified so to keep the current session usable
// for the user. As for now, no workaround is in hand.
Expand All @@ -115,7 +115,7 @@ extension AuthenticationClientImpl {
self.init(
configurations: configurations,
caller: AppAuthCaller(),
persistance: KeychainPersistance()
persistence: KeychainPersistence()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Persistance.swift
// Persistence.swift
// QuranEngine
//
// Created by Mohannad Hassan on 28/12/2024.
Expand All @@ -8,21 +8,21 @@
import Foundation
import VLogging

enum PersistanceError: Error {
case persistanceFailed
enum PersistenceError: Error {
case persistenceFailed
case retrievalFailed
}

/// An abstraction for secure persistance of the authentication state.
protocol Persistance {
/// An abstraction for secure persistence of the authentication state.
protocol Persistence {
func persist(state: AuthenticationData) throws

func retrieve() throws -> AuthenticationData?

func clear() throws
}

final class KeychainPersistance: Persistance {
final class KeychainPersistence: Persistence {
private let itemKey = "com.quran.oauth.state"

func persist(state: AuthenticationData) throws {
Expand All @@ -35,7 +35,7 @@ final class KeychainPersistance: Persistance {
let status = SecItemAdd(addquery as CFDictionary, nil)
if status != errSecSuccess {
logger.error("Failed to persist state -- \(status) status")
throw PersistanceError.persistanceFailed
throw PersistenceError.persistenceFailed
}
logger.info("State persisted successfully")
}
Expand All @@ -54,14 +54,14 @@ final class KeychainPersistance: Persistance {
return nil
} else if status != errSecSuccess {
logger.error("Failed to retrieve state -- \(status) status")
throw PersistanceError.retrievalFailed
throw PersistenceError.retrievalFailed
}
guard let data = result as? Data else {
logger.error("Invalid data type found")
throw PersistanceError.retrievalFailed
throw PersistenceError.retrievalFailed
}

// Both AuthenticationData and Persistance are internal types to the package, so it's
// Both AuthenticationData and Persistence are internal types to the package, so it's
// good enough to hardcode the type here. No need for the hassle of the extra field.
let state = try JSONDecoder().decode(AppAuthAuthenticationData.self, from: data)
logger.info("AuthenticationData restored.")
Expand All @@ -77,7 +77,7 @@ final class KeychainPersistance: Persistance {
let status = SecItemDelete(query as CFDictionary)
if status != errSecSuccess && status != errSecItemNotFound {
logger.error("Failed to clear state -- \(status) status")
throw PersistanceError.persistanceFailed
throw PersistenceError.persistenceFailed
}
}
}
28 changes: 14 additions & 14 deletions Data/AuthenticationClient/Tests/AuthenticationClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class AuthenticationClientTests: XCTestCase {

override func setUp() {
caller = OAuthCallerMock()
persistance = PersistanceMock()
persistence = PersistenceMock()
}

func testNoConfigurations() async throws {
Expand All @@ -39,16 +39,16 @@ final class AuthenticationClientTests: XCTestCase {
func testLoginSuccessful() async throws {
sut.set(appConfiguration: configuration)

persistance.currentState = AutehenticationDataMock()
persistence.currentState = AutehenticationDataMock()

let state = AutehenticationDataMock()
state.accessToken = "abcd"
caller.loginResult = .success(state)

try await sut.login(on: UIViewController())

XCTAssertTrue(persistance.clearCalled, "Expected to clear the persistance first")
XCTAssertEqual((persistance.currentState as? AutehenticationDataMock), state, "Expected to update the new state")
XCTAssertTrue(persistence.clearCalled, "Expected to clear the persistence first")
XCTAssertEqual((persistence.currentState as? AutehenticationDataMock), state, "Expected to update the new state")
XCTAssertEqual(sut.authenticationState, .authenticated, "Expected the auth manager to be in authenticated state")
}

Expand All @@ -57,7 +57,7 @@ final class AuthenticationClientTests: XCTestCase {

let state = AutehenticationDataMock()
state.accessToken = "abcd"
persistance.currentState = state
persistence.currentState = state

let result = try await sut.restoreState()
XCTAssert(result, "Expected to be signed in successfully")
Expand All @@ -67,7 +67,7 @@ final class AuthenticationClientTests: XCTestCase {
func testRestorationButNotAuthenticated() async throws {
sut.set(appConfiguration: configuration)

persistance.currentState = nil
persistence.currentState = nil

let result = try await sut.restoreState()
XCTAssertFalse(result, "Expected to not be signed in")
Expand All @@ -79,7 +79,7 @@ final class AuthenticationClientTests: XCTestCase {

let state = AutehenticationDataMock()
state.accessToken = "abcd"
persistance.currentState = state
persistence.currentState = state

_ = try await sut.restoreState()
let inputRequest = URLRequest(url: URL(string: "https://example.com")!)
Expand All @@ -100,19 +100,19 @@ final class AuthenticationClientTests: XCTestCase {

let state = AutehenticationDataMock()
state.accessToken = "abcd"
persistance.currentState = state
persistence.currentState = state

_ = try await sut.restoreState()

// Clear the mock persistance for test's sake
persistance.currentState = nil
persistance.clearCalled = false
// Clear the mock persistence for test's sake
persistence.currentState = nil
persistence.clearCalled = false

// Change the state
state.accessToken = "xyz"

XCTAssertEqual(
(persistance.currentState as? AutehenticationDataMock)?.accessToken,
(persistence.currentState as? AutehenticationDataMock)?.accessToken,
"xyz",
"Expected to persist the refreshed state"
)
Expand All @@ -127,7 +127,7 @@ final class AuthenticationClientTests: XCTestCase {

private var sut: AuthenticationClientImpl!
private var caller: OAuthCallerMock!
private var persistance: PersistanceMock!
private var persistence: PersistenceMock!
}

private final class OAuthCallerMock: OAuthCaller {
Expand Down Expand Up @@ -181,7 +181,7 @@ private final class AutehenticationDataMock: Equatable, AuthenticationData {
}
}

private final class PersistanceMock: Persistance {
private final class PersistenceMock: Persistence {
var clearCalled = false
var currentState: AuthenticationData?

Expand Down

0 comments on commit 4f7000a

Please sign in to comment.