Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: chrome/browser/chromeos/arc/arc_auth_service_unittest.cc

Issue 1618193003: arc: Pass auth token from Chrome to ARC instance. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored. Added LSO UI Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include <string>
6 #include <vector>
7
8 #include "base/bind.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h"
13 #include "base/run_loop.h"
14 #include "chrome/browser/chromeos/arc/arc_auth_fetcher.h"
15 #include "chrome/browser/chromeos/arc/arc_auth_service.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "components/arc/arc_bridge_service.h"
18 #include "components/arc/test/fake_arc_bridge_service.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "google_apis/gaia/gaia_constants.h"
22 #include "google_apis/gaia/gaia_urls.h"
23 #include "net/http/http_status_code.h"
24 #include "net/url_request/test_url_fetcher_factory.h"
25 #include "net/url_request/url_fetcher.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace arc {
29
30 namespace {
31
32 const int kThreadOptions = content::TestBrowserThreadBundle::IO_MAINLOOP;
33 const char kTestAuthCode[] = "4/Qa3CPIhh-WcMfWSf9HZaYcGUhEeax-F9sQK9CNRhZWs";
34
35 scoped_ptr<TestingProfile> CreateProfile() {
36 base::ScopedTempDir temp_dir;
xiyuan 2016/01/26 23:37:05 ScopedTempDir would auto delete the temp dir when
khmel 2016/01/27 22:36:21 Yes, overlooked this. Thanks.
37 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
38 base::FilePath path = temp_dir.path().AppendASCII("TestArcProfile");
39 EXPECT_TRUE(base::DeleteFile(path, true));
40 EXPECT_TRUE(base::CreateDirectory(path));
41
42 TestingProfile::Builder profile_builder;
43 profile_builder.SetPath(path);
44 return profile_builder.Build();
45 }
46
47 } // namespace
48
49 class ArcAuthServiceTest : public testing::Test {
50 public:
51 ArcAuthServiceTest()
52 : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions)),
53 url_fetcher_factory_(
54 nullptr,
55 base::Bind(&ArcAuthServiceTest::FakeURLFetcherCreator,
56 base::Unretained(this))) {}
57 ~ArcAuthServiceTest() override {}
58
59 void SetUp() override {
60 ArcAuthService::DisableUIForTesting();
61 profile_ = CreateProfile();
62 bridge_service_.reset(new FakeArcBridgeService());
63 auth_service_.reset(new ArcAuthService(bridge_service_.get()));
64
65 // Check initial conditions.
66 EXPECT_EQ(bridge_service_.get(), ArcBridgeService::Get());
67 EXPECT_EQ(true, !ArcBridgeService::Get()->available());
68 EXPECT_EQ(ArcBridgeService::State::STOPPED,
69 ArcBridgeService::Get()->state());
70 }
71
72 void TearDown() override {}
73
74 protected:
75 Profile* profile() { return profile_.get(); }
76 FakeArcBridgeService* bridge_service() { return bridge_service_.get(); }
77 ArcAuthService* auth_service() { return auth_service_.get(); }
78 net::FakeURLFetcherFactory& url_fetcher_factory() {
79 return url_fetcher_factory_;
80 }
81 void set_cookie(const std::string& cookie) { rt_cookie_ = cookie; }
82
83 private:
84 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
85 net::FakeURLFetcherFactory url_fetcher_factory_;
86 scoped_ptr<arc::FakeArcBridgeService> bridge_service_;
87 scoped_ptr<arc::ArcAuthService> auth_service_;
88 scoped_ptr<TestingProfile> profile_;
89 std::string rt_cookie_;
xiyuan 2016/01/26 23:37:05 Data bembers are always declared last, i.e. move t
khmel 2016/01/27 22:36:21 Done.
90
91 scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator(
92 const GURL& url,
93 net::URLFetcherDelegate* delegate,
94 const std::string& response_data,
95 net::HttpStatusCode response_code,
96 net::URLRequestStatus::Status status) {
97 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher(
98 url, delegate, response_data, response_code, status));
99 // Use cookie only once.
100 if (!rt_cookie_.empty()) {
101 net::ResponseCookies cookies;
102 cookies.push_back(rt_cookie_);
103 fetcher->set_cookies(cookies);
104 rt_cookie_ = "";
105 }
106 return fetcher;
107 }
108
109 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest);
110 };
111
112 TEST_F(ArcAuthServiceTest, Workflow) {
113 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
114 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
115 ASSERT_EQ("", auth_service()->GetAuthCode());
116
117 const GURL gaia_gurl = ArcAuthFetcher::CreateURL(profile());
118 url_fetcher_factory().SetFakeResponse(gaia_gurl, "", net::HTTP_OK,
119 net::URLRequestStatus::SUCCESS);
120 std::string cookie = "oauth_code=";
121 cookie += kTestAuthCode;
122 cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly";
123 set_cookie(cookie);
124 auth_service()->SetProfile(profile());
125
126 // Setting profile initiates a code fetching process.
127 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
128
129 content::BrowserThread::GetBlockingPool()->FlushForTesting();
130 base::RunLoop().RunUntilIdle();
131
132 ASSERT_EQ(ArcAuthService::State::ENABLE, auth_service()->state());
133 ASSERT_EQ(ArcBridgeService::State::READY, bridge_service()->state());
134 // Auth tolen valid only for one call.
135 ASSERT_EQ(kTestAuthCode, auth_service()->GetAuthCode());
136 ASSERT_EQ("", auth_service()->GetAuthCode());
137
138 // Empty profile disables opt-in.
139 auth_service()->SetProfile(nullptr);
140 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
141 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
142 ASSERT_EQ("", auth_service()->GetAuthCode());
143
144 // Send profile and don't provide a code.
145 auth_service()->SetProfile(profile());
146
147 // Setting profile initiates a code fetching process.
148 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
149
150 content::BrowserThread::GetBlockingPool()->FlushForTesting();
151 base::RunLoop().RunUntilIdle();
152
153 // UI is disabled in unit tests and this code is unchanged.
154 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
155
156 // Send error response.
157 url_fetcher_factory().SetFakeResponse(gaia_gurl, "", net::HTTP_BAD_REQUEST,
158 net::URLRequestStatus::SUCCESS);
159 auth_service()->SetProfile(nullptr);
160 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
161 auth_service()->SetProfile(profile());
162
163 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
164 content::BrowserThread::GetBlockingPool()->FlushForTesting();
165 base::RunLoop().RunUntilIdle();
166
167 ASSERT_EQ(ArcAuthService::State::NO_CODE, auth_service()->state());
168 }
169
170 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698