# Path: wwwhisper_auth/login_token.py
# def generate_login_token(site, site_url, email):
#     """Returns a signed token to login a user with a given email.
# 
#     The token should be emailed to the user to verify that the user
#     indeed owns the email.
# 
#     The token is valid only for the current site (it will be discarded
#     if it is submitted to a different site protected by the same
#     wwwhisper instance).
# 
#     The token allows only for one succesful login.
#     """
#     timestamp = 0
#     user = site.users.find_item_by_email(email)
#     if user is not None and user.last_login is not None:
#         # Successul login changes user.last_login, which invalidates
#         # all tokens generated for the user.
#         timestamp = _datetime_to_timestamp(user.last_login)
#     token_data = {
#         'site': site_url,
#         'email': email,
#         'timestamp': timestamp
#     }
#     return signing.dumps(token_data, salt=site_url, compress=True)
# 
# Path: wwwhisper_auth/tests/utils.py
# class HttpTestCase(TestCase):
#     def setUp(self):
#         self.client = Client()
#         self.sites = SitesCollection()
#         # For each test case, test site must exist, so it can be set
#         # by SetSiteMiddleware
#         self.site = self.sites.create_item(SINGLE_SITE_ID)
#         self.site.aliases.create_item(TEST_SITE)
# 
#     def post(self, url, args):
#         return self.client.post(
#             url, json.dumps(args), 'application/json; charset=UTF-8',
#             HTTP_SITE_URL=TEST_SITE)
# 
#     """ To be used for views that are not contacted via Ajax. """
#     def post_form(self, url, args):
#         return self.client.post(url, args)
# 
#     def get(self, url, **extra_headers):
#         return self.client.get(url, HTTP_SITE_URL=TEST_SITE, **extra_headers)
# 
#     def put(self, url, args=None):
#         if args is None:
#             return self.client.put(url, HTTP_SITE_URL=TEST_SITE)
#         return self.client.put(
#             url, data=json.dumps(args),
#             content_type='application/json;  charset=UTF-8',
#             HTTP_SITE_URL=TEST_SITE)
# 
# 
#     def delete(self, url):
#         return self.client.delete(url, HTTP_SITE_URL=TEST_SITE)
# 
# Path: wwwhisper_auth/tests/utils.py
# TEST_SITE = 'https://foo.example.org:8080'


from django.contrib.auth.models import User
from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from django.contrib.auth.backends import ModelBackend
from django.core import mail
from django.test import override_settings
from wwwhisper_auth.login_token import generate_login_token
from wwwhisper_auth.tests.utils import HttpTestCase
from wwwhisper_auth.tests.utils import TEST_SITE
import json
import urllib

    def test_login_succeeds_if_known_user(self):
        self.site.users.create_item('foo@example.org')
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        params = urllib.urlencode(dict(token=token, next='/foo'))
        response = self.get('/wwwhisper/auth/api/login/?' + params)
        self.assertEqual(302, response.status_code)
        self.assertEqual(TEST_SITE + '/foo', response['Location'])

    def test_login_fails_if_unknown_user(self):
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        response = self.get('/wwwhisper/auth/api/login/?token=' + token,
                            HTTP_ACCEPT='text/plain, text/html')
        self.assertEqual(403, response.status_code)
        self.assertRegexpMatches(response['Content-Type'], 'text/html')
        self.assertRegexpMatches(response.content, '<body')

    def test_tricky_redirection_replaced(self):
        # 'next' argument is not signed, so can be replaced by the
        # user. This is OK as long as all tricky paths are replaced.
        self.site.users.create_item('foo@example.org')
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        params = urllib.urlencode(dict(token=token, next='www.example.com'))
        response = self.get('/wwwhisper/auth/api/login/?' + params)
        self.assertEqual(302, response.status_code)
        # Ignore 'next' argument from the login URL
        self.assertEqual(TEST_SITE + '/', response['Location'])

    def test_successful_login_invalidates_token(self):
        self.site.users.create_item('foo@example.org')

