[libgdata/wip/rishi/drive: 6/8] Port the tests



commit e30bb792c914850f2a2cf1a79f6c17a792798117
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Sep 4 15:13:24 2015 +0200

    Port the tests
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684920

 gdata/tests/Makefile.am |    5 +-
 gdata/tests/documents.c |  167 ++++++++++++++++++++++++++++++++++-------------
 2 files changed, 122 insertions(+), 50 deletions(-)
---
diff --git a/gdata/tests/Makefile.am b/gdata/tests/Makefile.am
index 7ee767e..cc24bd7 100644
--- a/gdata/tests/Makefile.am
+++ b/gdata/tests/Makefile.am
@@ -52,6 +52,7 @@ test_programs = \
        calendar \
        contacts \
        picasaweb \
+       documents \
        freebase \
        tasks \
        perf \
@@ -62,10 +63,6 @@ test_programs = \
        youtube \
        $(NULL)
 
-# FIXME: Temporarily disabled until they are ported for the changes in the v2
-# to v3 API transitions.
-#all_test_programs += documents
-
 dist_test_data = \
        photo.jpg \
        sample.ogg \
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c
index 4546751..53bc4c3 100644
--- a/gdata/tests/documents.c
+++ b/gdata/tests/documents.c
@@ -29,9 +29,16 @@
 
 #include "gdata.h"
 #include "common.h"
+#include "gdata-dummy-authorizer.h"
 
 static UhmServer *mock_server = NULL;
 
+#undef CLIENT_ID  /* from common.h */
+
+#define CLIENT_ID "352818697630-nqu2cmt5quqd6lr17ouoqmb684u84l1f.apps.googleusercontent.com"
+#define CLIENT_SECRET "-fA4pHQJxR3zJ-FyAMPQsikg"
+#define REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob"
+
 static gboolean
 check_document_is_in_folder (GDataDocumentsDocument *document, GDataDocumentsFolder *folder)
 {
@@ -118,32 +125,43 @@ create_folder (GDataDocumentsService *service, const gchar *title)
 static void
 test_authentication (void)
 {
-       gboolean retval;
-       GDataClientLoginAuthorizer *authorizer;
-       GError *error = NULL;
+       GDataOAuth2Authorizer *authorizer = NULL;  /* owned */
+       gchar *authentication_uri, *authorisation_code;
 
        gdata_test_mock_server_start_trace (mock_server, "authentication");
 
-       /* Create an authorizer */
-       authorizer = gdata_client_login_authorizer_new (CLIENT_ID, GDATA_TYPE_DOCUMENTS_SERVICE);
+       authorizer = gdata_oauth2_authorizer_new (CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, 
GDATA_TYPE_DOCUMENTS_SERVICE);
 
-       g_assert_cmpstr (gdata_client_login_authorizer_get_client_id (authorizer), ==, CLIENT_ID);
+       /* Get an authentication URI. */
+       authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (authorizer, NULL, FALSE);
+       g_assert (authentication_uri != NULL);
 
-       /* Log in */
-       retval = gdata_client_login_authorizer_authenticate (authorizer, USERNAME, PASSWORD, NULL, &error);
-       g_assert_no_error (error);
-       g_assert (retval == TRUE);
-       g_clear_error (&error);
+       /* Get the authorisation code off the user. */
+       if (uhm_server_get_enable_online (mock_server)) {
+               authorisation_code = gdata_test_query_user_for_verifier (authentication_uri);
+       } else {
+               /* Hard coded, extracted from the trace file. */
+               authorisation_code = g_strdup ("4/UQC1p9JXnW6BU0JBXtuCp7Bo_pApf6MkyLtd_CjmWpI");
+       }
 
-       /* Check all is as it should be */
-       g_assert_cmpstr (gdata_client_login_authorizer_get_username (authorizer), ==, USERNAME);
-       g_assert_cmpstr (gdata_client_login_authorizer_get_password (authorizer), ==, PASSWORD);
+       g_free (authentication_uri);
+
+       if (authorisation_code == NULL) {
+               /* Skip tests. */
+               goto skip_test;
+       }
+
+       /* Authorise the token */
+       g_assert (gdata_oauth2_authorizer_request_authorization (authorizer, authorisation_code, NULL, NULL) 
== TRUE);
 
+       /* Check all is as it should be */
        g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer),
                                                             
gdata_documents_service_get_primary_authorization_domain ()) == TRUE);
        g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer),
                                                             
gdata_documents_service_get_spreadsheet_authorization_domain ()) == TRUE);
 
+skip_test:
+       g_free (authorisation_code);
        g_object_unref (authorizer);
 
        uhm_server_end_trace (mock_server);
