File: utils.py

package info (click to toggle)
python-awair 0.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,016 kB
  • sloc: python: 1,041; makefile: 12
file content (83 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Test utilities."""

import re
from collections import namedtuple
from contextlib import contextmanager
from datetime import datetime
from typing import Any, Dict, Generator, Optional
from unittest.mock import patch

import vcr

from python_awair.auth import AwairAuth
from python_awair.client import AwairClient
from python_awair.devices import AwairDevice
from python_awair.user import AwairUser
from tests.const import MOCK_GEN1_DEVICE_ATTRS, MOCK_USER_ATTRS


def mock_awair_user(client: AwairClient) -> AwairUser:
    """Return a mock awair user."""
    return AwairUser(client=client, attributes=MOCK_USER_ATTRS)


def mock_awair_device(
    client: AwairClient, device: Optional[Dict[str, Any]] = None,
) -> AwairDevice:
    """Return a mock awair device."""
    if not device:
        device = MOCK_GEN1_DEVICE_ATTRS

    return AwairDevice(client=client, attributes=device)


class SillyAuth(AwairAuth):
    """Testing auth class."""

    def __init__(self, access_token: str) -> None:
        """Store our access token."""
        self.access_token = access_token

    async def get_bearer_token(self) -> str:
        """Return the access_token."""
        return self.access_token


Scrubber = namedtuple("Scrubber", ["pattern", "replacement"])
SCRUBBERS = [
    Scrubber(pattern=r'"email":"[^"]+"', replacement='"email":"foo@bar.com"'),
    Scrubber(pattern=r'"dobYear":\d+', replacement='"dobYear":2020'),
    Scrubber(pattern=r'"dobMonth":\d+', replacement='"dobMonth":4'),
    Scrubber(pattern=r'"dobDay":\d+', replacement='"dobDay":8'),
    Scrubber(pattern=r'"latitude":-?\d+\.\d+', replacement='"latitude":0.0'),
    Scrubber(pattern=r'"longitude":-?\d+\.\d+', replacement='"longitude":0.0'),
]


def scrub(response: Any) -> Any:
    """Scrub sensitive data."""
    body = response["body"]["string"].decode("utf-8")
    for scrubber in SCRUBBERS:
        body = re.sub(scrubber.pattern, scrubber.replacement, body)

    response["body"]["string"] = body.encode("utf-8")
    return response


VCR = vcr.VCR(
    cassette_library_dir="tests/fixtures/cassettes",
    record_mode="none",
    filter_headers=[("authorization", "fake_token")],
    decode_compressed_response=True,
    before_record_response=scrub,
)


@contextmanager
def time_travel(target: datetime) -> Generator[Any, Any, Any]:
    """Manage time in our tests."""
    with patch("python_awair.devices.datetime") as mock_date:
        mock_date.now.return_value = target
        mock_date.side_effect = datetime

        yield