-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes GH-33
- Loading branch information
Showing
7 changed files
with
272 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[couch_httpd_auth] | ||
secret = bd42ab447cdaecb52f2b2dc3bda6ec10 | ||
|
||
[httpd] | ||
port = 8922 | ||
|
||
[admins] | ||
admin = -hashed-609ab15a7189304d14390b48876180f498a38008,35cee0c36d7a4bd5f1ba460eda70454f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# | ||
# Copyright (c) 2011 Daniel Truemper truemped@googlemail.com | ||
# | ||
# test_session.py 13-Oct-2011 | ||
# | ||
# Permission is hereby granted, free of charge, to any person | ||
# obtaining a copy of this software and associated documentation | ||
# files (the "Software"), to deal in the Software without | ||
# restriction, including without limitation the rights to use, copy, | ||
# modify, merge, publish, distribute, sublicense, and/or sell copies | ||
# of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | ||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | ||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
|
||
from __future__ import with_statement | ||
|
||
from nose.tools import eq_ as eq | ||
|
||
from ..couch_util import setup_with_admin as setup, teardown, with_couchdb | ||
from ..util import with_ioloop | ||
|
||
try: | ||
import json | ||
except ImportError: | ||
import simplejson as json | ||
|
||
try: | ||
# Python 3 | ||
from urllib.request import urlopen | ||
from urllib.error import HTTPError | ||
except ImportError: | ||
# Python 2 | ||
from urllib2 import urlopen | ||
from urllib2 import HTTPError | ||
|
||
import trombi | ||
import trombi.errors | ||
|
||
|
||
@with_ioloop | ||
@with_couchdb | ||
def test_session_api_with_wrong_credentials(baseurl, ioloop): | ||
s = trombi.Server(baseurl, io_loop=ioloop) | ||
|
||
def session_callback(response): | ||
assert response.error | ||
eq(response.msg, 'Name or password is incorrect.') | ||
ioloop.stop() | ||
|
||
s.login(username="daniel", password="daniel", callback=session_callback) | ||
ioloop.start() | ||
|
||
|
||
@with_ioloop | ||
@with_couchdb | ||
def test_session_with_user(baseurl, ioloop): | ||
s = trombi.Server(baseurl, io_loop=ioloop) | ||
result = {} | ||
|
||
def session_callback(session_info): | ||
result['session_info'] = session_info | ||
ioloop.stop() | ||
|
||
def add_user_callback(response): | ||
assert not response.error | ||
ioloop.stop() | ||
|
||
# add a user | ||
s.add_user('testuser', 'testpassword', add_user_callback) | ||
ioloop.start() | ||
|
||
# login | ||
s.login(username="testuser", password="testpassword", | ||
callback=session_callback) | ||
ioloop.start() | ||
|
||
# check for the cookie and user info | ||
eq(result['session_info'].content, {u'ok': True, u'name': u'testuser', | ||
u'roles': []}) | ||
assert s.session_cookie.startswith('AuthSession') | ||
|
||
# get the session info | ||
s.session(session_callback) | ||
ioloop.start() | ||
|
||
# check that no cookie has been sent and the session info is correct | ||
eq(result['session_info'].content, | ||
{u'info': {u'authentication_handlers': | ||
[u'oauth', u'cookie', u'default'], u'authentication_db': | ||
u'_users'}, u'userCtx': {u'name': None, u'roles': []}, | ||
u'ok': | ||
True}) | ||
|
||
# check that logout is working | ||
s.logout(session_callback) | ||
ioloop.start() | ||
|
||
assert not s.session_cookie | ||
eq(result['session_info'].content, {u'ok': True}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import re | ||
import collections | ||
import tornado.ioloop | ||
import urllib | ||
|
||
try: | ||
# Python 3 | ||
|
@@ -145,6 +146,7 @@ class Server(TrombiObject): | |
def __init__(self, baseurl, fetch_args=None, io_loop=None, | ||
json_encoder=None, **client_args): | ||
self.error = False | ||
self.session_cookie = None | ||
self.baseurl = baseurl | ||
if self.baseurl[-1] == '/': | ||
self.baseurl = self.baseurl[:-1] | ||
|
@@ -177,6 +179,14 @@ def _fetch(self, *args, **kwargs): | |
} | ||
fetch_args.update(self._fetch_args) | ||
fetch_args.update(kwargs) | ||
|
||
if self.session_cookie: | ||
fetch_args['X-CouchDB-WWW-Authenticate': 'Cookie'] | ||
if 'Cookie' in fetch_args: | ||
fetch_args['Cookie'] += '; %s' % self.session_cookie | ||
else: | ||
fetch_args['Cookie'] = self.sesison_cookie | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
truemped
Contributor
|
||
|
||
self._client.fetch(*args, **fetch_args) | ||
|
||
def create(self, name, callback): | ||
|
@@ -304,6 +314,42 @@ def delete_user(self, user_doc, callback): | |
userdb = Database(self, '_users') | ||
userdb.delete(user_doc, callback) | ||
|
||
def logout(self, callback): | ||
def _really_callback(response): | ||
if response.code == 200: | ||
self.session_cookie = None | ||
callback(TrombiResult(json.loads(response.body))) | ||
else: | ||
callback(_error_response(response)) | ||
|
||
url = '%s/%s' % (self.baseurl, '_session') | ||
self._client.fetch(url, _really_callback, method='DELETE') | ||
|
||
def login(self, username, password, callback): | ||
def _really_callback(response): | ||
if response.code in (200, 302): | ||
self.session_cookie = response.headers['Set-Cookie'] | ||
response_body = json.loads(response.body) | ||
callback(TrombiResult(response_body)) | ||
else: | ||
callback(_error_response(response)) | ||
|
||
body = urllib.urlencode({'name': username, 'password': password}) | ||
url = '%s/%s' % (self.baseurl, '_session') | ||
|
||
self._client.fetch(url, _really_callback, method='POST', body=body) | ||
|
||
def session(self, callback): | ||
def _really_callback(response): | ||
if response.code == 200: | ||
body = json.loads(response.body) | ||
callback(TrombiResult(body)) | ||
else: | ||
callback(_error_response(response)) | ||
|
||
url = '%s/%s' % (self.baseurl, '_session') | ||
self._client.fetch(url, _really_callback) | ||
|
||
|
||
class Database(TrombiObject): | ||
def __init__(self, server, name): | ||
|
1 comment
on commit 2736ec0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh snap. Yeah, that's definitely a typo. I blame the nightly hours I used to hack this together. Will fix ASAP!
self.sesison_cookie
looks like a typo :)