File size: 1,799 Bytes
c3b6521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import sys
import pytest
from unittest import mock
from main import check_env, check_dashboard_files


class _version_info:
    def __init__(self, major, minor):
        self.major = major
        self.minor = minor


def test_check_env(monkeypatch):
    version_info_correct = _version_info(3, 10)
    version_info_wrong = _version_info(3, 9)
    monkeypatch.setattr(sys, "version_info", version_info_correct)
    with mock.patch("os.makedirs") as mock_makedirs:
        check_env()
        mock_makedirs.assert_any_call("data/config", exist_ok=True)
        mock_makedirs.assert_any_call("data/plugins", exist_ok=True)
        mock_makedirs.assert_any_call("data/temp", exist_ok=True)

    monkeypatch.setattr(sys, "version_info", version_info_wrong)
    with pytest.raises(SystemExit):
        check_env()


@pytest.mark.asyncio
async def test_check_dashboard_files(monkeypatch):
    monkeypatch.setattr(os.path, "exists", lambda x: False)

    async def mock_get(*args, **kwargs):
        class MockResponse:
            status = 200

            async def read(self):
                return b"content"

        return MockResponse()

    with mock.patch("aiohttp.ClientSession.get", new=mock_get):
        with mock.patch("builtins.open", mock.mock_open()) as mock_file:
            with mock.patch("zipfile.ZipFile.extractall") as mock_extractall:

                async def mock_aenter(_):
                    await check_dashboard_files()
                    mock_file.assert_called_once_with("data/dashboard.zip", "wb")
                    mock_extractall.assert_called_once()

                async def mock_aexit(obj, exc_type, exc, tb):
                    return

                mock_extractall.__aenter__ = mock_aenter
                mock_extractall.__aexit__ = mock_aexit