[libgdata/wip/rishi/drive: 4/4] Port the tests



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

    Port the tests

 gdata/tests/Makefile.am |    5 +-
 gdata/tests/documents.c |  159 ++++++++++++++++++++++++++++++++++------------
 2 files changed, 118 insertions(+), 46 deletions(-)
---
diff --git a/gdata/tests/Makefile.am b/gdata/tests/Makefile.am
index a3ca626..597ea0a 100644
--- a/gdata/tests/Makefile.am
+++ b/gdata/tests/Makefile.am
@@ -51,6 +51,7 @@ test_programs = \
        calendar \
        contacts \
        picasaweb \
+       documents \
        freebase \
        tasks \
        perf \
@@ -61,10 +62,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 88bfac1..320b6b6 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)
 {
@@ -109,32 +116,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);
@@ -192,22 +210,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);
 }
@@ -240,15 +267,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));
 
@@ -266,25 +294,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
@@ -1179,7 +1208,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)));
@@ -2047,7 +2076,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
@@ -2069,11 +2148,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);


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