diff options
author | Debarshi Ray <debarshir@gnome.org> | 2017-09-19 15:21:58 +0200 |
---|---|---|
committer | Debarshi Ray <debarshir@gnome.org> | 2017-09-19 18:56:42 +0200 |
commit | 47d2085c50ee41e59430b90f30ada9ca7f121770 (patch) | |
tree | 5b436872565b9c7eff9ac2a117b3f04107989654 | |
parent | e941ca4a786dc89cc67e15a19cd82279bc82d0cd (diff) | |
download | libgdata-47d2085c50ee41e59430b90f30ada9ca7f121770.tar.gz |
tests: Use OAuth2 for authentication instead of ClientLogin
Based on similar code found elsewhere in the test suite.
https://bugzilla.gnome.org/show_bug.cgi?id=684920
-rw-r--r-- | gdata/tests/documents.c | 101 |
1 files changed, 82 insertions, 19 deletions
diff --git a/gdata/tests/documents.c b/gdata/tests/documents.c index 4107ca17..4c207e39 100644 --- a/gdata/tests/documents.c +++ b/gdata/tests/documents.c @@ -30,9 +30,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 void add_folder_link_to_entry (GDataDocumentsEntry *entry, GDataDocumentsFolder *folder) { @@ -134,32 +141,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 ("FIXME: to be updated from the trace file"); + } - /* 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); @@ -2027,6 +2045,55 @@ mock_server_notify_resolver_cb (GObject *object, GParamSpec *pspec, gpointer use } } +/* 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 main (int argc, char *argv[]) { @@ -2046,11 +2113,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); |