Skip to content

Commit

Permalink
Fix integration tests (#2207)
Browse files Browse the repository at this point in the history
* fix compilation error integration tests

* Set correctly URL for mock API when running end to end tests

* use same XCAssert command

* Update integration tests, do not skip tests anymore
  • Loading branch information
nilo-ms authored Jun 19, 2024
1 parent 006882e commit 9603f0b
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 146 deletions.
2 changes: 1 addition & 1 deletion MSAL/test/integration/native_auth/MockAPIHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class MockAPIHandlerTest: MSALNativeAuthIntegrationBaseTests {

func testAddNewResponse() async {
do {
try await mockAPIHandler.addResponse(endpoint: .signInInitiate, correlationId: correlationId, responses: [.invalidClient, .userNotFound])
try await mockAPIHandler.addResponse(endpoint: .signInInitiate, correlationId: correlationId, responses: [.unauthorizedClient, .userNotFound])
} catch {
XCTFail("Unexpected error: \(error)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ class MSALNativeAuthIntegrationBaseTests: XCTestCase {
var defaultTimeout: TimeInterval = 5
let mockAPIHandler = MockAPIHandler()
let correlationId = UUID()
let config: MSALNativeAuthConfiguration = try! MSALNativeAuthConfiguration(clientId: UUID().uuidString,
authority: MSALCIAMAuthority(url: URL(string: (ProcessInfo.processInfo.environment["authorityURL"] ?? "<mock api url not set>") + "/test")!),
challengeTypes: [.password, .oob, .redirect])
let config: MSALNativeAuthConfiguration = try! MSALNativeAuthConfiguration(
clientId: UUID().uuidString,
authority: MSALCIAMAuthority(url:
URL(string: (ProcessInfo.processInfo.environment["authorityURL"] ?? "<mock api url not set>") + "/testTenant")!),

challengeTypes: [.password, .oob, .redirect],
redirectUri: nil
)
var sut: MSIDHttpRequest!

override func tearDown() {
Expand Down Expand Up @@ -76,7 +81,7 @@ class MSALNativeAuthIntegrationBaseTests: XCTestCase {
try await mockResponse(response, endpoint: endpoint)
let response: Error = try await perform_uncheckedTestFail()

XCTAssertEqual(response.error?.rawValue, expectedError.error?.rawValue)
XCTAssertEqual(response.error.rawValue, expectedError.error.rawValue)

// TODO: Fix these checks
if expectedError.errorDescription != nil {
Expand Down
1 change: 0 additions & 1 deletion MSAL/test/integration/native_auth/common/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ enum MockAPIResponse: String {
case invalidGrant = "InvalidGrant"
case invalidScope = "InvalidScope"
case expiredToken = "ExpiredToken"
case invalidPurposeToken = "InvalidPurposeToken"
case authNotSupported = "AuthNotSupported"
case userAlreadyExists = "UserAlreadyExists"
case userNotFound = "UserNotFound"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {
class Configuration: NSObject {
static let clientId = ProcessInfo.processInfo.environment["clientId"] ?? "<clientId not set>"
static let authorityURLString = ProcessInfo.processInfo.environment["authorityURL"] ?? "<authorityURL not set>"
static let authorityURL = URL(string: authorityURLString) ?? URL(string: "https://microsoft.com")

static let authority = try? MSALCIAMAuthority(url: authorityURL!)
}

func mockResponse(_ response: MockAPIResponse, endpoint: MockAPIEndpoint) async throws {
Expand All @@ -55,19 +52,24 @@ class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {

override func setUpWithError() throws {
try super.setUpWithError()
let useMockAPIBooleanString = ProcessInfo.processInfo.environment["useMockAPI"] ?? "false"
usingMockAPI = Bool(useMockAPIBooleanString) ?? false

// mock API URL needs to contains a tenant
guard let authorityURL = URL(string: Configuration.authorityURLString + (usingMockAPI ? "/testTenant" : "")), let authority = try? MSALCIAMAuthority(url: authorityURL) else {
XCTFail("AuthorityURL not set or invalid")
return
}

sut = try MSALNativeAuthPublicClientApplication(
configuration: MSALPublicClientApplicationConfig(
clientId: Configuration.clientId,
redirectUri: nil,
authority: Configuration.authority
authority: authority
),
challengeTypes: [.OOB, .password]
)

let useMockAPIBooleanString = ProcessInfo.processInfo.environment["useMockAPI"] ?? "false"
usingMockAPI = Bool(useMockAPIBooleanString) ?? false

if usingMockAPI {
print("🤖🤖🤖 Using mock API: \(Configuration.authorityURLString)")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ final class MSALNativeAuthResetPasswordEndToEndTests: MSALNativeAuthEndToEndBase

// SSPR - with automatic sign in
func test_resetPassword_withAutomaticSignIn_succeeds() async throws {
try XCTSkipIf(true) // TODO: Remove once we update to continuation_token

let codeRequiredExp = expectation(description: "code required")
let resetPasswordStartDelegate = ResetPasswordStartDelegateSpy(expectation: codeRequiredExp)

Expand Down Expand Up @@ -141,7 +139,5 @@ final class MSALNativeAuthResetPasswordEndToEndTests: MSALNativeAuthEndToEndBase
XCTAssertEqual(signInAfterResetPasswordDelegate.result?.account.username, usernameOTP)
XCTAssertNotNil(signInAfterResetPasswordDelegate.result?.idToken)
XCTAssertNil(signInAfterResetPasswordDelegate.result?.account.accountClaims)
XCTAssertEqual(signInAfterResetPasswordDelegate.result?.scopes[0], "openid")
XCTAssertEqual(signInAfterResetPasswordDelegate.result?.scopes[1], "offline_access")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class MSALNativeAuthSignInUsernameEndToEndTests: MSALNativeAuthEndToEndBas

XCTAssertTrue(signInVerifyCodeDelegateSpy.onSignInVerifyCodeErrorCalled)
XCTAssertNotNil(signInVerifyCodeDelegateSpy.error)
XCTAssertTrue(signInVerifyCodeDelegateSpy.error!.isInvalidCode)
XCTAssertEqual(signInVerifyCodeDelegateSpy.error?.isInvalidCode, true)
}

// Hero Scenario 1.2.1. Sign in (Email & Email OTP)
Expand Down Expand Up @@ -204,7 +204,7 @@ final class MSALNativeAuthSignInUsernameEndToEndTests: MSALNativeAuthEndToEndBas
await fulfillment(of: [passwordRequiredExpectation], timeout: 2)

XCTAssertTrue(signInPasswordRequiredDelegateSpy.onSignInPasswordRequiredErrorCalled)
XCTAssertTrue(signInPasswordRequiredDelegateSpy.error!.isInvalidPassword)
XCTAssertEqual(signInPasswordRequiredDelegateSpy.error?.isInvalidPassword, true)
}

// Hero Scenario 2.2.2. Sign in – Email and Password on MULTIPLE screens (Email & Password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,5 @@ final class MSALNativeAuthSignUpUsernameAndPasswordEndToEndTests: MSALNativeAuth
XCTAssertEqual(delegate.result?.account.username, usernamePassword)
XCTAssertNotNil(delegate.result?.idToken)
XCTAssertNil(delegate.result?.account.accountClaims)
XCTAssertEqual(delegate.result?.scopes[0], "openid")
XCTAssertEqual(delegate.result?.scopes[1], "offline_access")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,5 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas
XCTAssertEqual(delegate.result?.account.username, usernameOTP)
XCTAssertNotNil(delegate.result?.idToken)
XCTAssertNil(delegate.result?.account.accountClaims)
XCTAssertEqual(delegate.result?.scopes[0], "openid")
XCTAssertEqual(delegate.result?.scopes[1], "offline_access")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ final class MSALNativeAuthResetPasswordChallengeIntegrationTests: MSALNativeAuth
}

func test_resetPasswordChallenge_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordChallenge,
response: .unauthorizedClient,
Expand All @@ -92,16 +90,6 @@ final class MSALNativeAuthResetPasswordChallengeIntegrationTests: MSALNativeAuth
)
}

func test_resetPasswordChallenge_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordChallenge,
response: .invalidContinuationToken,
expectedError: createError(.invalidRequest)
)
}

func test_resetPasswordChallenge_unsupportedChallengeType() async throws {
try await perform_testFail(
endpoint: .resetPasswordChallenge,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ final class MSALNativeAuthResetPasswordContinueIntegrationTests: MSALNativeAuthI
}

func test_resetPasswordContinue_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordContinue,
response: .unauthorizedClient,
Expand All @@ -79,16 +77,6 @@ final class MSALNativeAuthResetPasswordContinueIntegrationTests: MSALNativeAuthI
)
}

func test_resetPasswordContinue_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordContinue,
response: .invalidContinuationToken,
expectedError: createResetPasswordContinueError(error: .invalidRequest)
)
}

func test_resetPasswordContinue_invalidPassword() async throws {
try await perform_testFail(
endpoint: .resetPasswordContinue,
Expand All @@ -105,16 +93,6 @@ final class MSALNativeAuthResetPasswordContinueIntegrationTests: MSALNativeAuthI
)
}

func test_resetPasswordContinue_verificationRequired() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordContinue,
response: .verificationRequired,
expectedError: createResetPasswordContinueError(error: .verificationRequired)
)
}

private func createResetPasswordContinueError(
error: MSALNativeAuthResetPasswordContinueOauth2ErrorCode,
subError: MSALNativeAuthSubErrorCode? = nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,25 +104,13 @@ final class MSALNativeAuthResetPasswordPollCompletionIntegrationTests: MSALNativ
}

func test_resetPasswordPollCompletion_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordPollCompletion,
response: .unauthorizedClient,
expectedError: createResetPasswordPollCompletionError(error: .unauthorizedClient)
)
}

func test_resetPasswordPollCompletion_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordPollCompletion,
response: .invalidContinuationToken,
expectedError: createResetPasswordPollCompletionError(error: .invalidRequest)
)
}

