-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game): game info에서 토너먼트 다음 게임 유저 정보 전송 (#252)
* refactor(game): match 서비스로 이전 * feat(test): 기본 테스트 생성 * feat(game): 토너먼트 next_game 유저 정보 전송 * fix(consumer): consumers 중복 로그아웃 버그 수정 * docs(game): game info 필드 추
- Loading branch information
1 parent
72a39e4
commit 39b0765
Showing
14 changed files
with
174 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import asyncio | ||
import pytest | ||
|
||
from backend.redis import RedisConnection | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def event_loop(): | ||
try: | ||
loop = asyncio.get_running_loop() | ||
except RuntimeError: | ||
loop = asyncio.new_event_loop() | ||
yield loop | ||
loop.close() | ||
|
||
|
||
async def clear_redis(): | ||
redis_conn = await RedisConnection.get_instance() | ||
await redis_conn.redis.flushdb() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.pytest.ini_options] | ||
asyncio_mode = "auto" |
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,2 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = backend.settings |
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 |
---|---|---|
|
@@ -35,4 +35,8 @@ zope.interface==6.1 | |
|
||
channels_redis | ||
aioredis | ||
|
||
pytest | ||
pytest-django | ||
pytest-asyncio | ||
blinker |
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,37 @@ | ||
import json | ||
|
||
import pytest | ||
from channels.testing import WebsocketCommunicator | ||
|
||
from backend import settings | ||
from backend.asgi import application | ||
from conftest import clear_redis | ||
from game.message_type import GameMessageType | ||
from tests.test_socket_aute_middleware import create_test_user_and_token | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
@pytest.mark.asyncio | ||
async def test_single_game(): | ||
await clear_redis() | ||
user, refresh = await create_test_user_and_token() | ||
refresh['mfa_require'] = False | ||
access_token_bytes = bytes("access_token=" + str(refresh.access_token), 'utf-8') | ||
url = bytes(settings.BASE_URL, 'utf-8') | ||
headers = [(b'origin', url), (b'cookie', access_token_bytes)] | ||
communicator = WebsocketCommunicator(application, 'ws/game/', headers) | ||
connected, _ = await communicator.connect() | ||
assert connected, "Failed to connect to websocket" | ||
await communicator.send_to(json.dumps({"type": GameMessageType.SINGLE_GAME_CREATE})) | ||
try: | ||
while True: | ||
response = await communicator.receive_from(timeout=10) | ||
print(response) | ||
data = json.loads(response) | ||
if data["type"] == GameMessageType.INFO_GAME and data["status"] == "end": | ||
assert int(data["left_score"]) == 11 or int( | ||
data["right_score"]) == 11, "Game did not end with a score of 11 for one player" | ||
break | ||
finally: | ||
await communicator.disconnect() | ||
await clear_redis() |
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,67 @@ | ||
import asyncio | ||
|
||
import pytest | ||
from channels.db import database_sync_to_async | ||
from channels.testing import WebsocketCommunicator | ||
from rest_framework_simplejwt.tokens import RefreshToken | ||
|
||
from backend import settings | ||
from backend.asgi import application | ||
from conftest import clear_redis | ||
from user.models import User | ||
|
||
|
||
@database_sync_to_async | ||
def create_test_user_and_token(): | ||
user = User.objects.create_user(username="testuser", password="testpassword", email="[email protected]", | ||
nickname="middle") | ||
refresh = RefreshToken.for_user(user) | ||
return user, refresh | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
@pytest.mark.asyncio | ||
async def test_success_with_valid_token(): | ||
clear_redis() | ||
try: | ||
user, refresh = await create_test_user_and_token() | ||
refresh['mfa_require'] = False | ||
access_token_bytes = bytes("access_token=" + str(refresh.access_token), 'utf-8') | ||
url = bytes(settings.BASE_URL, 'utf-8') | ||
headers = [(b'origin', url), (b'cookie', access_token_bytes)] | ||
communicator = WebsocketCommunicator(application, 'ws/game/', headers) | ||
connected, subprotocol = await communicator.connect() | ||
assert connected | ||
finally: | ||
await communicator.disconnect() | ||
clear_redis() | ||
|
||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
@pytest.mark.asyncio | ||
async def test_fail_with_2fa_required_token(): | ||
clear_redis() | ||
user, refresh = await create_test_user_and_token() | ||
refresh['mfa_require'] = True | ||
access_token_bytes = bytes("access_token=" + str(refresh.access_token), 'utf-8') | ||
url = bytes(settings.BASE_URL, 'utf-8') | ||
headers = [(b'origin', url), (b'cookie', access_token_bytes)] | ||
communicator = WebsocketCommunicator(application, 'ws/game/', headers) | ||
connected, subprotocol = await communicator.connect() | ||
assert not connected | ||
await communicator.disconnect() | ||
clear_redis() | ||
|
||
|
||
@pytest.mark.django_db(transaction=True) | ||
@pytest.mark.asyncio | ||
async def test_fail_with_anonymous_user(): | ||
clear_redis() | ||
url = bytes(settings.BASE_URL, 'utf-8') | ||
headers = [(b'origin', url)] | ||
communicator = WebsocketCommunicator(application, 'ws/game/', headers) | ||
connected, subprotocol = await communicator.connect() | ||
assert not connected | ||
await communicator.disconnect() | ||
clear_redis() |