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