Changes in tests configuration, small refactoring of `User()`
[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
34 # Test connection setup
35 print('Running test no. {0}'.format(test_count))
36 print('Running tests on connection to pod: "{0}"'.format(__pod__))
37
38 print('Connecting to pod...\t', end='')
39 try:
40 test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
41 test_connection.login()
42 print('[ CONNECTED ]\n')
43 err = False
44 except:
45 print('[ FAIL ]')
46 input('Hit [Return] to continue...')
47 err = True
48 finally:
49 if err: raise
50
51
52 #######################################
53 #### TEST SUITE CODE ####
54 #######################################
55 class ConnectionTest(unittest.TestCase):
56 def testLoginWithoutUsername(self):
57 connection = diaspy.connection.Connection(pod=__pod__)
58 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
59
60 def testLoginWithoutPassword(self):
61 connection = diaspy.connection.Connection(pod=__pod__)
62 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
63
64 def testGettingUserInfo(self):
65 info = test_connection.getUserInfo()
66 self.assertEqual(dict, type(info))
67
68
69 class ClientTests(unittest.TestCase):
70 def testGettingStream(self):
71 client = diaspy.client.Client(test_connection)
72 stream = client.get_stream()
73 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
74
75 def testGettingNotifications(self):
76 client = diaspy.client.Client(test_connection)
77 notifications = client.get_notifications()
78 self.assertEqual(list, type(notifications))
79 if notifications: self.assertEqual(dict, type(notifications[0]))
80
81 def testGettingTagAsList(self):
82 client = diaspy.client.Client(test_connection)
83 tag = client.get_tag('foo')
84 self.assertEqual(list, type(tag))
85 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
86
87 def testGettingTagAsStream(self):
88 client = diaspy.client.Client(test_connection)
89 tag = client.get_tag('foo', stream=True)
90 self.assertEqual(diaspy.streams.Generic, type(tag))
91 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
92
93 def testGettingMailbox(self):
94 client = diaspy.client.Client(test_connection)
95 mailbox = client.get_mailbox()
96 self.assertEqual(list, type(mailbox))
97 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
98
99
100 class StreamTest(unittest.TestCase):
101 def testGetting(self):
102 stream = diaspy.streams.Generic(test_connection)
103
104 def testGettingLength(self):
105 stream = diaspy.streams.Generic(test_connection)
106 len(stream)
107
108 def testClearing(self):
109 stream = diaspy.streams.Stream(test_connection)
110 stream.clear()
111 self.assertEqual(0, len(stream))
112
113 def testPurging(self):
114 stream = diaspy.streams.Stream(test_connection)
115 post = stream.post('#diaspy test')
116 stream.update()
117 post.delete()
118 stream.purge()
119 self.assertNotIn(post.post_id, [p.post_id for p in stream])
120
121 def testPostingText(self):
122 stream = diaspy.streams.Stream(test_connection)
123 post = stream.post('#diaspy test no. {0}'.format(test_count))
124 self.assertEqual(diaspy.models.Post, type(post))
125
126 def testPostingImage(self):
127 stream = diaspy.streams.Stream(test_connection)
128 stream.post_picture('./test-image.png')
129
130 def testingAddingTag(self):
131 ft = diaspy.streams.FollowedTags(test_connection)
132 ft.add('test')
133
134
135 class UserTests(unittest.TestCase):
136 def testHandleSeparatorRaisingExceptions(self):
137 user = diaspy.people.User(test_connection)
138 handles = ['user.pod.example.com',
139 'user@podexamplecom',
140 '@pod.example.com',
141 'use r@pod.example.com',
142 'user0@pod300 example.com',
143 ]
144 for h in handles:
145 self.assertRaises(Exception, user._sephandle, h)
146
147 def testGettingUserByHandle(self):
148 user = diaspy.people.User(test_connection)
149 user.fetchhandle(testconf.diaspora_id)
150 self.assertEqual(testconf.guid, user['guid'])
151 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
152 self.assertIn('id', user.data)
153 self.assertIn('image_urls', user.data)
154 self.assertEqual(type(user.stream), diaspy.streams.Outer)
155
156 def testGettingUserByGUID(self):
157 user = diaspy.people.User(test_connection)
158 user.fetchguid(testconf.guid)
159 self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
160 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
161 self.assertIn('id', user.data)
162 self.assertIn('image_urls', user.data)
163 self.assertEqual(type(user.stream), diaspy.streams.Outer)
164
165
166 if __name__ == '__main__':
167 unittest.main()
168 c = diaspy.connection.Connection(__pod__, __username__, __passwd__)
169 c.login()
170 stream = diaspy.modules.Stream(c)
171 for post in stream:
172 if post['text'] == '#diaspy test': post.delete()