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