Merge branch 'master' into search
[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
14
15 #### SETUP STUFF
16 #### test suite configuration variables: can be adjusted to your liking
17 import testconf
18 __pod__ = testconf.__pod__
19 __username__ = testconf.__username__
20 __passwd__ = testconf.__passwd__
21
22
23 # Test counter
24 try:
25 test_count_file = open('TEST_COUNT', 'r')
26 test_count = int(test_count_file.read())
27 test_count_file.close()
28 except (IOError, ValueError):
29 test_count = 0
30 finally:
31 test_count += 1
32 test_count_file = open('TEST_COUNT', 'w')
33 test_count_file.write(str(test_count))
34 test_count_file.close()
35 print('Running test no. {0}'.format(test_count))
36
37 print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='')
38 test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
39 test_connection.login()
40 print('[ CONNECTED ]\n')
41
42 post_text = '#diaspy test no. {0}'.format(test_count)
43
44
45 #######################################
46 #### TEST SUITE CODE ####
47 #######################################
48 class ConnectionTest(unittest.TestCase):
49 def testLoginWithoutUsername(self):
50 connection = diaspy.connection.Connection(pod=__pod__)
51 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
52
53 def testLoginWithoutPassword(self):
54 connection = diaspy.connection.Connection(pod=__pod__)
55 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
56
57 def testGettingUserInfo(self):
58 info = test_connection.getUserInfo()
59 self.assertEqual(dict, type(info))
60
61
62 class ClientTests(unittest.TestCase):
63 def testGettingStream(self):
64 client = diaspy.client.Client(test_connection)
65 stream = client.get_stream()
66 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
67
68 def testGettingNotifications(self):
69 client = diaspy.client.Client(test_connection)
70 notifications = client.get_notifications()
71 self.assertEqual(diaspy.notifications.Notifications, type(notifications))
72 if notifications: self.assertEqual(diaspy.models.Notification, type(notifications[0]))
73
74 def testGettingTag(self):
75 client = diaspy.client.Client(test_connection)
76 tag = client.get_tag('foo')
77 self.assertEqual(diaspy.streams.Generic, type(tag))
78 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
79
80 def testGettingMailbox(self):
81 client = diaspy.client.Client(test_connection)
82 mailbox = client.get_mailbox()
83 self.assertEqual(list, type(mailbox))
84 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
85
86
87 class StreamTest(unittest.TestCase):
88 def testGetting(self):
89 stream = diaspy.streams.Generic(test_connection)
90
91 def testGettingLength(self):
92 stream = diaspy.streams.Generic(test_connection)
93 len(stream)
94
95 def testClearing(self):
96 stream = diaspy.streams.Stream(test_connection)
97 stream.clear()
98 self.assertEqual(0, len(stream))
99
100 def testPurging(self):
101 stream = diaspy.streams.Stream(test_connection)
102 post = stream.post('#diaspy test')
103 stream.update()
104 post.delete()
105 stream.purge()
106 self.assertNotIn(post.id, [p.id for p in stream])
107
108 def testPostingText(self):
109 stream = diaspy.streams.Stream(test_connection)
110 post = stream.post(post_text)
111 self.assertEqual(diaspy.models.Post, type(post))
112
113 @unittest.skip('returns internal server error -- not our fault that it is failing')
114 def testPostingImage(self):
115 stream = diaspy.streams.Stream(test_connection)
116 stream.post(text=post_text, photo='test-image.png')
117
118 def testingAddingTag(self):
119 ft = diaspy.streams.FollowedTags(test_connection)
120 ft.add('test')
121
122 def testAspectsAdd(self):
123 aspects = diaspy.streams.Aspects(test_connection)
124 aspects.add(testconf.test_aspect_name_fake)
125 testconf.test_aspect_id = aspects.add(testconf.test_aspect_name).id
126
127 def testAspectsGettingID(self):
128 aspects = diaspy.streams.Aspects(test_connection)
129 id = aspects.getAspectID(testconf.test_aspect_name)
130 self.assertEqual(testconf.test_aspect_id, id)
131
132 def testAspectsRemoveById(self):
133 aspects = diaspy.streams.Aspects(test_connection)
134 aspects.remove(testconf.test_aspect_id)
135 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
136
137 def testAspectsRemoveByName(self):
138 aspects = diaspy.streams.Aspects(test_connection)
139 aspects.remove(name=testconf.test_aspect_name_fake)
140 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
141
142 def testActivity(self):
143 activity = diaspy.streams.Activity(test_connection)
144
145 def testMentionsStream(self):
146 mentions = diaspy.streams.Mentions(test_connection)
147
148
149 class UserTests(unittest.TestCase):
150 def testHandleSeparatorRaisingExceptions(self):
151 user = diaspy.people.User(test_connection)
152 handles = ['user.pod.example.com',
153 'user@podexamplecom',
154 '@pod.example.com',
155 'use r@pod.example.com',
156 'user0@pod300 example.com',
157 ]
158 for h in handles:
159 self.assertRaises(Exception, user._sephandle, h)
160
161 def testGettingUserByHandle(self):
162 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
163 user.fetchhandle()
164 self.assertEqual(testconf.guid, user['guid'])
165 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
166 self.assertIn('id', user.data)
167 self.assertIn('image_urls', user.data)
168 self.assertEqual(type(user.stream), diaspy.streams.Outer)
169
170 def testGettingUserByGUID(self):
171 user = diaspy.people.User(test_connection, guid=testconf.guid)
172 user.fetchguid()
173 self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
174 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
175 self.assertIn('id', user.data)
176 self.assertIn('image_urls', user.data)
177 self.assertEqual(type(user.stream), diaspy.streams.Outer)
178
179
180 class ContactsTest(unittest.TestCase):
181 def testGetOnlySharing(self):
182 contacts = diaspy.people.Contacts(test_connection)
183 result = contacts.get(set='only_sharing')
184 for i in result:
185 self.assertEqual(diaspy.people.User, type(i))
186
187 def testGetAll(self):
188 contacts = diaspy.people.Contacts(test_connection)
189 result = contacts.get(set='all')
190 for i in result:
191 self.assertEqual(diaspy.people.User, type(i))
192
193 def testGet(self):
194 contacts = diaspy.people.Contacts(test_connection)
195 result = contacts.get()
196 for i in result:
197 self.assertEqual(diaspy.people.User, type(i))
198
199
200 class PostTests(unittest.TestCase):
201 def testStringConversion(self):
202 s = diaspy.streams.Stream(test_connection)
203
204 def testRepr(self):
205 s = diaspy.streams.Stream(test_connection)
206
207
208 class NotificationsTests(unittest.TestCase):
209 def testMarkgingRead(self):
210 notifications = diaspy.notifications.Notifications(test_connection)
211 notif = None
212 for n in notifications:
213 if n.unread:
214 notif = n
215 break
216 if notif is not None:
217 n.mark(unread=False)
218 else:
219 warnings.warn('test not sufficient: no unread notifications were found')
220
221 if __name__ == '__main__': unittest.main()