Changes in login and authentication procedure (improved security)
[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
66c3bb76
MM
43post_text = '#diaspy test no. {0}'.format(test_count)
44
62f1912f 45
a8fdc14a
MM
46#######################################
47#### TEST SUITE CODE ####
48#######################################
385e7ebe
MM
49class ConnectionTest(unittest.TestCase):
50 def testLoginWithoutUsername(self):
51 connection = diaspy.connection.Connection(pod=__pod__)
2cf8467c 52 self.assertRaises(diaspy.errors.LoginError, connection.login, password='foo')
385e7ebe
MM
53
54 def testLoginWithoutPassword(self):
55 connection = diaspy.connection.Connection(pod=__pod__)
2cf8467c 56 self.assertRaises(diaspy.errors.LoginError, connection.login, username='user')
385e7ebe 57
264336e2 58 def testGettingUserInfo(self):
c1490af7 59 info = test_connection.getUserInfo()
264336e2
MM
60 self.assertEqual(dict, type(info))
61
7a31b4aa 62
4882952f 63class MessagesTests(unittest.TestCase):
fab8b736 64 def testGettingMailbox(self):
4882952f
MM
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]))
fab8b736 69
f2eaa3c7 70
f3eaa601
MM
71class 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()
9aa1c960 90 self.assertNotIn(post.id, [p.id for p in stream])
f3eaa601
MM
91
92 def testPostingText(self):
93 stream = diaspy.streams.Stream(test_connection)
a98c6792 94 post = stream.post(post_text)
f3eaa601 95 self.assertEqual(diaspy.models.Post, type(post))
27f09973 96
f3eaa601
MM
97 def testPostingImage(self):
98 stream = diaspy.streams.Stream(test_connection)
2d4f6eeb
MM
99 try:
100 stream.post(text=post_text, photo='test-image.png')
1cff2093 101 except (diaspy.errors.StreamError) as e:
2d4f6eeb
MM
102 warnings.warn('{0}')
103 finally:
104 pass
f3eaa601
MM
105
106 def testingAddingTag(self):
107 ft = diaspy.streams.FollowedTags(test_connection)
108 ft.add('test')
109
27a28aaf
MM
110 def testAspectsAdd(self):
111 aspects = diaspy.streams.Aspects(test_connection)
278febce 112 aspects.add(testconf.test_aspect_name_fake)
2ec93347 113 testconf.test_aspect_id = aspects.add(testconf.test_aspect_name).id
27a28aaf 114
63cc182d
MM
115 def testAspectsGettingID(self):
116 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
117 id = aspects.getAspectID(testconf.test_aspect_name)
118 self.assertEqual(testconf.test_aspect_id, id)
63cc182d 119
1467ec15 120 def testAspectsRemoveById(self):
27a28aaf 121 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
122 aspects.remove(testconf.test_aspect_id)
123 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
27a28aaf 124
1467ec15
MM
125 def testAspectsRemoveByName(self):
126 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
127 aspects.remove(name=testconf.test_aspect_name_fake)
128 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
1467ec15 129
27a28aaf
MM
130 def testActivity(self):
131 activity = diaspy.streams.Activity(test_connection)
132
133 def testMentionsStream(self):
134 mentions = diaspy.streams.Mentions(test_connection)
135
f3eaa601
MM
136
137class UserTests(unittest.TestCase):
138 def testHandleSeparatorRaisingExceptions(self):
f3eaa601
MM
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:
6d8d47ce 146 self.assertRaises(Exception, diaspy.people.sephandle, h)
141216df 147
6d8d47ce
MM
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):
9aa1c960 159 user = diaspy.people.User(test_connection, handle=testconf.diaspora_id)
141216df 160 self.assertEqual(testconf.guid, user['guid'])
6d8d47ce
MM
161 self.assertEqual(testconf.diaspora_id, user['handle'])
162 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 163 self.assertIn('id', user.data)
6d8d47ce 164 self.assertIn('avatar', user.data)
141216df 165 self.assertEqual(type(user.stream), diaspy.streams.Outer)
6d8d47ce 166
141216df 167 def testGettingUserByGUID(self):
9aa1c960 168 user = diaspy.people.User(test_connection, guid=testconf.guid)
6d8d47ce
MM
169 self.assertEqual(testconf.diaspora_id, user['handle'])
170 self.assertEqual(testconf.diaspora_name, user['name'])
141216df 171 self.assertIn('id', user.data)
6d8d47ce 172 self.assertIn('avatar', user.data)
141216df 173 self.assertEqual(type(user.stream), diaspy.streams.Outer)
f3eaa601
MM
174
175
dd0a4d9f
MM
176class ContactsTest(unittest.TestCase):
177 def testGetOnlySharing(self):
178 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
179 result = contacts.get(set='only_sharing')
180 for i in result:
dd0a4d9f
MM
181 self.assertEqual(diaspy.people.User, type(i))
182
27f09973
MM
183 def testGetAll(self):
184 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
185 result = contacts.get(set='all')
186 for i in result:
27f09973
MM
187 self.assertEqual(diaspy.people.User, type(i))
188
189 def testGet(self):
190 contacts = diaspy.people.Contacts(test_connection)
7a818fdb
MM
191 result = contacts.get()
192 for i in result:
27f09973
MM
193 self.assertEqual(diaspy.people.User, type(i))
194
dd0a4d9f 195
1467ec15
MM
196class PostTests(unittest.TestCase):
197 def testStringConversion(self):
198 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
199
200 def testRepr(self):
201 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
202
203
b0b4c46d
MM
204class 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
dea56a86
MM
217
218class SettingsTests(unittest.TestCase):
219 def testGettingLanguages(self):
220 settings = diaspy.settings.Settings(test_connection)
221 self.assertIn(('English', 'en'), settings.getLanguages())
222
6d8d47ce
MM
223if __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!')