✅ Added tests (+mock) for the push service + updated image tests (missing + large image).
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
43cd1b2a73
commit
cd99f80d0a
After Width: | Height: | Size: 9.4 MiB |
@ -0,0 +1,30 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from fastapi import FastAPI
|
||||
from test_main import *
|
||||
|
||||
# Exemple de fausse souscription push
|
||||
FAKE_SUB = {
|
||||
"endpoint": "https://test",
|
||||
"keys": {"p256dh": "key", "auth": "auth"}
|
||||
}
|
||||
|
||||
def test_push_subscribe_first(token):
|
||||
response = client.post("/push/subscribe", json=FAKE_SUB, headers={"Authorization": f"Bearer {token}"})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Push subscription successful"
|
||||
|
||||
def test_push_subscribe_duplicate(token):
|
||||
response = client.post("/push/subscribe", json=FAKE_SUB, headers={"Authorization": f"Bearer {token}"})
|
||||
assert response.status_code == 400
|
||||
assert "already exists" in response.json()["detail"]
|
||||
|
||||
def test_push_subscribe_second(token):
|
||||
# On s'abonne avec un nouvel endpoint
|
||||
sub = {
|
||||
"endpoint": "https://test2",
|
||||
"keys": {"p256dh": "key2", "auth": "auth2"}
|
||||
}
|
||||
response = client.post("/push/subscribe", json=sub, headers={"Authorization": f"Bearer {token}"})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["message"] == "Push subscription successful"
|
@ -0,0 +1,125 @@
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
|
||||
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock, AsyncMock
|
||||
from bson import ObjectId
|
||||
from api.app.push_service import PushService
|
||||
|
||||
# Fixture pour obtenir une instance du service à tester
|
||||
@pytest.fixture
|
||||
def push_service():
|
||||
return PushService()
|
||||
|
||||
# Teste l'envoi de notification quand l'utilisateur existe et a des abonnements
|
||||
@patch('api.app.push_service.users_collection')
|
||||
@patch('api.app.push_service.PushService.send_notification_to_subscription', new_callable=AsyncMock)
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_success(mock_send, mock_users, push_service):
|
||||
user_id = ObjectId()
|
||||
# Simule un utilisateur avec une souscription push
|
||||
mock_users.find_one.return_value = {"push_subscriptions": ["{\"endpoint\": \"https://test\"}"]}
|
||||
mock_send.return_value = True
|
||||
|
||||
payload = {"msg": "test"}
|
||||
result = await push_service.send_notification(user_id, payload)
|
||||
|
||||
assert result is None or result is True
|
||||
mock_send.assert_awaited()
|
||||
|
||||
# Teste le cas où l'utilisateur n'existe pas
|
||||
@patch('api.app.push_service.users_collection')
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_no_user(mock_users, push_service):
|
||||
user_id = ObjectId()
|
||||
mock_users.find_one.return_value = None # Aucun utilisateur trouvé
|
||||
|
||||
payload = {"msg": "test"}
|
||||
result = await push_service.send_notification(user_id, payload)
|
||||
|
||||
assert result is False
|
||||
|
||||
# Teste le cas où l'utilisateur n'a pas de souscriptions push
|
||||
@patch('api.app.push_service.users_collection')
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_no_subs(mock_users, push_service):
|
||||
user_id = ObjectId()
|
||||
|
||||
mock_users.find_one.return_value = {"push_subscriptions": []} # Pas de souscriptions
|
||||
|
||||
payload = {"msg": "test"}
|
||||
result = await push_service.send_notification(user_id, payload)
|
||||
|
||||
assert result is False
|
||||
|
||||
# Teste l'envoi d'une notification à une souscription valide
|
||||
@patch('api.app.push_service.webpush')
|
||||
@patch('api.app.push_service.get_audience', return_value='https://test')
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_to_subscription_success(mock_aud, mock_webpush, push_service):
|
||||
subscription = '{"endpoint": "https://test"}'
|
||||
|
||||
payload = {"msg": "test"}
|
||||
user_id = ObjectId()
|
||||
result = await push_service.send_notification_to_subscription(subscription, payload, user_id)
|
||||
|
||||
assert result is True
|
||||
mock_webpush.assert_called_once()
|
||||
|
||||
# Teste le cas où webpush lève une exception (échec d'envoi)
|
||||
@patch('api.app.push_service.webpush', side_effect=Exception('fail'))
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_to_subscription_exception(mock_webpush, push_service):
|
||||
subscription = '{"endpoint": "https://test"}'
|
||||
|
||||
payload = {"msg": "test"}
|
||||
user_id = ObjectId()
|
||||
result = await push_service.send_notification_to_subscription(subscription, payload, user_id)
|
||||
|
||||
assert result is False
|
||||
|
||||
# Teste l'envoi à plusieurs souscriptions (succès et échec)
|
||||
@patch('api.app.push_service.PushService.send_notification', new_callable=AsyncMock)
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_notification_to_all(mock_send, push_service):
|
||||
mock_send.side_effect = [True, False] # Un succès, un échec
|
||||
|
||||
subscriptions = ["sub1", "sub2"]
|
||||
payload = {"msg": "test"}
|
||||
result = await push_service.send_notification_to_all(subscriptions, payload)
|
||||
|
||||
assert result["success"] == 1
|
||||
assert result["failed"] == 1
|
||||
|
||||
# Teste la création du payload de notification
|
||||
def test_create_notification_payload(push_service):
|
||||
title = "Titre"
|
||||
body = "Corps"
|
||||
payload = push_service.create_notification_payload(title, body)
|
||||
|
||||
assert payload["notification"]["title"] == title
|
||||
assert payload["notification"]["body"] == body
|
||||
assert "icon" in payload["notification"]
|
||||
|
||||
# Teste la suppression d'une souscription (succès)
|
||||
@patch('api.app.push_service.users_collection')
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_subscription_success(mock_users, push_service):
|
||||
mock_users.update_one.return_value = MagicMock() # Simule la suppression
|
||||
|
||||
user_id = ObjectId()
|
||||
subscription = "sub"
|
||||
result = await push_service.delete_subscription(subscription, user_id)
|
||||
|
||||
assert result is True
|
||||
|
||||
# Teste la suppression d'une souscription quand une exception est levée (échec)
|
||||
@patch('api.app.push_service.users_collection.update_one', side_effect=Exception('fail'))
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_subscription_exception(mock_update_one, push_service):
|
||||
user_id = ObjectId()
|
||||
subscription = "sub"
|
||||
result = await push_service.delete_subscription(subscription, user_id)
|
||||
|
||||
assert result is False
|
Loading…
Reference in new issue