test_oauth.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright 2009-2018 Canonical Ltd.
  2. # This file is part of lazr.restfulclient.
  3. #
  4. # lazr.restfulclient is free software: you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public License
  6. # as published by the Free Software Foundation, version 3 of the
  7. # License.
  8. #
  9. # lazr.restfulclient is distributed in the hope that it will be
  10. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  11. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with lazr.restfulclient. If not, see <http://www.gnu.org/licenses/>.
  16. """Tests for the OAuth-aware classes."""
  17. __metaclass__ = type
  18. import os
  19. import os.path
  20. import stat
  21. import unittest
  22. from fixtures import MockPatch, TempDir
  23. from testtools import TestCase
  24. from lazr.restfulclient.authorize.oauth import (
  25. AccessToken,
  26. Consumer,
  27. OAuthAuthorizer,
  28. SystemWideConsumer,
  29. )
  30. class TestConsumer(TestCase):
  31. def test_data_fields(self):
  32. consumer = Consumer("key", "secret", "application")
  33. self.assertEqual(consumer.key, "key")
  34. self.assertEqual(consumer.secret, "secret")
  35. self.assertEqual(consumer.application_name, "application")
  36. def test_default_application_name(self):
  37. # Application name defaults to None
  38. consumer = Consumer("key", "secret")
  39. self.assertEqual(consumer.application_name, None)
  40. class TestAccessToken(TestCase):
  41. def test_data_fields(self):
  42. access_token = AccessToken("key", "secret", "context")
  43. self.assertEqual(access_token.key, "key")
  44. self.assertEqual(access_token.secret, "secret")
  45. self.assertEqual(access_token.context, "context")
  46. def test_default_context(self):
  47. # Context defaults to None.
  48. access_token = AccessToken("key", "secret")
  49. self.assertIsNone(access_token.context)
  50. def test___str__(self):
  51. access_token = AccessToken("lock&key", "secret=password")
  52. self.assertEqual(
  53. "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key",
  54. str(access_token),
  55. )
  56. def test_from_string(self):
  57. access_token = AccessToken.from_string(
  58. "oauth_token_secret=secret%3Dpassword&oauth_token=lock%26key"
  59. )
  60. self.assertEqual(access_token.key, "lock&key")
  61. self.assertEqual(access_token.secret, "secret=password")
  62. class TestSystemWideConsumer(TestCase):
  63. def test_useful_distro_name(self):
  64. # If distro.name returns a useful string, as it does on Ubuntu,
  65. # we'll use the first string for the system type.
  66. self.useFixture(MockPatch("distro.name", return_value="Fooix"))
  67. self.useFixture(MockPatch("platform.system", return_value="FooOS"))
  68. self.useFixture(MockPatch("socket.gethostname", return_value="foo"))
  69. consumer = SystemWideConsumer("app name")
  70. self.assertEqual(consumer.key, "System-wide: Fooix (foo)")
  71. def test_empty_distro_name(self):
  72. # If distro.name returns an empty string, as it does on Windows and
  73. # Mac OS X, we fall back to the result of platform.system().
  74. self.useFixture(MockPatch("distro.name", return_value=""))
  75. self.useFixture(MockPatch("platform.system", return_value="BarOS"))
  76. self.useFixture(MockPatch("socket.gethostname", return_value="bar"))
  77. consumer = SystemWideConsumer("app name")
  78. self.assertEqual(consumer.key, "System-wide: BarOS (bar)")
  79. def test_broken_distro_name(self):
  80. # If distro.name raises an exception, we fall back to the result of
  81. # platform.system().
  82. self.useFixture(
  83. MockPatch("distro.name", side_effect=Exception("Oh noes!"))
  84. )
  85. self.useFixture(MockPatch("platform.system", return_value="BazOS"))
  86. self.useFixture(MockPatch("socket.gethostname", return_value="baz"))
  87. consumer = SystemWideConsumer("app name")
  88. self.assertEqual(consumer.key, "System-wide: BazOS (baz)")
  89. class TestOAuthAuthorizer(TestCase):
  90. """Test for the OAuth Authorizer."""
  91. def test_save_to_and_load_from__path(self):
  92. # Credentials can be saved to and loaded from a file using
  93. # save_to_path() and load_from_path().
  94. temp_dir = self.useFixture(TempDir()).path
  95. credentials_path = os.path.join(temp_dir, "credentials")
  96. credentials = OAuthAuthorizer(
  97. "consumer.key",
  98. consumer_secret="consumer.secret",
  99. access_token=AccessToken("access.key", "access.secret"),
  100. )
  101. credentials.save_to_path(credentials_path)
  102. self.assertTrue(os.path.exists(credentials_path))
  103. # Make sure the file is readable and writable by the user, but
  104. # not by anyone else.
  105. self.assertEqual(
  106. stat.S_IMODE(os.stat(credentials_path).st_mode),
  107. stat.S_IREAD | stat.S_IWRITE,
  108. )
  109. loaded_credentials = OAuthAuthorizer.load_from_path(credentials_path)
  110. self.assertEqual(loaded_credentials.consumer.key, "consumer.key")
  111. self.assertEqual(loaded_credentials.consumer.secret, "consumer.secret")
  112. self.assertEqual(loaded_credentials.access_token.key, "access.key")
  113. self.assertEqual(
  114. loaded_credentials.access_token.secret, "access.secret"
  115. )
  116. def test_suite():
  117. return unittest.TestLoader().loadTestsFromName(__name__)