Merge branch 'streams' of git://github.com/marekjm/diaspora-api into streams
[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}"\n'.format(__pod__))
36 test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
37 test_connection.login()
38
39
40 #### Test suite code
41 class StreamTest(unittest.TestCase):
42 def testGetting(self):
43 c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
44 c.login()
45 stream = diaspy.streams.Generic(c)
46
47 def testGettingLength(self):
48 c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
49 c.login()
50 stream = diaspy.streams.Generic(c)
51 len(stream)
52
53 def testClearing(self):
54 stream = diaspy.streams.Stream(test_connection)
55 stream.clear()
56 self.assertEqual(0, len(stream))
57
58 def testPurging(self):
59 stream = diaspy.streams.Stream(test_connection)
60 post = stream.post('#diaspy test')
61 stream.update()
62 post.delete()
63 stream.purge()
64 self.assertNotIn(post.post_id, [p.post_id for p in stream])
65
66 def testPostingText(self):
67 c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
68 c.login()
69 stream = diaspy.streams.Stream(c)
70 post = stream.post('#diaspy test no. {0}'.format(test_count))
71 self.assertEqual(diaspy.models.Post, type(post))
72
73 def testPostingImage(self):
74 c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
75 c.login()
76 stream = diaspy.streams.Stream(c)
77 stream.post_picture('./test-image.png')
78
79
80 class ConnectionTest(unittest.TestCase):
81 def testLoginWithoutUsername(self):
82 connection = diaspy.connection.Connection(pod=__pod__)
83 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
84
85 def testLoginWithoutPassword(self):
86 connection = diaspy.connection.Connection(pod=__pod__)
87 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
88
89 def testGettingUserInfo(self):
90 connection = diaspy.connection.Connection(__pod__, __username__, __passwd__)
91 connection.login()
92 info = connection.getUserInfo()
93 self.assertEqual(dict, type(info))
94
95
96 class ClientTests(unittest.TestCase):
97 def testGettingStream(self):
98 client = diaspy.client.Client(__pod__, __username__, __passwd__)
99 stream = client.get_stream()
100 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
101
102 def testGettingNotifications(self):
103 client = diaspy.client.Client(__pod__, __username__, __passwd__)
104 notifications = client.get_notifications()
105 self.assertEqual(list, type(notifications))
106 if notifications: self.assertEqual(dict, type(notifications[0]))
107
108 def testGettingTagAsList(self):
109 client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
110 tag = client.get_tag('foo')
111 self.assertEqual(list, type(tag))
112 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
113
114 def testGettingTagAsStream(self):
115 client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
116 tag = client.get_tag('foo', stream=True)
117 self.assertEqual(diaspy.streams.Generic, type(tag))
118 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
119
120 def testGettingMailbox(self):
121 client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
122 mailbox = client.get_mailbox()
123 self.assertEqual(list, type(mailbox))
124 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
125
126
127 if __name__ == '__main__':
128 unittest.main()
129 c = diaspy.connection.Connection(__pod__, __username__, __passwd__)
130 c.login()
131 stream = diaspy.modules.Stream(c)
132 for post in stream:
133 if post['text'] == '#diaspy test': post.delete()