diff options
author | Philip Withnall <philip@tecnocode.co.uk> | 2017-03-05 18:02:34 +0000 |
---|---|---|
committer | Philip Withnall <philip@tecnocode.co.uk> | 2017-03-05 18:44:32 +0000 |
commit | 3b2e08d68dcab8ea401f4d42bde86a50b50fbe62 (patch) | |
tree | 09701dc5a7e53a4a6dad49b4e21c93afb1d415e8 | |
parent | 27fe14b5217aebf49e8e559adb6ab2ec147bb5d2 (diff) | |
download | libgdata-3b2e08d68dcab8ea401f4d42bde86a50b50fbe62.tar.gz |
tests: Port the PicasaWeb tests to OAuth 2 authentication
ClientLogin is no longer supported.
-rw-r--r-- | gdata/tests/picasaweb.c | 148 | ||||
-rw-r--r-- | gdata/tests/traces/picasaweb/authentication | 42 | ||||
-rw-r--r-- | gdata/tests/traces/picasaweb/global-authentication | 42 |
3 files changed, 135 insertions, 97 deletions
diff --git a/gdata/tests/picasaweb.c b/gdata/tests/picasaweb.c index b77f7c8b..33a85ff8 100644 --- a/gdata/tests/picasaweb.c +++ b/gdata/tests/picasaweb.c @@ -30,8 +30,13 @@ #include "gdata.h" #include "common.h" +#include "gdata-dummy-authorizer.h" -#define PW_USERNAME "libgdata.picasaweb@gmail.com" +#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 UhmServer *mock_server = NULL; @@ -344,75 +349,48 @@ assert_files_equal (GDataPicasaWebFile *file1, GDataPicasaWebFile *file2, gboole 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_PICASAWEB_SERVICE); + authorizer = gdata_oauth2_authorizer_new (CLIENT_ID, CLIENT_SECRET, + REDIRECT_URI, + GDATA_TYPE_PICASAWEB_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, PW_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/OEX-S1iMbOA_dOnNgUlSYmGWh3TK.QrR73axcNMkWoiIBeO6P2m_su7cwkQI"); + } - /* Check all is as it should be */ - g_assert_cmpstr (gdata_client_login_authorizer_get_username (authorizer), ==, PW_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_picasaweb_service_get_primary_authorization_domain ()) == TRUE); +skip_test: + g_free (authorisation_code); g_object_unref (authorizer); uhm_server_end_trace (mock_server); } -GDATA_ASYNC_TEST_FUNCTIONS (authentication, void, -G_STMT_START { - GDataClientLoginAuthorizer *authorizer; - - /* Create an authorizer */ - authorizer = gdata_client_login_authorizer_new (CLIENT_ID, GDATA_TYPE_PICASAWEB_SERVICE); - - g_assert_cmpstr (gdata_client_login_authorizer_get_client_id (authorizer), ==, CLIENT_ID); - - gdata_client_login_authorizer_authenticate_async (authorizer, PW_USERNAME, PASSWORD, cancellable, async_ready_callback, async_data); - - g_object_unref (authorizer); -} G_STMT_END, -G_STMT_START { - gboolean retval; - GDataClientLoginAuthorizer *authorizer = GDATA_CLIENT_LOGIN_AUTHORIZER (obj); - - retval = gdata_client_login_authorizer_authenticate_finish (authorizer, async_result, &error); - - if (error == NULL) { - g_assert (retval == TRUE); - - /* Check all is as it should be */ - g_assert_cmpstr (gdata_client_login_authorizer_get_username (authorizer), ==, PW_USERNAME); - g_assert_cmpstr (gdata_client_login_authorizer_get_password (authorizer), ==, PASSWORD); - - g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer), - gdata_picasaweb_service_get_primary_authorization_domain ()) == TRUE); - } else { - g_assert (retval == FALSE); - - /* Check nothing's changed */ - g_assert_cmpstr (gdata_client_login_authorizer_get_username (authorizer), ==, NULL); - g_assert_cmpstr (gdata_client_login_authorizer_get_password (authorizer), ==, NULL); - - g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer), - gdata_picasaweb_service_get_primary_authorization_domain ()) == FALSE); - } -} G_STMT_END); - typedef struct { GDataPicasaWebAlbum *album; GDataPicasaWebFile *file1; @@ -2093,6 +2071,57 @@ 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 PicasaWeb API is limited to OAuth1 and 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_PICASAWEB_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_PICASAWEB_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[]) { @@ -2112,18 +2141,11 @@ 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_PICASAWEB_SERVICE)); - gdata_client_login_authorizer_authenticate (GDATA_CLIENT_LOGIN_AUTHORIZER (authorizer), PW_USERNAME, PASSWORD, NULL, NULL); - uhm_server_end_trace (mock_server); + authorizer = create_global_authorizer (); service = GDATA_SERVICE (gdata_picasaweb_service_new (authorizer)); g_test_add_func ("/picasaweb/authentication", test_authentication); - g_test_add ("/picasaweb/authentication/async", GDataAsyncTestData, NULL, gdata_set_up_async_test_data, test_authentication_async, - gdata_tear_down_async_test_data); - g_test_add ("/picasaweb/authentication/async/cancellation", GDataAsyncTestData, NULL, gdata_set_up_async_test_data, - test_authentication_async_cancellation, gdata_tear_down_async_test_data); g_test_add ("/picasaweb/query/all_albums", QueryAllAlbumsData, service, set_up_query_all_albums, test_query_all_albums, tear_down_query_all_albums); diff --git a/gdata/tests/traces/picasaweb/authentication b/gdata/tests/traces/picasaweb/authentication index b7153741..aaf98734 100644 --- a/gdata/tests/traces/picasaweb/authentication +++ b/gdata/tests/traces/picasaweb/authentication @@ -1,27 +1,35 @@ -> POST /accounts/ClientLogin HTTP/1.1 -> Soup-Debug-Timestamp: 1375193172 -> Soup-Debug: SoupSession 1 (0x6683f0), SoupMessage 1 (0x78d280), SoupSocket 1 (0x7cb970) -> Host: www.google.com +> POST /o/oauth2/token HTTP/1.1 +> Soup-Debug-Timestamp: 1488736754 +> Soup-Debug: SoupSession 1 (0x688340), SoupMessage 1 (0xbdd2b0), SoupSocket 1 (0x6a0460) +> Host: accounts.google.com > Content-Type: application/x-www-form-urlencoded > Accept-Encoding: gzip, deflate +> User-Agent: libgdata/0.17.7 - gzip > Connection: Keep-Alive > -> accountType=HOSTED%5FOR%5FGOOGLE&Email=libgdata%2Epicasaweb%40gmail%2Ecom&Passwd=gdata%2Dgdata&service=lh2&source=ytapi%2DGNOME%2Dlibgdata%2D444fubtt%2D0 +> client_id=352818697630-nqu2cmt5quqd6lr17ouoqmb684u84l1f.apps.googleusercontent.com&client_secret=-fA4pHQJxR3zJ-FyAMPQsikg&code=4%2Fv_gXJb78sdayihZYv2RA-U7P7q4lJqzoQhSDV3F9H7U&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&grant_type=authorization_code < HTTP/1.1 200 OK -< Soup-Debug-Timestamp: 1375193172 -< Soup-Debug: SoupMessage 1 (0x78d280) -< Content-Type: text/plain -< Cache-control: no-cache, no-store -< Pragma: no-cache -< Expires: Mon, 01-Jan-1990 00:00:00 GMT -< Date: Tue, 30 Jul 2013 14:06:12 GMT +< Soup-Debug-Timestamp: 1488736755 +< Soup-Debug: SoupMessage 1 (0xbdd2b0) +< Content-Type: application/json; charset=utf-8 < X-Content-Type-Options: nosniff +< Cache-Control: no-cache, no-store, max-age=0, must-revalidate +< Pragma: no-cache +< Expires: Mon, 01 Jan 1990 00:00:00 GMT +< Date: Sun, 05 Mar 2017 17:59:15 GMT +< Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt +< Content-Encoding: gzip +< Server: ESF < X-XSS-Protection: 1; mode=block -< Content-Length: 947 -< Server: GSE +< X-Frame-Options: SAMEORIGIN +< Alt-Svc: quic=":443"; ma=2592000; v="36,35,34" +< Transfer-Encoding: chunked < -< SID=DQAAANgAAAAm30JpMejldxrGZqDDBmyr9VzZMkWUTSlRiv_Sl1b5Nhv3adu8rz7j0dwxDtjA5M9sqauEWfWni0mRBPEIRJx8TLxjxcsXAfh9VoN5KWjIwCRevZYPM3QSoHHwFQK19y7Am4MNTa8NNPiYTkJJsJpltuX8e1XuNNg8PQiJ0_DkngQpWAsvU7dp9ZtiIP7cuDwq_7v_LoliaUmD-iX8xAyAshUsoJHCPUyh8tJS_C2-WW0pw3YWMwTQqvzjLw8T5bg4bwnmIWXnVoMOz3fxgFTGSWTrF69l1xG-ejYihsqDYA -< LSID=DQAAANoAAACBRkzDssWFvAbsJ__WhogKQOB1hOdZ-V7TTzopLOdrYUCoOa9HrmeNJp5iha8I-J_yWJILe434pnCflE8X2wjIpLFHaZPla_0jkC-0S2HV1LxQ2reZNPMHmM2hTxKUopLEBAVhKcarP8veCJxVScfrah5pCMob1GPgleDwnAvakA-KKzvkRbyErNM8XinL4whxINfDK6UTeLg_v_5_qQ4EIXtQA_VZQMgPgkF_bNqKLrN-RH4zADh4Wv9kMKeNjtjwbuhwcP50yxD05oF_kz4M7EIx0hX4c4lFmU-9KG5Dww -< Auth=DQAAANsAAACBRkzDssWFvAbsJ__WhogKQOB1hOdZ-V7TTzopLOdrYUCoOa9HrmeNJp5iha8I-J-YTle_CcHcfDVOcXqR5x439iCm1fiMPcoABzsfdLTKLKU2avMGICiYQ4M8donG0cblZbUDOW31-6bwXScW_yvDNARuAj0KQcJBCLpc3-WqhraDgc6ELRTI5TxhGmwu4XUFs6hDSd5UH9c9b79Qin0no0kBdLyj1cKXI-l5tH_oWHtpd0rV-Wgh5PAw5rtXmdEL9LPU4YSkIkBm6OGE-TFeUBby69BPDLKB15nntY_O_Q +< { +< "access_token" : "ya29.GlsFBA34dYY2-gu5C9DE4WYt6ilEBUvfPhJ6UIwj4KwqEK-uyMAhxBD-N-p3gIZ7ftYRvYxo_TPdTm89NhVfVa-YXGrdVkM1PcZMOftGEYbSVjDE-GCuhrzL2IPd", +< "expires_in" : 3600, +< "refresh_token" : "1/_F_QFRKDjrmwDaWi8MxGJ9ZtQcCEIm_KBbnZwmpCKFo", +< "token_type" : "Bearer" +< } diff --git a/gdata/tests/traces/picasaweb/global-authentication b/gdata/tests/traces/picasaweb/global-authentication index da043a61..4b1e5fcf 100644 --- a/gdata/tests/traces/picasaweb/global-authentication +++ b/gdata/tests/traces/picasaweb/global-authentication @@ -1,27 +1,35 @@ -> POST /accounts/ClientLogin HTTP/1.1 -> Soup-Debug-Timestamp: 1375368273 -> Soup-Debug: SoupSession 1 (0x6681d0), SoupMessage 1 (0x98c0b0), SoupSocket 1 (0x7830c0) -> Host: www.google.com +> POST /o/oauth2/token HTTP/1.1 +> Soup-Debug-Timestamp: 1488736743 +> Soup-Debug: SoupSession 1 (0x688100), SoupMessage 1 (0xbdd0d0), SoupSocket 1 (0x6a02c0) +> Host: accounts.google.com > Content-Type: application/x-www-form-urlencoded > Accept-Encoding: gzip, deflate +> User-Agent: libgdata/0.17.7 - gzip > Connection: Keep-Alive > -> accountType=HOSTED%5FOR%5FGOOGLE&Email=libgdata%2Epicasaweb%40gmail%2Ecom&Passwd=gdata%2Dgdata&service=lh2&source=ytapi%2DGNOME%2Dlibgdata%2D444fubtt%2D0 +> client_id=352818697630-nqu2cmt5quqd6lr17ouoqmb684u84l1f.apps.googleusercontent.com&client_secret=-fA4pHQJxR3zJ-FyAMPQsikg&code=4%2Fi6DYNxhQBZkaRkFvksB6wM2bdj8dGYxUK9BzCFDJYgU&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&grant_type=authorization_code < HTTP/1.1 200 OK -< Soup-Debug-Timestamp: 1375368274 -< Soup-Debug: SoupMessage 1 (0x98c0b0) -< Content-Type: text/plain -< Cache-control: no-cache, no-store -< Pragma: no-cache -< Expires: Mon, 01-Jan-1990 00:00:00 GMT -< Date: Thu, 01 Aug 2013 14:44:33 GMT +< Soup-Debug-Timestamp: 1488736743 +< Soup-Debug: SoupMessage 1 (0xbdd0d0) +< Content-Type: application/json; charset=utf-8 < X-Content-Type-Options: nosniff +< Cache-Control: no-cache, no-store, max-age=0, must-revalidate +< Pragma: no-cache +< Expires: Mon, 01 Jan 1990 00:00:00 GMT +< Date: Sun, 05 Mar 2017 17:59:03 GMT +< Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt +< Content-Encoding: gzip +< Server: ESF < X-XSS-Protection: 1; mode=block -< Content-Length: 947 -< Server: GSE +< X-Frame-Options: SAMEORIGIN +< Alt-Svc: quic=":443"; ma=2592000; v="36,35,34" +< Transfer-Encoding: chunked < -< SID=DQAAANgAAABOuZMrYf2kCl-mSO10kWzwQCikYNOSFd1u_FVG81HBnSCCplm1glYNKbmDVCurMWko64LgjbiBhySDFTnZxqZE6E2dcBMpFNpginqNciQbwFzCu8y_IBagl3iZ9ZRxtjEy1d5Rc8dxKLYU5dAobdHE6uOYqnkkCyppBzSMS12rz8TlC0XShs34nOYLYEcowmbtoDYaq3Zpj8ADsGS9uLnhRnnQdpQKD8tTQVzBXSrqs1BGdUN1NLZzNGU1cogGFKul2lR32KCjq9xZaci5kG-0x2VewrymokXHVT-bTqp2jw -< LSID=DQAAANsAAABqfAP7BRwUl4NRRp09PQSGS8uM-ZHOZSba6D-hWAtvmHRVj-ck_2T9BM_92VDR_pDsaIOVOTUlEAKdsBR0L2X3c1-0v2TlcNdJMdKw1-RYobio-563nxLcU_RkD2PoSwoD5IxnwJBAV0QacWyWCKtK_jXmjaghI9dv4y7wDe5teFH1Wp_bOiwRNgYrC3HB5kiLwiGK9m1kromXfMKr6ey5zr686g5k6kLV_Bn5HS3qn5Lce3ErYJlGiXlgbuX89efg61u3aXlvs4IoqnhW9JwchecisXVxJ-_4QQdHghQM5g -< Auth=DQAAANoAAABqfAP7BRwUl4NRRp09PQSGS8uM-ZHOZSba6D-hWAtvmHRVj-ck_2T9BM_92VDR_pDDGg2bApfteDk1ITzABtR_40EhsW_nXzefXLRJERXTVaZl9fs1z3wuPIApugLt5Vf5-wimwxfqmsGz_45loIQFnyvJ3i0jPnvGutxhdAD7OxQHugZ6U_79W74suTD6wqP8JTcAlbyAVDcA7cdyLccjx6Pz_EgrD72AYxCCrQTKlX-d4Wv1PEbNKxXSP1g1JM2ugqZF-JBLWh5SAjyTpKK9Igbs7M1DeVrhPb4DQMfsdg +< { +< "access_token" : "ya29.GlsFBPCOAdHSSn1lsVj-E1yy3lOfeAHvZeMHP4XZm1NFp0O7mgeklI6GWJGp8sUSvap5tk-cKrH6Q3JI0LNzim2WOBCjOY_LFZ1Hmp7gvcM0rJNbyAiWkv-vX4k0", +< "expires_in" : 3600, +< "refresh_token" : "1/zzEIneRh0ofYki_Pw_pIomJMe-Z36hKixeQ8zcbXZHZDPl160b0f8_0q380yJmr4", +< "token_type" : "Bearer" +< } |