forked from Azure/azure-sdk-for-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_credential_test.cpp
More file actions
98 lines (78 loc) · 3.4 KB
/
token_credential_test.cpp
File metadata and controls
98 lines (78 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include <azure/core/test/test_base.hpp>
#include <azure/identity/client_secret_credential.hpp>
#include <azure/identity/environment_credential.hpp>
#include <chrono>
#include <thread>
#include <gtest/gtest.h>
namespace Azure { namespace Identity { namespace Test {
class TokenCredentialTest : public Azure::Core::Test::TestBase {
protected:
// Required to rename the test properly once the test is started.
// We can only know the test instance name until the test instance is run.
std::unique_ptr<Azure::Identity::ClientSecretCredential> GetClientSecretCredential(
std::string const& testName)
{
// set the interceptor for the current test
m_testContext.RenameTest(testName);
Azure::Core::Credentials::TokenCredentialOptions options = GetTokenCredentialOptions();
return std::make_unique<Azure::Identity::ClientSecretCredential>(
GetEnv("AZURE_TENANT_ID"),
GetEnv("AZURE_CLIENT_ID"),
GetEnv("AZURE_CLIENT_SECRET"),
options);
}
// Required to rename the test properly once the test is started.
// We can only know the test instance name until the test instance is run.
std::unique_ptr<Azure::Identity::EnvironmentCredential> GetEnvironmentCredential(
std::string const& testName)
{
// set the interceptor for the current test
m_testContext.RenameTest(testName);
Azure::Core::Credentials::TokenCredentialOptions options = GetTokenCredentialOptions();
return std::make_unique<Azure::Identity::EnvironmentCredential>(options);
}
// Runs before every test.
virtual void SetUp() override
{
Azure::Core::Test::TestBase::SetUpTestBase(AZURE_TEST_RECORDING_DIR);
}
};
}}} // namespace Azure::Identity::Test
using namespace Azure::Identity::Test;
using namespace Azure::Identity;
TEST_F(TokenCredentialTest, ClientSecret)
{
if (m_testContext.IsLiveMode())
{
GTEST_SKIP_(
"Skipping ClientSecret test since it requires env vars that aren't set in live mode.");
}
std::string const testName(GetTestName());
auto const clientSecretCredential = GetClientSecretCredential(testName);
Azure::Core::Credentials::TokenRequestContext tokenRequestContext;
tokenRequestContext.Scopes = {"https://vault.azure.net/.default"};
tokenRequestContext.MinimumExpiration = std::chrono::hours(1000000);
auto const token = clientSecretCredential->GetToken(
tokenRequestContext, Azure::Core::Context::ApplicationContext);
EXPECT_FALSE(token.Token.empty());
EXPECT_GE(token.ExpiresOn, std::chrono::system_clock::now());
}
TEST_F(TokenCredentialTest, EnvironmentCredential)
{
if (m_testContext.IsLiveMode())
{
GTEST_SKIP_("Skipping EnvironmentCredential test since it requires env vars that aren't set in "
"live mode.");
}
std::string const testName(GetTestName());
auto const clientSecretCredential = GetEnvironmentCredential(testName);
Azure::Core::Credentials::TokenRequestContext tokenRequestContext;
tokenRequestContext.Scopes = {"https://vault.azure.net/.default"};
tokenRequestContext.MinimumExpiration = std::chrono::hours(1000000);
auto const token = clientSecretCredential->GetToken(
tokenRequestContext, Azure::Core::Context::ApplicationContext);
EXPECT_FALSE(token.Token.empty());
EXPECT_GE(token.ExpiresOn, std::chrono::system_clock::now());
}