Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / shell / browser / api / identity / identity_api_unittest.cc
blob03e6f71b513d7b08b1cfb3f9d474e2cb8d2f8411
1 // Copyright 2014 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 "extensions/shell/browser/api/identity/identity_api.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "extensions/browser/api_unittest.h"
13 #include "extensions/common/extension_builder.h"
14 #include "extensions/shell/browser/shell_oauth2_token_service.h"
15 #include "google_apis/gaia/oauth2_mint_token_flow.h"
17 namespace extensions {
18 namespace shell {
20 // A ShellOAuth2TokenService that does not make network requests.
21 class MockShellOAuth2TokenService : public ShellOAuth2TokenService {
22 public:
23 // The service starts with no account id or refresh token.
24 MockShellOAuth2TokenService() : ShellOAuth2TokenService(nullptr, "", "") {}
25 ~MockShellOAuth2TokenService() override {}
27 // OAuth2TokenService:
28 scoped_ptr<Request> StartRequest(const std::string& account_id,
29 const ScopeSet& scopes,
30 Consumer* consumer) override {
31 // Immediately return success.
32 consumer->OnGetTokenSuccess(nullptr, "logged-in-user-token", base::Time());
33 return nullptr;
37 // A mint token flow that immediately returns a known access token when started.
38 class MockOAuth2MintTokenFlow : public OAuth2MintTokenFlow {
39 public:
40 explicit MockOAuth2MintTokenFlow(Delegate* delegate)
41 : OAuth2MintTokenFlow(delegate, Parameters()), delegate_(delegate) {}
42 ~MockOAuth2MintTokenFlow() override {}
44 // OAuth2ApiCallFlow:
45 void Start(net::URLRequestContextGetter* context,
46 const std::string& access_token) override {
47 EXPECT_EQ("logged-in-user-token", access_token);
48 delegate_->OnMintTokenSuccess("app-access-token", 12345);
51 private:
52 // Cached here so OAuth2MintTokenFlow does not have to expose its delegate.
53 Delegate* delegate_;
56 class IdentityApiTest : public ApiUnitTest {
57 public:
58 IdentityApiTest() {}
59 ~IdentityApiTest() override {}
61 // testing::Test:
62 void SetUp() override {
63 ApiUnitTest::SetUp();
64 // Create an extension with OAuth2 scopes.
65 set_extension(
66 ExtensionBuilder()
67 .SetManifest(
68 DictionaryBuilder()
69 .Set("name", "Test")
70 .Set("version", "1.0")
71 .Set(
72 "oauth2",
73 DictionaryBuilder()
74 .Set("client_id",
75 "123456.apps.googleusercontent.com")
76 .Set(
77 "scopes",
78 ListBuilder().Append(
79 "https://www.googleapis.com/auth/drive"))))
80 .SetLocation(Manifest::UNPACKED)
81 .Build());
85 // Verifies that the getAuthToken function exists and can be called without
86 // crashing.
87 TEST_F(IdentityApiTest, GetAuthTokenNoRefreshToken) {
88 MockShellOAuth2TokenService token_service;
90 // Calling getAuthToken() before a refresh token is available causes an error.
91 std::string error =
92 RunFunctionAndReturnError(new IdentityGetAuthTokenFunction, "[{}]");
93 EXPECT_FALSE(error.empty());
96 // Verifies that getAuthToken() returns an app access token.
97 TEST_F(IdentityApiTest, GetAuthToken) {
98 MockShellOAuth2TokenService token_service;
100 // Simulate a refresh token being set.
101 token_service.SetRefreshToken("larry@google.com", "refresh-token");
103 // RunFunctionAndReturnValue takes ownership.
104 IdentityGetAuthTokenFunction* function = new IdentityGetAuthTokenFunction;
105 function->SetMintTokenFlowForTesting(new MockOAuth2MintTokenFlow(function));
107 // Function succeeds and returns a token (for its callback).
108 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(function, "[{}]");
109 ASSERT_TRUE(result.get());
110 std::string value;
111 result->GetAsString(&value);
112 EXPECT_NE("logged-in-user-token", value);
113 EXPECT_EQ("app-access-token", value);
116 // Verifies that the removeCachedAuthToken function exists and can be called
117 // without crashing.
118 TEST_F(IdentityApiTest, RemoveCachedAuthToken) {
119 MockShellOAuth2TokenService token_service;
121 // Function succeeds and returns nothing (for its callback).
122 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(
123 new IdentityRemoveCachedAuthTokenFunction, "[{}]");
124 EXPECT_FALSE(result.get());
127 } // namespace shell
128 } // namespace extensions