Merge branch 'users'
[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
94c2d637 6# modules used by diaspy
88ec6cda
MM
7import requests
8import re
94c2d637
MM
9# actual diaspy code
10import diaspy
11
12
62f1912f 13#### SETUP STUFF
94c2d637 14#### test suite configuration variables: can be adjusted to your liking
fab8b736
MM
15import testconf
16__pod__ = testconf.__pod__
17__username__ = testconf.__username__
18__passwd__ = testconf.__passwd__
94c2d637 19
505fc964 20
62f1912f
MM
21# Test counter
22try:
23 test_count_file = open('TEST_COUNT', 'r')
24 test_count = int(test_count_file.read())
25 test_count_file.close()
26except (IOError, ValueError):
27 test_count = 0
28finally:
29 test_count += 1
30test_count_file = open('TEST_COUNT', 'w')
31test_count_file.write(str(test_count))
32test_count_file.close()
33print('Running test no. {0}'.format(test_count))
94c2d637 34
c1490af7 35print('Running tests on connection to pod: "{0}"\t'.format(__pod__), end='')
62f1912f
MM
36test_connection = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__)
37test_connection.login()
c1490af7 38print('[ CONNECTED ]\n')
62f1912f 39
66c3bb76
MM
40post_text = '#diaspy test no. {0}'.format(test_count)
41
62f1912f 42
a8fdc14a
MM
43#######################################
44#### TEST SUITE CODE ####
45#######################################
385e7ebe
MM
46class ConnectionTest(unittest.TestCase):
47 def testLoginWithoutUsername(self):
48 connection = diaspy.connection.Connection(pod=__pod__)
49 self.assertRaises(diaspy.connection.LoginError, connection.login, password='foo')
50
51 def testLoginWithoutPassword(self):
52 connection = diaspy.connection.Connection(pod=__pod__)
53 self.assertRaises(diaspy.connection.LoginError, connection.login, username='user')
54
264336e2 55 def testGettingUserInfo(self):
c1490af7 56 info = test_connection.getUserInfo()
264336e2
MM
57 self.assertEqual(dict, type(info))
58
7a31b4aa
MM
59
60class ClientTests(unittest.TestCase):
264336e2 61 def testGettingStream(self):
c1490af7 62 client = diaspy.client.Client(test_connection)
264336e2 63 stream = client.get_stream()
7a31b4aa 64 if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0]))
264336e2
MM
65
66 def testGettingNotifications(self):
c1490af7 67 client = diaspy.client.Client(test_connection)
264336e2
MM
68 notifications = client.get_notifications()
69 self.assertEqual(list, type(notifications))
70 if notifications: self.assertEqual(dict, type(notifications[0]))
71
66c3bb76 72 def testGettingTag(self):
c1490af7 73 client = diaspy.client.Client(test_connection)
fab8b736 74 tag = client.get_tag('foo')
62f1912f
MM
75 self.assertEqual(diaspy.streams.Generic, type(tag))
76 if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
77
fab8b736 78 def testGettingMailbox(self):
c1490af7 79 client = diaspy.client.Client(test_connection)
fab8b736
MM
80 mailbox = client.get_mailbox()
81 self.assertEqual(list, type(mailbox))
82 self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
83
f2eaa3c7 84
f3eaa601
MM
85class StreamTest(unittest.TestCase):
86 def testGetting(self):
87 stream = diaspy.streams.Generic(test_connection)
88
89 def testGettingLength(self):
90 stream = diaspy.streams.Generic(test_connection)
91 len(stream)
92
93 def testClearing(self):
94 stream = diaspy.streams.Stream(test_connection)
95 stream.clear()
96 self.assertEqual(0, len(stream))
97
98 def testPurging(self):
99 stream = diaspy.streams.Stream(test_connection)
100 post = stream.post('#diaspy test')
101 stream.update()
102 post.delete()
103 stream.purge()
104 self.assertNotIn(post.post_id, [p.post_id for p in stream])
105
106 def testPostingText(self):
107 stream = diaspy.streams.Stream(test_connection)
a98c6792 108 post = stream.post(post_text)
f3eaa601
MM
109 self.assertEqual(diaspy.models.Post, type(post))
110
111 def testPostingImage(self):
112 stream = diaspy.streams.Stream(test_connection)
a98c6792 113 stream.post(text=post_text, photo='test-image.png')
f3eaa601
MM
114
115 def testingAddingTag(self):
116 ft = diaspy.streams.FollowedTags(test_connection)
117 ft.add('test')
118
27a28aaf
MM
119 def testAspectsAdd(self):
120 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
121 aspects.add(testconf.test_aspect_name_fake)
122 testconf.test_aspect_id = aspects.add(testconf.test_aspect_name)
27a28aaf 123
63cc182d
MM
124 def testAspectsGettingID(self):
125 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
126 id = aspects.getAspectID(testconf.test_aspect_name)
127 self.assertEqual(testconf.test_aspect_id, id)
63cc182d 128
1467ec15 129 def testAspectsRemoveById(self):
27a28aaf 130 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
131 aspects.remove(testconf.test_aspect_id)
132 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
27a28aaf 133
1467ec15
MM
134 def testAspectsRemoveByName(self):
135 aspects = diaspy.streams.Aspects(test_connection)
278febce
MM
136 aspects.remove(name=testconf.test_aspect_name_fake)
137 self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
1467ec15 138
27a28aaf
MM
139 def testActivity(self):
140 activity = diaspy.streams.Activity(test_connection)
141
142 def testMentionsStream(self):
143 mentions = diaspy.streams.Mentions(test_connection)
144
f3eaa601
MM
145
146class UserTests(unittest.TestCase):
147 def testHandleSeparatorRaisingExceptions(self):
148 user = diaspy.people.User(test_connection)
149 handles = ['user.pod.example.com',
150 'user@podexamplecom',
151 '@pod.example.com',
152 'use r@pod.example.com',
153 'user0@pod300 example.com',
154 ]
155 for h in handles:
141216df
MM
156 self.assertRaises(Exception, user._sephandle, h)
157
158 def testGettingUserByHandle(self):
159 user = diaspy.people.User(test_connection)
160 user.fetchhandle(testconf.diaspora_id)
161 self.assertEqual(testconf.guid, user['guid'])
a8fdc14a 162 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
141216df 163 self.assertIn('id', user.data)
a8fdc14a 164 self.assertIn('image_urls', user.data)
141216df
MM
165 self.assertEqual(type(user.stream), diaspy.streams.Outer)
166
167 def testGettingUserByGUID(self):
168 user = diaspy.people.User(test_connection)
169 user.fetchguid(testconf.guid)
170 self.assertEqual(testconf.diaspora_id, user['diaspora_id'])
a8fdc14a 171 self.assertEqual(testconf.diaspora_name, user['diaspora_name'])
141216df 172 self.assertIn('id', user.data)
a8fdc14a 173 self.assertIn('image_urls', user.data)
141216df 174 self.assertEqual(type(user.stream), diaspy.streams.Outer)
f3eaa601
MM
175
176
1467ec15
MM
177class PostTests(unittest.TestCase):
178 def testStringConversion(self):
179 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
180
181 def testRepr(self):
182 s = diaspy.streams.Stream(test_connection)
1467ec15
MM
183
184
33a45682 185if __name__ == '__main__': unittest.main()