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