File: test_auth.py

package info (click to toggle)
cloudprint 0.14-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 512 kB
  • sloc: python: 1,128; sh: 48; makefile: 16
file content (118 lines) | stat: -rw-r--r-- 2,923 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
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
import datetime
import json

from cloudprint.cloudprint import (
    CLIENT_ID,
    PRINT_CLOUD_URL,
    CloudPrintAuth,
)


def test_login(tmpdir, requests):
    requests.post(
        PRINT_CLOUD_URL + 'register',
        json={
            'token_duration': 30,
            'complete_invite_url': 'http://doit',
            'polling_url': 'http://poll',
        },
    )

    requests.get(
        'http://poll' + CLIENT_ID,
        json={
            'success': True,
            'xmpp_jid': 'my_xmpp',
            'user_email': 'me@example.com',
            'authorization_code': 'auth-123abc',
        },
    )

    requests.post(
        'https://accounts.google.com/o/oauth2/token',
        [
            {
                'json': {
                    'refresh_token': 'refresh-123abc',
                }
            },
            {
                'json': {
                    'access_token': 'access_token-123abc',
                    'expires_in': 15,
                }
            },
        ]
    )

    auth_path = tmpdir.join('auth')
    auth = CloudPrintAuth(str(auth_path))
    auth.AUTH_POLL_PERIOD = 0
    auth.login('name', 'description', 'ppd')

    with auth_path.open() as auth_file:
        auth_data = json.load(auth_file)
        assert auth_data['email'] == 'me@example.com'
        assert auth_data['xmpp_jid'] == 'my_xmpp'
        assert auth_data['refresh_token'] == 'refresh-123abc'

    assert auth.access_token == 'access_token-123abc'


def test_no_auth(tmpdir):
    auth_path = tmpdir.join('auth')
    auth = CloudPrintAuth(str(auth_path))

    assert auth.no_auth()

    auth_path.ensure()

    assert not auth.no_auth()


def test_refresh(tmpdir, requests):
    requests.post(
        'https://accounts.google.com/o/oauth2/token',
        json={
            'access_token': 'access_token-123abc',
            'expires_in': 15,
        }
    )

    auth_path = tmpdir.join('auth')
    auth = CloudPrintAuth(str(auth_path))
    auth._access_token = 'dead'
    auth.refresh_token = 'refresh-123abc'
    auth.exp_time = datetime.datetime.fromtimestamp(0)

    assert auth.access_token == 'access_token-123abc'


def test_load(tmpdir, requests):
    requests.post(
        'https://accounts.google.com/o/oauth2/token',
        json={
            'access_token': 'access_token-123abc',
            'expires_in': 15,
        }
    )

    auth_path = tmpdir.join('auth')
    with auth_path.open('w') as auth_file:
        json.dump(
            {
                'guid': 'guid123',
                'email': 'example@example.com',
                'xmpp_jid': 'my_xmpp',
                'refresh_token': 'refresh_123',
            },
            auth_file,
        )

    auth = CloudPrintAuth(str(auth_path))
    auth.load()

    assert auth.guid == 'guid123'
    assert auth.email == 'example@example.com'
    assert auth.xmpp_jid == 'my_xmpp'
    assert auth.refresh_token == 'refresh_123'