tests.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import unittest
  4. # failure to import any of the modules below indicates failed tests
  5. # =================================================================
  6. # modules used by diaspy
  7. import re
  8. import requests
  9. import warnings
  10. # actual diaspy code
  11. import diaspy
  12. #### SETUP STUFF
  13. #### test suite configuration variables: can be adjusted to your liking
  14. import testconf
  15. __pod__ = testconf.__pod__
  16. __username__ = testconf.__username__
  17. __passwd__ = testconf.__passwd__
  18. # Test counter
  19. try:
  20. test_count_file = open('TEST_COUNT', 'r')
  21. test_count = int(test_count_file.read())
  22. test_count_file.close()
  23. except (IOError, ValueError):
  24. test_count = 0
  25. finally:
  26. test_count += 1
  27. test_count_file = open('TEST_COUNT', 'w')
  28. test_count_file.write(str(test_count))
  29. test_count_file.close()
  30. print('Running test no. {0}'.format(test_count))
  31. print('Running tests on connection: "{0}:{1}@{2}"\t'.format(testconf.__username__, '*'*len(testconf.__passwd__), __pod__), end='')
  32. test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
  33. test_connection.login()
  34. print('[ CONNECTED ]\n')
  35. # Setup test aspects
  36. print('Adding test aspects...\t', end='')
  37. diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name_fake)
  38. testconf.test_aspect_id = diaspy.streams.Aspects(test_connection).add(testconf.test_aspect_name).id
  39. print('OK')
  40. print([i['name'] for i in test_connection.getUserData()['aspects']])
  41. post_text = '#diaspy test no. {0}'.format(test_count)
  42. #######################################
  43. #### TEST SUITE CODE ####
  44. #######################################
  45. class ConnectionTest(unittest.TestCase):
  46. def testGettingUserInfo(self):
  47. info = test_connection.getUserData()
  48. self.assertEqual(dict, type(info))
  49. class MessagesTests(unittest.TestCase):
  50. def testGettingMailbox(self):
  51. mailbox = diaspy.messages.Mailbox(test_connection)
  52. if mailbox:
  53. for i in range(len(mailbox)):
  54. self.assertEqual(diaspy.models.Conversation, type(mailbox[i]))
  55. class AspectsTests(unittest.TestCase):
  56. def testAspectsGettingID(self):
  57. aspects = diaspy.streams.Aspects(test_connection)
  58. id = aspects.getAspectID(testconf.test_aspect_name)
  59. self.assertEqual(testconf.test_aspect_id, id)
  60. def testAspectsRemoveById(self):
  61. aspects = diaspy.streams.Aspects(test_connection)
  62. for i in test_connection.getUserData()['aspects']:
  63. if i['name'] == testconf.test_aspect_name:
  64. print(i['id'], end=' ')
  65. aspects.remove(id=i['id'])
  66. break
  67. names = [i['name'] for i in test_connection.getUserData()['aspects']]
  68. print(names)
  69. self.assertNotIn(testconf.test_aspect_name, names)
  70. def testAspectsRemoveByName(self):
  71. aspects = diaspy.streams.Aspects(test_connection)
  72. print(testconf.test_aspect_name_fake, end=' ')
  73. aspects.remove(name=testconf.test_aspect_name_fake)
  74. names = [i['name'] for i in test_connection.getUserData()['aspects']]
  75. print(names)
  76. self.assertNotIn(testconf.test_aspect_name_fake, names)
  77. class StreamTest(unittest.TestCase):
  78. def testGetting(self):
  79. stream = diaspy.streams.Generic(test_connection)
  80. def testGettingLength(self):
  81. stream = diaspy.streams.Generic(test_connection)
  82. len(stream)
  83. def testClearing(self):
  84. stream = diaspy.streams.Stream(test_connection)
  85. stream.clear()
  86. self.assertEqual(0, len(stream))
  87. def testPurging(self):
  88. stream = diaspy.streams.Stream(test_connection)
  89. post = stream.post('#diaspy test')
  90. stream.update()
  91. post.delete()
  92. stream.purge()
  93. self.assertNotIn(post.id, [p.id for p in stream])
  94. def testPostingText(self):
  95. stream = diaspy.streams.Stream(test_connection)
  96. post = stream.post(post_text)
  97. self.assertEqual(diaspy.models.Post, type(post))
  98. def testPostingImage(self):
  99. stream = diaspy.streams.Stream(test_connection)
  100. try:
  101. stream.post(text=post_text, photo='test-image.png')
  102. except (diaspy.errors.StreamError) as e:
  103. warnings.warn('{0}')
  104. finally:
  105. pass
  106. def testingAddingTag(self):
  107. ft = diaspy.streams.FollowedTags(test_connection)
  108. ft.add('test')
  109. def testActivity(self):
  110. activity = diaspy.streams.Activity(test_connection)
  111. def testMentionsStream(self):
  112. mentions = diaspy.streams.Mentions(test_connection)
  113. class UserTests(unittest.TestCase):
  114. def testHandleSeparatorRaisingExceptions(self):
  115. handles = ['user.pod.example.com',
  116. 'user@podexamplecom',
  117. '@pod.example.com',
  118. 'use r@pod.example.com',
  119. 'user0@pod300 example.com',
  120. ]
  121. for h in handles:
  122. self.assertRaises(Exception, diaspy.people.sephandle, h)
  123. def testGettingUserByHandleData(self):
  124. user = diaspy.people.User(test_connection, handle=testconf.diaspora_id, fetch='data')
  125. self.assertEqual(testconf.guid, user['guid'])
  126. self.assertEqual(testconf.diaspora_id, user['handle'])
  127. self.assertEqual(testconf.diaspora_name, user['name'])
  128. self.assertEqual(type(user.stream), list)
  129. self.assertEqual(user.stream, [])
  130. self.assertIn('id', user.data)
  131. self.assertIn('avatar', user.data)
  132. def testGettingUserByHandlePosts(self):
  133. user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
  134. self.assertEqual(testconf.guid, user['guid'])
  135. self.assertEqual(testconf.diaspora_id, user['handle'])
  136. self.assertEqual(testconf.diaspora_name, user['name'])
  137. self.assertIn('id', user.data)
  138. self.assertIn('avatar', user.data)
  139. self.assertEqual(type(user.stream), diaspy.streams.Outer)
  140. def testGettingUserByGUID(self):
  141. user = diaspy.people.User(test_connection, guid=testconf.guid)
  142. self.assertEqual(testconf.diaspora_id, user['handle'])
  143. self.assertEqual(testconf.diaspora_name, user['name'])
  144. self.assertIn('id', user.data)
  145. self.assertIn('avatar', user.data)
  146. self.assertEqual(type(user.stream), diaspy.streams.Outer)
  147. def testReprMethod(self):
  148. user = diaspy.people.User(test_connection, guid=testconf.guid)
  149. repr(user)
  150. print(user)
  151. class ContactsTest(unittest.TestCase):
  152. def testGetOnlySharing(self):
  153. contacts = diaspy.people.Contacts(test_connection)
  154. result = contacts.get(set='only_sharing')
  155. for i in result:
  156. self.assertEqual(diaspy.people.User, type(i))
  157. def testGetAll(self):
  158. contacts = diaspy.people.Contacts(test_connection)
  159. result = contacts.get(set='all')
  160. for i in result:
  161. self.assertEqual(diaspy.people.User, type(i))
  162. def testGet(self):
  163. contacts = diaspy.people.Contacts(test_connection)
  164. result = contacts.get()
  165. for i in result:
  166. self.assertEqual(diaspy.people.User, type(i))
  167. class PostTests(unittest.TestCase):
  168. def testStringConversion(self):
  169. s = diaspy.streams.Stream(test_connection)
  170. def testRepr(self):
  171. s = diaspy.streams.Stream(test_connection)
  172. class NotificationsTests(unittest.TestCase):
  173. def testMarkingRead(self):
  174. notifications = diaspy.notifications.Notifications(test_connection)
  175. notif = None
  176. for n in notifications:
  177. if n.unread:
  178. notif = n
  179. break
  180. if notif is not None:
  181. n.mark(unread=False)
  182. else:
  183. warnings.warn('test not sufficient: no unread notifications were found')
  184. class SettingsTests(unittest.TestCase):
  185. profile = diaspy.settings.Profile(test_connection)
  186. account = diaspy.settings.Account(test_connection)
  187. def testGettingName(self):
  188. self.assertEqual(testconf.user_names_tuple, self.profile.getName())
  189. def testGettingLocation(self):
  190. self.assertEqual(testconf.user_location_string, self.profile.getLocation())
  191. def testGettingGender(self):
  192. self.assertEqual(testconf.user_gender_string, self.profile.getGender())
  193. def testGettingBirthDate(self):
  194. self.assertEqual(testconf.user_date_of_birth, self.profile.getBirthDate(named_month=False))
  195. self.assertEqual(testconf.user_date_of_birth_named, self.profile.getBirthDate(named_month=True))
  196. def testGettingInfoIfProfileIsSearchable(self):
  197. self.assertEqual(testconf.user_is_searchable, self.profile.isSearchable())
  198. def testGettingInfoIfProfileIsNSFW(self):
  199. self.assertEqual(testconf.user_is_nsfw, self.profile.isNSFW())
  200. def testGettingTags(self):
  201. self.assertEqual(sorted(testconf.user_tags), sorted(self.profile.getTags()))
  202. def testGettingLanguages(self):
  203. self.assertIn(('en', 'English'), self.account.getLanguages())
  204. def testGettingEmail(self):
  205. self.assertEqual(testconf.user_email, self.account.getEmail())
  206. if __name__ == '__main__':
  207. print('Hello World!')
  208. print('It\'s testing time!')
  209. n = unittest.main()
  210. print(n)
  211. print('Good! All tests passed!')