OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 = "https://test_scope_.googleapis.com/auth"; |
| 43 creds->auth_provider = AuthProvider::GOOGLE; |
| 44 creds->credential_type = CredentialType::DOWNSCOPED_OAUTH_REFRESH_TOKEN; |
| 45 accounts_db_manager_->UpdateCredentials(user, creds.Pass()); |
| 46 } |
| 47 |
| 48 AccountsDbManager* accountsDBPtr() { return accounts_db_manager_; } |
| 49 |
| 50 private: |
| 51 AccountsDbManager* accounts_db_manager_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(AccountsDBTest); |
| 54 }; |
| 55 |
| 56 TEST_F(AccountsDBTest, CanAddNewAccount) { |
| 57 PopulateCredential("new_user", "new_refresh_token"); |
| 58 |
| 59 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 60 EXPECT_EQ(1, (int)users.size()); |
| 61 EXPECT_EQ("new_user", users[0].get()); |
| 62 |
| 63 authentication::CredentialsPtr creds = |
| 64 accountsDBPtr()->GetCredentials("new_user"); |
| 65 ASSERT_TRUE(!creds->token.is_null()); |
| 66 EXPECT_EQ("new_refresh_token", creds->token); |
| 67 } |
| 68 |
| 69 TEST_F(AccountsDBTest, CanUpdateAnExistingAccount) { |
| 70 PopulateCredential("user1", "token1"); |
| 71 authentication::CredentialsPtr creds = |
| 72 accountsDBPtr()->GetCredentials("user1"); |
| 73 ASSERT_TRUE(!creds->token.is_null()); |
| 74 EXPECT_EQ("token1", creds->token); |
| 75 |
| 76 PopulateCredential("user2", "token2"); |
| 77 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 78 EXPECT_EQ(2, (int)users.size()); |
| 79 |
| 80 // update credential for an existing account |
| 81 PopulateCredential("user1", "token3"); |
| 82 users = accountsDBPtr()->GetAllUsers(); |
| 83 EXPECT_EQ(2, (int)users.size()); |
| 84 |
| 85 creds = accountsDBPtr()->GetCredentials("user1"); |
| 86 ASSERT_TRUE(!creds->token.is_null()); |
| 87 EXPECT_EQ("token3", creds->token); |
| 88 } |
| 89 |
| 90 TEST_F(AccountsDBTest, CanGetCredentials) { |
| 91 // No accounts |
| 92 authentication::CredentialsPtr creds = |
| 93 accountsDBPtr()->GetCredentials("test_user"); |
| 94 ASSERT_TRUE(creds->token.is_null()); |
| 95 |
| 96 // Only one account |
| 97 PopulateCredential("test_user", "test_refresh_token"); |
| 98 creds = accountsDBPtr()->GetCredentials("test_user"); |
| 99 ASSERT_TRUE(!creds->token.is_null()); |
| 100 EXPECT_EQ("test_refresh_token", creds->token); |
| 101 EXPECT_EQ("https://test_scope_.googleapis.com/auth", creds->scopes); |
| 102 EXPECT_EQ(AuthProvider::GOOGLE, creds->auth_provider); |
| 103 EXPECT_EQ(CredentialType::DOWNSCOPED_OAUTH_REFRESH_TOKEN, |
| 104 creds->credential_type); |
| 105 |
| 106 // Multiple accounts |
| 107 PopulateCredential("user31", "token31"); |
| 108 PopulateCredential("user11", "token11"); |
| 109 PopulateCredential("user21", "token21"); |
| 110 creds = accountsDBPtr()->GetCredentials("user11"); |
| 111 ASSERT_TRUE(!creds->token.is_null()); |
| 112 EXPECT_EQ("token11", creds->token); |
| 113 |
| 114 // For an invalid user |
| 115 PopulateCredential("test_user", "test_refresh_token"); |
| 116 creds = accountsDBPtr()->GetCredentials("test_"); |
| 117 ASSERT_TRUE(creds->token.is_null()); |
| 118 } |
| 119 |
| 120 TEST_F(AccountsDBTest, CanGetAllUsers) { |
| 121 // No accounts |
| 122 mojo::Array<mojo::String> users = accountsDBPtr()->GetAllUsers(); |
| 123 EXPECT_EQ(0, (int)users.size()); |
| 124 |
| 125 // More than one account |
| 126 PopulateCredential("user1", "token1"); |
| 127 PopulateCredential("user2", "token2"); |
| 128 PopulateCredential("user3", "token3"); |
| 129 |
| 130 users = accountsDBPtr()->GetAllUsers(); |
| 131 EXPECT_EQ(3, (int)users.size()); |
| 132 } |
| 133 |
| 134 TEST_F(AccountsDBTest, CanAddNewAuthorization) { |
| 135 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null()); |
| 136 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 137 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1"); |
| 138 } |
| 139 |
| 140 TEST_F(AccountsDBTest, CanUpdateExistingAuthorization) { |
| 141 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null()); |
| 142 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 143 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1"); |
| 144 accountsDBPtr()->UpdateAuthorization("url1", "user2"); |
| 145 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user2"); |
| 146 } |
| 147 |
| 148 TEST_F(AccountsDBTest, CanGetAuthorizedUserForInvalidApp) { |
| 149 accountsDBPtr()->UpdateAuthorization("url1", "user1"); |
| 150 ASSERT_TRUE( |
| 151 accountsDBPtr()->GetAuthorizedUserForApp("invalid_app_url").is_null()); |
| 152 } |
| 153 |
| 154 } // namespace |
| 155 } // namespace authentication |
OLD | NEW |