Changes in login and authentication procedure (improved security)
[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.errors.LoginError, connection.login, password='foo')
53
54 def testLoginWithoutPassword(self):
55 connection = diaspy.connection.Connection(pod=__pod__)
56 self.assertRaises(diaspy.errors.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 MessagesTests(unittest.TestCase):
64 def testGettingMailbox(self):
65 mailbox = diaspy.messages.Mailbox(test_connection)
66 if mailbox:
67 for i in range(len(mailbox)):
68 self.assertEqual(diaspy.models.Conversation, type(mailbox[i]))
69
70
71 class StreamTest(unittest.TestCase):
72 def testGetting(self):
73 stream = diaspy.streams.Generic(test_connection)
74
75 def testGettingLength(self):
76 stream = diaspy.streams.Generic(test_connection)
77 len(stream)
78
79 def testClearing(self):
80 stream = diaspy.streams.Stream(test_connection)
81 stream.clear()
82 self.assertEqual(0, len(stream))
83
84 def testPurging(self):
85 stream = diaspy.streams.Stream(test_connection)
86 post = stream.post('#diaspy test')
87 stream.update()
88 post.delete()
89 stream.purge()
90 self.assertNotIn(post.id, [p.id for p in stream])
91
92 def testPostingText(self):
93 stream = diaspy.streams.Stream(test_connection)
94 post = stream.post(post_text)
95 self.assertEqual(diaspy.models.Post, type(post))
96
97 def testPostingImage(self):
98 stream = diaspy.streams.Stream(test_connection)
99 try:
100 stream.post(text=post_text, photo='test-image.png')
101 except (diaspy.errors.StreamError) as e:
102 warnings.warn('{0}')
103 finally:
104 pass
105
106 def testingAddingTag(self):
107 ft = diaspy.streams.FollowedTags(test_connection)
108 ft.add('test')
109
110 def testAspectsAdd(self):
111 aspects = diaspy.streams.Aspects(test_connection)
112 aspects.add(testconf.test_aspect_name_fake)
113 testconf.test_aspect_id = aspects.add(testconf.test_aspect_name).id
114
115 def testAspectsGettingID(self):
116 aspects = diaspy.streams.Aspects(test_connection)
117 id = aspects.getAspectID(testconf.test_aspect_name)
118 self.assertEqual(testconf.test_aspect_id, id)
119
120 def testAspectsRemoveById(self):
121 aspects = diaspy.streams.Aspects(test_connection)
122 aspects.remove(testconf.test_aspect_id)
123 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
124
125 def testAspectsRemoveByName(self):
126 aspects = diaspy.streams.Aspects(test_connection)
127 aspects.remove(name=testconf.test_aspect_name_fake)
128 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
129
130 def testActivity(self):
131 activity = diaspy.streams.Activity(test_connection)
132
133 def testMentionsStream(self):
134 mentions = diaspy.streams.Mentions(test_connection)
135
136
137 class UserTests(unittest.TestCase):
138 def testHandleSeparatorRaisingExceptions(self):
139 handles = ['user.pod.example.com',
140 'user@podexamplecom',
141 '@pod.example.com',
142 'use r@pod.example.com',
143 'user0@pod300 example.com',
144 ]
145 for h in handles:
146 self.assertRaises(Exception, diaspy.people.sephandle, h)
147
148 def testGettingUserByHandleData(self):
149 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id, fetch='data')
150 self.assertEqual(testconf.guid, user['guid'])
151 self.assertEqual(testconf.diaspora_id, user['handle'])
152 self.assertEqual(testconf.diaspora_name, user['name'])
153 self.assertEqual(type(user.stream), list)
154 self.assertEqual(user.stream, [])
155 self.assertIn('id', user.data)
156 self.assertIn('avatar', user.data)
157
158 def testGettingUserByHandlePosts(self):
159 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
160 self.assertEqual(testconf.guid, user['guid'])
161 self.assertEqual(testconf.diaspora_id, user['handle'])
162 self.assertEqual(testconf.diaspora_name, user['name'])
163 self.assertIn('id', user.data)
164 self.assertIn('avatar', user.data)
165 self.assertEqual(type(user.stream), diaspy.streams.Outer)
166
167 def testGettingUserByGUID(self):
168 user = diaspy.people.User(test_connection, guid=testconf.guid)
169 self.assertEqual(testconf.diaspora_id, user['handle'])
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
176 class ContactsTest(unittest.TestCase):
177 def testGetOnlySharing(self):
178 contacts = diaspy.people.Contacts(test_connection)
179 result = contacts.get(set='only_sharing')
180 for i in result:
181 self.assertEqual(diaspy.people.User, type(i))
182
183 def testGetAll(self):
184 contacts = diaspy.people.Contacts(test_connection)
185 result = contacts.get(set='all')
186 for i in result:
187 self.assertEqual(diaspy.people.User, type(i))
188
189 def testGet(self):
190 contacts = diaspy.people.Contacts(test_connection)
191 result = contacts.get()
192 for i in result:
193 self.assertEqual(diaspy.people.User, type(i))
194
195
196 class PostTests(unittest.TestCase):
197 def testStringConversion(self):
198 s = diaspy.streams.Stream(test_connection)
199
200 def testRepr(self):
201 s = diaspy.streams.Stream(test_connection)
202
203
204 class NotificationsTests(unittest.TestCase):
205 def testMarkgingRead(self):
206 notifications = diaspy.notifications.Notifications(test_connection)
207 notif = None
208 for n in notifications:
209 if n.unread:
210 notif = n
211 break
212 if notif is not None:
213 n.mark(unread=False)
214 else:
215 warnings.warn('test not sufficient: no unread notifications were found')
216
217
218 class SettingsTests(unittest.TestCase):
219 def testGettingLanguages(self):
220 settings = diaspy.settings.Settings(test_connection)
221 self.assertIn(('English', 'en'), settings.getLanguages())
222
223 if __name__ == '__main__':
224 print('Hello World!')
225 print('It\'s testing time!')
226 n = unittest.main()
227 print(n)
228 print('It was testing time!')