Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: services/authentication/accounts_db_manager_unittest.cc

Issue 1466733002: Google OAuth Device Flow support for FNL (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "testing/gtest/include/gtest/gtest.h"
13
14 namespace authentication {
15 namespace {
16
17 class AccountsDBTest : public mojo::test::ApplicationTestBase {
18 public:
19 AccountsDBTest(){};
20 ~AccountsDBTest() override{};
21
22 protected:
23 void SetUp() override {
24 mojo::test::ApplicationTestBase::SetUp();
25 mojo::files::FilesPtr files;
26 application_impl()->ConnectToService("mojo:files", &files);
27
28 mojo::files::Error error = mojo::files::Error::INTERNAL;
29 mojo::files::DirectoryPtr directory;
30 files->OpenFileSystem(nullptr, GetProxy(&directory),
31 [&error](mojo::files::Error e) { error = e; });
32 CHECK(files.WaitForIncomingResponse());
33 CHECK_EQ(mojo::files::Error::OK, error);
34
35 accounts_db_manager_ = new AccountsDbManager(directory.Pass());
36 }
37
38 AccountsDbManager* accountsDBPtr() { return accounts_db_manager_; }
39
40 private:
41 AccountsDbManager* accounts_db_manager_;
42
43 DISALLOW_COPY_AND_ASSIGN(AccountsDBTest);
44 };
45
46 TEST_F(AccountsDBTest, CanAddNewAccount) {
47 std::string test_str("test,abcdefgh");
48 accountsDBPtr()->UpdateAccount("test", test_str);
49
50 std::string db_contents = accountsDBPtr()->GetAllUserAccounts();
51 EXPECT_EQ(test_str.size(), db_contents.size());
52 }
53
54 TEST_F(AccountsDBTest, CanUpdateAnExistingAccount) {
55 accountsDBPtr()->UpdateAccount("user1", "user1,hello");
56 accountsDBPtr()->UpdateAccount("user1", "user1,world");
57 accountsDBPtr()->UpdateAccount("user3", "user3,google");
58
59 std::string db_contents = accountsDBPtr()->GetAllUserAccounts();
60 base::StringTokenizer lines(db_contents, "\n");
61 int n = 0;
62 while (lines.GetNext()) {
63 n++;
64 }
65 ASSERT_FALSE(db_contents.find("hello") != std::string::npos);
66 ASSERT_TRUE(db_contents.find("world") != std::string::npos);
67 EXPECT_EQ(2, n);
68 }
69
70 TEST_F(AccountsDBTest, CanGetUserAccountDataByName) {
71 accountsDBPtr()->UpdateAccount("test_user", "test_user,turquoise_creds");
72 mojo::String user_data = accountsDBPtr()->GetAccountDataForUser("test_user");
73
74 ASSERT_TRUE(!user_data.is_null());
75 EXPECT_EQ("test_user,turquoise_creds", user_data);
76 }
77
78 TEST_F(AccountsDBTest, CanGetUserAccountDataByNamePrefix) {
79 accountsDBPtr()->UpdateAccount("test_user", "test_user,turquoise_creds");
80 mojo::String user_data = accountsDBPtr()->GetAccountDataForUser("test_");
81 ASSERT_TRUE(user_data.is_null());
82 }
83
84 TEST_F(AccountsDBTest, CanGetUserAccountDataByNameSuffix) {
85 accountsDBPtr()->UpdateAccount("test_user", "test_user,turquoise_creds");
86 mojo::String user_data = accountsDBPtr()->GetAccountDataForUser("user");
87 ASSERT_TRUE(user_data.is_null());
88 }
89
90 TEST_F(AccountsDBTest, CanGetUserAccountDataByNameSubstr) {
91 accountsDBPtr()->UpdateAccount("test_user", "test_user,turquoise_creds");
92 mojo::String user_data = accountsDBPtr()->GetAccountDataForUser("st_us");
93 ASSERT_TRUE(user_data.is_null());
94 }
95
96 TEST_F(AccountsDBTest, CanGetAllAccountsData) {
97 accountsDBPtr()->UpdateAccount("user1", "user1,hello");
98 accountsDBPtr()->UpdateAccount("user2", "user2,world");
99 accountsDBPtr()->UpdateAccount("user3", "user3,google");
100
101 std::string db_contents = accountsDBPtr()->GetAllUserAccounts();
102 base::StringTokenizer lines(db_contents, "\n");
103 int n = 0;
104 while (lines.GetNext()) {
105 n++;
106 }
107 EXPECT_EQ(3, n);
108 }
109
110 TEST_F(AccountsDBTest, CanAddNewAuthorization) {
111 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null());
112 accountsDBPtr()->UpdateAuthorization("url1", "user1");
113 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1");
114 }
115
116 TEST_F(AccountsDBTest, CanUpdateExistingAuthorization) {
117 ASSERT_TRUE(accountsDBPtr()->GetAuthorizedUserForApp("url1").is_null());
118 accountsDBPtr()->UpdateAuthorization("url1", "user1");
119 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user1");
120 accountsDBPtr()->UpdateAuthorization("url1", "user2");
121 EXPECT_EQ(accountsDBPtr()->GetAuthorizedUserForApp("url1").get(), "user2");
122 }
123
124 TEST_F(AccountsDBTest, CanGetAuthorizedUserForInvalidApp) {
125 accountsDBPtr()->UpdateAuthorization("url1", "user1");
126 ASSERT_TRUE(
127 accountsDBPtr()->GetAuthorizedUserForApp("invalid_app_url").is_null());
128 }
129
130 } // namespace
131 } // namespace authentication
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698