LWAAuthenticationProviderTest.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/Debug/Trace.h>
  10. #include <AzCore/UnitTest/TestTypes.h>
  11. #include <AzCore/std/smart_ptr/make_shared.h>
  12. #include <Authentication/LWAAuthenticationProvider.h>
  13. #include <AWSClientAuthGemMock.h>
  14. namespace AWSClientAuthUnitTest
  15. {
  16. class LWAAuthenticationProviderLocalMock
  17. : public AWSClientAuth::LWAAuthenticationProvider
  18. {
  19. public:
  20. using AWSClientAuth::LWAAuthenticationProvider::m_settings;
  21. };
  22. }
  23. class LWAAuthenticationProviderTest
  24. : public AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture
  25. {
  26. void SetUp() override
  27. {
  28. AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture::SetUp();
  29. AWSClientAuth::LWAProviderSetting::Reflect(*m_serializeContext);
  30. AZStd::string path = AZStd::string::format("%s/%s/awsCognitoAuthorization.setreg",
  31. m_testFolder->c_str(), AZ::SettingsRegistryInterface::RegistryFolder);
  32. CreateTestFile("awsCognitoAuthorization.setreg"
  33. , R"({"AWS":
  34. {
  35. "LoginWithAmazon":
  36. {
  37. "AppClientId": "TestLWAClientId",
  38. "GrantType": "device_code",
  39. "Scope": "profile",
  40. "ResponseType": "device_code",
  41. "OAuthCodeURL": "https://api.amazon.com/auth/o2/create/codepair",
  42. "OAuthTokensURL": "https://oauth2.googleapis.com/token"
  43. }
  44. }
  45. })");
  46. m_settingsRegistry->MergeSettingsFile(path, AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  47. m_lwaAuthenticationProviderLocalMock.Initialize();
  48. }
  49. void TearDown() override
  50. {
  51. AWSClientAuthUnitTest::AWSClientAuthGemAllocatorFixture::TearDown();
  52. }
  53. public:
  54. AWSClientAuthUnitTest::LWAAuthenticationProviderLocalMock m_lwaAuthenticationProviderLocalMock;
  55. AWSClientAuthUnitTest::HttpRequestorRequestBusMock m_httpRequestorRequestBusMock;
  56. };
  57. TEST_F(LWAAuthenticationProviderTest, Initialize_Success)
  58. {
  59. AWSClientAuthUnitTest::LWAAuthenticationProviderLocalMock mock;
  60. ASSERT_TRUE(mock.Initialize());
  61. ASSERT_EQ(mock.m_settings->m_appClientId, "TestLWAClientId");
  62. }
  63. TEST_F(LWAAuthenticationProviderTest, DeviceCodeGrantSignInAsync_Success)
  64. {
  65. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1);
  66. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantSignInSuccess(testing::_, testing::_, testing::_)).Times(1);
  67. m_lwaAuthenticationProviderLocalMock.DeviceCodeGrantSignInAsync();
  68. }
  69. TEST_F(LWAAuthenticationProviderTest, DeviceCodeGrantSignInAsync_RequestHttpError)
  70. {
  71. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1)
  72. .WillOnce(testing::Invoke(&m_httpRequestorRequestBusMock, &AWSClientAuthUnitTest::HttpRequestorRequestBusMock::AddRequestWithHeadersAndBodyError));
  73. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantSignInSuccess(testing::_, testing::_, testing::_)).Times(0);
  74. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantSignInFail(testing::_)).Times(1);
  75. m_lwaAuthenticationProviderLocalMock.DeviceCodeGrantSignInAsync();
  76. }
  77. TEST_F(LWAAuthenticationProviderTest, DeviceCodeGrantConfirmSignInAsync_Success)
  78. {
  79. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1);
  80. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantConfirmSignInSuccess(testing::_)).Times(1);
  81. m_lwaAuthenticationProviderLocalMock.DeviceCodeGrantConfirmSignInAsync();
  82. }
  83. TEST_F(LWAAuthenticationProviderTest, DeviceCodeGrantConfirmSignInAsync_Fail_RequestHttpError)
  84. {
  85. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1)
  86. .WillOnce(testing::Invoke(&m_httpRequestorRequestBusMock, &AWSClientAuthUnitTest::HttpRequestorRequestBusMock::AddRequestWithHeadersAndBodyError));
  87. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantConfirmSignInSuccess(testing::_)).Times(0);
  88. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnDeviceCodeGrantConfirmSignInFail(testing::_)).Times(1);
  89. m_lwaAuthenticationProviderLocalMock.DeviceCodeGrantConfirmSignInAsync();
  90. }
  91. TEST_F(LWAAuthenticationProviderTest, RefreshTokensAsync_Success)
  92. {
  93. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1);
  94. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensSuccess(testing::_)).Times(1);
  95. m_lwaAuthenticationProviderLocalMock.RefreshTokensAsync();
  96. }
  97. TEST_F(LWAAuthenticationProviderTest, RefreshTokensAsync_Fail_RequestHttpError)
  98. {
  99. EXPECT_CALL(m_httpRequestorRequestBusMock, AddRequestWithHeadersAndBody(testing::_, testing::_, testing::_, testing::_, testing::_)).Times(1)
  100. .WillOnce(testing::Invoke(&m_httpRequestorRequestBusMock, &AWSClientAuthUnitTest::HttpRequestorRequestBusMock::AddRequestWithHeadersAndBodyError));
  101. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensSuccess(testing::_)).Times(0);
  102. EXPECT_CALL(m_authenticationProviderNotificationsBusMock, OnRefreshTokensFail(testing::_)).Times(1);
  103. m_lwaAuthenticationProviderLocalMock.RefreshTokensAsync();
  104. }
  105. TEST_F(LWAAuthenticationProviderTest, Initialize_Fail_EmptyRegistry)
  106. {
  107. AZ::SettingsRegistry::Unregister(m_settingsRegistry.get());
  108. AZStd::shared_ptr<AZ::SettingsRegistryImpl> registry = AZStd::make_shared<AZ::SettingsRegistryImpl>();
  109. registry->SetContext(m_serializeContext.get());
  110. AZ::SettingsRegistry::Register(registry.get());
  111. AWSClientAuthUnitTest::LWAAuthenticationProviderLocalMock mock;
  112. ASSERT_FALSE(mock.Initialize());
  113. ASSERT_EQ(mock.m_settings->m_appClientId, "");
  114. AZ::SettingsRegistry::Unregister(registry.get());
  115. registry.reset();
  116. // Restore
  117. AZ::SettingsRegistry::Register(m_settingsRegistry.get());
  118. mock.Initialize();
  119. }