# Path: wwwhisper_auth/login_token.py
# def generate_login_token(site, site_url, email):
#     """Returns a signed token to login a user with a given email.
# 
#     The token should be emailed to the user to verify that the user
#     indeed owns the email.
# 
#     The token is valid only for the current site (it will be discarded
#     if it is submitted to a different site protected by the same
#     wwwhisper instance).
# 
#     The token allows only for one succesful login.
#     """
#     timestamp = 0
#     user = site.users.find_item_by_email(email)
#     if user is not None and user.last_login is not None:
#         # Successul login changes user.last_login, which invalidates
#         # all tokens generated for the user.
#         timestamp = _datetime_to_timestamp(user.last_login)
#     token_data = {
#         'site': site_url,
#         'email': email,
#         'timestamp': timestamp
#     }
#     return signing.dumps(token_data, salt=site_url, compress=True)
# 
# Path: wwwhisper_auth/tests/utils.py
# class HttpTestCase(TestCase):
#     def setUp(self):
#         self.client = Client()
#         self.sites = SitesCollection()
#         # For each test case, test site must exist, so it can be set
#         # by SetSiteMiddleware
#         self.site = self.sites.create_item(SINGLE_SITE_ID)
#         self.site.aliases.create_item(TEST_SITE)
# 
#     def post(self, url, args):
#         return self.client.post(
#             url, json.dumps(args), 'application/json; charset=UTF-8',
#             HTTP_SITE_URL=TEST_SITE)
# 
#     """ To be used for views that are not contacted via Ajax. """
#     def post_form(self, url, args):
#         return self.client.post(url, args)
# 
#     def get(self, url, **extra_headers):
#         return self.client.get(url, HTTP_SITE_URL=TEST_SITE, **extra_headers)
# 
#     def put(self, url, args=None):
#         if args is None:
#             return self.client.put(url, HTTP_SITE_URL=TEST_SITE)
#         return self.client.put(
#             url, data=json.dumps(args),
#             content_type='application/json;  charset=UTF-8',
#             HTTP_SITE_URL=TEST_SITE)
# 
# 
#     def delete(self, url):
#         return self.client.delete(url, HTTP_SITE_URL=TEST_SITE)
# 
# Path: wwwhisper_auth/tests/utils.py
# TEST_SITE = 'https://foo.example.org:8080'


# Path: wwwhisper_auth/tests/tests_views.py
from django.contrib.auth.models import User
from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from django.contrib.auth.backends import ModelBackend
from django.core import mail
from django.test import override_settings
from wwwhisper_auth.login_token import generate_login_token
from wwwhisper_auth.tests.utils import HttpTestCase
from wwwhisper_auth.tests.utils import TEST_SITE
import json
import urllib


    def test_login_succeeds_if_known_user(self):
        self.site.users.create_item('foo@example.org')
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        params = urllib.urlencode(dict(token=token, next='/foo'))
        response = self.get('/wwwhisper/auth/api/login/?' + params)
        self.assertEqual(302, response.status_code)
        self.assertEqual(TEST_SITE + '/foo', response['Location'])

    def test_login_fails_if_unknown_user(self):
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        response = self.get('/wwwhisper/auth/api/login/?token=' + token,
                            HTTP_ACCEPT='text/plain, text/html')
        self.assertEqual(403, response.status_code)
        self.assertRegexpMatches(response['Content-Type'], 'text/html')
        self.assertRegexpMatches(response.content, '<body')

    def test_tricky_redirection_replaced(self):
        # 'next' argument is not signed, so can be replaced by the
        # user. This is OK as long as all tricky paths are replaced.
        self.site.users.create_item('foo@example.org')
        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')
        params = urllib.urlencode(dict(token=token, next='www.example.com'))
        response = self.get('/wwwhisper/auth/api/login/?' + params)
        self.assertEqual(302, response.status_code)
        # Ignore 'next' argument from the login URL
        self.assertEqual(TEST_SITE + '/', response['Location'])

    def test_successful_login_invalidates_token(self):
        self.site.users.create_item('foo@example.org')

        token = generate_login_token(self.site, TEST_SITE, 'foo@example.org')