@@ -157,22 +175,31 @@ static void
 set_up_temp_folder (TempFolderData *data, gconstpointer service)
 {
        GDataDocumentsFolder *folder;
-       gchar *upload_uri;
+       GDataDocumentsFolder *root;
 
        gdata_test_mock_server_start_trace (mock_server, "setup-temp-folder");
 
+       root = GDATA_DOCUMENTS_FOLDER (gdata_service_query_single_entry (GDATA_SERVICE (service),
+                                                                        
gdata_documents_service_get_primary_authorization_domain (),
+                                                                        "root",
+                                                                        NULL,
+                                                                        GDATA_TYPE_DOCUMENTS_FOLDER,
+                                                                        NULL,
+                                                                        NULL));
+
        /* Create a folder */
        folder = gdata_documents_folder_new (NULL);
        gdata_entry_set_title (GDATA_ENTRY (folder), "Temporary Folder");
 
        /* Insert the folder */
-       upload_uri = gdata_documents_service_get_upload_uri (NULL);
-       data->folder = GDATA_DOCUMENTS_FOLDER (gdata_service_insert_entry (GDATA_SERVICE (service),
-                                                                          
gdata_documents_service_get_primary_authorization_domain (),
-                                                                          upload_uri, GDATA_ENTRY (folder), 
NULL, NULL));
+       data->folder = GDATA_DOCUMENTS_FOLDER (gdata_documents_service_add_entry_to_folder 
(GDATA_DOCUMENTS_SERVICE (service),
+                                                                                           
GDATA_DOCUMENTS_ENTRY (folder),
+                                                                                           root,
+                                                                                           NULL,
+                                                                                           NULL));
        g_assert (GDATA_IS_DOCUMENTS_FOLDER (data->folder));
-       g_free (upload_uri);
        g_object_unref (folder);
+       g_object_unref (root);
 
        uhm_server_end_trace (mock_server);
 }
@@ -205,15 +232,16 @@ _set_up_temp_document (GDataDocumentsEntry *entry, GDataService *service, GFile
 
        /* Query for information on the file. */
        file_info = g_file_query_info (document_file,
-                                      G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," 
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
-                                      G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &error);
+                                      G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," 
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                                      G_FILE_QUERY_INFO_NONE, NULL, &error);
        g_assert_no_error (error);
 
        /* Prepare the upload stream */
-       upload_stream = gdata_documents_service_upload_document_resumable (GDATA_DOCUMENTS_SERVICE (service), 
GDATA_DOCUMENTS_DOCUMENT (entry),
-                                                                          g_file_info_get_display_name 
(file_info),
-                                                                          g_file_info_get_content_type 
(file_info), g_file_info_get_size (file_info),
-                                                                          NULL, NULL, &error);
+       upload_stream = gdata_documents_service_upload_document (GDATA_DOCUMENTS_SERVICE (service),
+                                                                GDATA_DOCUMENTS_DOCUMENT (entry),
+                                                                g_file_info_get_display_name (file_info),
+                                                                g_file_info_get_content_type (file_info),
+                                                                NULL, NULL, &error);
        g_assert_no_error (error);
        g_assert (GDATA_IS_UPLOAD_STREAM (upload_stream));
 
@@ -231,25 +259,26 @@ _set_up_temp_document (GDataDocumentsEntry *entry, GDataService *service, GFile
        /* Finish the upload */
        document = gdata_documents_service_finish_upload (GDATA_DOCUMENTS_SERVICE (service), upload_stream, 
&error);
        g_assert_no_error (error);
+       g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (document));
 
        g_object_unref (upload_stream);
 
        /* HACK: Query for the new document, as Google's servers appear to modify it behind our back when 
creating the document:
         * http://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=2337. We have to wait a few 
seconds before trying this to allow the
         * various Google servers to catch up with each other. Thankfully, we don't have to wait when running 
against the mock server. */
-       if (uhm_server_get_enable_online (mock_server) == TRUE) {
-               g_usleep (5 * G_USEC_PER_SEC);
-       }
+       /* if (uhm_server_get_enable_online (mock_server) == TRUE) { */
+       /*      g_usleep (5 * G_USEC_PER_SEC); */
+       /* } */
 
-       new_document = GDATA_DOCUMENTS_DOCUMENT (gdata_service_query_single_entry (service,
-                                                                                  
gdata_documents_service_get_primary_authorization_domain (),
-                                                                                  gdata_entry_get_id 
(GDATA_ENTRY (document)), NULL,
-                                                                                  G_OBJECT_TYPE (document), 
NULL, NULL));
-       g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (new_document));
+       /* new_document = GDATA_DOCUMENTS_DOCUMENT (gdata_service_query_single_entry (service, */
+       /*                                                                            
gdata_documents_service_get_primary_authorization_domain (), */
+       /*                                                                            gdata_entry_get_id 
(GDATA_ENTRY (document)), NULL, */
+       /*                                                                            G_OBJECT_TYPE 
(document), NULL, NULL)); */
+       /* g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (new_document)); */
 
-       g_object_unref (document);
+       /* g_object_unref (document); */
 
-       return new_document;
+       return document;
 }
 
 static void
@@ -1129,7 +1158,7 @@ test_copy_document (TempCopyDocumentData *data, gconstpointer service)
        /* Copy the document */
        data->new_document = gdata_documents_service_copy_document (GDATA_DOCUMENTS_SERVICE (service), 
data->parent.document, NULL, &error);
        g_assert_no_error (error);
