Skip to content

Commit

Permalink
Add test and fix not to send message for test
Browse files Browse the repository at this point in the history
  • Loading branch information
retroinspect committed Feb 15, 2023
1 parent d4e80d2 commit a75a51c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
7 changes: 5 additions & 2 deletions ara/firebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from firebase_admin import messaging

from apps.user.models import FCMToken

from ara.settings.test import TEST

def fcm_notify_comment(user, title, body, open_url):
targets = FCMToken.objects.filter(user=user)
Expand All @@ -18,10 +18,13 @@ def fcm_notify_comment(user, title, body, open_url):
fcm_simple(i.token, title, body, open_url)
except:
FCMToken.objects.filter(token=i.token).delete()
pass


def fcm_simple(FCM_token, title="Title", body="Body", open_url="/"):
# Do not send message in test environment
if TEST:
return

# This registration token comes from the client FCM SDKs.
# See documentation on defining a message payload.
message = messaging.Message(
Expand Down
73 changes: 73 additions & 0 deletions tests/test_fcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from datetime import timedelta

import pytest
from django.utils import timezone

from apps.core.models import Board, Article
from apps.user.models import FCMToken
from tests.conftest import TestCase, RequestSetting


@pytest.fixture(scope="class")
def set_board(request):
request.cls.board = Board.objects.create(
slug="free",
ko_name="자유 게시판",
en_name="Free Board",
ko_description="자유 게시판 입니다.",
en_description="This is a free board.",
)

@pytest.fixture(scope="class")
def set_articles(request):
"""set_board 먼저 적용"""
request.cls.article = Article.objects.create(
title="테스트 글입니다.",
content="테스트 내용입니다.",
content_text="테스트 텍스트",
parent_board=request.cls.board,
created_by=request.cls.user2,
)

@pytest.fixture(scope="class")
def set_unexpired_mock_fcmtoken(request):
print(f"now: {timezone.now()}")
request.cls.fcm = FCMToken.objects.create(
token="token",
user=request.cls.user2,
last_activated_at=timezone.now(),
is_web=True
)

@pytest.fixture(scope="class")
def set_expired_mock_fcmtoken(request):
week_ago = timezone.now().date() - timedelta(days=7)

request.cls.fcm_expired = FCMToken.objects.create(
token="expired_token",
user=request.cls.user2,
last_activated_at=week_ago,
is_web=True
)

@pytest.mark.usefixtures(
"set_user_client", "set_user_client2", "set_board", "set_articles", "set_unexpired_mock_fcmtoken", "set_expired_mock_fcmtoken"
)
class TestFCMToken(TestCase, RequestSetting):
def test_expire_token_on_request(self):
# 유효하지 않은 토큰은 요청 발생 시 삭제
tokens = FCMToken.objects.filter(user=self.user2)
assert(len(tokens) == 2)

comment_str = "this is a test comment for test_expire_token_on_request"
comment_data = {
"content": comment_str,
"parent_article": self.article.id,
"parent_comment": None,
"attachment": None,
}

self.http_request(self.user, "post", "comments", comment_data)

filtered_tokens = FCMToken.objects.filter(user=self.user2)
assert(len(filtered_tokens) == 1)

0 comments on commit a75a51c

Please sign in to comment.