Skip to content

Commit

Permalink
add enum for clientId key, add more log in code retriever, generate e…
Browse files Browse the repository at this point in the history
…mail in codeRetriever class
  • Loading branch information
nilo-ms committed Jul 1, 2024
1 parent 5983d0f commit 04f38d5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 25 deletions.
4 changes: 4 additions & 0 deletions MSAL/MSAL.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
23F32F0C1FF4789100B2905E /* MSIDTestURLResponse+MSAL.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F32F061FF4787600B2905E /* MSIDTestURLResponse+MSAL.m */; };
23F32F0D1FF4789200B2905E /* MSIDTestURLResponse+MSAL.m in Sources */ = {isa = PBXBuildFile; fileRef = 23F32F061FF4787600B2905E /* MSIDTestURLResponse+MSAL.m */; };
23FB5C1E22542B99002BF1EB /* MSALJsonDeserializable.h in Headers */ = {isa = PBXBuildFile; fileRef = 23FB5C1C22542B99002BF1EB /* MSALJsonDeserializable.h */; settings = {ATTRIBUTES = (Public, ); }; };
280095EB2C32CAFC00F1653E /* ClientIdType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 280095EA2C32CAFC00F1653E /* ClientIdType.swift */; };
281A0E0C2C21E1F000CB30CB /* MSALNativeAuthSignUpUsernameEndToEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E272C4EA2A4447520013B805 /* MSALNativeAuthSignUpUsernameEndToEndTests.swift */; };
281A0E142C21E1F200CB30CB /* MSALNativeAuthSignUpUsernameAndPasswordEndToEndTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26E391A2A4C2BE200063C07 /* MSALNativeAuthSignUpUsernameAndPasswordEndToEndTests.swift */; };
281A0E152C21E1F500CB30CB /* SignUpDelegateSpies.swift in Sources */ = {isa = PBXBuildFile; fileRef = E26E39232A4C2D7400063C07 /* SignUpDelegateSpies.swift */; };
Expand Down Expand Up @@ -1553,6 +1554,7 @@
23F32F051FF4787600B2905E /* MSIDTestURLResponse+MSAL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MSIDTestURLResponse+MSAL.h"; sourceTree = "<group>"; };
23F32F061FF4787600B2905E /* MSIDTestURLResponse+MSAL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MSIDTestURLResponse+MSAL.m"; sourceTree = "<group>"; };
23FB5C1C22542B99002BF1EB /* MSALJsonDeserializable.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MSALJsonDeserializable.h; sourceTree = "<group>"; };
280095EA2C32CAFC00F1653E /* ClientIdType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientIdType.swift; sourceTree = "<group>"; };
282693272A0974740037B93A /* MSALNativeAuthTokenRequestParameters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MSALNativeAuthTokenRequestParameters.swift; sourceTree = "<group>"; };
2826933A2A0B98750037B93A /* MSALNativeAuthSignInParameters.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSALNativeAuthSignInParameters.swift; sourceTree = "<group>"; };
285D0D682B99C14F002A1D4A /* MSALNativeAuthTokenResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MSALNativeAuthTokenResult.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2805,6 +2807,7 @@
DEFB46E12A52B9B800DBC006 /* reset_password */,
DEFB46EB2A52BA1300DBC006 /* sign_out */,
9B235D9E2A3CFB4300657331 /* MSALNativeAuthEndToEndBaseTestCase.swift */,
280095EA2C32CAFC00F1653E /* ClientIdType.swift */,
);
path = end_to_end;
sourceTree = "<group>";
Expand Down Expand Up @@ -5560,6 +5563,7 @@
281A0E162C21E1F800CB30CB /* MSALNativeAuthSignInUsernameAndPasswordEndToEndTests.swift in Sources */,
281A0E142C21E1F200CB30CB /* MSALNativeAuthSignUpUsernameAndPasswordEndToEndTests.swift in Sources */,
281A0E1A2C21E20300CB30CB /* ResetPasswordDelegateSpies.swift in Sources */,
280095EB2C32CAFC00F1653E /* ClientIdType.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
33 changes: 33 additions & 0 deletions MSAL/test/integration/native_auth/end_to_end/ClientIdType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


import Foundation

enum ClientIdType {
case password
case passwordAndAttributes
case code
case codeAndAttributes
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import XCTest
import MSAL

class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {
class Constants {
private class Constants {
static let nativeAuthKey = "native_auth"
static let clientIdEmailPasswordKey = "email_password_client_id"
static let clientIdEmailCodeKey = "email_code_client_id"
Expand Down Expand Up @@ -56,18 +56,10 @@ class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {
}

func initialisePublicClientApplication(
useEmailPasswordClientId: Bool = true,
attributeRequiredClientId: Bool = false,
clientIdType: ClientIdType = .password,
challengeTypes: MSALNativeAuthChallengeTypes = [.OOB, .password]
) -> MSALNativeAuthPublicClientApplication? {
var clientIdKey = Constants.clientIdEmailPasswordKey
if useEmailPasswordClientId && attributeRequiredClientId {
clientIdKey = Constants.clientIdEmailPasswordAttributesKey
} else if !useEmailPasswordClientId && attributeRequiredClientId {
clientIdKey = Constants.clientIdEmailCodeAttributesKey
} else if !useEmailPasswordClientId && !attributeRequiredClientId {
clientIdKey = Constants.clientIdEmailCodeKey
}
let clientIdKey = getClientIdKey(type: clientIdType)
guard let clientId = confFileContent?[clientIdKey] as? String, let tenantSubdomain = confFileContent?[Constants.tenantSubdomainKey] as? String else {
XCTFail("ClientId or tenantSubdomain not found in conf.json")
return nil
Expand All @@ -76,8 +68,7 @@ class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {
}

func generateSignUpRandomEmail() -> String {
let randomId = UUID().uuidString.prefix(8)
return "native-auth-signup-\(randomId)@1secmail.org"
return codeRetriever.generateRandomEmailAddress()
}

func retrieveCodeFor(email: String) async -> String? {
Expand All @@ -87,4 +78,17 @@ class MSALNativeAuthEndToEndBaseTestCase: XCTestCase {
func getSignInUsernamePassword() -> String? {
return confFileContent?[Constants.signInEmailPasswordUsernameKey]
}

private func getClientIdKey(type: ClientIdType) -> String {
switch type {
case .password:
return Constants.clientIdEmailPasswordKey
case .passwordAndAttributes:
return Constants.clientIdEmailPasswordAttributesKey
case .code:
return Constants.clientIdEmailCodeKey
case .codeAndAttributes:
return Constants.clientIdEmailCodeAttributesKey
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class MSALNativeAuthEmailCodeRetriever: XCTestCase {
}
return emailOTPCode
}

func generateRandomEmailAddress() -> String {
let randomId = UUID().uuidString.prefix(8)
return "native-auth-signup-\(randomId)@1secmail.org"
}

private func retrieveLastMessage(local: String, domain: String, retryCounter: Int) async -> Int? {
guard retryCounter > 0, let url = URL(string: baseURLString + "getMessages&login=\(local)&domain=\(domain)") else {
Expand All @@ -60,15 +65,23 @@ class MSALNativeAuthEmailCodeRetriever: XCTestCase {
do {
let (data, response) = try await URLSession.shared.data(for: request)

let code = (response as? HTTPURLResponse)?.statusCode ?? 0
guard let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200,
var dataDictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] else {
httpResponse.statusCode == 200 else {
print("Unexpected response from 1secmail: \(code) status code")
return nil
}
guard var dataDictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] else {
return nil
}
if dataDictionary.count > 0 {
dataDictionary.sort(by: {($0["id"] as? Int ?? 0) > ($1["id"] as? Int ?? 0)})
return dataDictionary.first?["id"] as? Int
} else {
// log only for the final retry
if (retryCounter == 1) {
print("Unexpected behaviour: no email received for the following local: \(local)")
}
// no emails found, retry
return await retrieveLastMessage(local: local, domain: domain, retryCounter: retryCounter - 1)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class MSALNativeAuthSignInUsernameAndPasswordEndToEndTests: MSALNativeAuth
}

func test_signInAndSendingIncorrectPasswordResultsInError() async throws {
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false), let username = getSignInUsernamePassword() else {
guard let sut = initialisePublicClientApplication(clientIdType: .code), let username = getSignInUsernamePassword() else {
XCTFail("Missing information")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import XCTest

final class MSALNativeAuthSignInUsernameEndToEndTests: MSALNativeAuthEndToEndBaseTestCase {
func test_signInWithUnknownUsernameResultsInError() async throws {
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand All @@ -47,7 +47,7 @@ final class MSALNativeAuthSignInUsernameEndToEndTests: MSALNativeAuthEndToEndBas

func test_signInAndSendingIncorrectOTPResultsInError() async throws {
throw XCTSkip("Skipping this test because email+code signIn username is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down Expand Up @@ -84,7 +84,7 @@ final class MSALNativeAuthSignInUsernameEndToEndTests: MSALNativeAuthEndToEndBas
// Hero Scenario 1.2.1. Sign in (Email & Email OTP)
func test_signInAndSendingCorrectOTPResultsInSuccess() async throws {
throw XCTSkip("Skipping this test because email+code signIn username is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ final class MSALNativeAuthSignUpUsernameAndPasswordEndToEndTests: MSALNativeAuth
// Hero Scenario 2.2.2. Sign in – Email and Password on MULTIPLE screens (Email & Password)
func test_signInAndSendingCorrectPasswordResultsInSuccess() async throws {
throw XCTSkip("Skipping this test because native auth KeyVault is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas

// Hero Scenario 1.1.1. Sign up – with Email Verification (Email & Email OTP)
func test_signUpWithCode_withEmailVerification_succeeds() async throws {
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down Expand Up @@ -76,7 +76,7 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas
// Hero Scenario 1.1.2. Sign up – with Email Verification as LAST step & Custom Attributes (Email & Email OTP)
func test_signUpWithCode_withEmailVerificationAsLastStepAndCustomAttributes_succeeds() async throws {
throw XCTSkip("Skipping this test because application with custom attributes is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down Expand Up @@ -113,7 +113,7 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas
// Hero Scenario 1.1.3. Sign up – with Email Verification as FIRST step & Custom Attributes (Email & Email OTP)
func test_signUpWithCode_withEmailVerificationAsFirstStepAndCustomAttributes_succeeds() async throws {
throw XCTSkip("Skipping this test because application with custom attributes is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down Expand Up @@ -163,7 +163,7 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas
// Hero Scenario 1.1.4. Sign up – with Email Verification as FIRST step & Custom Attributes over MULTIPLE screens (Email & Email OTP)
func test_signUpWithCode_withEmailVerificationAsLastStepAndCustomAttributesOverMultipleScreens_succeeds() async throws {
throw XCTSkip("Skipping this test because application with custom attributes is missing")
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down Expand Up @@ -229,7 +229,7 @@ final class MSALNativeAuthSignUpUsernameEndToEndTests: MSALNativeAuthEndToEndBas

// Hero Scenario 1.1.5. Sign up – without automatic sign in (Email & Email OTP)
func test_signUpWithoutAutomaticSignIn() async throws {
guard let sut = initialisePublicClientApplication(useEmailPasswordClientId: false) else {
guard let sut = initialisePublicClientApplication(clientIdType: .code) else {
XCTFail("Missing information")
return
}
Expand Down

0 comments on commit 04f38d5

Please sign in to comment.