Initial refactoring, test for initialization of client
authorMarek Marecki <triviuss@gmail.com>
Mon, 18 Mar 2013 20:34:07 +0000 (21:34 +0100)
committerMarek Marecki <triviuss@gmail.com>
Mon, 18 Mar 2013 20:34:07 +0000 (21:34 +0100)
Makefile [new file with mode: 0644]
diaspy/__init__.py
diaspy/client.py
manual/connecting_to_pod.mdown [new file with mode: 0644]
tests.py [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..f8876b4
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,4 @@
+.PHONY: tests
+
+tests:
+       python3 -m unittest --verbose --catch --failfast tests.py
index fd6b6ab8c02189914fc576739b021e5f0ac4d379..e6486b2ac453f858faa7c031d4e762f49286267d 100644 (file)
@@ -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
index f3d0c5f5f6e05e11fb3f2b6cf554d5cf845f2dc8..e47d0e5c5e99818cb36cd55102ef18dd0e3b0a1a 100644 (file)
@@ -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 (file)
index 0000000..f338094
--- /dev/null
@@ -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 (file)
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()