File: test_live_stream_api.py

package info (click to toggle)
py-canary 0.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: python: 783; sh: 18; makefile: 8
file content (53 lines) | stat: -rw-r--r-- 1,398 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
"""The tests for the Canary sensor platform."""
import os
import unittest

import requests_mock

from canary.live_stream_api import LiveStreamApi
from canary.const import (
    URL_LOGIN_API,
    URL_LOGIN_PAGE,
    COOKIE_XSRF_TOKEN,
    COOKIE_SSESYRANAC,
)

COOKIE_XSRF_VAL = "xsrf"
COOKIE_COOKIE_SSESYRANAC_VAL = "ssesyranac"


def load_fixture(filename):
    """Load a fixture."""
    path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
    with open(path) as fptr:
        return fptr.read()


def _setup_responses(mock):
    mock.register_uri(
        "POST", URL_LOGIN_API, text=load_fixture("live_stream_api_login.json")
    )
    mock.register_uri(
        "GET",
        URL_LOGIN_PAGE,
        cookies={
            COOKIE_XSRF_TOKEN: COOKIE_XSRF_VAL,
            COOKIE_SSESYRANAC: COOKIE_COOKIE_SSESYRANAC_VAL,
        },
    )


class TestLiveStreamApi(unittest.TestCase):
    @requests_mock.Mocker()
    def test_login(self, mock):
        """Test login for canary live stream api"""
        _setup_responses(mock)
        api = LiveStreamApi("user", "pass")

        api.login()

        with self.subTest("stores the token on the api object"):
            self.assertEqual(api._token, "ffffffffffffffffffffffffffffffffffffffff")

        with self.subTest("stores ssesyranac cookie on the api object"):
            self.assertEqual(api._ssesyranac, "ssesyranac")