Debian Codegraph @

tests/test_account_link.py

raw
1 """Test Account Linking tools."""
2
3 import asyncio
4 from unittest.mock import AsyncMock, Mock
5
6 from aiohttp import web
7 import pytest
8
9 from hass_nabucasa import account_link
10
11
12 async def create_account_link_server(aiohttp_client, handle_server_msgs):
13 """Create a websocket server."""
14
15 async def websocket_handler(request):
16 ws = web.WebSocketResponse()
17 await ws.prepare(request)
18
19 try:
20 await handle_server_msgs(ws)
21 finally:
22 await ws.close()
23
24 return ws
25
26 app = web.Application()
27 app.add_routes([web.get("/ws", websocket_handler)])
28 client = await aiohttp_client(app)
29 return await client.ws_connect("/ws")
30
31
32 async def create_helper_instance(
33 aiohttp_client,
34 handle_server_msgs,
35 service,
36 ) -> account_link.AuthorizeAccountHelper:
37 """Create a auth helper instance."""
38 client = await create_account_link_server(aiohttp_client, handle_server_msgs)
39 mock_cloud = Mock(
40 client=Mock(websession=Mock(ws_connect=AsyncMock(return_value=client))),
41 )
42 return account_link.AuthorizeAccountHelper(mock_cloud, service)
43
44
45 async def test_auth_helper_works(aiohttp_client):
46 """Test authorize account helper."""
47 received = []
48
49 async def handle_msgs(ws):
50 """Handle the messages on the server."""
51 data = await ws.receive_json()
52 received.append(data)
53 await ws.send_json({"authorize_url": "http://mock-url"})
54 await ws.send_json({"tokens": {"refresh_token": "abcd", "expires_in": 10}})
55
56 helper = await create_helper_instance(aiohttp_client, handle_msgs, "mock-service")
57
58 assert await helper.async_get_authorize_url() == "http://mock-url"
59
60 assert await helper.async_get_tokens() == {
61 "refresh_token": "abcd",
62 "expires_in": 10,
63 "service": "mock-service",
64 }
65
66 assert helper._client is None
67 assert len(received) == 1
68 assert received[0] == {"service": "mock-service"}
69
70
71 async def test_auth_helper_unknown_service(aiohttp_client):
72 """Test authorize account helper."""
73
74 async def handle_msgs(ws):
75 """Handle the messages on the server."""
76 await ws.receive_json()
77 await ws.send_json({"error": "unknown"})
78
79 helper = await create_helper_instance(aiohttp_client, handle_msgs, "mock-service")
80
81 with pytest.raises(account_link.AccountLinkException) as err:
82 await helper.async_get_authorize_url()
83 assert err.value.code == "unknown"
84
85
86 async def test_auth_helper_token_timeout(aiohttp_client):
87 """Test timeout while waiting for tokens."""
88
89 async def handle_msgs(ws):
90 """Handle the messages on the server."""
91 await ws.receive_json()
92 await ws.send_json({"authorize_url": "http://mock-url"})
93 await ws.send_json({"error": "timeout"})
94
95 helper = await create_helper_instance(aiohttp_client, handle_msgs, "mock-service")
96
97 await helper.async_get_authorize_url()
98
99 with pytest.raises(asyncio.TimeoutError):
100 await helper.async_get_tokens()
101
102
103 async def test_auth_helper_token_other_error(aiohttp_client):
104 """Test error while waiting for tokens."""
105
106 async def handle_msgs(ws):
107 """Handle the messages on the server."""
108 await ws.receive_json()
109 await ws.send_json({"authorize_url": "http://mock-url"})
110 await ws.send_json({"error": "something"})
111
112 helper = await create_helper_instance(aiohttp_client, handle_msgs, "mock-service")
113
114 await helper.async_get_authorize_url()
115
116 with pytest.raises(account_link.AccountLinkException) as err:
117 await helper.async_get_tokens()
118
119 assert err.value.code == "something"