WebUI generator GN build file fixed.
[chromium-blink-merge.git] / remoting / test / app_remoting_test_driver_environment_unittest.cc
blob58e96ea17048afd7fec76ea0aa63e862a0660981
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/test/app_remoting_test_driver_environment.h"
7 #include "remoting/test/fake_access_token_fetcher.h"
8 #include "remoting/test/mock_access_token_fetcher.h"
9 #include "remoting/test/refresh_token_store.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace {
13 const char kAuthCodeValue[] = "4/892379827345jkefvkdfbv";
14 const char kRefreshTokenValue[] = "1/lkjalseLKJlsiJgr45jbv";
15 const char kUserNameValue[] = "remoting_user@gmail.com";
16 const char kDevEnvironmentValue[] = "dev";
19 namespace remoting {
20 namespace test {
22 using testing::_;
24 // Stubs out the file API and returns fake data so we can remove
25 // file system dependencies when testing the TestDriverEnvironment.
26 class FakeRefreshTokenStore : public RefreshTokenStore {
27 public:
28 FakeRefreshTokenStore() {
29 // Set some success defaults.
30 refresh_token_value = kRefreshTokenValue;
31 refresh_token_write_attempted = false;
32 refresh_token_write_succeeded = true;
34 ~FakeRefreshTokenStore() override {}
36 std::string FetchRefreshToken() override {
37 return refresh_token_value;
40 bool StoreRefreshToken(const std::string& refresh_token) override {
41 // Record the information passed to us to write.
42 refresh_token_write_attempted = true;
43 refresh_token_value_written = refresh_token;
45 return refresh_token_write_succeeded;
48 // Control members used to return specific data to the caller.
49 std::string refresh_token_value;
50 bool refresh_token_write_succeeded;
52 // Verification members to observe the value of the data being written.
53 bool refresh_token_write_attempted;
54 std::string refresh_token_value_written;
56 DISALLOW_COPY_AND_ASSIGN(FakeRefreshTokenStore);
59 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCode) {
60 MockAccessTokenFetcher mock_access_token_fetcher;
62 mock_access_token_fetcher.SetAccessTokenFetcher(
63 make_scoped_ptr(new FakeAccessTokenFetcher()));
65 FakeRefreshTokenStore fake_token_store;
67 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
68 .Times(1);
70 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
71 .Times(0);
73 AppRemotingTestDriverEnvironment environment_object(
74 kUserNameValue,
75 kDevEnvironmentValue);
77 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
79 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
81 bool init_result = environment_object.Initialize(kAuthCodeValue);
83 EXPECT_TRUE(init_result);
84 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted);
85 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue,
86 fake_token_store.refresh_token_value_written.c_str());
87 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str());
88 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue,
89 environment_object.access_token().c_str());
91 // Attempt to init again, we should not see any additional calls or errors.
92 init_result = environment_object.Initialize(kAuthCodeValue);
93 EXPECT_TRUE(init_result);
96 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithAuthCodeFailed) {
97 MockAccessTokenFetcher mock_access_token_fetcher;
99 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher(
100 new FakeAccessTokenFetcher());
102 fake_access_token_fetcher->set_fail_access_token_from_auth_code(true);
104 mock_access_token_fetcher.SetAccessTokenFetcher(
105 fake_access_token_fetcher.Pass());
107 FakeRefreshTokenStore fake_token_store;
109 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
110 .Times(1);
112 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
113 .Times(0);
115 AppRemotingTestDriverEnvironment environment_object(
116 kUserNameValue,
117 kDevEnvironmentValue);
119 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
121 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
123 bool init_result = environment_object.Initialize(kAuthCodeValue);
125 EXPECT_FALSE(init_result);
126 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted);
129 TEST(AppRemotingTestDriverEnvironmentTest, InitializeObjectWithRefreshToken) {
130 MockAccessTokenFetcher mock_access_token_fetcher;
132 mock_access_token_fetcher.SetAccessTokenFetcher(
133 make_scoped_ptr(new FakeAccessTokenFetcher()));
135 FakeRefreshTokenStore fake_token_store;
137 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
138 .Times(1);
140 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
141 .Times(0);
143 AppRemotingTestDriverEnvironment environment_object(
144 kUserNameValue,
145 kDevEnvironmentValue);
147 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
149 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
151 // Pass in an empty auth code since we are using a refresh token.
152 bool init_result = environment_object.Initialize(std::string());
154 EXPECT_TRUE(init_result);
156 // We should not write the refresh token a second time if we read from the
157 // disk originally.
158 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted);
160 // Verify the object was initialized correctly.
161 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str());
162 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue,
163 environment_object.access_token().c_str());
165 // Attempt to init again, we should not see any additional calls or errors.
166 init_result = environment_object.Initialize(std::string());
167 EXPECT_TRUE(init_result);
170 TEST(AppRemotingTestDriverEnvironmentTest,
171 InitializeObjectWithRefreshTokenFailed) {
172 MockAccessTokenFetcher mock_access_token_fetcher;
174 scoped_ptr<FakeAccessTokenFetcher> fake_access_token_fetcher(
175 new FakeAccessTokenFetcher());
177 fake_access_token_fetcher->set_fail_access_token_from_refresh_token(true);
179 mock_access_token_fetcher.SetAccessTokenFetcher(
180 fake_access_token_fetcher.Pass());
182 FakeRefreshTokenStore fake_token_store;
184 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
185 .Times(1);
187 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
188 .Times(0);
190 AppRemotingTestDriverEnvironment environment_object(
191 kUserNameValue,
192 kDevEnvironmentValue);
194 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
196 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
198 // Pass in an empty auth code since we are using a refresh token.
199 bool init_result = environment_object.Initialize(std::string());
201 EXPECT_FALSE(init_result);
202 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted);
205 TEST(AppRemotingTestDriverEnvironmentTest,
206 InitializeObjectNoAuthCodeOrRefreshToken) {
207 MockAccessTokenFetcher mock_access_token_fetcher;
209 mock_access_token_fetcher.SetAccessTokenFetcher(
210 make_scoped_ptr(new FakeAccessTokenFetcher()));
212 FakeRefreshTokenStore fake_token_store;
214 // Neither method should be called in this scenario.
215 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
216 .Times(0);
218 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
219 .Times(0);
221 AppRemotingTestDriverEnvironment environment_object(
222 kUserNameValue,
223 kDevEnvironmentValue);
225 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
227 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
229 // Clear out the 'stored' refresh token value.
230 fake_token_store.refresh_token_value = "";
232 // Pass in an empty auth code.
233 bool init_result = environment_object.Initialize(std::string());
235 // With no auth code or refresh token, then the initialization should fail.
236 EXPECT_FALSE(init_result);
237 EXPECT_FALSE(fake_token_store.refresh_token_write_attempted);
240 TEST(AppRemotingTestDriverEnvironmentTest,
241 InitializeObjectWithAuthCodeWriteFailed) {
242 MockAccessTokenFetcher mock_access_token_fetcher;
244 mock_access_token_fetcher.SetAccessTokenFetcher(
245 make_scoped_ptr(new FakeAccessTokenFetcher()));
247 FakeRefreshTokenStore fake_token_store;
249 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
250 .Times(1);
252 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromRefreshToken(_, _))
253 .Times(0);
255 AppRemotingTestDriverEnvironment environment_object(
256 kUserNameValue,
257 kDevEnvironmentValue);
259 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
261 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
263 // Simulate a failure writing the token to the disk.
264 fake_token_store.refresh_token_write_succeeded = false;
266 bool init_result = environment_object.Initialize(kAuthCodeValue);
268 EXPECT_FALSE(init_result);
269 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted);
272 TEST(AppRemotingTestDriverEnvironmentTest,
273 RefreshAccessTokenAfterUsingAuthCode) {
274 MockAccessTokenFetcher mock_access_token_fetcher;
276 mock_access_token_fetcher.SetAccessTokenFetcher(
277 make_scoped_ptr(new FakeAccessTokenFetcher()));
279 FakeRefreshTokenStore fake_token_store;
282 testing::Sequence call_sequence;
284 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
285 .Times(1);
287 EXPECT_CALL(mock_access_token_fetcher,
288 GetAccessTokenFromRefreshToken(_, _)).Times(1);
291 AppRemotingTestDriverEnvironment environment_object(
292 kUserNameValue,
293 kDevEnvironmentValue);
295 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
297 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
299 bool init_result = environment_object.Initialize(kAuthCodeValue);
301 EXPECT_TRUE(init_result);
302 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted);
303 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue,
304 fake_token_store.refresh_token_value_written.c_str());
305 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str());
306 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue,
307 environment_object.access_token().c_str());
309 // Attempt to init again, we should not see any additional calls or errors.
310 bool refresh_result = environment_object.RefreshAccessToken();
311 EXPECT_TRUE(refresh_result);
314 TEST(AppRemotingTestDriverEnvironmentTest, RefreshAccessTokenFailure) {
315 MockAccessTokenFetcher mock_access_token_fetcher;
317 // Use a raw pointer as we want to adjust behavior after we've handed off the
318 // mock class.
319 FakeAccessTokenFetcher* fake_access_token_fetcher =
320 new FakeAccessTokenFetcher();
321 mock_access_token_fetcher.SetAccessTokenFetcher(
322 make_scoped_ptr(fake_access_token_fetcher));
324 FakeRefreshTokenStore fake_token_store;
327 testing::Sequence call_sequence;
329 // Mock is set up for this call to succeed.
330 EXPECT_CALL(mock_access_token_fetcher, GetAccessTokenFromAuthCode(_, _))
331 .Times(1);
333 // Mock is set up for this call to fail.
334 EXPECT_CALL(mock_access_token_fetcher,
335 GetAccessTokenFromRefreshToken(_, _)).Times(1);
338 AppRemotingTestDriverEnvironment environment_object(
339 kUserNameValue,
340 kDevEnvironmentValue);
342 environment_object.SetAccessTokenFetcherForTest(&mock_access_token_fetcher);
344 environment_object.SetRefreshTokenStoreForTest(&fake_token_store);
346 bool init_result = environment_object.Initialize(kAuthCodeValue);
348 EXPECT_TRUE(init_result);
349 EXPECT_TRUE(fake_token_store.refresh_token_write_attempted);
350 EXPECT_STREQ(kFakeAccessTokenFetcherRefreshTokenValue,
351 fake_token_store.refresh_token_value_written.c_str());
352 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str());
353 EXPECT_STREQ(kFakeAccessTokenFetcherAccessTokenValue,
354 environment_object.access_token().c_str());
356 fake_access_token_fetcher->set_fail_access_token_from_refresh_token(true);
358 bool refresh_result = environment_object.RefreshAccessToken();
360 // We expect the refresh to have failed, the user name to remain valid,
361 // and the access token to have been cleared.
362 EXPECT_FALSE(refresh_result);
363 EXPECT_STREQ(kUserNameValue, environment_object.user_name().c_str());
364 EXPECT_STREQ("", environment_object.access_token().c_str());
367 } // namespace test
368 } // namespace remoting