self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
self.pod = pod
self.session = requests.Session()
+ self._post_data = {}
self._setlogindata(username, password)
def get_token(self):
headers={'accept': 'application/json'})
if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
-
+
+ def _set_post_data(self, text, aspect_id, photos):
+ """This method prepares data for post.
+
+ :param text: Text to post.
+ :type text: str
+ :param aspect_id: Aspect id to send post to.
+ :type aspect_id: str
+ """
+ data = {'aspect_ids': aspect_id,
+ 'status_message': {'text': text}}
+ if photos: data['photos'] = photos
+ self._post_data = data
+
+ def _send_post(self):
+ """This method sends a post to an aspect.
+ It uses `self._post_data`.
+
+ .. note::
+ _set_post_data() must be called before.
+ """
+
+
def post(self, text, aspect_id='public', photos=None):
"""This function sends a post to an aspect
:returns: diaspy.models.Post -- the Post which has been created
"""
- data = {'aspect_ids': aspect_id,
- 'status_message': {'text': text}}
-
- if photos:
- data['photos'] = photos
+ self._set_post_data(text, aspect_id, photos)
r = self.session.post('{0}/status_messages'.format(self.pod),
- data=json.dumps(data),
+ data=json.dumps(self._post_data),
headers={'content-type': 'application/json',
'accept': 'application/json',
'x-csrf-token': self.get_token()})
#!/usr/bin/env python3
import unittest
+import getpass
-# failing to import any of the modules below indicates failed tests
+# failure to import any of the modules below indicates failed tests
# modules used by diaspy
import requests, re
# actual diaspy code
#### test suite configuration variables: can be adjusted to your liking
# pod used by tests (has to be valid)
-__pod__ = "http://pod.orkz.net"
+__pod__ = 'http://pod.orkz.net'
+__username__ = 'testuser'
+__passwd__ = 'testpassword'
class ClientTests(unittest.TestCase):
- def testInit(self):
+ def testInitialization(self):
+ """This test checks initialization of Client() instance.
"""
- This test checks initialization of Client() instance.
- """
- client = diaspy.client.Client(pod=__pod__, username='testuser', password='testpassword')
+ client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
self.assertEqual(__pod__, client.pod)
- self.assertEqual('testuser', client._username)
- self.assertEqual('testpassword', client._password)
+ self.assertEqual(__username__, client._username)
+ self.assertEqual(__passwd__, client._password)
+ self.assertEqual(None, client._post_data)
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.
+ """
+
+
-if __name__ == "__main__": unittest.main()
+if __name__ == '__main__':
+ __passwd__ = getpass.getpass(prompt='Password used for testing: ')
+ if __passwd__ == '': __passwd__ = 'testpassword'
+ unittest.main()