~michihenning/storage-framework/no-exception-on-unknown-metadata

« back to all changes in this revision

Viewing changes to tests/provider-AccountData/AccountData_test.cpp

  • Committer: Tarmac
  • Author(s): James Henstridge
  • Date: 2016-08-10 12:37:53 UTC
  • mfrom: (39.2.7 provider-tests)
  • Revision ID: tarmac-20160810123753-ydofxui1hd2g7ft8
Add some tests for the AccountData and DBusPeerCache classes from the provider.

Approved by unity-api-1-bot, Michi Henning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2016 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: James Henstridge <james.henstridge@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/storage/provider/ProviderBase.h>
 
20
#include <unity/storage/provider/internal/AccountData.h>
 
21
#include <unity/storage/provider/internal/DBusPeerCache.h>
 
22
 
 
23
#include <utils/DBusEnvironment.h>
 
24
 
 
25
#include <gtest/gtest.h>
 
26
#include <OnlineAccounts/Account>
 
27
#include <OnlineAccounts/Manager>
 
28
#include <QCoreApplication>
 
29
#include <QSignalSpy>
 
30
 
 
31
#include <memory>
 
32
 
 
33
using namespace std;
 
34
using namespace unity::storage::provider;
 
35
 
 
36
class AccountDataTest : public ::testing::Test
 
37
{
 
38
public:
 
39
    QDBusConnection const& connection()
 
40
    {
 
41
        return dbus_->connection();
 
42
    }
 
43
 
 
44
protected:
 
45
    void SetUp() override
 
46
    {
 
47
        dbus_.reset(new DBusEnvironment);
 
48
        dbus_->start_services();
 
49
    }
 
50
 
 
51
    void TearDown() override
 
52
    {
 
53
        dbus_.reset();
 
54
    }
 
55
 
 
56
private:
 
57
    unique_ptr<DBusEnvironment> dbus_;
 
58
};
 
59
 
 
60
TEST_F(AccountDataTest, oauth1_credentials)
 
61
{
 
62
    OnlineAccounts::Manager manager("", connection());
 
63
    manager.waitForReady();
 
64
    ASSERT_TRUE(manager.isReady());
 
65
 
 
66
    auto accounts = manager.availableAccounts("oauth1-service");
 
67
    ASSERT_EQ(1, accounts.size());
 
68
 
 
69
    internal::AccountData account(unique_ptr<ProviderBase>(),
 
70
                                  shared_ptr<internal::DBusPeerCache>(),
 
71
                                  connection(),
 
72
                                  accounts[0]);
 
73
 
 
74
    QSignalSpy spy(&account, &internal::AccountData::authenticated);
 
75
    account.authenticate(true);
 
76
    ASSERT_TRUE(spy.wait());
 
77
 
 
78
    ASSERT_TRUE(account.has_credentials());
 
79
    auto creds = boost::get<OAuth1Credentials>(account.credentials());
 
80
 
 
81
    EXPECT_EQ("consumer_key", creds.consumer_key);
 
82
    EXPECT_EQ("consumer_secret", creds.consumer_secret);
 
83
    EXPECT_EQ("token", creds.token);
 
84
    EXPECT_EQ("token_secret", creds.token_secret);
 
85
}
 
86
 
 
87
TEST_F(AccountDataTest, oauth2_credentials)
 
88
{
 
89
    OnlineAccounts::Manager manager("", connection());
 
90
    manager.waitForReady();
 
91
    ASSERT_TRUE(manager.isReady());
 
92
 
 
93
    auto accounts = manager.availableAccounts("oauth2-service");
 
94
    ASSERT_EQ(1, accounts.size());
 
95
 
 
96
    internal::AccountData account(unique_ptr<ProviderBase>(),
 
97
                                  shared_ptr<internal::DBusPeerCache>(),
 
98
                                  connection(),
 
99
                                  accounts[0]);
 
100
 
 
101
    QSignalSpy spy(&account, &internal::AccountData::authenticated);
 
102
    account.authenticate(true);
 
103
    ASSERT_TRUE(spy.wait());
 
104
 
 
105
    ASSERT_TRUE(account.has_credentials());
 
106
    auto creds = boost::get<OAuth2Credentials>(account.credentials());
 
107
 
 
108
    EXPECT_EQ("access_token", creds.access_token);
 
109
}
 
110
 
 
111
TEST_F(AccountDataTest, password_credentials)
 
112
{
 
113
    OnlineAccounts::Manager manager("", connection());
 
114
    manager.waitForReady();
 
115
    ASSERT_TRUE(manager.isReady());
 
116
 
 
117
    auto accounts = manager.availableAccounts("password-service");
 
118
    ASSERT_EQ(1, accounts.size());
 
119
 
 
120
    internal::AccountData account(unique_ptr<ProviderBase>(),
 
121
                                  shared_ptr<internal::DBusPeerCache>(),
 
122
                                  connection(),
 
123
                                  accounts[0]);
 
124
 
 
125
    QSignalSpy spy(&account, &internal::AccountData::authenticated);
 
126
    account.authenticate(true);
 
127
    ASSERT_TRUE(spy.wait());
 
128
 
 
129
    ASSERT_TRUE(account.has_credentials());
 
130
    auto creds = boost::get<PasswordCredentials>(account.credentials());
 
131
 
 
132
    EXPECT_EQ("user", creds.username);
 
133
    EXPECT_EQ("pass", creds.password);
 
134
}
 
135
 
 
136
int main(int argc, char **argv)
 
137
{
 
138
    QCoreApplication app(argc, argv);
 
139
    ::testing::InitGoogleTest(&argc, argv);
 
140
    return RUN_ALL_TESTS();
 
141
}