3
0

AWSClientAuthGemMock.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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. #pragma once
  9. #include <AzTest/AzTest.h>
  10. #include <AzTest/Utils.h>
  11. #include <AzFramework/IO/LocalFileIO.h>
  12. #include <AzCore/UnitTest/TestTypes.h>
  13. #include <AzCore/Memory/PoolAllocator.h>
  14. #include <AzCore/std/smart_ptr/make_shared.h>
  15. #include <AzCore/Settings/SettingsRegistry.h>
  16. #include <AzCore/Settings/SettingsRegistryImpl.h>
  17. #include <AzCore/Serialization/Json/RegistrationContext.h>
  18. #include <AzCore/Serialization/Json/JsonSystemComponent.h>
  19. #include <AzCore/Component/ComponentBus.h>
  20. #include <AzCore/Jobs/JobManager.h>
  21. #include <AzCore/Jobs/JobManagerBus.h>
  22. #include <AzCore/Jobs/JobContext.h>
  23. #include <Authentication/AWSCognitoAuthenticationProvider.h>
  24. #include <Authentication/LWAAuthenticationProvider.h>
  25. #include <Authentication/AuthenticationProviderBus.h>
  26. #include <Authorization/AWSCognitoAuthorizationBus.h>
  27. #include <UserManagement/AWSCognitoUserManagementBus.h>
  28. #include <AWSCoreBus.h>
  29. #include <ResourceMapping/AWSResourceMappingBus.h>
  30. #include <AWSClientAuthBus.h>
  31. #include <AWSNativeSDKTestManager.h>
  32. #include <HttpRequestor/HttpRequestorBus.h>
  33. #include <aws/core/utils/Outcome.h>
  34. #include <aws/cognito-idp/model/InitiateAuthRequest.h>
  35. #include <aws/cognito-idp/model/InitiateAuthResult.h>
  36. #include <aws/cognito-idp/CognitoIdentityProviderClient.h>
  37. #include <aws/cognito-idp/model/SignUpRequest.h>
  38. #include <aws/cognito-idp/model/SignUpResult.h>
  39. #include <aws/cognito-idp/model/ConfirmSignUpRequest.h>
  40. #include <aws/cognito-idp/model/ConfirmSignUpResult.h>
  41. #include <aws/cognito-idp/model/RespondToAuthChallengeRequest.h>
  42. #include <aws/cognito-idp/model/RespondToAuthChallengeResult.h>
  43. #include <aws/cognito-idp/model/ForgotPasswordResult.h>
  44. #include <aws/cognito-idp/model/ForgotPasswordRequest.h>
  45. #include <aws/cognito-idp/model/ConfirmForgotPasswordRequest.h>
  46. #include <aws/cognito-idp/model/ConfirmForgotPasswordResult.h>
  47. #include <aws/cognito-idp/model/SetUserMFAPreferenceRequest.h>
  48. #include <aws/cognito-idp/model/SetUserMFAPreferenceResult.h>
  49. #include <aws/cognito-identity/CognitoIdentityClient.h>
  50. #include <aws/cognito-identity/model/GetCredentialsForIdentityRequest.h>
  51. #include <aws/cognito-identity/model/GetCredentialsForIdentityResult.h>
  52. #include <aws/cognito-identity/model/GetIdRequest.h>
  53. #include <aws/cognito-identity/model/GetIdResult.h>
  54. namespace AWSClientAuthUnitTest
  55. {
  56. constexpr char TEST_USERNAME[] = "TestUsername";
  57. constexpr char TEST_PASSWORD[] = "TestPassword";
  58. constexpr char TEST_NEW_PASSWORD[] = "TestNewPassword";
  59. constexpr char TEST_CODE[] = "TestCode";
  60. constexpr char TEST_REGION[] = "us-east-1";
  61. constexpr char TEST_EMAIL[] = "[email protected]";
  62. constexpr char TEST_PHONE[] = "+11234567890";
  63. constexpr char TEST_COGNITO_CLIENTID[] = "TestCognitoClientId";
  64. constexpr char TEST_EXCEPTION[] = "TestException";
  65. constexpr char TEST_SESSION[] = "TestSession";
  66. constexpr char TEST_TOKEN[] = "TestToken";
  67. constexpr char TEST_ACCOUNT_ID[] = "TestAccountId";
  68. constexpr char TEST_IDENTITY_POOL_ID[] = "TestIdenitityPoolId";
  69. constexpr char TEST_IDENTITY_ID[] = "TestIdenitityId";
  70. constexpr char TEST_ACCESS_TOKEN[] = "TestAccessToken";
  71. constexpr char TEST_REFRESH_TOKEN[] = "TestRefreshToken";
  72. constexpr char TEST_ID_TOKEN[] = "TestIdToken";
  73. constexpr char TEST_ACCESS_KEY_ID[] = "TestAccessKeyId";
  74. constexpr char TEST_SECRET_KEY_ID[] = "TestSecretKeyId";
  75. constexpr char TEST_RESOURCE_NAME_ID[] = "TestResourceNameId";
  76. class AWSResourceMappingRequestBusMock
  77. : public AWSCore::AWSResourceMappingRequestBus::Handler
  78. {
  79. public:
  80. AWSResourceMappingRequestBusMock()
  81. {
  82. AWSCore::AWSResourceMappingRequestBus::Handler::BusConnect();
  83. ON_CALL(*this, GetResourceRegion).WillByDefault(testing::Return(TEST_REGION));
  84. ON_CALL(*this, GetDefaultAccountId).WillByDefault(testing::Return(TEST_ACCOUNT_ID));
  85. ON_CALL(*this, HasResource).WillByDefault(testing::Return(true));
  86. ON_CALL(*this, GetResourceAccountId).WillByDefault(testing::Return(TEST_ACCOUNT_ID));
  87. ON_CALL(*this, GetResourceNameId).WillByDefault(testing::Return(TEST_RESOURCE_NAME_ID));
  88. ON_CALL(*this, GetDefaultRegion).WillByDefault(testing::Return(TEST_REGION));
  89. }
  90. ~AWSResourceMappingRequestBusMock() override
  91. {
  92. AWSCore::AWSResourceMappingRequestBus::Handler::BusDisconnect();
  93. }
  94. MOCK_CONST_METHOD0(GetDefaultAccountId, AZStd::string());
  95. MOCK_CONST_METHOD0(GetDefaultRegion, AZStd::string());
  96. MOCK_CONST_METHOD1(HasResource, bool(const AZStd::string& resourceKeyName));
  97. MOCK_CONST_METHOD1(GetResourceAccountId, AZStd::string(const AZStd::string& resourceKeyName));
  98. MOCK_CONST_METHOD1(GetResourceNameId, AZStd::string(const AZStd::string& resourceKeyName));
  99. MOCK_CONST_METHOD1(GetResourceRegion, AZStd::string(const AZStd::string& resourceKeyName));
  100. MOCK_CONST_METHOD1(GetResourceType, AZStd::string(const AZStd::string& resourceKeyName));
  101. MOCK_CONST_METHOD1(GetServiceUrlByServiceName, AZStd::string(const AZStd::string& serviceName));
  102. MOCK_CONST_METHOD2(
  103. GetServiceUrlByRESTApiIdAndStage,
  104. AZStd::string(const AZStd::string& restApiIdKeyName, const AZStd::string& restApiStageKeyName));
  105. MOCK_METHOD1(ReloadConfigFile, void(bool isReloadingConfigFileName));
  106. };
  107. class AWSCoreRequestBusMock
  108. : public AWSCore::AWSCoreRequestBus::Handler
  109. {
  110. public:
  111. AWSCoreRequestBusMock()
  112. {
  113. AWSCore::AWSCoreRequestBus::Handler::BusConnect();
  114. ON_CALL(*this, GetDefaultJobContext).WillByDefault(testing::Return(nullptr));
  115. ON_CALL(*this, GetDefaultConfig).WillByDefault(testing::Return(nullptr));
  116. }
  117. ~AWSCoreRequestBusMock() override
  118. {
  119. AWSCore::AWSCoreRequestBus::Handler::BusDisconnect();
  120. }
  121. MOCK_METHOD0(GetDefaultJobContext, AZ::JobContext*());
  122. MOCK_METHOD0(GetDefaultConfig, AWSCore::AwsApiJobConfig*());
  123. };
  124. class HttpRequestorRequestBusMock
  125. : public HttpRequestor::HttpRequestorRequestBus::Handler
  126. {
  127. public:
  128. HttpRequestorRequestBusMock()
  129. {
  130. ON_CALL(*this, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_))
  131. .WillByDefault(testing::Invoke(this, &HttpRequestorRequestBusMock::AddRequestWithHeadersAndBodyMock));
  132. HttpRequestor::HttpRequestorRequestBus::Handler::BusConnect();
  133. }
  134. ~HttpRequestorRequestBusMock() override
  135. {
  136. HttpRequestor::HttpRequestorRequestBus::Handler::BusDisconnect();
  137. }
  138. MOCK_METHOD5(AddRequestWithHeadersAndBody, void(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const AZStd::string& body, const HttpRequestor::Callback& callback));
  139. void AddRequestWithHeadersAndBodyError(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const AZStd::string& body, const HttpRequestor::Callback& callback)
  140. {
  141. AZ_UNUSED(URI);
  142. AZ_UNUSED(method);
  143. AZ_UNUSED(headers);
  144. AZ_UNUSED(body);
  145. Aws::Utils::Json::JsonValue jsonValue;
  146. jsonValue.WithString("error", "TestError");
  147. Aws::Utils::Json::JsonView jsonView(jsonValue);
  148. Aws::Http::HttpResponseCode code = Aws::Http::HttpResponseCode::INTERNAL_SERVER_ERROR;
  149. callback(jsonView, code);
  150. }
  151. void AddRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Callback& callback) override
  152. {
  153. AZ_UNUSED(URI);
  154. AZ_UNUSED(method);
  155. AZ_UNUSED(callback);
  156. }
  157. void AddRequestWithClientConfiguration(
  158. const AZStd::string& URI,
  159. Aws::Http::HttpMethod method,
  160. const HttpRequestor::Callback& callback,
  161. const Aws::Client::ClientConfiguration clientConfiguration) override
  162. {
  163. AZ_UNUSED(URI);
  164. AZ_UNUSED(method);
  165. AZ_UNUSED(clientConfiguration);
  166. AZ_UNUSED(callback);
  167. }
  168. void AddRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const HttpRequestor::Callback& callback) override
  169. {
  170. AZ_UNUSED(URI);
  171. AZ_UNUSED(method);
  172. AZ_UNUSED(headers);
  173. AZ_UNUSED(callback);
  174. }
  175. void AddRequestWithHeadersAndClientConfiguration(
  176. const AZStd::string& URI,
  177. Aws::Http::HttpMethod method,
  178. const HttpRequestor::Headers& headers,
  179. const HttpRequestor::Callback& callback,
  180. const Aws::Client::ClientConfiguration clientConfiguration) override
  181. {
  182. AZ_UNUSED(URI);
  183. AZ_UNUSED(method);
  184. AZ_UNUSED(headers);
  185. AZ_UNUSED(clientConfiguration);
  186. AZ_UNUSED(callback);
  187. }
  188. void AddRequestWithHeadersAndBodyMock(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const AZStd::string& body, const HttpRequestor::Callback& callback)
  189. {
  190. AZ_UNUSED(URI);
  191. AZ_UNUSED(method);
  192. AZ_UNUSED(headers);
  193. AZ_UNUSED(body);
  194. Aws::Utils::Json::JsonValue jsonValue;
  195. jsonValue.WithString("user_code", "TestCode");
  196. jsonValue.WithString("device_code", "TestDeviceCode");
  197. jsonValue.WithString("verification_uri", "TestVerificationURI");
  198. jsonValue.WithString("access_token", TEST_ACCESS_TOKEN);
  199. jsonValue.WithString("refresh_token", TEST_REFRESH_TOKEN);
  200. jsonValue.WithString("id_token", TEST_ID_TOKEN);
  201. jsonValue.WithInteger("expires_in", 600);
  202. Aws::Utils::Json::JsonView jsonView(jsonValue);
  203. Aws::Http::HttpResponseCode code = Aws::Http::HttpResponseCode::OK;
  204. callback(jsonView, code);
  205. }
  206. void AddRequestWithHeadersBodyAndClientConfiguration(
  207. const AZStd::string& URI,
  208. Aws::Http::HttpMethod method,
  209. const HttpRequestor::Headers& headers,
  210. const AZStd::string& body,
  211. const HttpRequestor::Callback& callback,
  212. const Aws::Client::ClientConfiguration clientConfiguration) override
  213. {
  214. AZ_UNUSED(URI);
  215. AZ_UNUSED(method);
  216. AZ_UNUSED(headers);
  217. AZ_UNUSED(body);
  218. AZ_UNUSED(clientConfiguration);
  219. AZ_UNUSED(callback);
  220. }
  221. void AddTextRequest(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::TextCallback& callback) override
  222. {
  223. AZ_UNUSED(URI);
  224. AZ_UNUSED(method);
  225. AZ_UNUSED(callback);
  226. }
  227. void AddTextRequestWithClientConfiguration(
  228. const AZStd::string& URI,
  229. Aws::Http::HttpMethod method,
  230. const HttpRequestor::TextCallback& callback,
  231. Aws::Client::ClientConfiguration clientConfiguration) override
  232. {
  233. AZ_UNUSED(URI);
  234. AZ_UNUSED(method);
  235. AZ_UNUSED(clientConfiguration);
  236. AZ_UNUSED(callback);
  237. }
  238. void AddTextRequestWithHeaders(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const HttpRequestor::TextCallback& callback) override
  239. {
  240. AZ_UNUSED(URI);
  241. AZ_UNUSED(method);
  242. AZ_UNUSED(headers);
  243. AZ_UNUSED(callback);
  244. }
  245. void AddTextRequestWithHeadersAndClientConfiguration(
  246. const AZStd::string& URI,
  247. Aws::Http::HttpMethod method,
  248. const HttpRequestor::Headers& headers,
  249. const HttpRequestor::TextCallback& callback,
  250. const Aws::Client::ClientConfiguration clientConfiguration) override
  251. {
  252. AZ_UNUSED(URI);
  253. AZ_UNUSED(method);
  254. AZ_UNUSED(headers);
  255. AZ_UNUSED(clientConfiguration);
  256. AZ_UNUSED(callback);
  257. }
  258. void AddTextRequestWithHeadersAndBody(const AZStd::string& URI, Aws::Http::HttpMethod method, const HttpRequestor::Headers& headers, const AZStd::string& body, const HttpRequestor::TextCallback& callback) override
  259. {
  260. AZ_UNUSED(URI);
  261. AZ_UNUSED(method);
  262. AZ_UNUSED(headers);
  263. AZ_UNUSED(body);
  264. AZ_UNUSED(callback);
  265. }
  266. void AddTextRequestWithHeadersBodyAndClientConfiguration(
  267. const AZStd::string& URI,
  268. Aws::Http::HttpMethod method,
  269. const HttpRequestor::Headers& headers,
  270. const AZStd::string& body,
  271. const HttpRequestor::TextCallback& callback,
  272. const Aws::Client::ClientConfiguration clientConfiguration) override
  273. {
  274. AZ_UNUSED(URI);
  275. AZ_UNUSED(method);
  276. AZ_UNUSED(headers);
  277. AZ_UNUSED(body);
  278. AZ_UNUSED(clientConfiguration);
  279. AZ_UNUSED(callback);
  280. }
  281. AZStd::chrono::milliseconds GetLastRoundTripTime() const override
  282. {
  283. return {};
  284. }
  285. };
  286. class CognitoIdentityProviderClientMock
  287. : public Aws::CognitoIdentityProvider::CognitoIdentityProviderClient
  288. {
  289. public:
  290. CognitoIdentityProviderClientMock()
  291. : Aws::CognitoIdentityProvider::CognitoIdentityProviderClient(Aws::Auth::AWSCredentials())
  292. {
  293. ON_CALL(*this, InitiateAuth(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::InitiateAuthMock));
  294. ON_CALL(*this, SignUp(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::SignUpMock));
  295. ON_CALL(*this, ConfirmSignUp(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::ConfirmSignUpMock));
  296. ON_CALL(*this, RespondToAuthChallenge(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::RespondToAuthChallengeMock));
  297. ON_CALL(*this, ForgotPassword(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::ForgotPasswordMock));
  298. ON_CALL(*this, ConfirmForgotPassword(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::ConfirmForgotPasswordMock));
  299. ON_CALL(*this, SetUserMFAPreference(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityProviderClientMock::SetUserMFAPreferenceMock));
  300. }
  301. MOCK_CONST_METHOD1(
  302. InitiateAuth,
  303. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome(const Aws::CognitoIdentityProvider::Model::InitiateAuthRequest& request));
  304. MOCK_CONST_METHOD1(
  305. RespondToAuthChallenge,
  306. Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeOutcome(const Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeRequest& request));
  307. MOCK_CONST_METHOD1(
  308. SignUp,
  309. Aws::CognitoIdentityProvider::Model::SignUpOutcome(const Aws::CognitoIdentityProvider::Model::SignUpRequest& request));
  310. MOCK_CONST_METHOD1(
  311. ConfirmSignUp,
  312. Aws::CognitoIdentityProvider::Model::ConfirmSignUpOutcome(const Aws::CognitoIdentityProvider::Model::ConfirmSignUpRequest& request));
  313. MOCK_CONST_METHOD1(
  314. ForgotPassword,
  315. Aws::CognitoIdentityProvider::Model::ForgotPasswordOutcome(const Aws::CognitoIdentityProvider::Model::ForgotPasswordRequest& request));
  316. MOCK_CONST_METHOD1(
  317. ConfirmForgotPassword,
  318. Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordOutcome(const Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordRequest& request));
  319. MOCK_CONST_METHOD1(
  320. SetUserMFAPreference,
  321. Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceOutcome(const Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceRequest& request));
  322. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome InitiateAuthMock(const Aws::CognitoIdentityProvider::Model::InitiateAuthRequest& request)
  323. {
  324. AZ_UNUSED(request);
  325. Aws::CognitoIdentityProvider::Model::AuthenticationResultType authenticationResult;
  326. authenticationResult.SetAccessToken(TEST_ACCESS_TOKEN);
  327. authenticationResult.SetRefreshToken(TEST_REFRESH_TOKEN);
  328. authenticationResult.SetIdToken(TEST_ID_TOKEN);
  329. authenticationResult.SetExpiresIn(5);
  330. Aws::CognitoIdentityProvider::Model::InitiateAuthResult result;
  331. result.SetChallengeName(Aws::CognitoIdentityProvider::Model::ChallengeNameType::NOT_SET);
  332. result.SetAuthenticationResult(authenticationResult);
  333. Aws::CognitoIdentityProvider::Model::InitiateAuthOutcome outcome(result);
  334. return outcome;
  335. }
  336. Aws::CognitoIdentityProvider::Model::SignUpOutcome SignUpMock(const Aws::CognitoIdentityProvider::Model::SignUpRequest& request)
  337. {
  338. AZ_UNUSED(request);
  339. Aws::CognitoIdentityProvider::Model::SignUpResult result;
  340. result.SetUserSub("TestUserUUID");
  341. Aws::CognitoIdentityProvider::Model::SignUpOutcome outcome(result);
  342. return outcome;
  343. }
  344. Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeOutcome RespondToAuthChallengeMock(const Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeRequest& request)
  345. {
  346. AZ_UNUSED(request);
  347. Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeResult result;
  348. Aws::CognitoIdentityProvider::Model::AuthenticationResultType authenticationResult;
  349. authenticationResult.SetAccessToken(TEST_ACCESS_TOKEN);
  350. authenticationResult.SetRefreshToken(TEST_REFRESH_TOKEN);
  351. authenticationResult.SetIdToken(TEST_ID_TOKEN);
  352. authenticationResult.SetExpiresIn(30);
  353. result.SetAuthenticationResult(authenticationResult);
  354. Aws::CognitoIdentityProvider::Model::RespondToAuthChallengeOutcome outcome(result);
  355. return outcome;
  356. }
  357. Aws::CognitoIdentityProvider::Model::ConfirmSignUpOutcome ConfirmSignUpMock(const Aws::CognitoIdentityProvider::Model::ConfirmSignUpRequest& request)
  358. {
  359. AZ_UNUSED(request);
  360. Aws::CognitoIdentityProvider::Model::ConfirmSignUpResult result;
  361. Aws::CognitoIdentityProvider::Model::ConfirmSignUpOutcome outcome(result);
  362. return outcome;
  363. }
  364. Aws::CognitoIdentityProvider::Model::ForgotPasswordOutcome ForgotPasswordMock(const Aws::CognitoIdentityProvider::Model::ForgotPasswordRequest& request)
  365. {
  366. AZ_UNUSED(request);
  367. Aws::CognitoIdentityProvider::Model::ForgotPasswordResult result;
  368. Aws::CognitoIdentityProvider::Model::ForgotPasswordOutcome outcome(result);
  369. return outcome;
  370. }
  371. Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordOutcome ConfirmForgotPasswordMock(const Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordRequest& request)
  372. {
  373. AZ_UNUSED(request);
  374. Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordResult result;
  375. Aws::CognitoIdentityProvider::Model::ConfirmForgotPasswordOutcome outcome(result);
  376. return outcome;
  377. }
  378. Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceOutcome SetUserMFAPreferenceMock(const Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceRequest& request)
  379. {
  380. AZ_UNUSED(request);
  381. Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceResult result;
  382. Aws::CognitoIdentityProvider::Model::SetUserMFAPreferenceOutcome outcome(result);
  383. return outcome;
  384. }
  385. };
  386. class CognitoIdentityClientMock
  387. : public Aws::CognitoIdentity::CognitoIdentityClient
  388. {
  389. public:
  390. CognitoIdentityClientMock()
  391. : Aws::CognitoIdentity::CognitoIdentityClient(Aws::Auth::AWSCredentials())
  392. {
  393. ON_CALL(*this, GetId(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityClientMock::GetIdMock));
  394. ON_CALL(*this, GetCredentialsForIdentity(testing::_)).WillByDefault(testing::Invoke(this, &CognitoIdentityClientMock::GetCredentialsForIdentityMock));
  395. }
  396. MOCK_CONST_METHOD1(GetId, Aws::CognitoIdentity::Model::GetIdOutcome(const Aws::CognitoIdentity::Model::GetIdRequest& request));
  397. MOCK_CONST_METHOD1(
  398. GetCredentialsForIdentity,
  399. Aws::CognitoIdentity::Model::GetCredentialsForIdentityOutcome(const Aws::CognitoIdentity::Model::GetCredentialsForIdentityRequest& request));
  400. Aws::CognitoIdentity::Model::GetIdOutcome GetIdMock(const Aws::CognitoIdentity::Model::GetIdRequest& request)
  401. {
  402. AZ_UNUSED(request);
  403. Aws::CognitoIdentity::Model::GetIdResult result;
  404. result.SetIdentityId(TEST_IDENTITY_ID);
  405. Aws::CognitoIdentity::Model::GetIdOutcome outcome(result);
  406. return outcome;
  407. }
  408. Aws::CognitoIdentity::Model::GetCredentialsForIdentityOutcome GetCredentialsForIdentityMock(const Aws::CognitoIdentity::Model::GetCredentialsForIdentityRequest& request)
  409. {
  410. AZ_UNUSED(request);
  411. Aws::CognitoIdentity::Model::Credentials creds;
  412. creds.SetAccessKeyId(TEST_ACCESS_KEY_ID);
  413. creds.SetSecretKey(TEST_SECRET_KEY_ID);
  414. creds.SetExpiration(Aws::Utils::DateTime(std::chrono::system_clock::now() + std::chrono::seconds(600)));
  415. Aws::CognitoIdentity::Model::GetCredentialsForIdentityResult result;
  416. result.SetIdentityId(TEST_IDENTITY_ID);
  417. result.SetCredentials(creds);
  418. Aws::CognitoIdentity::Model::GetCredentialsForIdentityOutcome outcome(result);
  419. return outcome;
  420. }
  421. };
  422. class AuthenticationProviderMock
  423. : public AWSClientAuth::AuthenticationProviderInterface
  424. {
  425. public:
  426. AuthenticationProviderMock()
  427. {
  428. ON_CALL(*this, Initialize()).WillByDefault(testing::Return(true));
  429. }
  430. ~AuthenticationProviderMock() override = default;
  431. MOCK_METHOD0(Initialize, bool());
  432. MOCK_METHOD2(PasswordGrantSingleFactorSignInAsync, void(const AZStd::string& username, const AZStd::string& password));
  433. MOCK_METHOD2(PasswordGrantMultiFactorSignInAsync, void(const AZStd::string& username, const AZStd::string& password));
  434. MOCK_METHOD2(PasswordGrantMultiFactorConfirmSignInAsync, void(const AZStd::string& username, const AZStd::string& confirmationCode));
  435. MOCK_METHOD0(DeviceCodeGrantSignInAsync, void());
  436. MOCK_METHOD0(DeviceCodeGrantConfirmSignInAsync, void());
  437. MOCK_METHOD0(RefreshTokensAsync, void());
  438. MOCK_METHOD0(GetAuthenticationTokens, AWSClientAuth::AuthenticationTokens());
  439. MOCK_METHOD0(SignOut, void());
  440. };
  441. class AuthenticationProviderNotificationsBusMock
  442. : public AWSClientAuth::AuthenticationProviderNotificationBus::Handler
  443. {
  444. public:
  445. AuthenticationProviderNotificationsBusMock()
  446. {
  447. ON_CALL(*this, OnPasswordGrantSingleFactorSignInSuccess(testing::_)).WillByDefault(testing::Invoke(this, &AuthenticationProviderNotificationsBusMock::AssertAuthenticationTokensPopulated));
  448. ON_CALL(*this, OnPasswordGrantMultiFactorConfirmSignInSuccess(testing::_)).WillByDefault(testing::Invoke(this, &AuthenticationProviderNotificationsBusMock::AssertAuthenticationTokensPopulated));
  449. ON_CALL(*this, OnDeviceCodeGrantConfirmSignInSuccess(testing::_)).WillByDefault(testing::Invoke(this, &AuthenticationProviderNotificationsBusMock::AssertAuthenticationTokensPopulated));
  450. ON_CALL(*this, OnRefreshTokensSuccess(testing::_)).WillByDefault(testing::Invoke(this, &AuthenticationProviderNotificationsBusMock::AssertAuthenticationTokensPopulated));
  451. AWSClientAuth::AuthenticationProviderNotificationBus::Handler::BusConnect();
  452. }
  453. ~AuthenticationProviderNotificationsBusMock() override
  454. {
  455. AWSClientAuth::AuthenticationProviderNotificationBus::Handler::BusDisconnect();
  456. }
  457. MOCK_METHOD1(OnPasswordGrantSingleFactorSignInSuccess, void(const AWSClientAuth::AuthenticationTokens& authenticationToken));
  458. MOCK_METHOD1(OnPasswordGrantSingleFactorSignInFail, void(const AZStd::string& error));
  459. MOCK_METHOD0(OnPasswordGrantMultiFactorSignInSuccess, void());
  460. MOCK_METHOD1(OnPasswordGrantMultiFactorSignInFail, void(const AZStd::string& error));
  461. MOCK_METHOD1(OnPasswordGrantMultiFactorConfirmSignInSuccess, void(const AWSClientAuth::AuthenticationTokens& authenticationToken));
  462. MOCK_METHOD1(OnPasswordGrantMultiFactorConfirmSignInFail, void(const AZStd::string& error));
  463. MOCK_METHOD3(OnDeviceCodeGrantSignInSuccess, void(const AZStd::string& userCode, const AZStd::string& verificationUrl, int codeExpiresInSeconds));
  464. MOCK_METHOD1(OnDeviceCodeGrantSignInFail, void(const AZStd::string& error));
  465. MOCK_METHOD1(OnDeviceCodeGrantConfirmSignInSuccess, void(const AWSClientAuth::AuthenticationTokens& authenticationToken));
  466. MOCK_METHOD1(OnDeviceCodeGrantConfirmSignInFail, void(const AZStd::string& error));
  467. MOCK_METHOD1(OnRefreshTokensSuccess, void(const AWSClientAuth::AuthenticationTokens& authenticationToken));
  468. MOCK_METHOD1(OnRefreshTokensFail, void(const AZStd::string& error));
  469. MOCK_METHOD1(OnSignOut, void(const AWSClientAuth::ProviderNameEnum& providerName));
  470. private:
  471. void AssertAuthenticationTokensPopulated([[maybe_unused]] const AWSClientAuth::AuthenticationTokens& authenticationToken)
  472. {
  473. AZ_Assert(authenticationToken.GetAccessToken() == TEST_ACCESS_TOKEN, "Access token expected to match");
  474. AZ_Assert(
  475. authenticationToken.GetProviderName() == AWSClientAuth::ProviderNameEnum::LoginWithAmazon
  476. ? authenticationToken.GetOpenIdToken() == TEST_ACCESS_TOKEN
  477. : authenticationToken.GetOpenIdToken() == TEST_ID_TOKEN,
  478. "Id token expected to match");
  479. AZ_Assert(authenticationToken.GetRefreshToken() == TEST_REFRESH_TOKEN, "Refresh token expected match");
  480. AZ_Assert(authenticationToken.GetTokensExpireTimeSeconds() != 0, "Access token expiry expected to be set");
  481. AZ_Assert(authenticationToken.AreTokensValid(), "Tokens expected to be valid");
  482. }
  483. };
  484. class AWSCognitoAuthorizationNotificationsBusMock
  485. : public AWSClientAuth::AWSCognitoAuthorizationNotificationBus::Handler
  486. {
  487. public:
  488. AWSCognitoAuthorizationNotificationsBusMock()
  489. {
  490. AWSClientAuth::AWSCognitoAuthorizationNotificationBus::Handler::BusConnect();
  491. }
  492. ~AWSCognitoAuthorizationNotificationsBusMock() override
  493. {
  494. AWSClientAuth::AWSCognitoAuthorizationNotificationBus::Handler::BusDisconnect();
  495. }
  496. MOCK_METHOD1(OnRequestAWSCredentialsSuccess, void(const AWSClientAuth::ClientAuthAWSCredentials& awsCredentials));
  497. MOCK_METHOD1(OnRequestAWSCredentialsFail, void(const AZStd::string& error));
  498. };
  499. class AWSCognitoUserManagementNotificationsBusMock
  500. : public AWSClientAuth::AWSCognitoUserManagementNotificationBus::Handler
  501. {
  502. public:
  503. AWSCognitoUserManagementNotificationsBusMock()
  504. {
  505. AWSClientAuth::AWSCognitoUserManagementNotificationBus::Handler::BusConnect();
  506. }
  507. ~AWSCognitoUserManagementNotificationsBusMock() override
  508. {
  509. AWSClientAuth::AWSCognitoUserManagementNotificationBus::Handler::BusDisconnect();
  510. }
  511. MOCK_METHOD1(OnEmailSignUpSuccess, void(const AZStd::string& uuid));
  512. MOCK_METHOD1(OnEmailSignUpFail, void(const AZStd::string& error));
  513. MOCK_METHOD1(OnPhoneSignUpSuccess, void(const AZStd::string& uuid));
  514. MOCK_METHOD1(OnPhoneSignUpFail, void(const AZStd::string& error));
  515. MOCK_METHOD0(OnConfirmSignUpSuccess, void());
  516. MOCK_METHOD1(OnConfirmSignUpFail, void(const AZStd::string& error));
  517. MOCK_METHOD0(OnForgotPasswordSuccess, void());
  518. MOCK_METHOD1(OnForgotPasswordFail, void(const AZStd::string& error));
  519. MOCK_METHOD0(OnConfirmForgotPasswordSuccess, void());
  520. MOCK_METHOD1(OnConfirmForgotPasswordFail, void(const AZStd::string& error));
  521. MOCK_METHOD0(OnEnableMFASuccess, void());
  522. MOCK_METHOD1(OnEnableMFAFail, void(const AZStd::string& error));
  523. };
  524. class AWSClientAuthGemAllocatorFixture
  525. : public UnitTest::LeakDetectionFixture
  526. , public AZ::ComponentApplicationBus::Handler
  527. , public AWSClientAuth::AWSClientAuthRequestBus::Handler
  528. {
  529. public:
  530. AWSClientAuthGemAllocatorFixture()
  531. {
  532. }
  533. AWSClientAuthGemAllocatorFixture(bool connectClientAuthBus)
  534. {
  535. m_connectClientAuthBus = connectClientAuthBus;
  536. }
  537. ~AWSClientAuthGemAllocatorFixture() override = default;
  538. protected:
  539. AZStd::shared_ptr<AZ::SerializeContext> m_serializeContext;
  540. AZStd::unique_ptr<AZ::JsonRegistrationContext> m_registrationContext;
  541. AZStd::shared_ptr<AZ::SettingsRegistryImpl> m_settingsRegistry;
  542. AZStd::unique_ptr<AZStd::string> m_testFolder;
  543. bool m_testFolderCreated = false;
  544. AZStd::unique_ptr<AZ::JobContext> m_jobContext;
  545. AZStd::unique_ptr<AZ::JobCancelGroup> m_jobCancelGroup;
  546. AZStd::unique_ptr<AZ::JobManager> m_jobManager;
  547. std::shared_ptr<CognitoIdentityProviderClientMock> m_cognitoIdentityProviderClientMock;
  548. std::shared_ptr<CognitoIdentityClientMock> m_cognitoIdentityClientMock;
  549. bool m_connectClientAuthBus = true;
  550. AuthenticationProviderNotificationsBusMock m_authenticationProviderNotificationsBusMock;
  551. AWSCognitoAuthorizationNotificationsBusMock m_awsCognitoAuthorizationNotificationsBusMock;
  552. AWSCognitoUserManagementNotificationsBusMock m_awsCognitoUserManagementNotificationsBusMock;
  553. bool m_hasCognitoControllers = true;
  554. void SetUp() override
  555. {
  556. AZ::IO::FileIOBase::SetInstance(aznew AZ::IO::LocalFileIO());
  557. m_serializeContext = AZStd::make_unique<AZ::SerializeContext>();
  558. m_registrationContext = AZStd::make_unique<AZ::JsonRegistrationContext>();
  559. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  560. m_settingsRegistry = AZStd::make_unique<AZ::SettingsRegistryImpl>();
  561. m_settingsRegistry->SetContext(m_serializeContext.get());
  562. m_settingsRegistry->SetContext(m_registrationContext.get());
  563. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  564. AZ::ComponentApplicationBus::Handler::BusConnect();
  565. AZ::Interface<AZ::ComponentApplicationRequests>::Register(this);
  566. if (m_connectClientAuthBus)
  567. {
  568. AZ::Interface<IAWSClientAuthRequests>::Register(this);
  569. AWSClientAuth::AWSClientAuthRequestBus::Handler::BusConnect();
  570. }
  571. m_testFolder = AZStd::make_unique<AZStd::string>("AWSClientAuthTest_");
  572. m_testFolder->append(AZ::Uuid::CreateRandom().ToString<AZStd::string>(false, false));
  573. AZ::JobManagerDesc jobManagerDesc;
  574. AZ::JobManagerThreadDesc threadDesc;
  575. m_jobManager.reset(aznew AZ::JobManager(jobManagerDesc));
  576. m_jobCancelGroup.reset(aznew AZ::JobCancelGroup());
  577. jobManagerDesc.m_workerThreads.push_back(threadDesc);
  578. jobManagerDesc.m_workerThreads.push_back(threadDesc);
  579. jobManagerDesc.m_workerThreads.push_back(threadDesc);
  580. m_jobContext.reset(aznew AZ::JobContext(*m_jobManager, *m_jobCancelGroup));
  581. AZ::JobContext::SetGlobalContext(m_jobContext.get());
  582. AWSNativeSDKTestLibs::AWSNativeSDKTestManager::Init();
  583. m_cognitoIdentityProviderClientMock = std::make_shared<CognitoIdentityProviderClientMock>();
  584. m_cognitoIdentityClientMock = std::make_shared<CognitoIdentityClientMock>();
  585. }
  586. void TearDown() override
  587. {
  588. AZ::JobContext::SetGlobalContext(nullptr);
  589. m_jobContext.reset();
  590. m_jobCancelGroup.reset();
  591. m_jobManager.reset();
  592. m_cognitoIdentityProviderClientMock.reset();
  593. m_cognitoIdentityClientMock.reset();
  594. AWSNativeSDKTestLibs::AWSNativeSDKTestManager::Shutdown();
  595. if (m_testFolderCreated)
  596. {
  597. DeleteFolderRecursive(*m_testFolder);
  598. }
  599. m_registrationContext->EnableRemoveReflection();
  600. AZ::JsonSystemComponent::Reflect(m_registrationContext.get());
  601. m_registrationContext->DisableRemoveReflection();
  602. AZ::Interface<AZ::ComponentApplicationRequests>::Unregister(this);
  603. AZ::ComponentApplicationBus::Handler::BusDisconnect();
  604. if (m_connectClientAuthBus)
  605. {
  606. AZ::Interface<IAWSClientAuthRequests>::Unregister(this);
  607. AWSClientAuth::AWSClientAuthRequestBus::Handler::BusDisconnect();
  608. }
  609. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  610. m_testFolder.reset();
  611. m_settingsRegistry.reset();
  612. m_serializeContext.reset();
  613. m_registrationContext.reset();
  614. delete AZ::IO::FileIOBase::GetInstance();
  615. AZ::IO::FileIOBase::SetInstance(nullptr);
  616. }
  617. // ComponentApplicationBus overrides. Required by settings registry for json serialization context.
  618. AZ::ComponentApplication* GetApplication() override
  619. {
  620. return nullptr;
  621. }
  622. void RegisterComponentDescriptor(const AZ::ComponentDescriptor*) override
  623. {
  624. }
  625. void UnregisterComponentDescriptor(const AZ::ComponentDescriptor*) override
  626. {
  627. }
  628. void RegisterEntityAddedEventHandler(AZ::EntityAddedEvent::Handler&) override
  629. {
  630. }
  631. void RegisterEntityRemovedEventHandler(AZ::EntityRemovedEvent::Handler&) override
  632. {
  633. }
  634. void RegisterEntityActivatedEventHandler(AZ::EntityActivatedEvent::Handler&) override
  635. {
  636. }
  637. void RegisterEntityDeactivatedEventHandler(AZ::EntityDeactivatedEvent::Handler&) override
  638. {
  639. }
  640. void SignalEntityActivated(AZ::Entity*) override
  641. {
  642. }
  643. void SignalEntityDeactivated(AZ::Entity*) override
  644. {
  645. }
  646. bool AddEntity(AZ::Entity*) override
  647. {
  648. return true;
  649. }
  650. bool RemoveEntity(AZ::Entity*) override
  651. {
  652. return true;
  653. }
  654. bool DeleteEntity(const AZ::EntityId&) override
  655. {
  656. return true;
  657. }
  658. AZ::Entity* FindEntity(const AZ::EntityId&) override
  659. {
  660. return nullptr;
  661. }
  662. AZ::BehaviorContext* GetBehaviorContext() override
  663. {
  664. return nullptr;
  665. }
  666. const char* GetExecutableFolder() const override
  667. {
  668. return nullptr;
  669. }
  670. const char* GetEngineRoot() const override
  671. {
  672. return nullptr;
  673. }
  674. void EnumerateEntities(const EntityCallback& /*callback*/) override
  675. {
  676. }
  677. void QueryApplicationType(AZ::ApplicationTypeQuery& /*appType*/) const override
  678. {
  679. }
  680. AZ::SerializeContext* GetSerializeContext() override
  681. {
  682. return m_serializeContext.get();
  683. }
  684. AZ::JsonRegistrationContext* GetJsonRegistrationContext() override
  685. {
  686. return m_registrationContext.get();
  687. }
  688. // AWSClientAuthBus
  689. std::shared_ptr<Aws::CognitoIdentityProvider::CognitoIdentityProviderClient> GetCognitoIDPClient() override
  690. {
  691. return m_cognitoIdentityProviderClientMock;
  692. }
  693. std::shared_ptr<Aws::CognitoIdentity::CognitoIdentityClient> GetCognitoIdentityClient() override
  694. {
  695. return m_cognitoIdentityClientMock;
  696. }
  697. bool HasCognitoControllers() const override
  698. {
  699. return m_hasCognitoControllers;
  700. }
  701. // TODO Add safety check. Also use pattern to create and remove one file.
  702. static void DeleteFolderRecursive(const AZStd::string& path)
  703. {
  704. auto callback = [&path](const char* filename, bool isFile) -> bool
  705. {
  706. if (isFile)
  707. {
  708. AZStd::string filePath = path;
  709. filePath += '/';
  710. filePath += filename;
  711. AZ::IO::SystemFile::Delete(filePath.c_str());
  712. }
  713. else
  714. {
  715. if (strcmp(filename, ".") != 0 && strcmp(filename, "..") != 0)
  716. {
  717. AZStd::string folderPath = path;
  718. folderPath += '/';
  719. folderPath += filename;
  720. DeleteFolderRecursive(folderPath);
  721. }
  722. }
  723. return true;
  724. };
  725. AZStd::string searchPath = path;
  726. searchPath += "/*";
  727. AZ::IO::SystemFile::FindFiles(searchPath.c_str(), callback);
  728. AZ::IO::SystemFile::DeleteDir(path.c_str());
  729. }
  730. AZStd::string CreateTestFile(AZStd::string_view name, AZStd::string_view content)
  731. {
  732. using namespace AZ::IO;
  733. AZStd::string path = AZStd::string::format("%s/%s/%.*s", m_testFolder->c_str(),
  734. AZ::SettingsRegistryInterface::RegistryFolder, static_cast<int>(name.length()), name.data());
  735. SystemFile file;
  736. if (!file.Open(path.c_str(), SystemFile::OpenMode::SF_OPEN_CREATE | SystemFile::SF_OPEN_CREATE_PATH | SystemFile::SF_OPEN_WRITE_ONLY))
  737. {
  738. AZ_Assert(false, "Unable to open test file for writing: %s", path.c_str());
  739. return path;
  740. }
  741. if (file.Write(content.data(), content.size()) != content.size())
  742. {
  743. AZ_Assert(false, "Unable to write content to test file: %s", path.c_str());
  744. }
  745. m_testFolderCreated = true;
  746. return path;
  747. }
  748. };
  749. } // namespace AWSClientAuthUnitTest