unit test for sts and location

This commit is contained in:
zhangzifa
2019-01-17 08:54:52 +08:00
committed by Jackson Tian
parent 7b7c45cb26
commit 6cfcc11657
6 changed files with 333 additions and 1 deletions

View File

@@ -63,9 +63,11 @@ add_executable(core_ut
url_ut.cc
utils_ut.cc
locationclient_ut.cc
location_model_describeendpoints_request_ut.cc
location_model_describeendpoints_result_ut.cc
stsclient_ut.cc
sts_model_assumerole_request_ut.cc
sts_model_assumerole_result_ut.cc
sts_model_getcalleridentity_request_ut.cc

View File

@@ -0,0 +1,174 @@
#include <iostream>
#include <stdio.h>
#include "utils.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "alibabacloud/core/AlibabaCloud.h"
#include "alibabacloud/core/SimpleCredentialsProvider.h"
#include "alibabacloud/core/location/LocationClient.h"
using namespace std;
using namespace AlibabaCloud;
using namespace AlibabaCloud::Location;
using ::testing::_;
using ::testing::DefaultValue;
namespace {
class mockLocationClient : public LocationClient {
public:
mockLocationClient(const string& key, const string& secret, const ClientConfiguration& cfg):
LocationClient(key, secret, cfg) {}
mockLocationClient(const Credentials& credentials, const ClientConfiguration& cfg):
LocationClient(credentials, cfg) {}
mockLocationClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration):
LocationClient(credentialsProvider, configuration) {}
MOCK_CONST_METHOD3(makeRequest, RpcServiceClient::JsonOutcome (const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method));
};
TEST(LocationClient, sync_ak) {
ClientConfiguration configuration("cn-hangzhou");
Model::DescribeEndpointsRequest req;
req.setId("cn-hangzhou");
req.setServiceCode("ecs");
req.setType("openAPI");
mockLocationClient client("key", "secret", configuration);
RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().success());
EXPECT_TRUE(out.result().requestId() == "test-request-id");
EXPECT_TRUE(out.result().endpoints().size() == 1);
Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
EXPECT_TRUE(ep.endpoint == "test-endpoint");
EXPECT_TRUE(ep.id == "cn-hangzhou");
EXPECT_TRUE(ep.namespace_ == "test-namespace");
EXPECT_TRUE(ep.type == "test-type");
EXPECT_TRUE(ep.protocols.size() == 2);
std::vector<std::string>::iterator it;
std::vector<std::string> protocols = ep.protocols;
it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
EXPECT_TRUE(it != protocols.end());
it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
EXPECT_TRUE(it != protocols.end());
}
//{"Endpoints":{"Endpoint":[{"Protocols":{"Protocols":["HTTP","HTTPS"]},"Type":"openAPI","Namespace":"","Id":"cn-hangzhou","SerivceCode":"ecs","Endpoint":"ecs-cn-hangzhou.aliyuncs.com"}]},"RequestId":"test-request-id","Success":true}
TEST(LocationClient, sync_credentials) {
Credentials credentials("key", "secret");
ClientConfiguration configuration("cn-hangzhou");
Model::DescribeEndpointsRequest req;
req.setId("cn-hangzhou");
req.setServiceCode("ecs");
req.setType("openAPI");
mockLocationClient client(credentials, configuration);
RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().success());
EXPECT_TRUE(out.result().requestId() == "test-request-id");
EXPECT_TRUE(out.result().endpoints().size() == 1);
Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
EXPECT_TRUE(ep.endpoint == "test-endpoint");
EXPECT_TRUE(ep.id == "cn-hangzhou");
EXPECT_TRUE(ep.namespace_ == "test-namespace");
EXPECT_TRUE(ep.type == "test-type");
EXPECT_TRUE(ep.protocols.size() == 2);
std::vector<std::string>::iterator it;
std::vector<std::string> protocols = ep.protocols;
it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
EXPECT_TRUE(it != protocols.end());
it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
EXPECT_TRUE(it != protocols.end());
}
TEST(LocationClient, sync_credentials_ptr) {
Credentials credentials("key", "secret");
ClientConfiguration configuration("cn-hangzhou");
Model::DescribeEndpointsRequest req;
req.setId("cn-hangzhou");
req.setServiceCode("ecs");
req.setType("openAPI");
mockLocationClient client(std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().success());
EXPECT_TRUE(out.result().requestId() == "test-request-id");
EXPECT_TRUE(out.result().endpoints().size() == 1);
Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
EXPECT_TRUE(ep.endpoint == "test-endpoint");
EXPECT_TRUE(ep.id == "cn-hangzhou");
EXPECT_TRUE(ep.namespace_ == "test-namespace");
EXPECT_TRUE(ep.type == "test-type");
EXPECT_TRUE(ep.protocols.size() == 2);
std::vector<std::string>::iterator it;
std::vector<std::string> protocols = ep.protocols;
it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
EXPECT_TRUE(it != protocols.end());
it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
EXPECT_TRUE(it != protocols.end());
}
TEST(LocationClient, callable) {
InitializeSdk();
ClientConfiguration configuration("cn-hangzhou");
Model::DescribeEndpointsRequest req;
req.setId("cn-hangzhou");
req.setServiceCode("ecs");
req.setType("openAPI");
mockLocationClient client("key", "secret", configuration);
RpcServiceClient::JsonOutcome xout("{\"Endpoints\":{\"Endpoint\":[{\"Protocols\":{\"Protocols\":[\"HTTP\",\"HTTPS\"]},\"Type\":\"test-type\",\"Namespace\":\"test-namespace\",\"Id\":\"cn-hangzhou\",\"SerivceCode\":\"test-servicecode\",\"Endpoint\":\"test-endpoint\"}]},\"RequestId\":\"test-request-id\",\"Success\":true}");
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
LocationClient::DescribeEndpointsOutcomeCallable cb = client.describeEndpointsCallable(req);
LocationClient::DescribeEndpointsOutcome out = cb.get();
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().success());
EXPECT_TRUE(out.result().requestId() == "test-request-id");
EXPECT_TRUE(out.result().endpoints().size() == 1);
Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
EXPECT_TRUE(ep.endpoint == "test-endpoint");
EXPECT_TRUE(ep.id == "cn-hangzhou");
EXPECT_TRUE(ep.namespace_ == "test-namespace");
EXPECT_TRUE(ep.type == "test-type");
EXPECT_TRUE(ep.protocols.size() == 2);
std::vector<std::string>::iterator it;
std::vector<std::string> protocols = ep.protocols;
it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
EXPECT_TRUE(it != protocols.end());
it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
EXPECT_TRUE(it != protocols.end());
ShutdownSdk();
}
}

