Skip to content

Commit

Permalink
add unit test for hostedUI error
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh62 committed Jul 4, 2024
1 parent 931fa6a commit 6f83258
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ class ShowHostedUISignOut: NSObject, Action {

guard let environment = environment as? AuthEnvironment,
let hostedUIEnvironment = environment.hostedUIEnvironment else {
let message = AuthPluginErrorConstants.configurationError
let error = AuthenticationError.configuration(message: message)
let error = HostedUIError.pluginConfiguration(AuthPluginErrorConstants.configurationError)
await sendEvent(with: error, dispatcher: dispatcher, environment: environment)
return
}
let hostedUIConfig = hostedUIEnvironment.configuration

guard let callbackURL = URL(string: hostedUIConfig.oauth.signOutRedirectURI),
let callbackURLScheme = callbackURL.scheme else {
let error = AuthenticationError.configuration(message: "Callback URL could not be retrieved")
await sendEvent(with: error, dispatcher: dispatcher, environment: environment)
await sendEvent(with: HostedUIError.signOutRedirectURI, dispatcher: dispatcher, environment: environment)
return
}

Expand All @@ -48,13 +45,7 @@ class ShowHostedUISignOut: NSObject, Action {
callbackScheme: callbackURLScheme,
inPrivate: false,
presentationAnchor: signOutEvent.presentationAnchor)

await sendEvent(with: nil, dispatcher: dispatcher, environment: environment)

} catch HostedUIError.signOutURI {
let error = AuthenticationError.configuration(message: "Could not create logout URL")
await sendEvent(with: error, dispatcher: dispatcher, environment: environment)
return
} catch {
self.logVerbose("\(#fileID) Received error \(error)", environment: environment)
await sendEvent(with: error, dispatcher: dispatcher, environment: environment)
Expand All @@ -65,31 +56,30 @@ class ShowHostedUISignOut: NSObject, Action {
dispatcher: EventDispatcher,
environment: Environment) async {

var hostedUIError: AWSCognitoHostedUIError?
let event: SignOutEvent
if let hostedUIInternalError = error as? HostedUIError {
let event = SignOutEvent(eventType: .hostedUISignOutError(hostedUIInternalError))
self.logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
return
event = SignOutEvent(eventType: .hostedUISignOutError(hostedUIInternalError))
} else if let error = error as? AuthErrorConvertible {
event = getEvent(for: AWSCognitoHostedUIError(error: error.authError))
} else if let error = error{

Check failure on line 64 in AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Actions/SignOut/ShowHostedUISignOut.swift

View workflow job for this annotation

GitHub Actions / run-swiftlint

Opening braces should be preceded by a single space and on the same line as the declaration (opening_brace)
let serviceError = AuthError.service(
"HostedUI failed with error", "", error)
event = getEvent(for: AWSCognitoHostedUIError(error: serviceError))
} else {
event = getEvent(for: nil)
}
self.logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
}

if let error = error as? AuthErrorConvertible {
hostedUIError = AWSCognitoHostedUIError(error: error.authError)
} else if let error = error {
let serviceError = AuthError.service("HostedUI failed with error",
"", error)
hostedUIError = AWSCognitoHostedUIError(error: serviceError)
}
let event: SignOutEvent
func getEvent(for hostedUIError: AWSCognitoHostedUIError?) -> SignOutEvent {
if self.signOutEvent.globalSignOut {
event = SignOutEvent(eventType: .signOutGlobally(self.signInData,
return SignOutEvent(eventType: .signOutGlobally(self.signInData,
hostedUIError: hostedUIError))
} else {
event = SignOutEvent(eventType: .revokeToken(self.signInData,
return SignOutEvent(eventType: .revokeToken(self.signInData,
hostedUIError: hostedUIError))
}
self.logVerbose("\(#fileID) Sending event \(event.type)", environment: environment)
await dispatcher.send(event)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ enum HostedUIError: Error {

case signOutURI

case signOutRedirectURI

case proofCalculation

case codeValidation
Expand All @@ -23,6 +25,8 @@ enum HostedUIError: Error {

case serviceMessage(String)

case pluginConfiguration(String)

case cancelled

case invalidContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ extension HostedUIError: AuthErrorConvertible {
AuthPluginErrorConstants.hostedUISignOutURI.errorDescription,
AuthPluginErrorConstants.hostedUISignOutURI.recoverySuggestion)

case .signOutRedirectURI:
return .service(
AuthPluginErrorConstants.hostedUISignOutRedirectURI.errorDescription,
AuthPluginErrorConstants.hostedUISignOutRedirectURI.recoverySuggestion)

case .proofCalculation:
return .invalidState(
AuthPluginErrorConstants.hostedUIProofCalculation.errorDescription,
Expand Down Expand Up @@ -113,6 +118,9 @@ extension HostedUIError: AuthErrorConvertible {
case .serviceMessage(let message):
return .service(message, AuthPluginErrorConstants.serviceError)

case .pluginConfiguration(let message):
return .configuration(message, AuthPluginErrorConstants.configurationError)

case .unknown:
return .unknown("WebUI signIn encountered an unknown error", nil)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ enum AuthPluginErrorConstants {
"SignOut URI could not be created",
"Check the configuration to make sure that HostedUI related information are present")

static let hostedUISignOutRedirectURI: AuthPluginErrorString = (
"Callback URL could not be retrieved",
"Check the configuration to make sure that HostedUI related information are present")

static let hostedUIProofCalculation: AuthPluginErrorString = (
"Proof calculation failed",
"Reach out with amplify team via github to raise an issue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ extension SignedInData {
signInMethod: .apiBased(.userSRP),
cognitoUserPoolTokens: tokens)
}

static var hostedUISignInData: SignedInData {
let tokens = AWSCognitoUserPoolTokens.testData
return SignedInData(signedInDate: Date(),
signInMethod: .hostedUI(.init(
scopes: [],
providerInfo: .init(authProvider: .google, idpIdentifier: ""),
presentationAnchor: nil,
preferPrivateSession: false)),
cognitoUserPoolTokens: tokens)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ extension AmplifyCredentials {
credentials: .testData)
}

static var hostedUITestData: AmplifyCredentials {
AmplifyCredentials.userPoolAndIdentityPool(signedInData: .hostedUISignInData,
identityID: "identityId",
credentials: AuthAWSCognitoCredentials.testData)
}

static var testDataWithExpiredTokens: AmplifyCredentials {
AmplifyCredentials.userPoolAndIdentityPool(signedInData: .expiredTestData,
identityID: "identityId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ class AWSAuthSignOutTaskTests: BasePluginTest {

}

func testInvalidStateForSignOutWhenSignedInUsingHostedUI() async {

let initialState = AuthState.configured(
AuthenticationState.signedIn(.hostedUISignInData),
AuthorizationState.sessionEstablished(.hostedUITestData))

let authPlugin = configureCustomPluginWith(initialState: initialState)

let result = await authPlugin.signOut() as? AWSCognitoSignOutResult

guard case .failed(let authError) = result else {
XCTFail("Sign out should have failed.")
return
}
guard case .configuration = authError else {
XCTFail("Auth error should be service but got: \(authError)")
return
}
}

func testGuestSignOut() async {

let initialState = AuthState.configured(
Expand Down

0 comments on commit 6f83258

Please sign in to comment.