test_twitter_auth.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for static parts of Twitter package
  4. """
  5. import os
  6. import unittest
  7. from nose import SkipTest
  8. try:
  9. import twython
  10. except ImportError as e:
  11. raise SkipTest("The twython library has not been installed.")
  12. from nltk.twitter import Authenticate
  13. class TestCredentials(unittest.TestCase):
  14. """
  15. Tests that Twitter credentials information from file is handled correctly.
  16. """
  17. def setUp(self):
  18. self.subdir = os.path.join(os.path.dirname(__file__), 'files')
  19. self.auth = Authenticate()
  20. os.environ['TWITTER'] = 'twitter-files'
  21. def test_environment(self):
  22. """
  23. Test that environment variable has been read correctly.
  24. """
  25. fn = os.path.basename(self.auth.creds_subdir)
  26. self.assertEqual(fn, os.environ['TWITTER'])
  27. def test_empty_subdir1(self):
  28. """
  29. Setting subdir to empty path should raise an error.
  30. """
  31. try:
  32. self.auth.load_creds(subdir='')
  33. # raises ValueError (zero length field name in format) for python 2.6
  34. # OSError for the rest
  35. except OSError:
  36. pass
  37. except ValueError:
  38. pass
  39. except Exception as e:
  40. self.fail('Unexpected exception thrown: %s' % e)
  41. else:
  42. self.fail('OSError exception not thrown.')
  43. def test_empty_subdir2(self):
  44. """
  45. Setting subdir to `None` should raise an error.
  46. """
  47. self.auth.creds_subdir = None
  48. try:
  49. self.auth.load_creds()
  50. except ValueError:
  51. pass
  52. except Exception as e:
  53. self.fail('Unexpected exception thrown: %s' % e)
  54. else:
  55. self.fail('ValueError exception not thrown.')
  56. def test_missingdir(self):
  57. """
  58. Setting subdir to nonexistent directory should raise an error.
  59. """
  60. try:
  61. self.auth.load_creds(subdir='/nosuchdir')
  62. # raises ValueError (zero length field name in format) for python 2.6
  63. # OSError for the rest
  64. except OSError:
  65. pass
  66. except ValueError:
  67. pass
  68. except Exception as e:
  69. self.fail('Unexpected exception thrown: %s' % e)
  70. else:
  71. self.fail('OSError exception not thrown.')
  72. def test_missingfile1(self):
  73. """
  74. Defaults for authentication will fail since 'credentials.txt' not
  75. present in default subdir, as read from `os.environ['TWITTER']`.
  76. """
  77. try:
  78. self.auth.load_creds()
  79. # raises ValueError (zero length field name in format) for python 2.6
  80. # OSError for the rest
  81. except OSError:
  82. pass
  83. except ValueError:
  84. pass
  85. except Exception as e:
  86. self.fail('Unexpected exception thrown: %s' % e)
  87. else:
  88. self.fail('OSError exception not thrown.')
  89. def test_missingfile2(self):
  90. """
  91. Credentials file 'foobar' cannot be found in default subdir.
  92. """
  93. try:
  94. self.auth.load_creds(creds_file='foobar')
  95. # raises ValueError (zero length field name in format) for python 2.6
  96. # OSError for the rest
  97. except OSError:
  98. pass
  99. except ValueError:
  100. pass
  101. except Exception as e:
  102. self.fail('Unexpected exception thrown: %s' % e)
  103. else:
  104. self.fail('OSError exception not thrown.')
  105. def test_incomplete_file(self):
  106. """
  107. Credentials file 'bad_oauth1-1.txt' is incomplete
  108. """
  109. try:
  110. self.auth.load_creds(creds_file='bad_oauth1-1.txt', subdir=self.subdir)
  111. except ValueError:
  112. pass
  113. except Exception as e:
  114. self.fail('Unexpected exception thrown: %s' % e)
  115. else:
  116. self.fail('ValueError exception not thrown.')
  117. def test_malformed_file1(self):
  118. """
  119. First key in credentials file 'bad_oauth1-2.txt' is ill-formed
  120. """
  121. try:
  122. self.auth.load_creds(creds_file='bad_oauth1-2.txt', subdir=self.subdir)
  123. except ValueError:
  124. pass
  125. except Exception as e:
  126. self.fail('Unexpected exception thrown: %s' % e)
  127. else:
  128. self.fail('ValueError exception not thrown.')
  129. def test_malformed_file2(self):
  130. """
  131. First key in credentials file 'bad_oauth1-2.txt' is ill-formed
  132. """
  133. try:
  134. self.auth.load_creds(creds_file='bad_oauth1-3.txt', subdir=self.subdir)
  135. except ValueError:
  136. pass
  137. except Exception as e:
  138. self.fail('Unexpected exception thrown: %s' % e)
  139. else:
  140. self.fail('ValueError exception not thrown.')
  141. def test_correct_path(self):
  142. """
  143. Path to default credentials file is well-formed, given specified
  144. subdir.
  145. """
  146. self.auth.load_creds(subdir=self.subdir)
  147. self.auth.creds_fullpath = os.path.join(self.subdir, self.auth.creds_file)
  148. def test_correct_file1(self):
  149. """
  150. Default credentials file is identified
  151. """
  152. self.auth.load_creds(subdir=self.subdir)
  153. self.assertEqual(self.auth.creds_file, 'credentials.txt')
  154. def test_correct_file2(self):
  155. """
  156. Default credentials file has been read correctluy
  157. """
  158. oauth = self.auth.load_creds(subdir=self.subdir)
  159. self.assertEqual(oauth['app_key'], 'a')
  160. if __name__ == '__main__':
  161. unittest.main()