File: auth_test.py

package info (click to toggle)
python-mypermobil 0.1.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: python: 1,172; makefile: 3
file content (159 lines) | stat: -rw-r--r-- 6,272 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
""" test auth control flow """

import aiounittest
import datetime
import unittest
from unittest.mock import MagicMock, AsyncMock
from mypermobil import MyPermobil, MyPermobilClientException, MyPermobilAPIException, MyPermobilEulaException


# pylint: disable=missing-docstring
class TestAuth(aiounittest.AsyncTestCase):
    def setUp(self):
        self.api = MyPermobil(
            "test",
            AsyncMock(),
            region="http://example.com",
        )

    async def test_not_authenticated(self):
        with self.assertRaises(MyPermobilClientException):
            _ = self.api.headers
        assert not self.api.authenticated

    async def test_request_application_code(self):
        self.api.set_email("valid@email.com")
        self.api.set_region("http://example.com")
        resp = MagicMock(status=204)
        resp.text = AsyncMock(return_value="OK")
        self.api.make_request = AsyncMock(return_value=resp)
        await self.api.request_application_code()

    async def test_request_application_code_invalid_response(self):
        resp = MagicMock(status=400)
        resp.text = AsyncMock(return_value="Error")
        self.api.make_request = AsyncMock(return_value=resp)
        with self.assertRaises(MyPermobilAPIException):
            await self.api.request_application_code(
                email="test@example.com",
                region="http://example.com",
                application="myapp",
            )

    async def test_request_application_code_client_errors(self):
        resp = MagicMock(status=400)
        resp.text = AsyncMock(return_value="Error")

        self.api.authenticated = True
        with self.assertRaises(MyPermobilClientException):
            await self.api.request_application_code(
                email="test@example.com",
                region="http://example.com",
            )

        self.api.authenticated = False
        self.api.application = ""
        with self.assertRaises(MyPermobilClientException):
            await self.api.request_application_code(
                email="test@example.com",
                region="http://example.com",
                application="",
            )

    async def test_request_application_token(self):
        resp = AsyncMock(status=200)
        resp.json = AsyncMock(return_value={"token": "mytoken"})
        self.api.make_request = AsyncMock(return_value=resp)
        self.api.set_email("test@example.com")
        self.api.set_code("123123")
        result = await self.api.request_application_token()
        expected_date = datetime.datetime.now() + datetime.timedelta(days=365)
        self.assertEqual(result, ("mytoken", expected_date.strftime("%Y-%m-%d")))

        # test that it cannot be done twice
        self.api.authenticated = True
        with self.assertRaises(MyPermobilClientException):
            await self.api.request_application_token()

    async def test_request_application_token_invalid_response(self):
        self.api.make_request = AsyncMock(return_value=MagicMock(status=401))
        with self.assertRaises(MyPermobilAPIException):
            await self.api.request_application_token(
                email="test@example.com", code="123123"
            )

        self.api.make_request = AsyncMock(return_value=MagicMock(status=403))
        with self.assertRaises(MyPermobilAPIException):
            await self.api.request_application_token(
                email="test@example.com", code="123123"
            )
        resp = AsyncMock(status=400)
        resp.json = AsyncMock(return_value={"error": "test"})
        self.api.make_request = AsyncMock(return_value=resp)
        with self.assertRaises(MyPermobilAPIException):
            await self.api.request_application_token(
                email="test@example.com", code="123123"
            )
        resp = AsyncMock(status=430)
        resp.json = AsyncMock(return_value={"error": "test"})
        self.api.make_request = AsyncMock(return_value=resp)
        with self.assertRaises(MyPermobilEulaException):
            await self.api.request_application_token(
                email="test@example.com", code="123123"
            )

        resp = AsyncMock(status=123123)
        resp.test = AsyncMock(return_value="test")
        self.api.make_request = AsyncMock(return_value=resp)
        with self.assertRaises(MyPermobilAPIException):
            await self.api.request_application_token(
                email="test@example.com", code="123123"
            )

    async def test_reauthenticate(self):
        self.api.authenticated = True
        with self.assertRaises(MyPermobilClientException):
            self.api.self_reauthenticate()

        self.api.authenticated = False
        self.api.application = ""
        with self.assertRaises(MyPermobilClientException):
            self.api.self_reauthenticate()

        self.api.authenticated = False
        self.api.application = "test"
        self.api.self_reauthenticate()
        assert self.api.authenticated is False
        assert self.api.token == None
        assert self.api.expiration_date == None
        assert self.api.code == None

    async def test_deauthenticate(self):
        # test deauth when not authenticated
        self.api.authenticated = False
        self.api.set_email("test@example.com")
        self.api.set_code("123123")
        with self.assertRaises(MyPermobilClientException) as e:
            await self.api.deauthenticate()
            assert str(e.exception) == "Not authenticated"

        # test deauth when not application
        self.api.authenticated = True
        self.api.application = ""
        with self.assertRaises(MyPermobilClientException) as e:
            await self.api.deauthenticate()
            assert str(e.exception) == "Missing application name"

        self.api.authenticated = True
        self.api.application = "test"
        # test successful deauth
        text = AsyncMock(return_value="text")
        session = AsyncMock()
        session.make_request = AsyncMock(return_value=AsyncMock(status=123, text=text))
        self.api.session = session
        with self.assertRaises(MyPermobilAPIException):
            await self.api.deauthenticate()


if __name__ == "__main__":
    unittest.main()