File: test_contacts.py

package info (click to toggle)
pyicloud 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 828 kB
  • sloc: python: 9,741; sh: 17; makefile: 3
file content (100 lines) | stat: -rw-r--r-- 3,462 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Unit tests for the ContactsService and MeCard classes."""

from unittest.mock import MagicMock, patch

import pytest

from pyicloud.services.contacts import ContactsService, MeCard


def test_contacts_service_initialization(contacts_service: ContactsService) -> None:
    """Test the initialization of ContactsService."""
    assert contacts_service._contacts_endpoint == "https://example.com/co"  # pylint: disable=protected-access
    assert contacts_service._contacts_refresh_url == "https://example.com/co/startup"  # pylint: disable=protected-access
    assert contacts_service._contacts_next_url == "https://example.com/co/contacts"  # pylint: disable=protected-access
    assert (
        contacts_service._contacts_changeset_url == "https://example.com/co/changeset"  # pylint: disable=protected-access
    )
    assert contacts_service._contacts_me_card_url == "https://example.com/co/mecard"  # pylint: disable=protected-access
    assert contacts_service._contacts is None  # pylint: disable=protected-access


@patch("requests.Response")
def test_refresh_client(
    mock_response, contacts_service: ContactsService, mock_session: MagicMock
) -> None:
    """Test the refresh_client method."""
    mock_response.json.return_value = {
        "prefToken": "test_pref_token",
        "syncToken": "test_sync_token",
        "contacts": [{"firstName": "John", "lastName": "Doe"}],
    }
    mock_session.get.return_value = mock_response

    contacts_service.refresh_client()

    mock_session.get.assert_called()
    assert contacts_service._contacts == [  # pylint: disable=protected-access
        {
            "firstName": "John",
            "lastName": "Doe",
        }
    ]


@patch("requests.Response")
def test_all_property(
    mock_response, contacts_service: ContactsService, mock_session: MagicMock
) -> None:
    """Test the all property."""
    mock_response.json.return_value = {
        "prefToken": "test_pref_token",
        "syncToken": "test_sync_token",
        "contacts": [{"firstName": "John", "lastName": "Doe"}],
    }
    mock_session.get.return_value = mock_response

    contacts = contacts_service.all

    mock_session.get.assert_called()
    assert contacts == [{"firstName": "John", "lastName": "Doe"}]


@patch("requests.Response")
def test_me_property(
    mock_response, contacts_service: ContactsService, mock_session: MagicMock
) -> None:
    """Test the me property."""
    mock_response.json.return_value = {
        "contacts": [{"firstName": "Jane", "lastName": "Smith", "photo": "photo_url"}]
    }
    mock_session.get.return_value = mock_response

    me_card: MeCard = contacts_service.me

    mock_session.get.assert_called()
    assert isinstance(me_card, MeCard)
    assert me_card.first_name == "Jane"
    assert me_card.last_name == "Smith"
    assert me_card.photo == "photo_url"


def test_me_card_initialization() -> None:
    """Test the initialization of MeCard."""
    data: dict[str, list[dict[str, str]]] = {
        "contacts": [
            {"firstName": "Alice", "lastName": "Johnson", "photo": "photo_url"}
        ]
    }
    me_card = MeCard(data)

    assert me_card.first_name == "Alice"
    assert me_card.last_name == "Johnson"
    assert me_card.photo == "photo_url"
    assert me_card.raw_data == data


def test_me_card_invalid_data() -> None:
    """Test MeCard initialization with invalid data."""
    with pytest.raises(KeyError):
        MeCard({"invalid_key": "value"})