r = self.session.post('{0}/users/sign_in'.format(self.pod),
data=self._login_data,
headers={'accept': 'application/json'})
- if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
-
+ if r.status_code != 201:
+ raise Exception('{0}: Login failed.'.format(r.status_code))
- def _setpostdata(self, text, aspect_id, photos):
+ def _setpostdata(self, text, aspect_ids, photos):
"""This function prepares data for posting.
+
:param text: Text to post.
:type text: str
- :param aspect_id: Aspect id to send post to.
- :type aspect_id: str
+ :param aspect_ids: Aspect ids to send post to.
+ :type aspect_ids: str
"""
data = {}
- data['aspect_id'] = aspect_id
+ data['aspect_ids'] = aspect_ids
data['status_message'] = {'text': text}
- if photos: data['photos'] = photos
+ if photos:
+ data['photos'] = photos
self._post_data = data
def _post(self):
if r.status_code != 404:
raise Exception('wrong status code: {0}'.format(r.status_code))
+ def get_mailbox(self):
+ """This functions returns a list of messages found in the conversation.
+
+ :returns: list -- list of Conversation objects.
+
+ """
+
+ r = self.session.get('{0}/conversations.json'.format(self.pod))
+
+ if r.status_code != 200:
+ raise Exception('wrong status code: {0}'.format(r.status_code))
+
+ mailbox = r.json()
+ conversations = [diaspy.conversations.Conversation(
+ str(conversation['conversation']['id']), self) for
+ conversation in mailbox]
+
+ return conversations
+
def new_conversation(self, contacts, subject, text):
- """ start a new conversation
+ """Start a new conversation.
:param contacts: recipients ids, no guids, comma sperated.
:type contacts: str
data=data,
headers={'accept': 'application/json'})
if r.status_code != 200:
- raise Exception('{0}: Conversation could not be started.'.format(r.status_code))
-
+ raise Exception('{0}: Conversation could not be started.'
+ .format(r.status_code))
-
return r.json()
.. note::
Remember that you need to have access to the post.
Remember that you also need to be logged in.
--
"""
def __init__(self, post_id, client):
"""
"""
data = {'authenticity_token': self._client.get_token()}
- if r.status_code != 204: raise Exception('{0}: Post could not be deleted'.format(r.status_code))
+ r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
+ data=data,
+ headers={'accept': 'application/json'})
++ if r.status_code != 204:
++ raise Exception('{0}: Post could not be deleted'.format(r.status_code))
+
+ r = self._client.session.delete('{0}/posts/{1}'
+ .format(self._client.pod,
+ self.post_id),
+ data=data,
+ headers={'accept': 'application/json'})
+ if r.status_code != 204:
+ raise Exception('{0}: Post could not be deleted.'
+ .format(r.status_code))
class ClientTests(unittest.TestCase):
def testInitialization(self):
- client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
- """This test checks initialization of Client() instance.
- """
+ client = diaspy.client.Client(pod=__pod__,
+ username=__username__,
+ password=__passwd__)
self.assertEqual(__pod__, client.pod)
self.assertEqual(__username__, client._username)
self.assertEqual(__passwd__, client._password)
- self.assertEqual(None, client._post_data)
+ self.assertEqual({}, client._post_data)
- self.assertEqual(client._token_regex, re.compile(r'content="(.*?)"\s+name="csrf-token'))
- self.assertEqual(client._login_data['user[username]'], __username__)
- self.assertEqual(client._login_data['user[password]'], __passwd__)
- self.assertEqual(client._login_data['authenticity_token'], client.get_token())
+ self.assertEqual(client._token_regex,
+ re.compile(r'content="(.*?)"\s+name="csrf-token'))
+ self.assertEqual(client._login_data['user[username]'], 'testuser')
+ self.assertEqual(client._login_data['user[password]'], 'testpassword')
+ self.assertEqual(client._login_data['authenticity_token'],
+ client.get_token())
- def testPreparationOfPostData(self):
- """This test checks correctness of data set for posting.
- """
+ def testGettingUserInfo(self):
+ client = diaspy.client.Client(__pod__, __username__, __passwd__)
+ info = client.get_user_info()
+ self.assertEqual(dict, type(info))
+ def testGettingStream(self):
+ client = diaspy.client.Client(__pod__, __username__, __passwd__)
+ stream = client.get_stream()
+ self.assertEqual(list, type(stream))
+ if stream: self.assertEqual(diaspy.models.Post, type(stream[0]))
-if __name__ == '__main__':
- __passwd__ = getpass.getpass(prompt='Password used for testing: ')
- if __passwd__ == '':
- __passwd__ = 'testpassword'
- unittest.main()
+ def testGettingNotifications(self):
+ client = diaspy.client.Client(__pod__, __username__, __passwd__)
+ notifications = client.get_notifications()
+ self.assertEqual(list, type(notifications))
+ if notifications: self.assertEqual(dict, type(notifications[0]))
+
+ def testGettingTag(self):
+ client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
+ tag = client.get_tag('foo')
+ self.assertEqual(list, type(tag))
+ if tag: self.assertEqual(diaspy.models.Post, type(tag[0]))
+
+ def testGettingMailbox(self):
+ client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
+ mailbox = client.get_mailbox()
+ self.assertEqual(list, type(mailbox))
+ self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0]))
+
+if __name__ == '__main__': unittest.main()