From 313c9233b77ef40b0dd673060989924d4f96903c Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Mon, 25 Mar 2013 16:58:17 +0100 Subject: [PATCH] This is not a good commit. Broken many things. --- diaspy/client.py | 33 ++++++++++++++++++++++++++------- manual/connecting_to_pod.mdown | 2 +- tests.py | 29 ++++++++++++++++++++--------- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/diaspy/client.py b/diaspy/client.py index 2dc088e..55f22ee 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -21,6 +21,7 @@ class Client: 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): @@ -56,7 +57,29 @@ class Client: 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 @@ -68,13 +91,9 @@ class Client: :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()}) diff --git a/manual/connecting_to_pod.mdown b/manual/connecting_to_pod.mdown index 93267bb..83838c9 100644 --- a/manual/connecting_to_pod.mdown +++ b/manual/connecting_to_pod.mdown @@ -6,7 +6,7 @@ Then, if no errors are raised, you can `_login()` to the pod with given username import diaspy - client = diaspy.Client(pod="http://pod.example.com/", username="foo", password="bar") + client = diaspy.Client(pod="http://pod.example.com", username="foo", password="bar") client._login() diff --git a/tests.py b/tests.py index 8cca369..d64b235 100644 --- a/tests.py +++ b/tests.py @@ -1,8 +1,9 @@ #!/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 @@ -11,22 +12,32 @@ import diaspy #### 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() -- 2.25.1