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"
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
{
20 // A ShellOAuth2TokenService that does not make network requests.
21 class MockShellOAuth2TokenService
: public ShellOAuth2TokenService
{
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());
37 // A mint token flow that immediately returns a known access token when started.
38 class MockOAuth2MintTokenFlow
: public OAuth2MintTokenFlow
{
40 explicit MockOAuth2MintTokenFlow(Delegate
* delegate
)
41 : OAuth2MintTokenFlow(delegate
, Parameters()), delegate_(delegate
) {}
42 ~MockOAuth2MintTokenFlow() override
{}
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);
52 // Cached here so OAuth2MintTokenFlow does not have to expose its delegate.
56 class IdentityApiTest
: public ApiUnitTest
{
59 ~IdentityApiTest() override
{}
62 void SetUp() override
{
64 // Create an extension with OAuth2 scopes.
70 .Set("version", "1.0")
75 "123456.apps.googleusercontent.com")
79 "https://www.googleapis.com/auth/drive"))))
80 .SetLocation(Manifest::UNPACKED
)
85 // Verifies that the getAuthToken function exists and can be called without
87 TEST_F(IdentityApiTest
, GetAuthTokenNoRefreshToken
) {
88 MockShellOAuth2TokenService token_service
;
90 // Calling getAuthToken() before a refresh token is available causes an 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());
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
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());
128 } // namespace extensions