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

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: fix accidental change 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(const base::FilePath& path) {
36 EXPECT_TRUE(base::DeleteFile(path, true));
37 EXPECT_TRUE(base::CreateDirectory(path));
xiyuan 2016/01/27 23:56:05 These two lines are not really needed since we onl
khmel 2016/01/28 05:22:08 Makes sense, thanks
38
39 TestingProfile::Builder profile_builder;
40 profile_builder.SetPath(path);
41 return profile_builder.Build();
42 }
43
44 } // namespace
45
46 class ArcAuthServiceTest : public testing::Test {
47 public:
48 ArcAuthServiceTest()
49 : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions)),
50 url_fetcher_factory_(
51 nullptr,
52 base::Bind(&ArcAuthServiceTest::FakeURLFetcherCreator,
53 base::Unretained(this))) {}
54 ~ArcAuthServiceTest() override {}
55
56 void SetUp() override {
57 ArcAuthService::DisableUIForTesting();
58
59 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
60 base::FilePath path = temp_dir_.path().AppendASCII("TestArcProfile");
61
62 profile_ = CreateProfile(path);
63 bridge_service_.reset(new FakeArcBridgeService());
64 auth_service_.reset(new ArcAuthService(bridge_service_.get()));
65
66 // Check initial conditions.
67 EXPECT_EQ(bridge_service_.get(), ArcBridgeService::Get());
68 EXPECT_EQ(true, !ArcBridgeService::Get()->available());
69 EXPECT_EQ(ArcBridgeService::State::STOPPED,
70 ArcBridgeService::Get()->state());
71 }
72
73 void TearDown() override {}
74
75 protected:
76 Profile* profile() { return profile_.get(); }
77 FakeArcBridgeService* bridge_service() { return bridge_service_.get(); }
78 ArcAuthService* auth_service() { return auth_service_.get(); }
79 net::FakeURLFetcherFactory& url_fetcher_factory() {
80 return url_fetcher_factory_;
81 }
82 void set_cookie(const std::string& cookie) { rt_cookie_ = cookie; }
83
84 private:
85 scoped_ptr<net::FakeURLFetcher> FakeURLFetcherCreator(
86 const GURL& url,
87 net::URLFetcherDelegate* delegate,
88 const std::string& response_data,
89 net::HttpStatusCode response_code,
90 net::URLRequestStatus::Status status) {
91 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher(
92 url, delegate, response_data, response_code, status));
93 // Use cookie only once.
94 if (!rt_cookie_.empty()) {
95 net::ResponseCookies cookies;
96 cookies.push_back(rt_cookie_);
97 fetcher->set_cookies(cookies);
98 rt_cookie_ = "";
99 }
100 return fetcher;
101 }
102
103 scoped_ptr<content::TestBrowserThreadBundle> thread_bundle_;
104 net::FakeURLFetcherFactory url_fetcher_factory_;
105 scoped_ptr<arc::FakeArcBridgeService> bridge_service_;
106 scoped_ptr<arc::ArcAuthService> auth_service_;
107 scoped_ptr<TestingProfile> profile_;
108 base::ScopedTempDir temp_dir_;
109 std::string rt_cookie_;
110
111 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest);
112 };
113
114 TEST_F(ArcAuthServiceTest, Workflow) {
115 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
116 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
117 ASSERT_EQ("", auth_service()->GetAuthCode());
118
119 const GURL gaia_gurl = ArcAuthFetcher::CreateURL();
120 url_fetcher_factory().SetFakeResponse(gaia_gurl, "", net::HTTP_OK,
121 net::URLRequestStatus::SUCCESS);
122 std::string cookie = "oauth_code=";
123 cookie += kTestAuthCode;
124 cookie += "; Path=/o/oauth2/programmatic_auth; Secure; HttpOnly";
125 set_cookie(cookie);
126 auth_service()->OnPrimaryUserProfilePrepared(profile());
127
128 // Setting profile initiates a code fetching process.
129 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
130
131 content::BrowserThread::GetBlockingPool()->FlushForTesting();
132 base::RunLoop().RunUntilIdle();
133
134 ASSERT_EQ(ArcAuthService::State::ENABLE, auth_service()->state());
135 ASSERT_EQ(ArcBridgeService::State::READY, bridge_service()->state());
136 // Auth tolen valid only for one call.
137 ASSERT_EQ(kTestAuthCode, auth_service()->GetAuthCode());
138 ASSERT_EQ("", auth_service()->GetAuthCode());
139
140 // Empty profile disables opt-in.
xiyuan 2016/01/27 23:56:05 nit: Update comment?
khmel 2016/01/28 05:22:08 Done.
141 auth_service()->Shutdown();
142 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
143 ASSERT_EQ(ArcBridgeService::State::STOPPED, bridge_service()->state());
144 ASSERT_EQ("", auth_service()->GetAuthCode());
145
146 // Send profile and don't provide a code.
147 auth_service()->OnPrimaryUserProfilePrepared(profile());
148
149 // Setting profile initiates a code fetching process.
150 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
151
152 content::BrowserThread::GetBlockingPool()->FlushForTesting();
153 base::RunLoop().RunUntilIdle();
154
155 // UI is disabled in unit tests and this code is unchanged.
156 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
157
158 // Send error response.
159 url_fetcher_factory().SetFakeResponse(gaia_gurl, "", net::HTTP_BAD_REQUEST,
160 net::URLRequestStatus::SUCCESS);
161 auth_service()->Shutdown();
162 ASSERT_EQ(ArcAuthService::State::DISABLE, auth_service()->state());
163 auth_service()->OnPrimaryUserProfilePrepared(profile());
164
165 ASSERT_EQ(ArcAuthService::State::FETCHING_CODE, auth_service()->state());
166 content::BrowserThread::GetBlockingPool()->FlushForTesting();
167 base::RunLoop().RunUntilIdle();
168
169 ASSERT_EQ(ArcAuthService::State::NO_CODE, auth_service()->state());
170 }
171
172 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698