OLD | NEW |
(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 "accounts_db_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string_tokenizer.h" |
| 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_test_base.h" |
| 11 #include "mojo/services/files/interfaces/types.mojom.h" |
| 12 #include "services/authentication/credentials_impl_db.mojom.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace authentication { |
| 16 namespace { |
| 17 |
| 18 class AccountsDBTest : public mojo::test::ApplicationTestBase { |
| 19 public: |
| 20 AccountsDBTest(){}; |
| 21 ~AccountsDBTest() override{}; |
| 22 |
| 23 protected: |
| 24 void SetUp() override { |
| 25 mojo::test::ApplicationTestBase::SetUp(); |
| 26 mojo::files::FilesPtr files; |
| 27 application_impl()->ConnectToService("mojo:files", &files); |
| 28 |
| 29 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 30 mojo::files::DirectoryPtr directory; |
| 31 files->OpenFileSystem(nullptr, GetProxy(&directory), |
| 32 [&error](mojo::files::Error e) { error = e; }); |
| 33 CHECK(files.WaitForIncomingResponse()); |
| 34 CHECK_EQ(mojo::files::Error::OK, error); |
| 35 |
| 36 accounts_db_manager_ = new AccountsDbManager(directory.Pass()); |
| 37 } |
| 38 |
| 39 void PopulateCredential(const mojo::String& user, const mojo::String& token) { |
| 40 authentication::CredentialsPtr creds = authentication::Credentials::New(); |
| 41 creds->token = token; |
| 42 creds->scopes = |
| 43 "https://test_scope_.googleapis.com/auth " |
| 44 "https://test_scope_.googleapis.com/profile"; |
| 45 creds->auth_provider = AuthProvider::GOOGLE; |
| 46 creds->credential_type = CredentialType::DOWNSCOPED_OAUTH_REFRESH_TOKEN; |
| 47 accounts_db_manager_->UpdateCredentials(user, creds.Pass()); |
| 48 } |
| 49 |
| 50 AccountsDbManager* accountsDBPtr() { return accounts_db_manager_; } |
| 51 |
| 52 private: |
| 53 AccountsDbManager* accounts_db_manager_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(AccountsDBTest); |
| 56 }; |
| 57 |
| 58 TEST_F(AccountsDBTest, CanAddNewAccount) { |
| 59 PopulateCredential("new_user", "new_refresh_token"); |
| 60 |
| 61 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 62 EXPECT_EQ(1, (int)users.size()); |
| 63 EXPECT_EQ("new_user", users[0].get()); |
| 64 |
| 65 authentication::CredentialsPtr creds = |
| 66 accountsDBPtr()->GetCredentials("new_user"); |
| 67 ASSERT_TRUE(!creds->token.is_null()); |
| 68 EXPECT_EQ("new_refresh_token", creds->token); |
| 69 } |
| 70 |
| 71 TEST_F(AccountsDBTest, CanUpdateAnExistingAccount) { |
| 72 PopulateCredential("user1", "token1"); |
| 73 authentication::CredentialsPtr creds = |
| 74 accountsDBPtr()->GetCredentials("user1"); |
| 75 ASSERT_TRUE(!creds->token.is_null()); |
| 76 EXPECT_EQ("token1", creds->token); |
| 77 |
| 78 PopulateCredential("user2", "token2"); |
| 79 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 80 EXPECT_EQ(2, (int)users.size()); |
| 81 |
| 82 // update credential for an existing account |
| 83 PopulateCredential("user1", "token3"); |
| 84 users = accountsDBPtr()->GetAllUsers(); |
| 85 EXPECT_EQ(2, (int)users.size()); |
| 86 |
| 87 creds = accountsDBPtr()->GetCredentials("user1"); |
| 88 ASSERT_TRUE(!creds->token.is_null()); |
| 89 EXPECT_EQ("token3", creds->token); |
| 90 } |
| 91 |
| 92 TEST_F(AccountsDBTest, CanGetCredentials) { |
| 93 // No accounts |
| 94 authentication::CredentialsPtr creds = |
| 95 accountsDBPtr()->GetCredentials("test_user"); |
| 96 ASSERT_TRUE(creds->token.is_null()); |
| 97 |
| 98 // Only one account |
| 99 PopulateCredential("test_user", "test_refresh_token"); |
| 100 creds = accountsDBPtr()->GetCredentials("test_user"); |
| 101 ASSERT_TRUE(!creds->token.is_null()); |
| 102 EXPECT_EQ("test_refresh_token", creds->token); |
| 103 EXPECT_EQ( |
| 104 "https://test_scope_.googleapis.com/auth " |
| 105 "https://test_scope_.googleapis.com/profile", |
| 106 creds->scopes); |
| 107 EXPECT_EQ(AuthProvider::GOOGLE, creds->auth_provider); |
| 108 EXPECT_EQ(CredentialType::DOWNSCOPED_OAUTH_REFRESH_TOKEN, |
| 109 creds->credential_type); |
| 110 |
| 111 // Multiple accounts |
| 112 PopulateCredential("user31", "token31"); |
| 113 PopulateCredential("user11", "token11"); |
| 114 PopulateCredential("user21", "token21"); |
| 115 creds = accountsDBPtr()->GetCredentials("user11"); |
| 116 ASSERT_TRUE(!creds->token.is_null()); |
| 117 EXPECT_EQ("token11", creds->token); |
| 118 |
| 119 // For an invalid user |
| 120 PopulateCredential("test_user", "test_refresh_token"); |
| 121 creds = accountsDBPtr()->GetCredentials("test_"); |
| 122 ASSERT_TRUE(creds->token.is_null()); |
| 123 } |
| 124 |
| 125 TEST_F(AccountsDBTest, CanGetAllUsers) { |
| 126 // No accounts |
| 127 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 128 EXPECT_EQ(0, (int)users.size()); |
| 129 |
| 130 // More than one account |
| 131 PopulateCredential("user1", "token1"); |
| 132 PopulateCredential("user2", "token2"); |
| 133 PopulateCredential("user3", "token3"); |
| 134 |
| 135 users = accountsDBPtr()->GetAllUsers(); |
| 136 EXPECT_EQ(3, (int)users.size()); |
| 137 } |
| 138 |
| 139 TEST_F(AccountsDBTest, CanAddNewAuthorization) { |
| 140 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null()); |
| 141 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 142 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1"); |
| 143 } |
| 144 |
| 145 TEST_F(AccountsDBTest, CanUpdateExistingAuthorization) { |
| 146 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null()); |
| 147 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 148 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1"); |
| 149 accountsDBPtr()->UpdateAuthorization("url1", "user2"); |
| 150 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user2"); |
| 151 } |
| 152 |
| 153 TEST_F(AccountsDBTest, CanGetAuthorizedUserForInvalidApp) { |
| 154 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 155 ASSERT_TRUE( |
| 156 accountsDBPtr()->GetAuthorizedUserForApp("invalid_app_url").is_null()); |
| 157 } |
| 158 |
| 159 } // namespace |
| 160 } // namespace authentication |
OLD | NEW |