|
| 1 | +"""Fixtures for tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any, Generator |
| 6 | +from unittest.mock import AsyncMock |
| 7 | + |
| 8 | +import pytest |
| 9 | +from aioresponses import aioresponses |
| 10 | + |
| 11 | +from ravyapi.api.endpoints import Avatars, Guilds, KSoft, Tokens, URLs, Users |
| 12 | +from ravyapi.client import Client |
| 13 | +from ravyapi.http import HTTPClient |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def valid_ravy_token() -> str: |
| 18 | + """Valid Ravy token for testing.""" |
| 19 | + return "abcdefghijklmnopqrstuvwx.1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def valid_ksoft_token() -> str: |
| 24 | + """Valid KSoft token for testing.""" |
| 25 | + return "1234567890abcdef1234567890abcdef12345678" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def invalid_token() -> str: |
| 30 | + """Invalid token for testing.""" |
| 31 | + return "invalid_token" |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def sample_check_avatar_response(): |
| 36 | + """Sample check avatar response.""" |
| 37 | + return { |
| 38 | + "matched": True, |
| 39 | + "key": "test_key", |
| 40 | + "similarity": 0.95, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture |
| 45 | +def sample_get_token_response(): |
| 46 | + """Sample get token response.""" |
| 47 | + return { |
| 48 | + "user": 12345, |
| 49 | + "access": ["users", "avatars", "urls"], |
| 50 | + "application": 67890, |
| 51 | + "token_type": "ravy", |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def mock_aioresponses() -> Generator[aioresponses, Any, None]: |
| 57 | + """Mock aioresponses fixture.""" |
| 58 | + with aioresponses() as m: |
| 59 | + yield m |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def mock_http_client(valid_ravy_token: str) -> HTTPClient: |
| 64 | + """Mock HTTPClient for testing.""" |
| 65 | + # Create a mock session to avoid the event loop issue |
| 66 | + mock_session = AsyncMock() |
| 67 | + client = HTTPClient.__new__(HTTPClient) |
| 68 | + client._token = valid_ravy_token # type: ignore |
| 69 | + client._permissions = None # type: ignore |
| 70 | + client._phisherman_token = None # type: ignore |
| 71 | + client._headers = { # type: ignore |
| 72 | + "Authorization": valid_ravy_token, |
| 73 | + "User-Agent": "Test-Agent", |
| 74 | + } |
| 75 | + client._session = mock_session # type: ignore |
| 76 | + return client |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture |
| 80 | +def mock_client(valid_ravy_token: str, mock_http_client: HTTPClient) -> Client: |
| 81 | + """Mock Client for testing.""" |
| 82 | + client = Client.__new__(Client) |
| 83 | + client._token = valid_ravy_token # type: ignore |
| 84 | + client._http = mock_http_client # type: ignore |
| 85 | + client._closed = False # type: ignore |
| 86 | + client._avatars = Avatars(mock_http_client) # type: ignore |
| 87 | + client._guilds = Guilds(mock_http_client) # type: ignore |
| 88 | + client._ksoft = KSoft(mock_http_client) # type: ignore |
| 89 | + client._users = Users(mock_http_client) # type: ignore |
| 90 | + client._urls = URLs(mock_http_client) # type: ignore |
| 91 | + client._tokens = Tokens(mock_http_client) # type: ignore |
| 92 | + return client |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture |
| 96 | +def mock_avatars_endpoint(mock_http_client: HTTPClient) -> Avatars: |
| 97 | + """Mock Avatars endpoint for testing.""" |
| 98 | + return Avatars(mock_http_client) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture |
| 102 | +def mock_tokens_endpoint(mock_http_client: HTTPClient) -> Tokens: |
| 103 | + """Mock Tokens endpoint for testing.""" |
| 104 | + return Tokens(mock_http_client) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.fixture |
| 108 | +def sample_get_guild_response(): |
| 109 | + """Sample get guild response.""" |
| 110 | + return { |
| 111 | + "trust": {"level": 3, "label": "Neutral"}, |
| 112 | + "bans": [ |
| 113 | + { |
| 114 | + "provider": "ravy", |
| 115 | + "reason": "Test reason", |
| 116 | + "moderator": "987654321", |
| 117 | + "reason_key": "test_key", |
| 118 | + } |
| 119 | + ], |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture |
| 124 | +def sample_get_user_response(): |
| 125 | + """Sample get user response.""" |
| 126 | + return { |
| 127 | + "pronouns": "he/him", |
| 128 | + "trust": {"level": 3, "label": "Neutral"}, |
| 129 | + "whitelists": [ |
| 130 | + {"provider": "ravy", "reason": "Test whitelist", "moderator": "987654321"} |
| 131 | + ], |
| 132 | + "bans": [ |
| 133 | + { |
| 134 | + "provider": "ravy", |
| 135 | + "reason": "Test ban", |
| 136 | + "moderator": "987654321", |
| 137 | + "reason_key": "test_key", |
| 138 | + } |
| 139 | + ], |
| 140 | + "rep": [{"provider": "ravy", "reputation": 100, "moderator": "987654321"}], |
| 141 | + "sentinel": { |
| 142 | + "tracked": True, |
| 143 | + "reason": "Test sentinel", |
| 144 | + "moderator": "987654321", |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | +@pytest.fixture |
| 150 | +def sample_get_website_response(): |
| 151 | + """Sample get website response.""" |
| 152 | + return { |
| 153 | + "is_fraudulent": False, |
| 154 | + "message": "Safe website", |
| 155 | + "scan_date": "2023-01-01T00:00:00Z", |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +@pytest.fixture |
| 160 | +def sample_get_ksoft_ban_response(): |
| 161 | + """Sample get KSoft ban response.""" |
| 162 | + return { |
| 163 | + "banned": True, |
| 164 | + "reason": "Test ban reason", |
| 165 | + "moderator": "987654321", |
| 166 | + "appeal": "https://example.com/appeal", |
| 167 | + "date": "2023-01-01T00:00:00Z", |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | +@pytest.fixture |
| 172 | +def mock_guilds_endpoint(mock_http_client: HTTPClient) -> Guilds: |
| 173 | + """Mock Guilds endpoint for testing.""" |
| 174 | + return Guilds(mock_http_client) |
| 175 | + |
| 176 | + |
| 177 | +@pytest.fixture |
| 178 | +def mock_users_endpoint(mock_http_client: HTTPClient) -> Users: |
| 179 | + """Mock Users endpoint for testing.""" |
| 180 | + return Users(mock_http_client) |
| 181 | + |
| 182 | + |
| 183 | +@pytest.fixture |
| 184 | +def mock_urls_endpoint(mock_http_client: HTTPClient) -> URLs: |
| 185 | + """Mock URLs endpoint for testing.""" |
| 186 | + return URLs(mock_http_client) |
| 187 | + |
| 188 | + |
| 189 | +@pytest.fixture |
| 190 | +def mock_ksoft_endpoint(mock_http_client: HTTPClient) -> KSoft: |
| 191 | + """Mock KSoft endpoint for testing.""" |
| 192 | + return KSoft(mock_http_client) |
0 commit comments