--- /dev/null
+.PHONY: tests
+
+tests:
+ python3 -m unittest --verbose --catch --failfast tests.py
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
"""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.
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
: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,
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
--- /dev/null
+#### 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
--- /dev/null
+#!/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()