-       g_assert (GDATA_IS_DOCUMENTS_SPREADSHEET (data->new_document));
+       g_assert (GDATA_IS_DOCUMENTS_DOCUMENT (data->new_document));
 
        /* Check their IDs are different but that their other properties (e.g. title) are the same. */
        g_assert_cmpstr (gdata_entry_get_id (GDATA_ENTRY (data->parent.document)), !=, gdata_entry_get_id 
(GDATA_ENTRY (data->new_document)));
@@ -2002,7 +2031,57 @@ mock_server_notify_resolver_cb (GObject *object, GParamSpec *pspec, gpointer use
                uhm_resolver_add_A (resolver, "lh3.googleusercontent.com", ip_address);
                uhm_resolver_add_A (resolver, "lh5.googleusercontent.com", ip_address);
                uhm_resolver_add_A (resolver, "lh6.googleusercontent.com", ip_address);
+               uhm_resolver_add_A (resolver, "www.googleapis.com", ip_address);
+       }
+}
+
+/* Set up a global GDataAuthorizer to be used for all the tests. Unfortunately,
+ * the Google Drive API is effectively limited to OAuth2 authorisation,
+ * so this requires user interaction when online.
+ *
+ * If not online, use a dummy authoriser. */
+static GDataAuthorizer *
+create_global_authorizer (void)
+{
+       GDataOAuth2Authorizer *authorizer = NULL;  /* owned */
+       gchar *authentication_uri, *authorisation_code;
+       GError *error = NULL;
+
+       /* If not online, just return a dummy authoriser. */
+       if (!uhm_server_get_enable_online (mock_server)) {
+               return GDATA_AUTHORIZER (gdata_dummy_authorizer_new (GDATA_TYPE_DOCUMENTS_SERVICE));
+       }
+
+       /* Otherwise, go through the interactive OAuth dance. */
+       gdata_test_mock_server_start_trace (mock_server, "global-authentication");
+       authorizer = gdata_oauth2_authorizer_new (CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, 
GDATA_TYPE_DOCUMENTS_SERVICE);
+
+       /* Get an authentication URI */
+       authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (authorizer, NULL, FALSE);
+       g_assert (authentication_uri != NULL);
+
+       /* Get the authorisation code off the user. */
+       authorisation_code = gdata_test_query_user_for_verifier (authentication_uri);
+
+       g_free (authentication_uri);
+
+       if (authorisation_code == NULL) {
+               /* Skip tests. */
+               g_object_unref (authorizer);
+               authorizer = NULL;
+               goto skip_test;
        }
+
+       /* Authorise the token */
+       g_assert (gdata_oauth2_authorizer_request_authorization (authorizer, authorisation_code, NULL, 
&error));
+       g_assert_no_error (error);
+
+skip_test:
+       g_free (authorisation_code);
+
+       uhm_server_end_trace (mock_server);
+
+       return GDATA_AUTHORIZER (authorizer);
 }
 
 int
@@ -2024,11 +2103,7 @@ main (int argc, char *argv[])
        uhm_server_set_trace_directory (mock_server, trace_directory);
        g_object_unref (trace_directory);
 
-       gdata_test_mock_server_start_trace (mock_server, "global-authentication");
-       authorizer = GDATA_AUTHORIZER (gdata_client_login_authorizer_new (CLIENT_ID, 
GDATA_TYPE_DOCUMENTS_SERVICE));
-       gdata_client_login_authorizer_authenticate (GDATA_CLIENT_LOGIN_AUTHORIZER (authorizer), 
DOCUMENTS_USERNAME, PASSWORD, NULL, NULL);
-       uhm_server_end_trace (mock_server);
-
+       authorizer = create_global_authorizer ();
        service = GDATA_SERVICE (gdata_documents_service_new (authorizer));
 
        g_test_add_func ("/documents/authentication", test_authentication);
@@ -2155,10 +2230,10 @@ main (int argc, char *argv[])
                    set_up_folders_remove_from_folder_async, 
test_folders_remove_from_folder_async_cancellation,
                    tear_down_folders_remove_from_folder_async);
 
-       g_test_add_data_func ("/documents/batch", service, test_batch);
-       g_test_add ("/documents/batch/async", BatchAsyncData, service, set_up_batch_async, test_batch_async, 
tear_down_batch_async);
-       g_test_add ("/documents/batch/async/cancellation", BatchAsyncData, service, set_up_batch_async, 
test_batch_async_cancellation,
-                   tear_down_batch_async);
+       /* g_test_add_data_func ("/documents/batch", service, test_batch); */
+       /* g_test_add ("/documents/batch/async", BatchAsyncData, service, set_up_batch_async, 
test_batch_async, tear_down_batch_async); */
+       /* g_test_add ("/documents/batch/async/cancellation", BatchAsyncData, service, set_up_batch_async, 
test_batch_async_cancellation, */
+       /*             tear_down_batch_async); */
 
        g_test_add_func ("/documents/folder/parser/normal", test_folder_parser_normal);
        g_test_add_func ("/documents/query/etag", test_query_etag);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]