File: test_certificate_refresher.py

package info (click to toggle)
python-proton-vpn-api-core 0.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 6,582; makefile: 8
file content (62 lines) | stat: -rw-r--r-- 2,019 bytes parent folder | download
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
57
58
59
60
61
62
"""
Copyright (c) 2023 Proton AG

This file is part of Proton VPN.

Proton VPN is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Proton VPN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ProtonVPN.  If not, see <https://www.gnu.org/licenses/>.
"""
from unittest.mock import Mock, AsyncMock

import pytest

from proton.vpn.core.refresher.certificate_refresher import CertificateRefresher, generate_backoff_value
from proton.vpn.core.refresher.scheduler import RunAgain


@pytest.mark.asyncio
async def test_refresh_fetches_certificate_if_expired_and_returns_next_refresh_delay():
    session_holder = Mock()
    session = session_holder.session

    refresher = CertificateRefresher(session_holder=session_holder)

    session.fetch_certificate = AsyncMock()
    new_certificate = Mock()
    new_certificate.remaining_time_to_next_refresh = 600
    session.fetch_certificate.return_value= new_certificate

    next_refresh_delay = await refresher.refresh()

    assert next_refresh_delay == RunAgain.after_seconds(new_certificate.remaining_time_to_next_refresh)


@pytest.mark.parametrize("nth_failed_attempt, expected_backoff", [
    (0, 1),
    (1, 2),
    (2, 4),
    (3, 8),
    (4, 16),
    (5, 32)
])
def test_generate_backoff_value_generates_expected_value(nth_failed_attempt, expected_backoff):
    backoff_in_seconds = 1
    random_component = 1

    backoff = generate_backoff_value(
        number_of_failed_refresh_attempts=nth_failed_attempt,
        backoff_in_seconds=backoff_in_seconds,
        random_component=random_component
    )

    assert backoff == expected_backoff