This source file includes following definitions.
- StartAuthentication
- request_sender_
- weak_ptr_factory_
- FinishRequestWithSuccess
- passed_access_token
- passed_reauth_callback
- Start
- Cancel
- OnAuthFailed
- GetWeakPtr
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "google_apis/drive/request_sender.h"
#include "base/strings/string_number_conversions.h"
#include "google_apis/drive/base_requests.h"
#include "google_apis/drive/dummy_auth_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
namespace {
const char kTestRefreshToken[] = "valid-refresh-token";
const char kTestAccessToken[] = "valid-access-token";
enum FinishReason {
NONE,
SUCCESS,
CANCEL,
AUTH_FAILURE,
};
class TestAuthService : public DummyAuthService {
public:
TestAuthService() : auth_try_count_(0) {}
virtual void StartAuthentication(
const AuthStatusCallback& callback) OVERRIDE {
EXPECT_FALSE(HasAccessToken());
++auth_try_count_;
if (refresh_token() == kTestRefreshToken) {
const std::string token =
kTestAccessToken + base::IntToString(auth_try_count_);
set_access_token(token);
callback.Run(HTTP_SUCCESS, token);
} else {
set_access_token("");
callback.Run(HTTP_UNAUTHORIZED, "");
}
}
private:
int auth_try_count_;
};
class RequestSenderTest : public testing::Test {
protected:
RequestSenderTest()
: auth_service_(new TestAuthService),
request_sender_(auth_service_, NULL, NULL, "dummy-user-agent") {
auth_service_->set_refresh_token(kTestRefreshToken);
auth_service_->set_access_token(kTestAccessToken);
}
TestAuthService* auth_service_;
RequestSender request_sender_;
};
class TestRequest : public AuthenticatedRequestInterface {
public:
TestRequest(RequestSender* sender,
bool* start_called,
FinishReason* finish_reason)
: sender_(sender),
start_called_(start_called),
finish_reason_(finish_reason),
weak_ptr_factory_(this) {
}
void FinishRequestWithSuccess() {
*finish_reason_ = SUCCESS;
sender_->RequestFinished(this);
}
const std::string& passed_access_token() const {
return passed_access_token_;
}
const ReAuthenticateCallback& passed_reauth_callback() const {
return passed_reauth_callback_;
}
virtual void Start(const std::string& access_token,
const std::string& custom_user_agent,
const ReAuthenticateCallback& callback) OVERRIDE {
*start_called_ = true;
passed_access_token_ = access_token;
passed_reauth_callback_ = callback;
}
virtual void Cancel() OVERRIDE {
EXPECT_TRUE(*start_called_);
*finish_reason_ = CANCEL;
sender_->RequestFinished(this);
}
virtual void OnAuthFailed(GDataErrorCode code) OVERRIDE {
*finish_reason_ = AUTH_FAILURE;
sender_->RequestFinished(this);
}
virtual base::WeakPtr<AuthenticatedRequestInterface> GetWeakPtr() OVERRIDE {
return weak_ptr_factory_.GetWeakPtr();
}
private:
RequestSender* sender_;
bool* start_called_;
FinishReason* finish_reason_;
std::string passed_access_token_;
ReAuthenticateCallback passed_reauth_callback_;
base::WeakPtrFactory<TestRequest> weak_ptr_factory_;
};
}
TEST_F(RequestSenderTest, StartAndFinishRequest) {
bool start_called = false;
FinishReason finish_reason = NONE;
TestRequest* request = new TestRequest(&request_sender_,
&start_called,
&finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::Closure cancel_closure = request_sender_.StartRequestWithRetry(request);
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken, request->passed_access_token());
request->FinishRequestWithSuccess();
EXPECT_FALSE(weak_ptr);
cancel_closure.Run();
EXPECT_EQ(SUCCESS, finish_reason);
}
TEST_F(RequestSenderTest, StartAndCancelRequest) {
bool start_called = false;
FinishReason finish_reason = NONE;
TestRequest* request = new TestRequest(&request_sender_,
&start_called,
&finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::Closure cancel_closure = request_sender_.StartRequestWithRetry(request);
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
cancel_closure.Run();
EXPECT_EQ(CANCEL, finish_reason);
EXPECT_FALSE(weak_ptr);
}
TEST_F(RequestSenderTest, NoRefreshToken) {
auth_service_->ClearRefreshToken();
auth_service_->ClearAccessToken();
bool start_called = false;
FinishReason finish_reason = NONE;
TestRequest* request = new TestRequest(&request_sender_,
&start_called,
&finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::Closure cancel_closure = request_sender_.StartRequestWithRetry(request);
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_FALSE(start_called);
EXPECT_EQ(AUTH_FAILURE, finish_reason);
EXPECT_FALSE(weak_ptr);
}
TEST_F(RequestSenderTest, ValidRefreshTokenAndNoAccessToken) {
auth_service_->ClearAccessToken();
bool start_called = false;
FinishReason finish_reason = NONE;
TestRequest* request = new TestRequest(&request_sender_,
&start_called,
&finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::Closure cancel_closure = request_sender_.StartRequestWithRetry(request);
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken + std::string("1"),
request->passed_access_token());
request->FinishRequestWithSuccess();
EXPECT_EQ(SUCCESS, finish_reason);
EXPECT_FALSE(weak_ptr);
}
TEST_F(RequestSenderTest, AccessTokenRejectedSeveralTimes) {
bool start_called = false;
FinishReason finish_reason = NONE;
TestRequest* request = new TestRequest(&request_sender_,
&start_called,
&finish_reason);
base::WeakPtr<AuthenticatedRequestInterface> weak_ptr = request->GetWeakPtr();
base::Closure cancel_closure = request_sender_.StartRequestWithRetry(request);
EXPECT_TRUE(!cancel_closure.is_null());
EXPECT_TRUE(start_called);
EXPECT_EQ(kTestAccessToken, request->passed_access_token());
request->passed_reauth_callback().Run(request);
EXPECT_EQ(kTestAccessToken + std::string("1"),
request->passed_access_token());
request->passed_reauth_callback().Run(request);
EXPECT_EQ(kTestAccessToken + std::string("2"),
request->passed_access_token());
request->passed_reauth_callback().Run(request);
EXPECT_EQ(kTestAccessToken + std::string("3"),
request->passed_access_token());
request->FinishRequestWithSuccess();
EXPECT_EQ(SUCCESS, finish_reason);
EXPECT_FALSE(weak_ptr);
}
}