test_utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import datetime
  2. import os
  3. from oauthlib.oauth2.rfc6749.utils import (
  4. escape, generate_age, host_from_uri, is_secure_transport, list_to_scope,
  5. params_from_uri, scope_to_list,
  6. )
  7. from tests.unittest import TestCase
  8. class ScopeObject:
  9. """
  10. Fixture for testing list_to_scope()/scope_to_list() with objects other
  11. than regular strings.
  12. """
  13. def __init__(self, scope):
  14. self.scope = scope
  15. def __str__(self):
  16. return self.scope
  17. class UtilsTests(TestCase):
  18. def test_escape(self):
  19. """Assert that we are only escaping unicode"""
  20. self.assertRaises(ValueError, escape, b"I am a string type. Not a unicode type.")
  21. self.assertEqual(escape("I am a unicode type."), "I%20am%20a%20unicode%20type.")
  22. def test_host_from_uri(self):
  23. """Test if hosts and ports are properly extracted from URIs.
  24. This should be done according to the MAC Authentication spec.
  25. Defaults ports should be provided when none is present in the URI.
  26. """
  27. self.assertEqual(host_from_uri('http://a.b-c.com:8080'), ('a.b-c.com', '8080'))
  28. self.assertEqual(host_from_uri('https://a.b.com:8080'), ('a.b.com', '8080'))
  29. self.assertEqual(host_from_uri('http://www.example.com'), ('www.example.com', '80'))
  30. self.assertEqual(host_from_uri('https://www.example.com'), ('www.example.com', '443'))
  31. def test_is_secure_transport(self):
  32. """Test check secure uri."""
  33. if 'OAUTHLIB_INSECURE_TRANSPORT' in os.environ:
  34. del os.environ['OAUTHLIB_INSECURE_TRANSPORT']
  35. self.assertTrue(is_secure_transport('https://example.com'))
  36. self.assertFalse(is_secure_transport('http://example.com'))
  37. os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
  38. self.assertTrue(is_secure_transport('http://example.com'))
  39. del os.environ['OAUTHLIB_INSECURE_TRANSPORT']
  40. def test_params_from_uri(self):
  41. self.assertEqual(params_from_uri('http://i.b/?foo=bar&g&scope=a+d'),
  42. {'foo': 'bar', 'g': '', 'scope': ['a', 'd']})
  43. def test_generate_age(self):
  44. issue_time = datetime.datetime.now() - datetime.timedelta(
  45. days=3, minutes=1, seconds=4)
  46. self.assertGreater(float(generate_age(issue_time)), 259263.0)
  47. def test_list_to_scope(self):
  48. expected = 'foo bar baz'
  49. string_list = ['foo', 'bar', 'baz']
  50. self.assertEqual(list_to_scope(string_list), expected)
  51. string_tuple = ('foo', 'bar', 'baz')
  52. self.assertEqual(list_to_scope(string_tuple), expected)
  53. obj_list = [ScopeObject('foo'), ScopeObject('bar'), ScopeObject('baz')]
  54. self.assertEqual(list_to_scope(obj_list), expected)
  55. set_list = set(string_list)
  56. set_scope = list_to_scope(set_list)
  57. assert len(set_scope.split(' ')) == 3
  58. for x in string_list:
  59. assert x in set_scope
  60. self.assertRaises(ValueError, list_to_scope, object())
  61. def test_scope_to_list(self):
  62. expected = ['foo', 'bar', 'baz']
  63. string_scopes = 'foo bar baz '
  64. self.assertEqual(scope_to_list(string_scopes), expected)
  65. string_list_scopes = ['foo', 'bar', 'baz']
  66. self.assertEqual(scope_to_list(string_list_scopes), expected)
  67. tuple_list_scopes = ('foo', 'bar', 'baz')
  68. self.assertEqual(scope_to_list(tuple_list_scopes), expected)
  69. obj_list_scopes = [ScopeObject('foo'), ScopeObject('bar'), ScopeObject('baz')]
  70. self.assertEqual(scope_to_list(obj_list_scopes), expected)
  71. set_list_scopes = set(string_list_scopes)
  72. set_list = scope_to_list(set_list_scopes)
  73. self.assertEqual(sorted(set_list), sorted(string_list_scopes))