Skip to content

Commit

Permalink
updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 committed Nov 16, 2023
1 parent e3a98c3 commit 2e540c3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class ShowHostedUISignIn: NSObject, Action {

let signingInData: HostedUISigningInState

var sessionAdapter: HostedUISessionBehavior?

init(signInData: HostedUISigningInState) {
self.signingInData = signInData
}
Expand Down Expand Up @@ -48,15 +46,15 @@ class ShowHostedUISignIn: NSObject, Action {
self.logVerbose("\(#fileID) Showing url \(url.absoluteString)", environment: environment)

do {
sessionAdapter = hostedUIEnvironment.hostedUISessionFactory()
let queryItems = try await sessionAdapter?.showHostedUI(
let sessionAdapter = hostedUIEnvironment.hostedUISessionFactory()
let queryItems = try await sessionAdapter.showHostedUI(
url: url,
callbackScheme: callbackURLScheme,
inPrivate: signingInData.options.preferPrivateSession,
presentationAnchor: signingInData.presentationAnchor)

guard let code = queryItems?.first(where: { $0.name == "code" })?.value,
let state = queryItems?.first(where: { $0.name == "state" })?.value,
guard let code = queryItems.first(where: { $0.name == "code" })?.value,
let state = queryItems.first(where: { $0.name == "state" })?.value,
self.signingInData.state == state else {

let event = HostedUIEvent(eventType: .throwError(.hostedUI(.codeValidation)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class ShowHostedUISignOut: NSObject, Action {
let signOutEvent: SignOutEventData
let signInData: SignedInData

var sessionAdapter: HostedUISessionBehavior?

init(signOutEvent: SignOutEventData, signInData: SignedInData) {
self.signInData = signInData
self.signOutEvent = signOutEvent
Expand All @@ -44,8 +42,8 @@ class ShowHostedUISignOut: NSObject, Action {

do {
let logoutURL = try HostedUIRequestHelper.createSignOutURL(configuration: hostedUIConfig)
sessionAdapter = hostedUIEnvironment.hostedUISessionFactory()
_ = try await sessionAdapter?.showHostedUI(
let sessionAdapter = hostedUIEnvironment.hostedUISessionFactory()
_ = try await sessionAdapter.showHostedUI(
url: logoutURL,
callbackScheme: callbackURLScheme,
inPrivate: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ class MockHostedUISession: HostedUISessionBehavior {
self.result = result
}

func showHostedUI(url: URL,
callbackScheme: String,
inPrivate: Bool,
presentationAnchor: AuthUIPresentationAnchor?,
callback: @escaping (Result<[URLQueryItem], HostedUIError>) -> Void) {
callback(result)
func showHostedUI(
url: URL,
callbackScheme: String,
inPrivate: Bool,
presentationAnchor: AuthUIPresentationAnchor?
) async throws -> [URLQueryItem] {
return try result.get()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,77 +29,50 @@ class HostedUIASWebAuthenticationSessionTests: XCTestCase {
/// Given: A HostedUIASWebAuthenticationSession
/// When: showHostedUI is invoked and the session factory returns a URL with query items
/// Then: An array of query items should be returned
func testShowHostedUI_withUrlInCallback_withQueryItems_shouldReturnQueryItems() {
let expectation = expectation(description: "showHostedUI")
func testShowHostedUI_withUrlInCallback_withQueryItems_shouldReturnQueryItems() async throws {
factory.mockedURL = createURL(queryItems: [.init(name: "name", value: "value")])

session.showHostedUI() { result in
do {
let queryItems = try result.get()
XCTAssertEqual(queryItems.count, 1)
XCTAssertEqual(queryItems.first?.name, "name")
XCTAssertEqual(queryItems.first?.value, "value")
} catch {
XCTFail("Expected .success(queryItems), got \(result)")
}
expectation.fulfill()
}
waitForExpectations(timeout: 1)
let queryItems = try await session.showHostedUI()
XCTAssertEqual(queryItems.count, 1)
XCTAssertEqual(queryItems.first?.name, "name")
XCTAssertEqual(queryItems.first?.value, "value")
}

/// Given: A HostedUIASWebAuthenticationSession
/// When: showHostedUI is invoked and the session factory returns a URL without query items
/// Then: An empty array should be returned
func testShowHostedUI_withUrlInCallback_withoutQueryItems_shouldReturnEmptyQueryItems() {
let expectation = expectation(description: "showHostedUI")
func testShowHostedUI_withUrlInCallback_withoutQueryItems_shouldReturnEmptyQueryItems() async throws {
factory.mockedURL = createURL()

session.showHostedUI() { result in
do {
let queryItems = try result.get()
XCTAssertTrue(queryItems.isEmpty)
} catch {
XCTFail("Expected .success(queryItems), got \(result)")
}
expectation.fulfill()
}
waitForExpectations(timeout: 1)
let queryItems = try await session.showHostedUI()
XCTAssertTrue(queryItems.isEmpty)
}

/// Given: A HostedUIASWebAuthenticationSession
/// When: showHostedUI is invoked and the session factory returns a URL with query items representing errors
/// Then: A HostedUIError.serviceMessage should be returned
func testShowHostedUI_withUrlInCallback_withErrorInQueryItems_shouldReturnServiceMessageError() {
let expectation = expectation(description: "showHostedUI")
func testShowHostedUI_withUrlInCallback_withErrorInQueryItems_shouldReturnServiceMessageError() async {
factory.mockedURL = createURL(
queryItems: [
.init(name: "error", value: "Error."),
.init(name: "error_description", value: "Something went wrong")
]
)

session.showHostedUI() { result in
do {
_ = try result.get()
XCTFail("Expected failure(.serviceMessage), got \(result)")
} catch let error as HostedUIError {
if case .serviceMessage(let message) = error {
XCTAssertEqual(message, "Error. Something went wrong")
} else {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
} catch {
do {
_ = try await session.showHostedUI()
} catch let error as HostedUIError {
if case .serviceMessage(let message) = error {
XCTAssertEqual(message, "Error. Something went wrong")
} else {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
expectation.fulfill()
} catch {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
waitForExpectations(timeout: 1)
}

/// Given: A HostedUIASWebAuthenticationSession
/// When: showHostedUI is invoked and the session factory returns ASWebAuthenticationSessionErrors
/// Then: A HostedUIError corresponding to the error code should be returned
func testShowHostedUI_withASWebAuthenticationSessionErrors_shouldReturnRightError() {
func testShowHostedUI_withASWebAuthenticationSessionErrors_shouldReturnRightError() async {
let errorMap: [ASWebAuthenticationSessionError.Code: HostedUIError] = [
.canceledLogin: .cancelled,
.presentationContextNotProvided: .invalidContext,
Expand All @@ -116,40 +89,28 @@ class HostedUIASWebAuthenticationSessionTests: XCTestCase {
for code in errorCodes {
factory.mockedError = ASWebAuthenticationSessionError(code)
let expectedError = errorMap[code] ?? .unknown
let expectation = expectation(description: "showHostedUI for error \(code)")
session.showHostedUI() { result in
do {
_ = try result.get()
XCTFail("Expected failure(.\(expectedError)), got \(result)")
} catch let error as HostedUIError {
XCTAssertEqual(error, expectedError)
} catch {
XCTFail("Expected HostedUIError.\(expectedError), got \(error)")
}
expectation.fulfill()
do {
_ = try await session.showHostedUI()
} catch let error as HostedUIError {
XCTAssertEqual(error, expectedError)
} catch {
XCTFail("Expected HostedUIError.\(expectedError), got \(error)")
}
waitForExpectations(timeout: 1)
}
}

/// Given: A HostedUIASWebAuthenticationSession
/// When: showHostedUI is invoked and the session factory returns an error
/// Then: A HostedUIError.unknown should be returned
func testShowHostedUI_withOtherError_shouldReturnUnknownError() {
func testShowHostedUI_withOtherError_shouldReturnUnknownError() async {
factory.mockedError = CancellationError()
let expectation = expectation(description: "showHostedUI")
session.showHostedUI() { result in
do {
_ = try result.get()
XCTFail("Expected failure(.unknown), got \(result)")
} catch let error as HostedUIError {
XCTAssertEqual(error, .unknown)
} catch {
XCTFail("Expected HostedUIError.unknown, got \(error)")
}
expectation.fulfill()
do {
_ = try await session.showHostedUI()
} catch let error as HostedUIError {
XCTAssertEqual(error, .unknown)
} catch {
XCTFail("Expected HostedUIError.unknown, got \(error)")
}
waitForExpectations(timeout: 1)
}

private func createURL(queryItems: [URLQueryItem] = []) -> URL {
Expand Down Expand Up @@ -203,13 +164,12 @@ class MockASWebAuthenticationSession: ASWebAuthenticationSession {
}

extension HostedUIASWebAuthenticationSession {
func showHostedUI(callback: @escaping (Result<[URLQueryItem], HostedUIError>) -> Void) {
showHostedUI(
func showHostedUI() async throws -> [URLQueryItem] {
return try await showHostedUI(
url: URL(string: "https://test.com")!,
callbackScheme: "https",
inPrivate: false,
presentationAnchor: nil,
callback: callback)
presentationAnchor: nil)
}
}
#else
Expand All @@ -218,30 +178,19 @@ extension HostedUIASWebAuthenticationSession {
import XCTest

class HostedUIASWebAuthenticationSessionTests: XCTestCase {
func testShowHostedUI_shouldThrowServiceError() {
let expectation = expectation(description: "showHostedUI")
func testShowHostedUI_shouldThrowServiceError() async {
let session = HostedUIASWebAuthenticationSession()
session.showHostedUI(
url: URL(string: "https://test.com")!,
callbackScheme: "https",
inPrivate: false,
presentationAnchor: nil
) { result in
do {
_ = try result.get()
XCTFail("Expected failure(.serviceMessage), got \(result)")
} catch let error as HostedUIError {
if case .serviceMessage(let message) = error {
XCTAssertEqual(message, "HostedUI is only available in iOS and macOS")
} else {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
} catch {
do {
_ = try await session.showHostedUI()
} catch let error as HostedUIError {
if case .serviceMessage(let message) = error {
XCTAssertEqual(message, "HostedUI is only available in iOS and macOS")
} else {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
expectation.fulfill()
} catch {
XCTFail("Expected HostedUIError.serviceMessage, got \(error)")
}
waitForExpectations(timeout: 1)
}
}

Expand Down

0 comments on commit 2e540c3

Please sign in to comment.