| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/shell/browser/api/identity/identity_api.h" | 5 #include "extensions/shell/browser/api/identity/identity_api.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "extensions/browser/api_unittest.h" | 13 #include "extensions/browser/api_unittest.h" |
| 14 #include "extensions/common/extension_builder.h" | 14 #include "extensions/common/extension_builder.h" |
| 15 #include "extensions/shell/browser/shell_oauth2_token_service.h" | 15 #include "extensions/shell/browser/shell_oauth2_token_service.h" |
| 16 #include "google_apis/gaia/oauth2_mint_token_flow.h" | 16 #include "google_apis/gaia/oauth2_mint_token_flow.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 namespace shell { | 19 namespace shell { |
| 20 | 20 |
| 21 // A ShellOAuth2TokenService that does not make network requests. | 21 // A ShellOAuth2TokenService that does not make network requests. |
| 22 class MockShellOAuth2TokenService : public ShellOAuth2TokenService { | 22 class MockShellOAuth2TokenService : public ShellOAuth2TokenService { |
| 23 public: | 23 public: |
| 24 // The service starts with no account id or refresh token. | 24 // The service starts with no account id or refresh token. |
| 25 MockShellOAuth2TokenService() : ShellOAuth2TokenService(nullptr, "", "") {} | 25 MockShellOAuth2TokenService() : ShellOAuth2TokenService(nullptr, "", "") {} |
| 26 ~MockShellOAuth2TokenService() override {} | 26 ~MockShellOAuth2TokenService() override {} |
| 27 | 27 |
| 28 // OAuth2TokenService: | 28 // OAuth2TokenService: |
| 29 scoped_ptr<Request> StartRequest(const std::string& account_id, | 29 std::unique_ptr<Request> StartRequest(const std::string& account_id, |
| 30 const ScopeSet& scopes, | 30 const ScopeSet& scopes, |
| 31 Consumer* consumer) override { | 31 Consumer* consumer) override { |
| 32 // Immediately return success. | 32 // Immediately return success. |
| 33 consumer->OnGetTokenSuccess(nullptr, "logged-in-user-token", base::Time()); | 33 consumer->OnGetTokenSuccess(nullptr, "logged-in-user-token", base::Time()); |
| 34 return nullptr; | 34 return nullptr; |
| 35 } | 35 } |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 // A mint token flow that immediately returns a known access token when started. | 38 // A mint token flow that immediately returns a known access token when started. |
| 39 class MockOAuth2MintTokenFlow : public OAuth2MintTokenFlow { | 39 class MockOAuth2MintTokenFlow : public OAuth2MintTokenFlow { |
| 40 public: | 40 public: |
| 41 explicit MockOAuth2MintTokenFlow(Delegate* delegate) | 41 explicit MockOAuth2MintTokenFlow(Delegate* delegate) |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 MockShellOAuth2TokenService token_service; | 101 MockShellOAuth2TokenService token_service; |
| 102 | 102 |
| 103 // Simulate a refresh token being set. | 103 // Simulate a refresh token being set. |
| 104 token_service.SetRefreshToken("larry@google.com", "refresh-token"); | 104 token_service.SetRefreshToken("larry@google.com", "refresh-token"); |
| 105 | 105 |
| 106 // RunFunctionAndReturnValue takes ownership. | 106 // RunFunctionAndReturnValue takes ownership. |
| 107 IdentityGetAuthTokenFunction* function = new IdentityGetAuthTokenFunction; | 107 IdentityGetAuthTokenFunction* function = new IdentityGetAuthTokenFunction; |
| 108 function->SetMintTokenFlowForTesting(new MockOAuth2MintTokenFlow(function)); | 108 function->SetMintTokenFlowForTesting(new MockOAuth2MintTokenFlow(function)); |
| 109 | 109 |
| 110 // Function succeeds and returns a token (for its callback). | 110 // Function succeeds and returns a token (for its callback). |
| 111 scoped_ptr<base::Value> result = RunFunctionAndReturnValue(function, "[{}]"); | 111 std::unique_ptr<base::Value> result = |
| 112 RunFunctionAndReturnValue(function, "[{}]"); |
| 112 ASSERT_TRUE(result.get()); | 113 ASSERT_TRUE(result.get()); |
| 113 std::string value; | 114 std::string value; |
| 114 result->GetAsString(&value); | 115 result->GetAsString(&value); |
| 115 EXPECT_NE("logged-in-user-token", value); | 116 EXPECT_NE("logged-in-user-token", value); |
| 116 EXPECT_EQ("app-access-token", value); | 117 EXPECT_EQ("app-access-token", value); |
| 117 } | 118 } |
| 118 | 119 |
| 119 // Verifies that the removeCachedAuthToken function exists and can be called | 120 // Verifies that the removeCachedAuthToken function exists and can be called |
| 120 // without crashing. | 121 // without crashing. |
| 121 TEST_F(IdentityApiTest, RemoveCachedAuthToken) { | 122 TEST_F(IdentityApiTest, RemoveCachedAuthToken) { |
| 122 MockShellOAuth2TokenService token_service; | 123 MockShellOAuth2TokenService token_service; |
| 123 | 124 |
| 124 // Function succeeds and returns nothing (for its callback). | 125 // Function succeeds and returns nothing (for its callback). |
| 125 scoped_ptr<base::Value> result = RunFunctionAndReturnValue( | 126 std::unique_ptr<base::Value> result = RunFunctionAndReturnValue( |
| 126 new IdentityRemoveCachedAuthTokenFunction, "[{}]"); | 127 new IdentityRemoveCachedAuthTokenFunction, "[{}]"); |
| 127 EXPECT_FALSE(result.get()); | 128 EXPECT_FALSE(result.get()); |
| 128 } | 129 } |
| 129 | 130 |
| 130 } // namespace shell | 131 } // namespace shell |
| 131 } // namespace extensions | 132 } // namespace extensions |
| OLD | NEW |