AWSCognitoAuthenticationProviderTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <Authentication/AWSCognitoAuthenticationProvider.h>
  11. #include <AWSClientAuthGemMock.h>
  12. #include <aws/core/utils/Outcome.h>
  13. #include <aws/cognito-idp/CognitoIdentityProviderErrors.h>
  14. namespace AWSClientAuthUnitTest
  15. {
  16. class AWSCognitoAuthenticationProviderrLocalMock
  17. : public AWSClientAuth::AWSCognitoAuthenticationProvider
  18. {
  19. public:
  20. using AWSClientAuth::AWSCognitoAuthenticationProvider::m_cognitoAppClientId;
  21. };
  22. }
  23. class AWSCognitoAuthenticationProviderTest
  24. : public AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture
  25. , public AWSCore::AWSCoreRequestBus::Handler
  26. {
  27. void SetUp() override
  28. {
  29. AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture::SetUp();
  30. m_cognitoAuthenticationProviderMock.Initialize();
  31. AWSCore::AWSCoreRequestBus::Handler::BusConnect();
  32. }
  33. void TearDown() override
  34. {
  35. AWSCore::AWSCoreRequestBus::Handler::BusDisconnect();
  36. AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture::TearDown();
  37. }
  38. // AWSCore::AWSCoreRequestBus overrides
  39. AZ::JobContext* GetDefaultJobContext() override
  40. {
  41. return m_jobContext.get();
  42. }
  43. // Returns the default client configuration setting to use as a starting point in AWS requests
  44. AWSCore::AwsApiJobConfig* GetDefaultConfig() override
  45. {
  46. return nullptr;
  47. }
  48. public:
  49. AWSClientAuthUnitTest::AWSCognitoAuthenticationProviderrLocalMock m_cognitoAuthenticationProviderMock;
  50. testing::NiceMock<AWSClientAuthUnitTest::AWSResourceMappingRequestBusMock> m_awsResourceMappingRequestBusMock;
  51. void AssertAuthenticationTokensPopulated()
  52. {
  53. AZ_Assert(
  54. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetAccessToken() ==
  55. "",
  56. "Access token expected to be empty");
  57. AZ_Assert(
  58. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetOpenIdToken() ==
  59. "",
  60. "Id token expected to be empty");
  61. AZ_Assert(
  62. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetRefreshToken() ==
  63. AWSClientAuthUnitTest::TEST_REFRESH_TOKEN,
  64. "Refresh token expected to match");
  65. AZ_Assert(
  66. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetTokensExpireTimeSeconds() != 0,
  67. "Access token expiry expected to be set");
  68. AZ_Assert(m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().AreTokensValid(), "Tokens expected to be valid");
  69. }
  70. void AssertAuthenticationTokensEmpty()
  71. {
  72. AZ_Assert(
  73. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetAccessToken() == "", "Access token expected to be empty");
  74. AZ_Assert(m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetOpenIdToken() == "", "Id token expected to be empty");
  75. AZ_Assert(
  76. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetRefreshToken() == "", "Refresh token expected to be empty");
  77. AZ_Assert(
  78. m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().GetTokensExpireTimeSeconds() == 0,
  79. "Access token expiry expected to be 0");
  80. AZ_Assert(!m_cognitoAuthenticationProviderMock.GetAuthenticationTokens().AreTokensValid(), "Tokens expected to be invalid");
  81. }
  82. };
  83. TEST_F(AWSCognitoAuthenticationProviderTest, Initialize_Success)
  84. {
  85. EXPECT_CALL(m_awsResourceMappingRequestBusMock, GetResourceNameId(testing::_)).Times(1);
  86. AWSClientAuthUnitTest::AWSCognitoAuthenticationProviderrLocalMock mock;
  87. ASSERT_TRUE(mock.Initialize());
  88. ASSERT_EQ(mock.m_cognitoAppClientId, AWSClientAuthUnitTest::TEST_RESOURCE_NAME_ID);
  89. }
  90. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantSingleFactorSignInAsync_Success)
  91. {
  92. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1);
  93. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInSuccess(testing::_)).Times(1);
  94. m_cognitoAuthenticationProviderMock.PasswordGrantSingleFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  95. AssertAuthenticationTokensPopulated();
  96. }
  97. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantSingleFactorSignInAsync_Fail_InitiateAuthError)
  98. {
  99. Aws::Client::AWSError<Aws::CognitoIdentityProvider::CognitoIdentityProviderErrors> error;
  100. error.SetExceptionName(AWSClientAuthUnitTest::TEST_EXCEPTION);
  101. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(error);
  102. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  103. .WillOnce(testing::Return(outcome));
  104. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInSuccess(testing::_)).Times(0);
  105. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInFail(testing::_)).Times(1);
  106. m_cognitoAuthenticationProviderMock.PasswordGrantSingleFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  107. AssertAuthenticationTokensEmpty();
  108. }
  109. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantSingleFactorSignInAsync_Fail_IncorrectChallengeTypeError)
  110. {
  111. Aws::CognitoIdentityProvider::Model::InitiateAuthResult result;
  112. result.SetChallengeName(Aws::CognitoIdentityProvider::Model::ChallengeNameType::CUSTOM_CHALLENGE);
  113. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(result);
  114. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  115. .WillOnce(testing::Return(outcome));
  116. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInSuccess(testing::_)).Times(0);
  117. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInFail(testing::_)).Times(1);
  118. m_cognitoAuthenticationProviderMock.PasswordGrantSingleFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  119. }
  120. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantMultiFactorSignInAsync_Success)
  121. {
  122. Aws::CognitoIdentityProvider::Model::InitiateAuthResult result;
  123. result.SetChallengeName(Aws::CognitoIdentityProvider::Model::ChallengeNameType::SMS_MFA);
  124. result.SetSession(AWSClientAuthUnitTest::TEST_SESSION);
  125. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(result);
  126. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  127. .WillOnce(testing::Return(outcome));
  128. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorSignInSuccess()).Times(1);
  129. m_cognitoAuthenticationProviderMock.PasswordGrantMultiFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  130. }
  131. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantMultiFactorSignInAsync_Fail_InitiateAuthError)
  132. {
  133. Aws::Client::AWSError<Aws::CognitoIdentityProvider::CognitoIdentityProviderErrors> error;
  134. error.SetExceptionName(AWSClientAuthUnitTest::TEST_EXCEPTION);
  135. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(error);
  136. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  137. .WillOnce(testing::Return(outcome));
  138. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorSignInSuccess()).Times(0);
  139. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorSignInFail(testing::_)).Times(1);
  140. m_cognitoAuthenticationProviderMock.PasswordGrantMultiFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  141. }
  142. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantMultiFactorSignInAsync_Fail_IncorrectChallengeTypeError)
  143. {
  144. Aws::CognitoIdentityProvider::Model::InitiateAuthResult result;
  145. result.SetChallengeName(Aws::CognitoIdentityProvider::Model::ChallengeNameType::CUSTOM_CHALLENGE);
  146. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(result);
  147. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  148. .WillOnce(testing::Return(outcome));
  149. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorSignInSuccess()).Times(0);
  150. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorSignInFail(testing::_)).Times(1);
  151. m_cognitoAuthenticationProviderMock.PasswordGrantMultiFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  152. }
  153. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantMultiFactorConfirmSignInAsync_Success)
  154. {
  155. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, RespondToAuthChallenge(testing::_)).Times(1);
  156. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorConfirmSignInSuccess(testing::_)).Times(1);
  157. m_cognitoAuthenticationProviderMock.PasswordGrantMultiFactorConfirmSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  158. AssertAuthenticationTokensPopulated();
  159. }
  160. TEST_F(AWSCognitoAuthenticationProviderTest, PasswordGrantMultiFactorConfirmSignInAsync_Fail_RespondToAuthChallengeError)
  161. {
  162. Aws::Client::AWSError<Aws::CognitoIdentityProvider::CognitoIdentityProviderErrors> error;
  163. error.SetExceptionName(AWSClientAuthUnitTest::TEST_EXCEPTION);
  164. Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeOutcome outcome(error);
  165. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, RespondToAuthChallenge(testing::_)).Times(1)
  166. .WillOnce(testing::Return(outcome));
  167. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorConfirmSignInSuccess(testing::_)).Times(0);
  168. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantMultiFactorConfirmSignInFail(testing::_)).Times(1);
  169. m_cognitoAuthenticationProviderMock.PasswordGrantMultiFactorConfirmSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  170. AssertAuthenticationTokensEmpty();
  171. }
  172. TEST_F(AWSCognitoAuthenticationProviderTest, RefreshTokensAsync_Success)
  173. {
  174. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1);
  175. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensSuccess(testing::_)).Times(1);
  176. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensFail(testing::_)).Times(0);
  177. m_cognitoAuthenticationProviderMock.RefreshTokensAsync();
  178. AssertAuthenticationTokensPopulated();
  179. }
  180. TEST_F(AWSCognitoAuthenticationProviderTest, RefreshTokensAsync_Fail_InitiateAuthError)
  181. {
  182. Aws::Client::AWSError<Aws::CognitoIdentityProvider::CognitoIdentityProviderErrors> error;
  183. error.SetExceptionName(AWSClientAuthUnitTest::TEST_EXCEPTION);
  184. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(error);
  185. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  186. .WillOnce(testing::Return(outcome));
  187. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensSuccess(testing::_)).Times(0);
  188. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensFail(testing::_)).Times(1);
  189. m_cognitoAuthenticationProviderMock.RefreshTokensAsync();
  190. AssertAuthenticationTokensEmpty();
  191. }
  192. TEST_F(AWSCognitoAuthenticationProviderTest, RefreshTokensAsync_Fail_IncorrectChallengeType)
  193. {
  194. Aws::CognitoIdentityProvider::Model::InitiateAuthResult result;
  195. result.SetChallengeName(Aws::CognitoIdentityProvider::Model::ChallengeNameType::CUSTOM_CHALLENGE);
  196. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(result);
  197. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1)
  198. .WillOnce(testing::Return(outcome));
  199. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensSuccess(testing::_)).Times(0);
  200. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensFail(testing::_)).Times(1);
  201. m_cognitoAuthenticationProviderMock.RefreshTokensAsync();
  202. AssertAuthenticationTokensEmpty();
  203. }
  204. TEST_F(AWSCognitoAuthenticationProviderTest, SignOut_Success)
  205. {
  206. EXPECT_CALL(*m_cognitoIdentityProviderClientMock, InitiateAuth(testing::_)).Times(1);
  207. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnPasswordGrantSingleFactorSignInSuccess(testing::_)).Times(1);
  208. m_cognitoAuthenticationProviderMock.PasswordGrantSingleFactorSignInAsync(AWSClientAuthUnitTest::TEST_USERNAME, AWSClientAuthUnitTest::TEST_PASSWORD);
  209. AssertAuthenticationTokensPopulated();
  210. m_cognitoAuthenticationProviderMock.SignOut();
  211. AssertAuthenticationTokensEmpty();
  212. }
  213. TEST_F(AWSCognitoAuthenticationProviderTest, Initialize_Fail_EmptyResourceName)
  214. {
  215. AWSClientAuthUnitTest::AWSCognitoAuthenticationProviderrLocalMock mock;
  216. EXPECT_CALL(m_awsResourceMappingRequestBusMock, GetResourceNameId(testing::_)).Times(1).WillOnce(testing::Return(""));
  217. ASSERT_FALSE(mock.Initialize());
  218. }