From 94c2d63720f20491e87b1ad4b77f464b46f46023 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Mon, 18 Mar 2013 21:34:07 +0100 Subject: [PATCH] Initial refactoring, test for initialization of client --- Makefile | 4 ++++ diaspy/__init__.py | 7 ++++++- diaspy/client.py | 13 +++++-------- manual/connecting_to_pod.mdown | 12 ++++++++++++ tests.py | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 Makefile create mode 100644 manual/connecting_to_pod.mdown create mode 100644 tests.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f8876b4 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: tests + +tests: + python3 -m unittest --verbose --catch --failfast tests.py diff --git a/diaspy/__init__.py b/diaspy/__init__.py index fd6b6ab..e6486b2 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -1,3 +1,8 @@ import diaspy.client import diaspy.models -import diaspy.conversations \ No newline at end of file +import diaspy.conversations + +class Client(diaspy.client.Client): + """Wrapper class for easier imports. + """ + pass diff --git a/diaspy/client.py b/diaspy/client.py index f3d0c5f..e47d0e5 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -8,7 +8,6 @@ class Client: """This is the client class to connect to diaspora. """ - def __init__(self, pod, username, password): """ :param pod: The complete url of the diaspora pod to use. @@ -22,8 +21,8 @@ class Client: self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') self.pod = pod self.session = requests.Session() - self._setlogindata(self, username, password) - self._login() + self._setlogindata(username, password) + # self._login() def get_token(self): """This function gets a token needed for authentication in most cases @@ -31,21 +30,19 @@ class Client: :returns: string -- token used to authenticate """ - r = self.session.get(self.pod + '/stream') token = self._token_regex.search(r.text).group(1) return token def _setlogindata(self, username, password): """This function is used to set data for login. + .. note:: It should be called before _login() function. """ - self._username = username - self._password = password #r = self.session.get(self.pod + '/users/sign_in') #token = self._token_regex.search(r.text).group(1) - + self._username, self._password = username, password self._login_data = { 'user[username]': self._username, 'user[password]': self._password, @@ -63,7 +60,7 @@ class Client: headers={'accept': 'application/json'}) if r.status_code != 201: raise Exception(str(r.status_code) + ': Login failed.') - + def post(self, text, aspect_id='public', photos=None): """This function sends a post to an aspect diff --git a/manual/connecting_to_pod.mdown b/manual/connecting_to_pod.mdown new file mode 100644 index 0000000..f338094 --- /dev/null +++ b/manual/connecting_to_pod.mdown @@ -0,0 +1,12 @@ +#### Connecting to D* pod via `diaspy` +###### [Index](index.mdown) +---- + +First thing you have to do is to create new instace of `Client()`. +Then, if no errors are raised, you can `_login()` to the pod with given username and password. + + client = diaspy.Client(pod="http://pod.example.com/", username="foo", password="bar") + client._login() + + +If everything worked you are now connected to D* pod at given URL. \ No newline at end of file diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..e350182 --- /dev/null +++ b/tests.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import unittest + +# failing to import any of the modules below indicates failed tests +# modules used by diaspy +import requests, re +# actual diaspy code +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/" + + +class ClientTests(unittest.TestCase): + def testInit(self): + """ + This test checks correct initialization of Client() instance. + + .. note:: + This does not includes setting login data. + """ + client = diaspy.Client(pod=__pod__, username='testuser', password='testpassword') + self.assertEqual(__pod__, client.pod) + self.assertEqual('testuser', client._username) + self.assertEqual('testpassword', client._password) + 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()) + + +if __name__ == "__main__": unittest.main() -- 2.25.1