amplify-swift/AmplifyPlugins/Auth/AWSCognitoAuthPluginTests/AuthUserBehaviorTests/UserBehaviorResendCodeTests...

483 lines
19 KiB
Swift

//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
import XCTest
@testable import Amplify
@testable import AWSCognitoAuthPlugin
@testable import AWSMobileClient
// swiftlint:disable file_length
// swiftlint:disable type_body_length
class UserBehaviorResendCodeTests: BaseUserBehaviorTest {
/// Test a successful resendConfirmationCode call with .done as next step
///
/// - Given: an auth plugin with mocked service. Mocked service calls should mock a successul response
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a successful result with .email as the attribute's destination
///
func testSuccessfulResendConfirmationCode() {
mockAWSMobileClient.verifyUserAttributeMockResult =
.success(UserCodeDeliveryDetails(deliveryMedium: .email,
destination: "destination",
attributeName: "attributeName"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success(let attribute):
guard case .email = attribute.destination else {
XCTFail("Result should be .email for attributeKey")
return
}
case .failure(let error):
XCTFail("Received failure with error \(error)")
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with invalid result
///
/// - Given: an auth plugin with mocked service. Mocked service calls should mock a invalid response
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get an .unknown error
///
func testResendConfirmationCodeWithInvalidResult() {
mockAWSMobileClient?.verifyUserAttributeMockResult = nil
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .unknown = error else {
XCTFail("Should produce an unknown error")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with CodeMismatchException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// CodeMismatchException response
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .codeMismatch as underlyingError
///
func testResendConfirmationCodeWithCodeMismatchException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.codeMismatch(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .codeMismatch = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be codeMismatch \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with CodeExpiredException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// CodeExpiredException response
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .codeExpired as underlyingError
///
func testResendConfirmationCodeWithExpiredCodeException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.expiredCode(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .codeExpired = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be codeExpired \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with InternalErrorException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a InternalErrorException response
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get an .unknown error
///
func testResendConfirmationCodeWithInternalErrorException() {
mockAWSMobileClient?.signupMockResult =
.failure(AWSMobileClientError.internalError(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .unknown = error else {
XCTFail("Should produce an unknown error instead of \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with InvalidParameterException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// InvalidParameterException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .invalidParameter as underlyingError
///
func testResendConfirmationCodeWithInvalidParameterException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.invalidParameter(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .invalidParameter = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be invalidParameter \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with LimitExceededException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// LimitExceededException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .limitExceeded as underlyingError
///
func testResendConfirmationCodeWithLimitExceededException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.limitExceeded(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .limitExceeded = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be limitExceeded \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with NotAuthorizedException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// NotAuthorizedException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .notAuthorized error
///
func testResendConfirmationCodeWithNotAuthorizedException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.notAuthorized(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .notAuthorized = error else {
XCTFail("Should produce notAuthorized error instead of \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with PasswordResetRequiredException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// PasswordResetRequiredException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .passwordResetRequired as underlyingError
///
func testResendConfirmationCodeWithPasswordResetRequiredException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.passwordResetRequired(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .passwordResetRequired = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be passwordResetRequired \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with ResourceNotFoundException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// ResourceNotFoundException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .resourceNotFound as underlyingError
///
func testResendConfirmationCodeWithResourceNotFoundException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.resourceNotFound(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .resourceNotFound = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be resourceNotFound \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with TooManyRequestsException response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// TooManyRequestsException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .requestLimitExceeded as underlyingError
///
func testResendConfirmationCodeWithTooManyRequestsException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.tooManyRequests(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .requestLimitExceeded = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be requestLimitExceeded \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with UserNotFound response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// UserNotConfirmedException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .userNotConfirmed as underlyingError
///
func testResendConfirmationCodeWithUserNotConfirmedException() {
mockAWSMobileClient?.verifyUserAttributeMockResult = .failure(
AWSMobileClientError.userNotConfirmed(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .userNotConfirmed = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be userNotConfirmed \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
/// Test a resendConfirmationCode call with UserNotFound response from service
///
/// - Given: an auth plugin with mocked service. Mocked service should mock a
/// UserNotFoundException response
///
/// - When:
/// - I invoke resendConfirmationCode
/// - Then:
/// - I should get a .service error with .userNotFound as underlyingError
///
func testResendConfirmationCodeWithUserNotFoundException() {
mockAWSMobileClient?.verifyUserAttributeMockResult =
.failure(AWSMobileClientError.userNotFound(message: "Error"))
let resultExpectation = expectation(description: "Should receive a result")
_ = plugin.resendConfirmationCode(for: .email) { result in
defer {
resultExpectation.fulfill()
}
switch result {
case .success:
XCTFail("Should return an error if the result from service is invalid")
case .failure(let error):
guard case .service(_, _, let underlyingError) = error else {
XCTFail("Should produce service error instead of \(error)")
return
}
guard case .userNotFound = (underlyingError as? AWSCognitoAuthError) else {
XCTFail("Underlying error should be userNotFound \(error)")
return
}
}
}
wait(for: [resultExpectation], timeout: apiTimeout)
}
}