summaryrefslogtreecommitdiff
path: root/tests/gtestutils.c
blob: 3006bc0a4eeec5cc25e6a3700e71d4c848d9a4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*-  */
/*
 * libgfbgraph - GObject library for Facebook Graph API
 * Copyright (C) 2013-2015 Álvaro Peña <alvaropg@gmail.com>
 *
 * GFBGraph is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GFBGraph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GFBGraph.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <glib.h>
#include <json-glib/json-glib.h>
#include <rest/rest-proxy.h>
#include <string.h>

#include <gfbgraph/gfbgraph.h>
#include <gfbgraph/gfbgraph-simple-authorizer.h>

/* #include "config.h" */

typedef struct _GFBGraphTestFixture GFBGraphTestFixture;
typedef struct _GFBGraphTestApp     GFBGraphTestApp;

struct _GFBGraphTestFixture
{
        gchar *user_id;
        gchar *user_email;

        GFBGraphSimpleAuthorizer *authorizer;
};

struct _GFBGraphTestApp
{
        gchar *client_id;
        gchar *client_secret;
        gchar *access_token;
};

#define FACEBOOK_ENDPOINT "https://graph.facebook.com/v2.3"

#define FACEBOOK_TEST_USER_PERMISSIONS "user_about_me,user_photos,email,publish_actions"

GFBGraphTestApp*
gfbgraph_test_app_setup (void)
{
        GFBGraphTestApp *app;
        RestProxy *proxy;
        RestProxyCall *rest_call;
        gchar *function_path;
        GKeyFile *app_key_file;
        gchar *app_key_filename;
        GError *error = NULL;
        const char *payload;
        JsonNode *jnode;
        JsonParser *jparser;
        JsonReader *jreader;

        app_key_filename = g_test_build_filename (G_TEST_BUILT,
                                                  "credentials.ini",
                                                  NULL);
        app_key_file = g_key_file_new ();
        g_key_file_load_from_file (app_key_file,
                                   app_key_filename,
                                   G_KEY_FILE_NONE,
                                   &error);
        g_assert_no_error(error);

        app = g_new0(GFBGraphTestApp, 1);

        app->client_id = g_key_file_get_string (app_key_file,
                                                "Client",
                                                "ClientId",
                                                &error);
        g_assert_no_error(error);
        app->client_secret = g_key_file_get_string (app_key_file,
                                                    "Client",
                                                    "ClientSecret",
                                                    &error);
        g_assert_no_error(error);

        proxy = rest_proxy_new (FACEBOOK_ENDPOINT, FALSE);
        rest_call = rest_proxy_new_call (proxy);

        rest_proxy_call_add_param (rest_call, "client_id", app->client_id);
        rest_proxy_call_add_param (rest_call, "client_secret", app->client_secret);
        rest_proxy_call_add_param (rest_call, "grant_type", "client_credentials");

        rest_proxy_call_set_method (rest_call, "GET");
        function_path = g_strdup ("/oauth/access_token");
        rest_proxy_call_set_function (rest_call, function_path);

        rest_proxy_call_sync (rest_call, &error);
        g_assert_no_error (error);

        payload = rest_proxy_call_get_payload (rest_call);
        jparser = json_parser_new ();
        json_parser_load_from_data (jparser, payload, -1, &error);
        g_assert_no_error (error);
        jnode = json_parser_get_root (jparser);
        jreader = json_reader_new (jnode);
        json_reader_read_element (jreader, 0);
        app->access_token = g_strdup(json_reader_get_string_value (jreader));
        json_reader_end_element (jreader);
        g_object_unref(jparser);

        g_clear_object(&rest_call);
        g_clear_object(&proxy);

        return app;
}

