Test PresentationUseCase
This commit is contained in:
Родитель
9faae5cdd6
Коммит
20614781ca
|
@ -10,7 +10,7 @@ enum PresentationResponseError: Error {
|
|||
public struct PresentationResponseContainer {
|
||||
let request: PresentationRequest
|
||||
let expiryInSeconds: Int
|
||||
public let audience: String?
|
||||
public let audience: String
|
||||
public var requestedIdTokenMap: RequestedIdTokenMap = [:]
|
||||
public var requestedSelfAttestedClaimMap: RequestedSelfAttestedClaimMap = [:]
|
||||
public var requestVCMap: RequestedVerifiableCredentialMap = [:]
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -9,7 +9,10 @@ import VCRepository
|
|||
import VCEntities
|
||||
|
||||
enum PresentationUseCaseError: Error {
|
||||
case notImplemented
|
||||
case inputStringNotUri
|
||||
case noQueryParametersOnUri
|
||||
case noValueForRequestUriQueryParameter
|
||||
case noRequestUriQueryParameter
|
||||
}
|
||||
|
||||
public class PresentationUseCase {
|
||||
|
@ -31,7 +34,7 @@ public class PresentationUseCase {
|
|||
|
||||
public func getRequest(usingUrl urlStr: String) -> Promise<PresentationRequest> {
|
||||
return firstly {
|
||||
self.getRequestUri(from: urlStr)
|
||||
self.getRequestUriPromise(from: urlStr)
|
||||
}.then { requestUri in
|
||||
self.repo.getRequest(withUrl: requestUri)
|
||||
}
|
||||
|
@ -41,28 +44,35 @@ public class PresentationUseCase {
|
|||
return firstly {
|
||||
self.formatPresentationResponse(response: response, identifier: identifier)
|
||||
}.then { signedToken in
|
||||
self.repo.sendResponse(usingUrl: response.audience!, withBody: signedToken)
|
||||
self.repo.sendResponse(usingUrl: response.audience, withBody: signedToken)
|
||||
}
|
||||
}
|
||||
|
||||
private func getRequestUri(from urlStr: String) -> Promise<String> {
|
||||
private func getRequestUriPromise(from urlStr: String) -> Promise<String> {
|
||||
return Promise { seal in
|
||||
|
||||
guard let urlComponents = URLComponents(string: urlStr) else { return seal.reject(PresentationUseCaseError.notImplemented) }
|
||||
|
||||
guard let queryItems = urlComponents.percentEncodedQueryItems else { return seal.reject(PresentationUseCaseError.notImplemented) }
|
||||
|
||||
for queryItem in queryItems {
|
||||
if queryItem.name == "request_uri" {
|
||||
guard let value = queryItem.value else { return seal.reject(PresentationUseCaseError.notImplemented) }
|
||||
return seal.fulfill(value)
|
||||
}
|
||||
do {
|
||||
seal.fulfill(try self.getRequestUri(from: urlStr))
|
||||
} catch {
|
||||
seal.reject(error)
|
||||
}
|
||||
|
||||
return seal.reject(PresentationUseCaseError.notImplemented)
|
||||
}
|
||||
}
|
||||
|
||||
private func getRequestUri(from urlStr: String) throws -> String {
|
||||
|
||||
guard let urlComponents = URLComponents(string: urlStr) else { throw PresentationUseCaseError.inputStringNotUri }
|
||||
guard let queryItems = urlComponents.percentEncodedQueryItems else { throw PresentationUseCaseError.noQueryParametersOnUri }
|
||||
|
||||
for queryItem in queryItems {
|
||||
if queryItem.name == "request_uri" {
|
||||
guard let value = queryItem.value else { throw PresentationUseCaseError.noValueForRequestUriQueryParameter }
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
throw PresentationUseCaseError.noRequestUriQueryParameter
|
||||
}
|
||||
|
||||
private func formatPresentationResponse(response: PresentationResponseContainer, identifier: MockIdentifier) -> Promise<PresentationResponse> {
|
||||
return Promise { seal in
|
||||
do {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import XCTest
|
||||
import VCRepository
|
||||
|
@ -12,18 +12,17 @@ import VCEntities
|
|||
class PresentationUseCaseTests: XCTestCase {
|
||||
|
||||
var usecase: PresentationUseCase!
|
||||
var contract: Contract!
|
||||
var presentationRequest: PresentationRequest!
|
||||
let expectedUrl = "openid://vc/?request_uri=https://test-relyingparty.azurewebsites.net/request/UZWlr4uOY13QiA"
|
||||
|
||||
|
||||
override func setUpWithError() throws {
|
||||
let repo = PresentationRepository(apiCalls: MockApiCalls())
|
||||
let formatter = PresentationResponseFormatter()
|
||||
self.usecase = PresentationUseCase(formatter: formatter, repo: repo)
|
||||
|
||||
let encodedContract = TestData.aiContract.rawValue.data(using: .utf8)!
|
||||
self.contract = try JSONDecoder().decode(Contract.self, from: encodedContract)
|
||||
self.presentationRequest = PresentationRequest(from: TestData.presentationRequest.rawValue)!
|
||||
|
||||
// MockPResponseFormatter.wasFormatCalled = false
|
||||
MockPresentationResponseFormatter.wasFormatCalled = false
|
||||
MockApiCalls.wasPostCalled = false
|
||||
MockApiCalls.wasGetCalled = false
|
||||
}
|
||||
|
@ -33,7 +32,7 @@ class PresentationUseCaseTests: XCTestCase {
|
|||
XCTAssertNotNil(usecase.formatter)
|
||||
XCTAssertNotNil(usecase.repo)
|
||||
}
|
||||
|
||||
|
||||
func testGetRequest() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
usecase.getRequest(usingUrl: expectedUrl).done {
|
||||
|
@ -51,39 +50,113 @@ class PresentationUseCaseTests: XCTestCase {
|
|||
wait(for: [expec], timeout: 5)
|
||||
}
|
||||
|
||||
// func testSendResponse() throws {
|
||||
// let expec = self.expectation(description: "Fire")
|
||||
// let response = try PresentationResponseContainer(from: PresentationResponseContainer(from: <#T##PresentationRequest#>))
|
||||
// usecase.send(response: response, identifier: MockIdentifier()).done {
|
||||
// response in
|
||||
// print(response)
|
||||
// XCTFail()
|
||||
// expec.fulfill()
|
||||
// }.catch { error in
|
||||
// XCTAssert(MockIssuanceResponseFormatter.wasFormatCalled)
|
||||
// XCTAssert(MockApiCalls.wasPostCalled)
|
||||
// XCTAssert(error is MockError)
|
||||
// expec.fulfill()
|
||||
// }
|
||||
//
|
||||
// wait(for: [expec], timeout: 20)
|
||||
// }
|
||||
func testGetRequestMalformedUri() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
let malformedUrl = " "
|
||||
usecase.getRequest(usingUrl: malformedUrl).done {
|
||||
request in
|
||||
print(request)
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
XCTAssert(error is PresentationUseCaseError)
|
||||
XCTAssertEqual(error as? PresentationUseCaseError, .inputStringNotUri)
|
||||
expec.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [expec], timeout: 5)
|
||||
}
|
||||
|
||||
func testGetRequestNoQueryParameters() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
let malformedUrl = "https://test.com"
|
||||
usecase.getRequest(usingUrl: malformedUrl).done {
|
||||
request in
|
||||
print(request)
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
XCTAssert(error is PresentationUseCaseError)
|
||||
XCTAssertEqual(error as? PresentationUseCaseError, .noQueryParametersOnUri)
|
||||
expec.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [expec], timeout: 5)
|
||||
}
|
||||
|
||||
func testGetRequestNoValueOnRequestUri() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
let malformedUrl = "https://test.com?request_uri"
|
||||
usecase.getRequest(usingUrl: malformedUrl).done {
|
||||
request in
|
||||
print(request)
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
print(error)
|
||||
XCTAssert(error is PresentationUseCaseError)
|
||||
XCTAssertEqual(error as? PresentationUseCaseError, .noValueForRequestUriQueryParameter)
|
||||
expec.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [expec], timeout: 5)
|
||||
}
|
||||
|
||||
func testGetRequestNoRequestUri() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
let malformedUrl = "https://test.com?testparam=33423"
|
||||
usecase.getRequest(usingUrl: malformedUrl).done {
|
||||
request in
|
||||
print(request)
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
XCTAssert(error is PresentationUseCaseError)
|
||||
XCTAssertEqual(error as? PresentationUseCaseError, .noRequestUriQueryParameter)
|
||||
expec.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [expec], timeout: 5)
|
||||
}
|
||||
|
||||
func testSendResponse() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
|
||||
let repo = PresentationRepository(apiCalls: MockApiCalls())
|
||||
let formatter = MockPresentationResponseFormatter(shouldSucceed: true)
|
||||
let usecase = PresentationUseCase(formatter: formatter, repo: repo)
|
||||
|
||||
let response = try PresentationResponseContainer(from: self.presentationRequest)
|
||||
usecase.send(response: response, identifier: MockIdentifier()).done {
|
||||
response in
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
print(error)
|
||||
XCTAssert(MockPresentationResponseFormatter.wasFormatCalled)
|
||||
XCTAssert(MockApiCalls.wasPostCalled)
|
||||
XCTAssert(error is MockError)
|
||||
expec.fulfill()
|
||||
}
|
||||
|
||||
wait(for: [expec], timeout: 20)
|
||||
}
|
||||
|
||||
func testSendResponseFailedToFormat() throws {
|
||||
let expec = self.expectation(description: "Fire")
|
||||
|
||||
let repo = IssuanceRepository(apiCalls: MockApiCalls())
|
||||
let formatter = IssuanceResponseFormatter()
|
||||
let usecase = IssuanceUseCase(formatter: formatter, repo: repo)
|
||||
let repo = PresentationRepository(apiCalls: MockApiCalls())
|
||||
let formatter = MockPresentationResponseFormatter(shouldSucceed: false)
|
||||
let usecase = PresentationUseCase(formatter: formatter, repo: repo)
|
||||
|
||||
let response = try IssuanceResponseContainer(from: contract, contractUri: expectedUrl)
|
||||
let response = try PresentationResponseContainer(from: self.presentationRequest)
|
||||
usecase.send(response: response, identifier: MockIdentifier()).done {
|
||||
response in
|
||||
print(response)
|
||||
XCTFail()
|
||||
expec.fulfill()
|
||||
}.catch { error in
|
||||
XCTAssert(MockIssuanceResponseFormatter.wasFormatCalled)
|
||||
print(error)
|
||||
XCTAssert(MockPresentationResponseFormatter.wasFormatCalled)
|
||||
XCTAssertFalse(MockApiCalls.wasPostCalled)
|
||||
XCTAssert(error is MockIssuanceResponseFormatterError)
|
||||
expec.fulfill()
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Загрузка…
Ссылка в новой задаче