154
test/core/stsclient_ut.cc Normal file
View File

@@ -0,0 +1,154 @@
#include <iostream>
#include <stdio.h>
#include "utils.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "alibabacloud/core/AlibabaCloud.h"
#include "alibabacloud/core/SimpleCredentialsProvider.h"
#include "alibabacloud/core/sts/StsClient.h"
using ::testing::_;
using ::testing::DefaultValue;
using namespace std;
using namespace AlibabaCloud;
using namespace AlibabaCloud::Sts;
namespace {
class mockStsClient : public StsClient {
public:
mockStsClient(const string& key, const string& secret, const ClientConfiguration& cfg):
StsClient(key, secret, cfg) {}
mockStsClient(const Credentials& credentials, const ClientConfiguration& cfg):
StsClient(credentials, cfg) {}
mockStsClient(const std::shared_ptr<CredentialsProvider> &credentialsProvider, const ClientConfiguration &configuration):
StsClient(credentialsProvider, configuration) {}
MOCK_CONST_METHOD3(makeRequest, RpcServiceClient::JsonOutcome (const std::string &endpoint, const RpcServiceRequest &msg, HttpRequest::Method method));
};
TEST(StsClient, getCallerIdentity_ak) {
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}");
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::GetCallerIdentityRequest req;
StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().accountId() == "123456789");
EXPECT_TRUE(out.result().userId() == "any-id");
EXPECT_TRUE(out.result().arn() == "any-arn");
}
TEST(StsClient, getCallerIdentity_credentials) {
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}");
Credentials credentials("key", "secret");
mockStsClient client(credentials, configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::GetCallerIdentityRequest req;
StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().accountId() == "123456789");
EXPECT_TRUE(out.result().userId() == "any-id");
EXPECT_TRUE(out.result().arn() == "any-arn");
}
TEST(StsClient, getCallerIdentity_credentials_ptr) {
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}");
Credentials credentials("key", "secret");
mockStsClient client(std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::GetCallerIdentityRequest req;
StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().accountId() == "123456789");
EXPECT_TRUE(out.result().userId() == "any-id");
EXPECT_TRUE(out.result().arn() == "any-arn");
}
TEST(StsClient, getCallerIdentity_ak_error) {
InitializeSdk();
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout(Error("any-error-code", "any-error-message"));
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::GetCallerIdentityRequest req;
StsClient::GetCallerIdentityOutcome out = client.getCallerIdentity(req);
EXPECT_TRUE(out.error().errorCode() == "any-error-code");
ShutdownSdk();
}
TEST(StsClient, getCallerIdentity_callable) {
InitializeSdk();
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"Request\":\"test-req-id\",\"AccountId\":\"123456789\",\"Arn\":\"any-arn\",\"UserId\":\"any-id\"}");
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::GetCallerIdentityRequest req;
StsClient::GetCallerIdentityOutcomeCallable cb = client.getCallerIdentityCallable(req);
StsClient::GetCallerIdentityOutcome out = cb.get();
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().accountId() == "123456789");
EXPECT_TRUE(out.result().userId() == "any-id");
EXPECT_TRUE(out.result().arn() == "any-arn");
ShutdownSdk();
}
TEST(StsClient, assumeRole) {
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}");
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::AssumeRoleRequest req;
StsClient::AssumeRoleOutcome out = client.assumeRole(req);
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().assumedRoleUser().arn == "any-arn");
EXPECT_TRUE(out.result().assumedRoleUser().assumedRoleId == "123456789");
EXPECT_TRUE(out.result().credentials().accessKeyId == "any-key");
EXPECT_TRUE(out.result().credentials().accessKeySecret == "any-secret");
EXPECT_TRUE(out.result().credentials().expiration == "2019-01-15T16:31:10Z");
EXPECT_TRUE(out.result().credentials().securityToken == "any-token");
}
TEST(StsClient, assumeRole_callable) {
InitializeSdk();
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}");
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::AssumeRoleRequest req;
StsClient::AssumeRoleOutcomeCallable cb = client.assumeRoleCallable(req);
StsClient::AssumeRoleOutcome out = cb.get();
EXPECT_TRUE(out.error().errorCode().empty());
EXPECT_TRUE(out.result().assumedRoleUser().arn == "any-arn");
EXPECT_TRUE(out.result().assumedRoleUser().assumedRoleId == "123456789");
EXPECT_TRUE(out.result().credentials().accessKeyId == "any-key");
EXPECT_TRUE(out.result().credentials().accessKeySecret == "any-secret");
EXPECT_TRUE(out.result().credentials().expiration == "2019-01-15T16:31:10Z");
EXPECT_TRUE(out.result().credentials().securityToken == "any-token");
ShutdownSdk();
}
TEST(StsClient, assumeRole_error) {
ClientConfiguration configuration;
RpcServiceClient::JsonOutcome xout(Error("any-error-code", "any-error-message"));
mockStsClient client("mock-key", "mock-secret", configuration);
DefaultValue<RpcServiceClient::JsonOutcome>::Set(xout);
EXPECT_CALL(client, makeRequest(_, _, _));
Model::AssumeRoleRequest req;
StsClient::AssumeRoleOutcome out = client.assumeRole(req);
EXPECT_TRUE(out.error().errorCode() == "any-error-code");
}
}