Skip to content

Commit

Permalink
Working on unit tests and overall improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
stzouvaras committed Dec 2, 2024
1 parent 6b9b4da commit b5da391
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 83 deletions.
36 changes: 36 additions & 0 deletions Sources/Domain/Entities/SessionData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/
import RqesKit

@Copyable
struct SessionData {
let document: DocumentData?
let qtsp: QTSPData?
let certificate: CredentialInfo?
let code: String?

init(
document: DocumentData? = nil,
qtsp: QTSPData? = nil,
certificate: CredentialInfo? = nil,
code: String? = nil
) {
self.document = document
self.qtsp = qtsp
self.certificate = certificate
self.code = code
}
}
23 changes: 10 additions & 13 deletions Sources/Domain/Interactor/RQESInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import RqesKit

protocol RQESInteractor: Sendable {
func signDocument() async throws -> Document?
func getCurrentSelection() async -> CurrentSelection?
func getSession() async -> SessionData?
func getQTSps() async -> [QTSPData]
func fetchCredentials() async throws -> Result<[CredentialInfo], any Error>
func updateQTSP(_ qtsp: QTSPData) async
Expand All @@ -40,7 +40,7 @@ final class RQESInteractorImpl: RQESInteractor {
func createRQESService(_ qtsp: QTSPData) async throws {
let rQESConfig = await rqesUi.getRQESConfig()
guard
let fileExtension = await getCurrentSelection()?.document?.uri.pathExtension
let fileExtension = await getSession()?.document?.uri.pathExtension
else {
throw EudiRQESUiError.noDocumentProvided
}
Expand All @@ -61,30 +61,27 @@ final class RQESInteractorImpl: RQESInteractor {
}

func signDocument() async throws -> Document? {
let authorizationCode = await self.rqesUi.selection.code
let authorizationCode = await self.getSession()?.code
let rQESServiceAuthorized = await self.rqesUi.getRQESServiceAuthorized()
if let authorizationCode,
let rQESServiceAuthorized {

if let authorizationCode, let rQESServiceAuthorized {
let authorizedCredential = try await rQESServiceAuthorized.authorizeCredential(authorizationCode: authorizationCode)
let signedDocuments = try await authorizedCredential.signDocuments()

return signedDocuments.first
} else {
throw EudiRQESUiError.unableToSignHashDocument
}
}

func getCurrentSelection() async -> CurrentSelection? {
await self.rqesUi.selection
func getSession() async -> SessionData? {
await self.rqesUi.getSessionData()
}

func updateQTSP(_ qtsp: QTSPData) async {
await self.rqesUi.updateQTSP(with: qtsp)
}

func updateDocument(_ url: URL) async {
let name = await self.rqesUi.selection.document?.documentName
let name = await self.getSession()?.document?.documentName
if let name {
let document = DocumentData(documentName: name, uri: url)
await self.rqesUi.updateSelectionDocument(with: document)
Expand All @@ -105,8 +102,8 @@ final class RQESInteractorImpl: RQESInteractor {
}

func openCredentialAuthrorizationURL() async throws -> URL {
if let uri = await self.rqesUi.selection.document?.uri,
let certificate = await self.rqesUi.selection.certificate {
if let uri = await self.getSession()?.document?.uri,
let certificate = await self.getSession()?.certificate {
let unsignedDocuments = [
Document(
id: UUID().uuidString,
Expand All @@ -131,7 +128,7 @@ final class RQESInteractorImpl: RQESInteractor {

func fetchCredentials() async throws -> Result<[CredentialInfo], any Error> {
if let rqesService = await self.rqesUi.getRQESService(),
let authorizationCode = await self.rqesUi.selection.code {
let authorizationCode = await self.getSession()?.code {
do {
let rQESServiceAuthorized = try await rqesService.authorizeService(authorizationCode: authorizationCode)
await self.rqesUi.setRQESServiceAuthorized(rQESServiceAuthorized)
Expand Down
87 changes: 44 additions & 43 deletions Sources/Infrastructure/EudiRQESUi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@
import UIKit
import RqesKit

@Copyable
struct CurrentSelection {
let document: DocumentData?
let qtsp: QTSPData?
let certificate: CredentialInfo?
let code: String?

init(
document: DocumentData? = nil,
qtsp: QTSPData? = nil,
certificate: CredentialInfo? = nil,
code: String? = nil
) {
self.document = document
self.qtsp = qtsp
self.certificate = certificate
self.code = code
}
}

public final actor EudiRQESUi {

private static var _shared: EudiRQESUi?
Expand All @@ -44,7 +24,7 @@ public final actor EudiRQESUi {
private static var _viewController: UIViewController?

private let router: any RouterGraph
var selection = CurrentSelection()
private var session = SessionData()

private static var _rqesService: RQESService?
private static var _rQESServiceAuthorized: RQESServiceAuthorized?
Expand All @@ -56,6 +36,24 @@ public final actor EudiRQESUi {
Self._config = config
Self._shared = self
}

init(
config: any EudiRQESUiConfig,
router: any RouterGraph,
state: State = .none,
selection: SessionData = .init(),
rqesService: RQESService? = nil,
rQESServiceAuthorized: RQESServiceAuthorized? = nil
) {
DIGraph.shared.load()
self.router = router
self.session = selection
Self._config = config
Self._state = state
Self._shared = self
Self._rqesService = rqesService
Self._rQESServiceAuthorized = rQESServiceAuthorized
}

@MainActor
public func initiate(
Expand Down Expand Up @@ -92,22 +90,6 @@ public final actor EudiRQESUi {

try await launcSDK(on: container, animated: animated)
}

func updateSelectionDocument(with document: DocumentData? = nil) async {
selection = selection.copy(document: document)
}

func updateQTSP(with qtsp: QTSPData? = nil) async {
selection = selection.copy(qtsp: qtsp)
}

func updateCertificate(with certificate: CredentialInfo) async {
selection = selection.copy(certificate: certificate)
}

public func updateAuthorizationCode(with code: String) async {
selection = selection.copy(code: code)
}
}

public extension EudiRQESUi {
Expand All @@ -122,10 +104,6 @@ public extension EudiRQESUi {

private extension EudiRQESUi {

func getState() -> State {
return Self._state
}

func getViewController() -> UIViewController? {
return Self._viewController
}
Expand Down Expand Up @@ -164,15 +142,18 @@ private extension EudiRQESUi {
}

func resetCache() async {
selection = CurrentSelection()
session = SessionData()
setRQESService(nil)
setRQESServiceAuthorized(nil)
}

func updateAuthorizationCode(with code: String) async {
session = session.copy(code: code)
}
}

extension EudiRQESUi {

static func forceConfig() -> any EudiRQESUiConfig {
return Self._config!
}
Expand All @@ -181,6 +162,26 @@ extension EudiRQESUi {
return Self._shared!
}

func updateSelectionDocument(with document: DocumentData? = nil) async {
session = session.copy(document: document)
}

func updateQTSP(with qtsp: QTSPData? = nil) async {
session = session.copy(qtsp: qtsp)
}

func updateCertificate(with certificate: CredentialInfo) async {
session = session.copy(certificate: certificate)
}

func getSessionData() -> SessionData {
return self.session
}

func getState() -> State {
return Self._state
}

func getRQESService() -> RQESService? {
Self._rqesService
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class CredentialSelectionViewModel<Router: RouterGraph>: ViewModel<Router,
}

private func getDocument() async throws {
let documentName = await interactor.getCurrentSelection()?.document?.documentName
let documentName = await interactor.getSession()?.document?.documentName

if let documentName {
setState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DocumentSelectionViewModel<Router: RouterGraph>: ViewModel<Router, Documen
}

func initiate() async {
let documentName = await interactor.getCurrentSelection()?.document?.documentName
let documentName = await interactor.getSession()?.document?.documentName

if let documentName {
setState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DocumentViewModel<Router: RouterGraph>: ViewModel<Router, DocumentState> {
}

func initiate() async {
let uri = await interactor.getCurrentSelection()?.document?.uri
let uri = await interactor.getSession()?.document?.uri
if let uri {
let source = DocumentSource.pdfUrl(uri)
loadDocument(from: source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SignedDocumentViewModel<Router: RouterGraph>: ViewModel<Router, SignedDocu

do {
let signedDocument = try await interactor.signDocument()
let selection = await interactor.getCurrentSelection()
let selection = await interactor.getSession()

pdfURL = signedDocument?.fileURL
if let fileURL = signedDocument?.fileURL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import XCTest
@testable import EudiRQESUi

final class eudi_lib_ios_rqes_uiTests: XCTestCase {
func testExample() throws {
// XCTest Documentation
// https://developer.apple.com/documentation/xctest

// Defining Test Cases and Test Methods
// https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
}
final class TestLocalizationController: XCTestCase {

override func setUp() {
}
override func tearDown() {
}

}
27 changes: 27 additions & 0 deletions Tests/Domain/Interactor/TestRQESInteractor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/
import XCTest
@testable import EudiRQESUi

final class TestRQESInteractor: XCTestCase {

override func setUp() {
}

override func tearDown() {
}

}
19 changes: 19 additions & 0 deletions Tests/Extension/EudiRQESUi.State+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 European Commission
*
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
* Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work
* except in compliance with the Licence.
*
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/software/page/eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the Licence for the specific language
* governing permissions and limitations under the Licence.
*/
@testable import EudiRQESUi
import Cuckoo

extension EudiRQESUi.State: Matchable {}
Loading

0 comments on commit b5da391

Please sign in to comment.