~online-accounts/webaccounts-browser-extension/trunk

« back to all changes in this revision

Viewing changes to npapi-plugin/tests/main.cpp

  • Committer: Alberto Mardegan
  • Date: 2012-06-06 07:35:28 UTC
  • mfrom: (8.1.6 cleanups)
  • Revision ID: alberto.mardegan@canonical.com-20120606073528-cay1vlt36zmjarcm
Add unit tests, misc project cleanups.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the webaccounts-browser-plugin.
 
3
 * Copyright (C) Canonical Ltd. 2012
 
4
 *
 
5
 * Author: Alberto Mardegan <alberto.mardegan@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
 
 
21
#include "fake-capture-service.h"
 
22
#include "login-handler.h"
 
23
 
 
24
#include <glib-object.h>
 
25
#include <gtest/gtest.h>
 
26
#include <libaccounts-glib/ag-account.h>
 
27
#include <libaccounts-glib/ag-manager.h>
 
28
 
 
29
TEST (npapi, wrong_domain) {
 
30
    FakeCaptureService service;
 
31
    static const gchar json_string[] = "{"
 
32
        "  'login': 'my user name', "
 
33
        "  'domain': 'invalid.com' "
 
34
        "}";
 
35
 
 
36
    fake_capture_service_init (&service);
 
37
 
 
38
    /* This should fail because there isn't a provider handling the domain
 
39
     * "invalid.com" */
 
40
    webaccounts_login_handler_set_json (json_string);
 
41
    EXPECT_FALSE (service.called);
 
42
 
 
43
    fake_capture_service_unset (&service);
 
44
}
 
45
 
 
46
TEST (npapi, existing_account) {
 
47
    AgManager *manager;
 
48
    AgAccount *account;
 
49
    FakeCaptureService service;
 
50
 
 
51
    static const gchar json_string[] = "{"
 
52
        "  'login': 'user@test.com', "
 
53
        "  'domain': 'example.com' "
 
54
        "}";
 
55
 
 
56
    /* first, create an account for user@test.com */
 
57
    manager = ag_manager_new ();
 
58
    ASSERT_TRUE (manager != NULL);
 
59
 
 
60
    account = ag_manager_create_account (manager, "MyProvider");
 
61
    ASSERT_TRUE (account != NULL);
 
62
    ag_account_set_display_name (account, "user@test.com");
 
63
    ag_account_store_blocking (account, NULL);
 
64
 
 
65
    g_object_unref (account);
 
66
    g_object_unref (manager);
 
67
 
 
68
    fake_capture_service_init (&service);
 
69
 
 
70
    /* This should fail because an account for that username already exists */
 
71
    webaccounts_login_handler_set_json (json_string);
 
72
    EXPECT_FALSE (service.called);
 
73
 
 
74
    fake_capture_service_unset (&service);
 
75
}
 
76
 
 
77
TEST (npapi, username) {
 
78
    FakeCaptureService service;
 
79
    static const gchar json_string[] = "{"
 
80
        "  'login': 'my user name', "
 
81
        "  'domain': 'example.com' "
 
82
        "}";
 
83
 
 
84
    fake_capture_service_init (&service);
 
85
 
 
86
    webaccounts_login_handler_set_json (json_string);
 
87
    EXPECT_TRUE (service.called);
 
88
    EXPECT_STREQ ("my user name", service.username);
 
89
    EXPECT_STREQ ("MyProvider", service.provider_name);
 
90
 
 
91
    fake_capture_service_unset (&service);
 
92
}
 
93
 
 
94
TEST (npapi, cookies) {
 
95
    FakeCaptureService service;
 
96
    gboolean ok;
 
97
    gchar *cookie;
 
98
    static const gchar json_string[] = "{"
 
99
        "  'login': 'Happy User', "
 
100
        "  'domain': 'example.com', "
 
101
        "  'cookies': { "
 
102
        "    'example.com': 'a list of cookies', "
 
103
        "    'test.com': 'more cookies' "
 
104
        "  } "
 
105
        "}";
 
106
 
 
107
    fake_capture_service_init (&service);
 
108
 
 
109
    webaccounts_login_handler_set_json (json_string);
 
110
    EXPECT_TRUE (service.called);
 
111
    EXPECT_STREQ ("Happy User", service.username);
 
112
    EXPECT_STREQ ("MyProvider", service.provider_name);
 
113
    ASSERT_TRUE (service.cookies != NULL);
 
114
 
 
115
    /* check the cookie contents */
 
116
    ok = g_variant_lookup (service.cookies, "example.com", "s", &cookie);
 
117
    EXPECT_TRUE (ok);
 
118
    EXPECT_STREQ ("a list of cookies", cookie);
 
119
    g_free (cookie);
 
120
 
 
121
    ok = g_variant_lookup (service.cookies, "test.com", "s", &cookie);
 
122
    EXPECT_TRUE (ok);
 
123
    EXPECT_STREQ ("more cookies", cookie);
 
124
    g_free (cookie);
 
125
 
 
126
    fake_capture_service_unset (&service);
 
127
}
 
128
 
 
129
int main (int argc, char **argv)
 
130
{
 
131
    ::testing::InitGoogleTest (&argc, argv);
 
132
 
 
133
    g_type_init ();
 
134
 
 
135
    return RUN_ALL_TESTS ();
 
136
}