This drops the duplication of `googleapiclient` inside the `oauth2client` repo, which involved several steps: * Delete the actual `googleapiclient` files. * Remove the duplicated tests, since they're also hosted in the `googleapiclient` repo. * Add a copy of three testing-related functions back in, since they're also used in the tests here. This was `HttpMock` and `HttpMockSequence` in `tests/http_mock.py`, and `assertUrisEqual` in `tests/test_oauth2client.py`. Once that was done, I did just the tiniest bit of cleanup in the tests: * Remove the now-superfluous `_oauth2client_` in the filenames for all the tests. * Drop unneeded files from `data/`. * Add `py26` back as a `tox` testing environment. * Temporarily disable the `appengine` test for both environments. In a coming PR, I'll actually clean up the GAE test to work correctly in the presence or absence of `dev_appserver`, but this isn't a bad first step. (In particular, my next PR will just add a `.travis.yml`.)
112 lines
4.5 KiB
Python
112 lines
4.5 KiB
Python
# Copyright 2012 Google Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""Tests for oauth2client.xsrfutil.
|
|
|
|
Unit tests for oauth2client.xsrfutil.
|
|
"""
|
|
|
|
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
|
|
|
|
import unittest
|
|
|
|
from oauth2client import xsrfutil
|
|
|
|
# Jan 17 2008, 5:40PM
|
|
TEST_KEY = 'test key'
|
|
TEST_TIME = 1200609642081230
|
|
TEST_USER_ID_1 = 123832983
|
|
TEST_USER_ID_2 = 938297432
|
|
TEST_ACTION_ID_1 = 'some_action'
|
|
TEST_ACTION_ID_2 = 'some_other_action'
|
|
TEST_EXTRA_INFO_1 = 'extra_info_1'
|
|
TEST_EXTRA_INFO_2 = 'more_extra_info'
|
|
|
|
|
|
class XsrfUtilTests(unittest.TestCase):
|
|
"""Test xsrfutil functions."""
|
|
|
|
def testGenerateAndValidateToken(self):
|
|
"""Test generating and validating a token."""
|
|
token = xsrfutil.generate_token(TEST_KEY,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
when=TEST_TIME)
|
|
|
|
# Check that the token is considered valid when it should be.
|
|
self.assertTrue(xsrfutil.validate_token(TEST_KEY,
|
|
token,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=TEST_TIME))
|
|
|
|
# Should still be valid 15 minutes later.
|
|
later15mins = TEST_TIME + 15*60
|
|
self.assertTrue(xsrfutil.validate_token(TEST_KEY,
|
|
token,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later15mins))
|
|
|
|
# But not if beyond the timeout.
|
|
later2hours = TEST_TIME + 2*60*60
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
token,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later2hours))
|
|
|
|
# Or if the key is different.
|
|
self.assertFalse(xsrfutil.validate_token('another key',
|
|
token,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later15mins))
|
|
|
|
# Or the user ID....
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
token,
|
|
TEST_USER_ID_2,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later15mins))
|
|
|
|
# Or the action ID...
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
token,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_2,
|
|
current_time=later15mins))
|
|
|
|
# Invalid when truncated
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
token[:-1],
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later15mins))
|
|
|
|
# Invalid with extra garbage
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
token + 'x',
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1,
|
|
current_time=later15mins))
|
|
|
|
# Invalid with token of None
|
|
self.assertFalse(xsrfutil.validate_token(TEST_KEY,
|
|
None,
|
|
TEST_USER_ID_1,
|
|
action_id=TEST_ACTION_ID_1))
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|