void
gfbgraph_test_fixture_setup (GFBGraphTestFixture *fixture, gconstpointer user_data)
{
        RestProxy *proxy;
        RestProxyCall *rest_call;
        gchar *function_path;
        const gchar *payload;
        GError *error = NULL;
        const GFBGraphTestApp *app = user_data;
        JsonNode *jnode;
        JsonParser *jparser;
        JsonReader *jreader;
        const gchar *access_token;

        /* Create a new user */

        proxy = rest_proxy_new (FACEBOOK_ENDPOINT, FALSE);
        rest_call = rest_proxy_new_call (proxy);

        /* Params as documented here: https://developers.facebook.com/docs/graph-api/reference/app/accounts/test-users#publish */
        rest_proxy_call_add_param (rest_call, "installed", "true");
        rest_proxy_call_add_param (rest_call, "permissions", FACEBOOK_TEST_USER_PERMISSIONS);
        rest_proxy_call_add_param (rest_call, "access_token", app->access_token);

        rest_proxy_call_set_method (rest_call, "POST");
        function_path = g_strdup_printf ("%s/accounts/test-users", app->client_id);
        rest_proxy_call_set_function (rest_call, function_path);

        rest_proxy_call_sync (rest_call, &error);
        g_assert_no_error (error);

        payload = rest_proxy_call_get_payload (rest_call);
        jparser = json_parser_new ();
        json_parser_load_from_data (jparser, payload, -1, &error);
        g_assert_no_error (error);
        jnode = json_parser_get_root (jparser);
        jreader = json_reader_new (jnode);

        json_reader_read_element (jreader, 0);
        fixture->user_id = g_strdup (json_reader_get_string_value (jreader));
        json_reader_end_element (jreader);
        json_reader_read_element (jreader, 1);
        access_token = g_strdup (json_reader_get_string_value (jreader));
        json_reader_end_element (jreader);
        json_reader_read_element (jreader, 3);
        fixture->user_email = g_strdup (json_reader_get_string_value (jreader));
        json_reader_end_element (jreader);

        fixture->authorizer = gfbgraph_simple_authorizer_new (access_token);

        if (function_path)
                g_free (function_path);
        g_clear_object (&rest_call);
        g_clear_object (&proxy);
}

static void
gfbgraph_test_fixture_teardown (GFBGraphTestFixture *fixture, gconstpointer user_data)
{
        SoupSession *ssession;
        SoupMessage *smessage;
        gchar *function_path;
        gchar *auth_value;
        guint status;
        const GFBGraphTestApp *app = user_data;
        SoupURI *uri;

        /* Delete the test user and clean up memory */

        ssession = soup_session_new ();

        function_path = g_strdup_printf ("%s/%s", FACEBOOK_ENDPOINT, fixture->user_id);
        smessage = soup_message_new ("DELETE", function_path);
        gfbgraph_authorizer_process_message (GFBGRAPH_AUTHORIZER (fixture->authorizer), smessage);

        status = soup_session_send_message (ssession, smessage);
        g_assert_cmpint(status, ==, 200);

        g_free (function_path);
        g_free (auth_value);
        g_free (fixture->user_id);
        g_free (fixture->user_email);
        g_object_unref (fixture->authorizer);
}

static void
gfbgraph_test_me (GFBGraphTestFixture *fixture, gconstpointer user_data)
{
        GFBGraphUser *me;
        GError *error = NULL;

        me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (fixture->authorizer), &error);
        g_assert_no_error (error);
        g_assert (GFBGRAPH_IS_USER (me));

        g_assert_cmpstr (fixture->user_id, ==, gfbgraph_node_get_id (GFBGRAPH_NODE (me)));

        g_assert_cmpstr (fixture->user_email, ==, gfbgraph_user_get_email (me));

        g_object_unref (me);
}

static void
gfbgraph_test_album (GFBGraphTestFixture *fixture, gconstpointer user_data)
{
        GFBGraphUser *me;
        GFBGraphAlbum *album;
        GError *error = NULL;
        gboolean result;

        me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (fixture->authorizer), &error);
        g_assert_no_error (error);
        g_assert (GFBGRAPH_IS_USER (me));

        /* Create a photo album */
        album = gfbgraph_album_new ();
        gfbgraph_album_set_name (album, "Vanilla Sky");
        gfbgraph_album_set_description (album, "Great sunset photos in Mars!");
        result = gfbgraph_node_append_connection (GFBGRAPH_NODE (me),
                                                  GFBGRAPH_NODE (album),
                                                  GFBGRAPH_AUTHORIZER (fixture->authorizer),
                                                  &error);
        /* Asserting the connection */
        g_assert_no_error (error);
        g_assert (result);

        /* Asserting the Album ID */
        g_assert_cmpstr (gfbgraph_node_get_id (GFBGRAPH_NODE (album)), !=, "");
}

int
main (int argc, char **argv)
{
        GFBGraphTestApp *app = NULL;
        int test_result;

        g_test_init (&argc, &argv, NULL);

        g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL | G_LOG_LEVEL_CRITICAL);

        app = gfbgraph_test_app_setup ();

        g_test_add ("/GFBGraph/Me",
                    GFBGraphTestFixture,
                    app,
                    gfbgraph_test_fixture_setup,
                    gfbgraph_test_me,
                    gfbgraph_test_fixture_teardown);

        g_test_add ("/GFBGraph/Album",
                    GFBGraphTestFixture,
                    app,
                    gfbgraph_test_fixture_setup,
                    gfbgraph_test_album,
                    gfbgraph_test_fixture_teardown);

        test_result = g_test_run ();

        if (app) {
                if (app->client_id)
                        g_free (app->client_id);
                if (app->client_secret)
                        g_free (app->client_secret);
                if (app->access_token)
                        g_free (app->access_token);
        }

        return test_result;
}