Skip to content

Commit

Permalink
test: update mfa e2e tests to prevent concurrent run user collisions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian authored Nov 9, 2023
1 parent 870e83d commit bffb88a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ class AWSAuthBaseTest: XCTestCase {
var defaultTestEmail = "test-\(UUID().uuidString)@amazon.com"
var defaultTestPassword = UUID().uuidString

var randomEmail: String {
"test-\(UUID().uuidString)@amazon.com"
}

var randomPhoneNumber: String {
"+1" + (1...10)
.map { _ in String(Int.random(in: 0...9)) }
.joined()
}

var amplifyConfigurationFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration"
let credentialsFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-credentials"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class MFAPreferenceTests: AWSAuthBaseTest {
AuthSessionHelper.clearSession()
}

func signUpAndSignIn(phoneNumber: String? = nil) async throws {
func signUpAndSignIn(
email: String? = nil,
phoneNumber: String? = nil
) async throws {
let username = "integTest\(UUID().uuidString)"
let password = "P123@\(UUID().uuidString)"

let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail,
email: email ?? defaultTestEmail,
phoneNumber: phoneNumber)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")
Expand Down Expand Up @@ -69,7 +72,7 @@ class MFAPreferenceTests: AWSAuthBaseTest {
///
func testFetchAndUpdateMFAPreferenceForTOTP() async throws {
do {
try await signUpAndSignIn()
try await signUpAndSignIn(email: randomEmail)

let authCognitoPlugin = try Amplify.Auth.getPlugin(
for: "awsCognitoAuthPlugin") as! AWSCognitoAuthPlugin
Expand Down Expand Up @@ -198,76 +201,78 @@ class MFAPreferenceTests: AWSAuthBaseTest {
/// - I should get valid fetchMFAPreference results corresponding to the updateMFAPreference
///
func testFetchAndUpdateMFAPreferenceForSMSAndTOTP() async throws {
do {
try await signUpAndSignIn(phoneNumber: "+16135550116") // Fake number for testing

let authCognitoPlugin = try Amplify.Auth.getPlugin(
for: "awsCognitoAuthPlugin") as! AWSCognitoAuthPlugin

var fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNil(fetchMFAResult.enabled)
XCTAssertNil(fetchMFAResult.preferred)

let totpSetupDetails = try await Amplify.Auth.setUpTOTP()
let totpCode = TOTPHelper.generateTOTPCode(sharedSecret: totpSetupDetails.sharedSecret)
try await Amplify.Auth.verifyTOTPSetup(code: totpCode)

// Test both MFA types as enabled
try await authCognitoPlugin.updateMFAPreference(
sms: .enabled,
totp: .enabled)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNil(fetchMFAResult.preferred)

// Test SMS as preferred, TOTP as enabled
try await authCognitoPlugin.updateMFAPreference(
sms: .preferred,
totp: .enabled)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .sms)

// Test SMS as notPreferred, TOTP as preferred
try await authCognitoPlugin.updateMFAPreference(
sms: .notPreferred,
totp: .preferred)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .totp)

// Test SMS as disabled, no change to TOTP
try await authCognitoPlugin.updateMFAPreference(
sms: .disabled,
totp: nil)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .totp)

// Test SMS as preferred, no change to TOTP (which should remove TOTP from preferred list)
try await authCognitoPlugin.updateMFAPreference(
sms: .preferred,
totp: nil)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .sms)
} catch {
XCTFail("API should succeed without any errors instead failed with \(error)")
}
let randomPhoneNumber = "+1" + (1...10)
.map { _ in String(Int.random(in: 0...9)) }
.joined()

try await signUpAndSignIn(
phoneNumber: randomPhoneNumber
)

let authCognitoPlugin = try Amplify.Auth.getPlugin(
for: "awsCognitoAuthPlugin") as! AWSCognitoAuthPlugin

var fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNil(fetchMFAResult.enabled)
XCTAssertNil(fetchMFAResult.preferred)

let totpSetupDetails = try await Amplify.Auth.setUpTOTP()
let totpCode = TOTPHelper.generateTOTPCode(sharedSecret: totpSetupDetails.sharedSecret)
try await Amplify.Auth.verifyTOTPSetup(code: totpCode)

// Test both MFA types as enabled
try await authCognitoPlugin.updateMFAPreference(
sms: .enabled,
totp: .enabled)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNil(fetchMFAResult.preferred)

// Test SMS as preferred, TOTP as enabled
try await authCognitoPlugin.updateMFAPreference(
sms: .preferred,
totp: .enabled)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .sms)

// Test SMS as notPreferred, TOTP as preferred
try await authCognitoPlugin.updateMFAPreference(
sms: .notPreferred,
totp: .preferred)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .totp)

// Test SMS as disabled, no change to TOTP
try await authCognitoPlugin.updateMFAPreference(
sms: .disabled,
totp: nil)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .totp)

// Test SMS as preferred, no change to TOTP (which should remove TOTP from preferred list)
try await authCognitoPlugin.updateMFAPreference(
sms: .preferred,
totp: nil)

fetchMFAResult = try await authCognitoPlugin.fetchMFAPreference()
XCTAssertNotNil(fetchMFAResult.enabled)
XCTAssertEqual(fetchMFAResult.enabled, [.sms, .totp])
XCTAssertNotNil(fetchMFAResult.preferred)
XCTAssertEqual(fetchMFAResult.preferred, .sms)
}

/// Test invalidParameter exception in updateMFAPreference API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class MFASignInTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")

Expand Down Expand Up @@ -115,8 +116,9 @@ class MFASignInTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail,
phoneNumber: "+16135550116")
email: randomEmail,
phoneNumber: randomPhoneNumber
)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")

Expand Down Expand Up @@ -175,8 +177,8 @@ class MFASignInTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail,
phoneNumber: "+16135550116")
email: randomEmail,
phoneNumber: randomPhoneNumber)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")

Expand Down Expand Up @@ -254,8 +256,9 @@ class MFASignInTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail,
phoneNumber: "+16135550116")
email: randomEmail,
phoneNumber: randomPhoneNumber
)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class TOTPSetupWhenAuthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.registerAndSignInUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup and sign in should succeed")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class TOTPSetupWhenUnauthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup should succeed")

Expand Down Expand Up @@ -82,8 +83,9 @@ class TOTPSetupWhenUnauthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: defaultTestEmail,
phoneNumber: "+16135550116")
email: randomEmail,
phoneNumber: randomPhoneNumber
)

XCTAssertTrue(didSucceed, "Signup should succeed")

Expand Down Expand Up @@ -132,7 +134,8 @@ class TOTPSetupWhenUnauthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup should succeed")

Expand Down Expand Up @@ -179,7 +182,8 @@ class TOTPSetupWhenUnauthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup should succeed")

Expand Down Expand Up @@ -246,7 +250,8 @@ class TOTPSetupWhenUnauthenticatedTests: AWSAuthBaseTest {
let didSucceed = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: defaultTestEmail)
email: randomEmail
)

XCTAssertTrue(didSucceed, "Signup should succeed")

Expand Down

0 comments on commit bffb88a

Please sign in to comment.