a37541374bf84a57a547e1c07841376a3e5d994c
[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 # modules used by diaspy
7 import requests
8 import re
9 # actual diaspy code
10 import diaspy
11
12
13 #### SETUP STUFF
14 #### test suite configuration variables: can be adjusted to your liking
15 import testconf
16 __pod__ = testconf.__pod__
17 __username__ = testconf.__username__
18 __passwd__ = testconf.__passwd__
19
20
21 # Test counter
22 try:
23 test_count_file = open('TEST_COUNT', 'r')
24 test_count = int(test_count_file.read())
25 test_count_file.close()
26 except (IOError, ValueError):
27 test_count = 0
28 finally:
29 test_count += 1
30 test_count_file = open('TEST_COUNT', 'w')
31 test_count_file.write(str(test_count))
32 test_count_file.close()
33 print('Running test no. {0}'.format(test_count))
34
35 print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='')
36 test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
37 test_connection.login()
38 print('[ CONNECTED ]\n')
39
40
41 #### Test suite code
42 class ConnectionTest(unittest.TestCase):
43 def testLoginWithoutUsername(self):
44 connection = diaspy.connection.Connection(pod=__pod__)
45 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
46
47 def testLoginWithoutPassword(self):
48 connection = diaspy.connection.Connection(pod=__pod__)
49 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
50
51 def testGettingUserInfo(self):
52 info = test_connection.getUserInfo()
53 self.assertEqual(dict, type(info))
54
55
56 class ClientTests(unittest.TestCase):
57 def testGettingStream(self):
58 client = diaspy.client.Client(test_connection)
59 stream = client.get_stream()
60 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
61
62 def testGettingNotifications(self):
63 client = diaspy.client.Client(test_connection)
64 notifications = client.get_notifications()
65 self.assertEqual(list, type(notifications))
66 if notifications: self.assertEqual(dict, type(notifications[0]))
67
68 def testGettingTagAsList(self):
69 client = diaspy.client.Client(test_connection)
70 tag = client.get_tag('foo')
71 self.assertEqual(list, type(tag))
72 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
73
74 def testGettingTagAsStream(self):
75 client = diaspy.client.Client(test_connection)
76 tag = client.get_tag('foo', stream=True)
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.post_id, [p.post_id for p in stream])
107
108 def testPostingText(self):
109 stream = diaspy.streams.Stream(test_connection)
110 post = stream.post('#diaspy test no. {0}'.format(test_count))
111 self.assertEqual(diaspy.models.Post, type(post))
112
113 def testPostingImage(self):
114 stream = diaspy.streams.Stream(test_connection)
115 stream.post_picture('./test-image.png')
116
117 def testingAddingTag(self):
118 ft = diaspy.streams.FollowedTags(test_connection)
119 ft.add('test')
120
121
122 class UserTests(unittest.TestCase):
123 def testHandleSeparatorRaisingExceptions(self):
124 user = diaspy.people.User(test_connection)
125 handles = ['user.pod.example.com',
126 'user@podexamplecom',
127 '@pod.example.com',
128 'use r@pod.example.com',
129 'user0@pod300 example.com',
130 ]
131 for h in handles:
132 self.assertRaises(Exception, user._sephandle, h)
133
134 def testGettingUserByHandle(self):
135 user = diaspy.people.User(test_connection)
136 user.fetchhandle(testconf.diaspora_id)
137 self.assertEqual(testconf.guid, user['guid'])
138 self.assertEqual(testconf.name, user['name'])
139 self.assertIn('id', user.data)
140 self.assertIn('avatar', user.data)
141 self.assertEqual(type(user.stream), diaspy.streams.Outer)
142
143 def testGettingUserByGUID(self):
144 user = diaspy.people.User(test_connection)
145 user.fetchguid(testconf.guid)
146 self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
147 self.assertEqual(testconf.name, user['name'])
148 self.assertIn('id', user.data)
149 self.assertIn('avatar', user.data)
150 self.assertEqual(type(user.stream), diaspy.streams.Outer)
151
152
153 if __name__ == '__main__':
154 unittest.main()
155 c = diaspy.connection.Connection(__pod__, __username__, __passwd__)
156 c.login()
157 stream = diaspy.modules.Stream(c)
158 for post in stream:
159 if post['text'] == '#diaspy test': post.delete()