func test_resetPasswordPollCompletion_expiredToken() async throws {
try await perform_testFail(
endpoint: .resetPasswordPollCompletion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ final class MSALNativeAuthResetPasswordStartIntegrationTests: MSALNativeAuthInte
XCTAssertEqual(response?.challengeType, .redirect)
}

func test_resetPasswordStart_unauthorizedClient() async throws {
throw XCTSkip()

func test_resetPasswordStart_unauthorizedClient() async throws {
try await perform_testFail(
endpoint: .resetPasswordStart,
response: .unauthorizedClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,13 @@ final class MSALNativeAuthResetPasswordSubmitIntegrationTests: MSALNativeAuthInt
}

func test_resetPasswordSubmit_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordSubmit,
response: .unauthorizedClient,
expectedError: createError(.unauthorizedClient)
)
}

func test_resetPasswordSubmit_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .resetPasswordSubmit,
response: .invalidContinuationToken,
expectedError: createError(.invalidRequest)
)
}

func test_resetPasswordSubmit_expiredToken() async throws {
try await perform_testFail(
endpoint: .resetPasswordSubmit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,19 @@ class MSALNativeAuthSignInChallengeIntegrationTests: MSALNativeAuthIntegrationBa


func test_failRequest_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .signInChallenge,
response: .unauthorizedClient,
expectedError: Error(error: .unauthorizedClient, errorDescription: nil, errorCodes: nil, errorURI: nil, innerErrors: nil)
)
}

