diff options
author | Álvaro Peña <alvaropg@gmail.com> | 2014-04-10 20:50:31 +0200 |
---|---|---|
committer | Álvaro Peña <alvaropg@gmail.com> | 2014-04-10 20:50:31 +0200 |
commit | ebc61ab79d952c88b64953ab3b2f06895f68e52c (patch) | |
tree | 1230628896d8060eb01367b90150a8824c664e38 | |
parent | 5f24a45bf0d6c8d2d7b63a2faecba605a24ab08e (diff) | |
download | libgfbgraph-ebc61ab79d952c88b64953ab3b2f06895f68e52c.tar.gz |
Cleaned up the tests
Now just a GTest based test, which creates a new test user
to run all the tests with this fake user and avoid to
break something with a real user.
-rw-r--r-- | tests/Makefile.am | 13 | ||||
-rw-r--r-- | tests/README | 8 | ||||
-rw-r--r-- | tests/credentials.h | 2 | ||||
-rw-r--r-- | tests/gtestutils.c | 222 | ||||
-rw-r--r-- | tests/test-async.c | 119 | ||||
-rw-r--r-- | tests/test.c | 87 |
6 files changed, 201 insertions, 250 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index e5b589e..efe441e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,19 +1,10 @@ -TESTS = gtestutils test test-async - -COMMON_SRC = credentials.h +TESTS = gtestutils AM_CPPFLAGS = -I$(top_srcdir) $(LIBGFBGRAPH_CFLAGS) AM_LDFLAGS = $(top_builddir)/gfbgraph/libgfbgraph-@API_VERSION@.la $(LIBGFBGRAPH_LIBS) noinst_PROGRAMS = $(TESTS) -gtestutils_SOURCES = $(COMMON_SRC) \ - gtestutils.c - -test_SOURCES = $(COMMON_SRC) \ - test.c - -test_async_SOURCES = $(COMMON_SRC) \ - test-async.c +gtestutils_SOURCES = gtestutils.c -include $(top_srcdir)/git.mk diff --git a/tests/README b/tests/README new file mode 100644 index 0000000..45db00d --- /dev/null +++ b/tests/README @@ -0,0 +1,8 @@ +To run the tests it's required a file called "credentials.ini" placed in this +folder. In that file you need to put your cliend id and your client secret +codes. You can get both from the Facebook Developer Page. + +This is an example content of credentials.ini: +[Client] +ClientId=000000000000000 +ClientSecret=00000000000000000000000000000000 diff --git a/tests/credentials.h b/tests/credentials.h deleted file mode 100644 index ca153dd..0000000 --- a/tests/credentials.h +++ /dev/null @@ -1,2 +0,0 @@ -#define GFBGRAPH_TEST_CLIENT_ID "" -#define GFBGRAPH_TEST_ACCESS_TOKEN "" diff --git a/tests/gtestutils.c b/tests/gtestutils.c index 7933655..e7a9e48 100644 --- a/tests/gtestutils.c +++ b/tests/gtestutils.c @@ -1,48 +1,193 @@ -#include "config.h" -#include "credentials.h" +/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*- */ +/* + * libgfbgraph - GObject library for Facebook Graph API + * Copyright (C) 2013 Á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> -static void -gfbgraph_test_me_albums (GFBGraphSimpleAuthorizer *authorizer) +/* #include "config.h" */ + +typedef struct _GFBGraphTestFixture GFBGraphTestFixture; +typedef struct _GFBGraphTestApp GFBGraphTestApp; + +struct _GFBGraphTestFixture { - GFBGraphUser *me; - GList *albums = NULL; - gint albums_count = 0; - GFBGraphAlbum *album; + gchar *user_id; + + GFBGraphSimpleAuthorizer *authorizer; +}; + +struct _GFBGraphTestApp +{ + gchar *client_id; + gchar *client_secret; + gchar *access_token; +}; + +#define FACEBOOK_ENDPOINT "https://graph.facebook.com" + +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; - me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (authorizer), &error); + 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); - g_assert (GFBGRAPH_IS_USER (me)); - albums = gfbgraph_user_get_albums (GFBGRAPH_USER (me), GFBGRAPH_AUTHORIZER (authorizer), &error); + app->access_token = g_strdup(g_strrstr(rest_proxy_call_get_payload (rest_call), "=") + 1); + + 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", "user_about_me,user_photos"); + 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); - /* Just testing one album */ - while (albums) { - album = GFBGRAPH_ALBUM (albums->data); - g_assert (GFBGRAPH_IS_ALBUM (album)); - albums = g_list_next (albums); - albums_count++; - } + 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); - g_list_free_full (albums, g_object_unref); - g_object_unref (me); + 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); - if (albums_count == 0) { - g_test_fail (); - } + 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_free (function_path); + g_free (auth_value); + g_free (fixture->user_id); + g_object_unref (fixture->authorizer); } static void -gfbgraph_test_me (GFBGraphSimpleAuthorizer *authorizer) +gfbgraph_test_me (GFBGraphTestFixture *fixture, gconstpointer user_data) { GFBGraphUser *me; GError *error = NULL; - me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (authorizer), &error); + me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (fixture->authorizer), &error); g_assert_no_error (error); g_assert (GFBGRAPH_IS_USER (me)); @@ -52,15 +197,30 @@ gfbgraph_test_me (GFBGraphSimpleAuthorizer *authorizer) int main (int argc, char **argv) { - GFBGraphSimpleAuthorizer *authorizer; + GFBGraphTestApp *app = NULL; + int test_result; - g_type_init (); g_test_init (&argc, &argv, NULL); - authorizer = gfbgraph_simple_authorizer_new (GFBGRAPH_TEST_ACCESS_TOKEN); + app = gfbgraph_test_app_setup (); - g_test_add_data_func ("/GFBGraph/Me", authorizer, (GTestDataFunc) gfbgraph_test_me); - g_test_add_data_func ("/GFBGraph/Me/Albums", authorizer, (GTestDataFunc) gfbgraph_test_me_albums); + g_test_add ("/GFBGraph/Me", + GFBGraphTestFixture, + app, + gfbgraph_test_fixture_setup, + gfbgraph_test_me, + 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 g_test_run (); + return test_result; } diff --git a/tests/test-async.c b/tests/test-async.c deleted file mode 100644 index 600f05d..0000000 --- a/tests/test-async.c +++ /dev/null @@ -1,119 +0,0 @@ -#include <gfbgraph/gfbgraph.h> -#include <gfbgraph/gfbgraph-simple-authorizer.h> -#include "credentials.h" - -static GMainLoop *main_loop; - -void -photo_async_cb (GFBGraphNode *album_node, GAsyncResult *res, GFBGraphAuthorizer *authorizer) -{ - GList *photos; - GError *error = NULL; - - photos = gfbgraph_node_get_connection_nodes_async_finish (GFBGRAPH_NODE (album_node), res, &error); - - if (error != NULL) { - g_print ("Error: %s\n", error->message); - g_main_loop_quit (main_loop); - } - - while (photos) { - GFBGraphPhoto *photo; - gchar *name, *source; - guint width, height; - - photo = GFBGRAPH_PHOTO (photos->data); - g_object_get (photo, "name", &name, "width", &width, "height", &height, "source", &source, NULL); - g_print ("\t\t%s (%dx%d): %s\n", name, width, height, source); - g_free (name); - - photos = g_list_next (photos); - } - - g_object_unref (album_node); - g_list_free (photos); - g_main_loop_quit (main_loop); -} - -void -albums_async_cb (GFBGraphNode *me_node, GAsyncResult *res, GFBGraphAuthorizer *authorizer) -{ - GList *albums; - GFBGraphAlbum *one_album = NULL; - GError *error = NULL; - - albums = gfbgraph_node_get_connection_nodes_async_finish (GFBGRAPH_NODE (me_node), res, &error); - - if (error != NULL) { - g_print ("Error: %s\n", error->message); - g_main_loop_quit (main_loop); - } - - /* Print all albums names and count */ - while (albums) { - GFBGraphAlbum *album; - gchar *album_name; - guint album_count; - - album = GFBGRAPH_ALBUM (albums->data); - if (one_album == NULL) { - one_album = album; - g_object_ref (one_album); - } - g_object_get (album, "name", &album_name, "count", &album_count, NULL); - g_print ("\tAlbum: %s - Photos: %d\n", album_name, album_count); - g_free (album_name); - - albums = g_list_next (albums); - } - - - /* For one album, get the photos */ - if (one_album != NULL) { - gfbgraph_node_get_connection_nodes_async (GFBGRAPH_NODE (one_album), GFBGRAPH_TYPE_PHOTO, authorizer, - NULL, (GAsyncReadyCallback) photo_async_cb, authorizer); - } - - g_list_free (albums); -} - -void -me_async_cb (GFBGraphAuthorizer *authorizer, GAsyncResult *res, gpointer user_data) -{ - GFBGraphUser *me; - GError *error = NULL; - - me = gfbgraph_user_get_me_async_finish (authorizer, res, &error); - if (error != NULL) { - g_print ("Error getting me: %s\n", error->message); - g_main_loop_quit (main_loop); - } else { - gchar *me_name; - - g_object_get (G_OBJECT (me), "name", &me_name, NULL); - g_print ("User: %s\n", me_name); - gfbgraph_node_get_connection_nodes_async (GFBGRAPH_NODE (me), GFBGRAPH_TYPE_ALBUM, authorizer, - NULL, (GAsyncReadyCallback) albums_async_cb, authorizer); - } -} - -int -main (int argc, char **argv) -{ - GFBGraphSimpleAuthorizer *authorizer; - - g_type_init (); - main_loop = g_main_loop_new (NULL, TRUE); - - authorizer = gfbgraph_simple_authorizer_new (GFBGRAPH_TEST_ACCESS_TOKEN); - - /* Get "me" user */ - gfbgraph_user_get_me_async (GFBGRAPH_AUTHORIZER (authorizer), NULL, (GAsyncReadyCallback) me_async_cb, NULL); - - g_main_loop_run (main_loop); - - g_main_loop_unref (main_loop); - g_clear_object (&authorizer); - - return 0; -} diff --git a/tests/test.c b/tests/test.c deleted file mode 100644 index e0d4fa7..0000000 --- a/tests/test.c +++ /dev/null @@ -1,87 +0,0 @@ -#include <gfbgraph/gfbgraph.h> -#include <gfbgraph/gfbgraph-simple-authorizer.h> -#include "credentials.h" - -int -main (int argc, char **argv) -{ - GFBGraphSimpleAuthorizer *authorizer; - GFBGraphUser *me; - gchar *me_name; - GError *error = NULL; - GList *albums; - GFBGraphPhoto *photo; - GInputStream *in_stream; - GOutputStream *out_stream; - GFile *out_file; - GFBGraphPhotoImage *smaller; - - g_type_init (); - - authorizer = gfbgraph_simple_authorizer_new (GFBGRAPH_TEST_ACCESS_TOKEN); - - /* Get "me" user */ - me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (authorizer), &error); - if (error != NULL) { - g_print ("Error getting \"me\" user: %s\n", error->message); - return -1; - } - g_object_get (G_OBJECT (me), "name", &me_name, NULL); - g_print ("User: %s\n", me_name); - - /* Get my albums */ - albums = gfbgraph_node_get_connection_nodes (GFBGRAPH_NODE (me), GFBGRAPH_TYPE_ALBUM, GFBGRAPH_AUTHORIZER (authorizer), &error); - if (error != NULL) { - g_print ("Error get connected nodes\n"); - return -1; - } - if (albums == NULL) { - g_print ("Es nulo\n"); - } - while (albums) { - GFBGraphAlbum *album; - gchar *album_name; - guint album_count; - - album = GFBGRAPH_ALBUM (albums->data); - g_object_get (album, "name", &album_name, "count", &album_count, NULL); - g_print ("\tAlbum: %s - Photos: %d\n", album_name, album_count); - g_free (album_name); - - albums = g_list_next (albums); - } - - photo = gfbgraph_photo_new_from_id (GFBGRAPH_AUTHORIZER (authorizer), "553619791342827", &error); - if (error != NULL) { - g_print ("Error getting photo\n"); - return -1; - } - - smaller = gfbgraph_photo_get_image_near_width (photo, 1); - if (smaller == NULL) - g_error ("Can't get the smaller image\n"); - else - g_print ("%dx%d %s", smaller->width, smaller->height, smaller->source); - - in_stream = gfbgraph_photo_download_default_size (photo, GFBGRAPH_AUTHORIZER (authorizer), NULL); - out_file = g_file_new_for_path ("/tmp/facebook.jpeg"); - out_stream = G_OUTPUT_STREAM (g_file_create (out_file, G_FILE_CREATE_PRIVATE, NULL, &error)); - if (error != NULL) { - g_print ("Error creating temp file\n"); - return -1; - } - - g_output_stream_splice (G_OUTPUT_STREAM (out_stream), in_stream, - G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, - NULL, &error); - if (error != NULL) { - g_print ("Error splicing streams\n"); - return -1; - } - - g_list_free_full (albums, g_object_unref); - g_clear_object (&me); - g_clear_object (&authorizer); - - return 0; -} |