Skip to content

Commit

Permalink
Merge pull request #97 from aronysidoro/try_example
Browse files Browse the repository at this point in the history
update example project
  • Loading branch information
jrief committed Jun 14, 2015
2 parents 6faf0d7 + 213d07a commit 1af0d5d
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.egg-info
*.coverage
sqlite.db
db.sqlite3
*~
.tmp*
build
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=========
CHANGELOG
=========

**2015-06-11**

- requirements
- created 1 requirements file under ``examples/chatserver/requirements.txt``

- tests
- renamed chatclient.py to test_chatclient.py - for django-nose testrunner

- django 1.7
- migrated example project to django 1.7

- docs
- edited ``docs/testing.rst`` to show new changes for using example project
3 changes: 3 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
* Use >100 greenlets when running the unit test. This shall take no longer than 1 second.

* Add prototype methods such as select() to base class, which raise a NotImplementedError.

* Fix broken tests with examples/chatserver
- 7/12 tests passing
26 changes: 19 additions & 7 deletions docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@ the SQLite database

.. code-block:: bash
./manage.py syncdb
Creating tables ...
...
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'johndoe'):
...
# create python2 virtualenv
virtualenv - p /path/to/python2 /path/to/virtualenv
# activate virtualenv
source /path/to/virtualenv/bin/activate
# pip
pip install /django-websocket/redis/examples/chatserver/requirements.txt
# Django 1.7+
# Load test data
./manage.py loaddata chatserver/fixtures/data.json # if needed
./manage.py makemigrations
./manage.py migrate
and then start the server

.. code-block:: bash
# start Redis Server
# (or follow quickstart instructions http://redis.io/topics/quickstart)
# start Django
./manage.py runserver
Point a browser onto http://localhost:8000/admin/, login and add additional users. Enable their
staff status, so that they can use the admin interface to log into the testing application.

With http://localhost:8000/userchat/ you can send messages to specific users, provided they are
With http://localhost:8000/chat/ you can send messages to specific users, provided they are
logged in. To log in as another user, use Django's admin interface.

Simple Broadcasting
Expand Down
29 changes: 25 additions & 4 deletions examples/chatserver/settings.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# Django settings for unit test project.
import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'sqlite.db',
},
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

SITE_ID = 1
Expand Down Expand Up @@ -49,13 +55,25 @@
'django.template.loaders.app_directories.Loader',
)

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Europe/Berlin'

USE_I18N = True

USE_L10N = True

USE_TZ = True

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.messages',
'django.contrib.staticfiles',
'ws4redis',
'chatserver',
Expand All @@ -64,10 +82,13 @@
# These two middleware classes must be present, if messages sent or received through a websocket
# connection shall be delivered to an authenticated Django user.
MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

# This setting is required to override the Django's main loop, when running in
Expand Down
2 changes: 1 addition & 1 deletion examples/chatserver/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -*- coding: utf-8 -*-
from .chatclient import *
from .test_chatclient import *
16 changes: 12 additions & 4 deletions examples/chatserver/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
six
nose
django-nose
Django==1.7.8
backports.ssl-match-hostname==3.4.0.2
django-nose==1.4
django-redis-sessions==0.4.0
django-websocket-redis==0.4.4
gevent==1.0.2
greenlet==0.4.7
nose==1.3.7
redis==2.10.3
requests==2.7.0
six==1.9.0
websocket-client==0.13.0
requests
wsgiref==0.1.2
19 changes: 18 additions & 1 deletion examples/chatserver/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,31 @@
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

SESSION_ENGINE = 'redis_sessions.session'

SESSION_REDIS_PREFIX = 'session'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.Loader',
)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'


TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.static',
'django.core.context_processors.request',
'ws4redis.context_processors.default',
)

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_nose',
'ws4redis',
Expand All @@ -54,8 +67,12 @@
# connection shall be delivered to an authenticated Django user.
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

# This setting is required to override the Django's main loop, when running in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-
import os
import time
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.sessions.backends.db import SessionStore
from django.test import LiveServerTestCase
from django.test import LiveServerTestCase, TestCase
from django.test.client import RequestFactory
from django.utils.importlib import import_module
from websocket import create_connection, WebSocketException
from ws4redis.django_runserver import application
from ws4redis.publisher import RedisPublisher
Expand All @@ -16,6 +19,7 @@ class WebsocketTests(LiveServerTestCase):

@classmethod
def setUpClass(cls):
os.environ.update(DJANGO_LIVE_TEST_SERVER_ADDRESS="localhost:8000-8010,8080,9200-9300")
super(WebsocketTests, cls).setUpClass()
cls.server_thread.httpd.set_app(application)

Expand All @@ -24,6 +28,14 @@ def setUp(self):
self.websocket_base_url = self.live_server_url.replace('http:', 'ws:', 1) + u'/ws/' + self.facility
self.message = RedisMessage(''.join(unichr(c) for c in range(33, 128)))
self.factory = RequestFactory()
# SessionStore
# as used here: http://stackoverflow.com/a/7722483/1913888
settings.SESSION_ENGINE = 'redis_sessions.session'
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save()
self.session = store
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -97,7 +109,7 @@ def test_publish_user(self):
request.user = User.objects.get(username='john')
result = publisher.fetch_message(request, self.facility, 'user')
self.assertEqual(result, self.message)
request.user = User.objects.get(username='mary')
request.user = None
result = publisher.fetch_message(request, self.facility, 'user')
self.assertEqual(result, None)

Expand Down Expand Up @@ -140,7 +152,7 @@ def test_publish_group(self):
def test_subscribe_session(self):
logged_in = self.client.login(username='john', password='secret')
self.assertTrue(logged_in, 'John is not logged in')
self.assertIsInstance(self.client.session, (dict, SessionStore), 'Did not receive a session key')
self.assertIsInstance(self.client.session, (dict, type(self.session)), 'Did not receive a session key')
session_key = self.client.session.session_key
self.assertGreater(len(session_key), 30, 'Session key is too short')
request = self.factory.get('/chat/')
Expand All @@ -160,7 +172,7 @@ def test_subscribe_session(self):
def test_publish_session(self):
logged_in = self.client.login(username='mary', password='secret')
self.assertTrue(logged_in, 'Mary is not logged in')
self.assertIsInstance(self.client.session, (dict, SessionStore), 'Did not receive a session key')
self.assertIsInstance(self.client.session, (dict, type(self.session)), 'Did not receive a session key')
session_key = self.client.session.session_key
self.assertGreater(len(session_key), 30, 'Session key is too short')
websocket_url = self.websocket_base_url + u'?publish-session'
Expand Down
13 changes: 13 additions & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Django==1.7.8
backports.ssl-match-hostname==3.4.0.2
django-nose==1.4
django-redis-sessions==0.4.0
django-websocket-redis==0.4.4
gevent==1.0.2
greenlet==0.4.7
nose==1.3.7
redis==2.10.3
requests==2.7.0
six==1.9.0
websocket-client==0.13.0
wsgiref==0.1.2

0 comments on commit 1af0d5d

Please sign in to comment.