func test_failRequest_invalidPurposeToken() async throws {
throw XCTSkip()

let response = try await perform_testFail(
func test_failRequest_invalidContinuationToken() async throws {
try await perform_testFail(
endpoint: .signInChallenge,
response: .invalidPurposeToken,
expectedError: Error(error: .invalidRequest, errorDescription: nil, errorCodes: nil, errorURI: nil, innerErrors: nil)
response: .invalidContinuationToken,
expectedError: Error(error: .invalidRequest, errorDescription: nil, errorCodes: [55000], errorURI: nil, innerErrors: nil)
)

guard let innerError = response.innerErrors?.first else {
return XCTFail("There should be an inner error")
}

XCTAssertEqual(innerError.error, "invalid_purpose_token")
XCTAssertNotNil(innerError.errorDescription)
}

func test_failRequest_expiredToken() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ class MSALNativeAuthSignInInitiateIntegrationTests: MSALNativeAuthIntegrationBas
XCTAssertEqual(response?.challengeType, .redirect)
}

func test_failRequest_unauthorizedClient() async throws {
throw XCTSkip()

func test_failRequest_unauthorizedClient() async throws {
try await perform_testFail(
endpoint: .signInInitiate,
response: .unauthorizedClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ final class MSALNativeAuthSignUpChallengeIntegrationTests: MSALNativeAuthIntegra
}

func test_signUpChallenge_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .signUpChallenge,
response: .unauthorizedClient,
Expand All @@ -98,16 +96,6 @@ final class MSALNativeAuthSignUpChallengeIntegrationTests: MSALNativeAuthIntegra
)
}

func test_signUpChallenge_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .signUpChallenge,
response: .invalidContinuationToken,
expectedError: createError(.invalidRequest)
)
}

private func createError(_ error: MSALNativeAuthSignUpChallengeOauth2ErrorCode) -> MSALNativeAuthSignUpChallengeResponseError {
.init(error: error, errorDescription: nil, errorCodes: nil, errorURI: nil, innerErrors: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ final class MSALNativeAuthSignUpContinueIntegrationTests: MSALNativeAuthIntegrat
}

func test_signUpContinue_unauthorizedClient() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .signUpContinue,
response: .unauthorizedClient,
Expand All @@ -102,16 +100,6 @@ final class MSALNativeAuthSignUpContinueIntegrationTests: MSALNativeAuthIntegrat
)
}

func test_signUpContinue_invalidContinuationToken() async throws {
throw XCTSkip()

try await perform_testFail(
endpoint: .signUpContinue,
response: .invalidContinuationToken,
expectedError: createError(.invalidRequest)
)
}

func test_signUpContinue_expiredToken() async throws {
try await perform_testFail(
endpoint: .signUpContinue,
Expand Down Expand Up @@ -198,15 +186,13 @@ final class MSALNativeAuthSignUpContinueIntegrationTests: MSALNativeAuthIntegrat
}

func test_signUpContinue_validationFailed() async throws {
throw XCTSkip()

let response = try await perform_testFail(
endpoint: .signUpContinue,
response: .attributeValidationFailed,
expectedError: createError(.invalidGrant, subError: .attributeValidationFailed)
)

XCTAssertNotNil(response.continuationToken)
XCTAssertNil(response.continuationToken)
XCTAssertNotNil(response.invalidAttributes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ final class MSALNativeAuthSignUpStartIntegrationTests: MSALNativeAuthIntegration
XCTAssertEqual(response?.challengeType, .redirect)
}

func test_signUpStart_unauthorizedClient() async throws {
throw XCTSkip()

func test_signUpStart_unauthorizedClient() async throws {
try await perform_testFail(
endpoint: .signUpStart,
response: .unauthorizedClient,
Expand Down
Loading

0 comments on commit 9603f0b

Please sign in to comment.