Merge branch 'master' into session-refactoring
authorMarek Marecki <triviuss@gmail.com>
Sat, 30 Mar 2013 13:06:13 +0000 (14:06 +0100)
committerMarek Marecki <triviuss@gmail.com>
Sat, 30 Mar 2013 13:06:13 +0000 (14:06 +0100)
Conflicts:
diaspy/client.py
diaspy/conversations.py
diaspy/models.py
tests.py

1  2 
diaspy/client.py
diaspy/conversations.py
diaspy/models.py
tests.py

index d88ae61f2e2d4dc1d144d118c445fb09204628fd,b558c4da2c7d41998cccb3a7d0d8c6b042f53c7f..2af284b6ce71fc33a98a18748155d468550023b5
@@@ -83,19 -53,23 +82,22 @@@ class Client
          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()
Simple merge
index bccf351b823c4239fff18c9a55d1d9fb570ef976,95e2043040d56d75a17923f617552702ca95e783..34a675b9bde1e1d5dcc3f88415611e4324eda606
@@@ -7,7 -4,7 +7,6 @@@ class Post
      .. 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))
diff --cc tests.py
index 039cbfcc31e84c8f8e91618f861449ec2b207d7d,cc48bcc3faa2795ed1265c635117d280a00f96e8..0415837943ae45ba5265ffeb7c8dd71e6cb5226c
+++ b/tests.py
@@@ -19,43 -20,29 +19,47 @@@ __passwd__ = testconf.__passwd_
  
  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()