-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
494 additions
and
407 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
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
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
django/applications/catmaid/static/libs/streamsaver/polyfill.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from abc import ABC | ||
|
||
from django.db import connection | ||
from django.test import TestCase | ||
from django.test import TestCase, TransactionTestCase | ||
from django.test.client import Client | ||
from catmaid.apps import get_system_user | ||
from catmaid.models import Project | ||
|
@@ -84,6 +84,32 @@ def fake_authentication(self): | |
self.client.login(username='temporary', password='temporary') | ||
|
||
|
||
class CatmaidTransactionTestCase(TransactionTestCase, AssertStatusMixin): | ||
fixtures = ['catmaid_testdata'] | ||
|
||
maxDiff = None | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
init_consistent_data() | ||
# Set up data for the whole TestCase | ||
cls.test_project_id = 3 | ||
cls.user = User.objects.create_user('temporary', | ||
'[email protected]', 'temporary') | ||
|
||
# Add admin user and test2 users to the test1 group | ||
g = Group.objects.get(name='test1') | ||
g.user_set.add(User.objects.get(username='admin')) | ||
g.user_set.add(User.objects.get(username='test2')) | ||
g.save() | ||
|
||
def setUp(self): | ||
self.client = Client() | ||
|
||
def fake_authentication(self): | ||
self.client.login(username='temporary', password='temporary') | ||
|
||
|
||
def round_list(l, digits=6): | ||
"""Round floating point values in a list recursively to the specified number | ||
of digits. | ||
|
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,56 @@ | ||
import pytest | ||
from django.test import AsyncClient, TestCase, override_settings | ||
from channels.testing import WebsocketCommunicator | ||
from channels.auth import get_user, login, logout | ||
from channels.db import database_sync_to_async | ||
from catmaid.consumers import UpdateConsumer | ||
from catmaid.models import User | ||
from .common import CatmaidTestCase, CatmaidTransactionTestCase | ||
import functools | ||
from asgiref.sync import sync_to_async | ||
|
||
|
||
@database_sync_to_async | ||
def get_catmaid_user(username): | ||
try: | ||
return User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
return User.objects.get(username='AnonymousUser') | ||
|
||
|
||
class AuthWebsocketCommunicator(WebsocketCommunicator): | ||
def __init__(self, application, path, user, *args, **kwargs): | ||
super().__init__(self._asgi_with_user(application, user), path, | ||
*args, **kwargs) | ||
|
||
@classmethod | ||
def _asgi_with_user(cls, asgi_app, user): | ||
""" | ||
Update the scope of an ASGI app such that a particular user | ||
is already assumed to have been authenticated. | ||
""" | ||
async def app(scope, receive, send): | ||
scope['user'] = user | ||
return await asgi_app(scope, receive, send) | ||
functools.update_wrapper(app, asgi_app) | ||
return app | ||
|
||
|
||
@override_settings(CHANNEL_LAYERS={"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}) | ||
class TestWebsockets(CatmaidTransactionTestCase): | ||
|
||
async def test_basic_connection(self): | ||
client = AsyncClient() | ||
user = await get_catmaid_user('temporary') | ||
|
||
headers = [(b'origin', b'http://localhost:80')] # Bypass AllowedHostsOriginValidator | ||
communicator = AuthWebsocketCommunicator(UpdateConsumer.as_asgi(), | ||
'/channels/updates', user, | ||
headers=headers) | ||
|
||
#communicator = WebsocketCommunicator(UpdateConsumer.as_asgi(), "GET", "/channels/updates") | ||
#communicator.instance.scope["user"] = await get_user(username='temporary') | ||
connected, subprotocol = await communicator.connect() | ||
assert connected | ||
|
||
await communicator.disconnect() |
Oops, something went wrong.