File: test_session.py

package info (click to toggle)
python-internetarchive 5.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,028 kB
  • sloc: python: 8,392; makefile: 235; xml: 180
file content (213 lines) | stat: -rw-r--r-- 7,158 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os

import responses

import internetarchive.session
from internetarchive import __version__
from tests.conftest import NASA_METADATA_PATH, PROTOCOL, IaRequestsMock

CONFIG = {
    's3': {
        'access': 'test_access',
        'secret': 'test_secret',
    },
    'cookies': {
        'logged-in-user': 'test%40example.com; path=/; domain=.archive.org',
        'logged-in-sig': 'testsig; path=/; domain=.archive.org',
    },
    'logging': {
        'level': 'INFO',
        'file': 'test.log',
    },
}


def test_archive_session(tmpdir):
    tmpdir.chdir()

    # Use an empty config file to avoid merging with user's real config
    empty_config_file = str(tmpdir.join('empty_ia.ini'))
    with open(empty_config_file, 'w'):
        pass

    s = internetarchive.session.ArchiveSession(CONFIG, config_file=empty_config_file)
    assert os.path.isfile('test.log')

    assert CONFIG == s.config
    assert set(s.cookies.keys()) == set(CONFIG['cookies'].keys())
    assert s.secure is True
    assert s.protocol == PROTOCOL
    assert s.access_key == 'test_access'
    assert s.secret_key == 'test_secret'
    assert s.headers['user-agent'].startswith(f'internetarchive/{__version__}')


def test_get_item(tmpdir):
    tmpdir.chdir()

    with open(NASA_METADATA_PATH) as fh:
        item_metadata = fh.read().strip()

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//archive.org/metadata/nasa',
                 body=item_metadata,
                 content_type='application/json')

        s = internetarchive.session.ArchiveSession()
        item = s.get_item('nasa')
        assert item.exists is True
        assert item.identifier == 'nasa'

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//archive.org/metadata/nasa',
                 body=item_metadata,
                 status=400,
                 content_type='application/json')

        s = internetarchive.session.ArchiveSession(CONFIG)
        try:
            item = s.get_item('nasa')
        except Exception:
            with open('test.log') as fh:
                assert '400 Client Error' in fh.read()


def test_s3_is_overloaded():
    test_body = """{
        "accesskey": "test_access",
        "bucket": "nasa",
        "detail": {
            "accesskey_ration": 74,
            "accesskey_tasks_queued": 0,
            "bucket_ration": 24,
            "bucket_tasks_queued": 0,
            "limit_reason": "",
            "rationing_engaged": 0,
            "rationing_level": 1399,
            "total_global_limit": 1799,
            "total_tasks_queued": 308
        },
        "over_limit": 0
    }"""

    with IaRequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//s3.us.archive.org',
                 body=test_body,
                 content_type='application/json')
        s = internetarchive.session.ArchiveSession(CONFIG)
        r = s.s3_is_overloaded('nasa')
        assert r is False

    test_body = """{
        "accesskey": "test_access",
        "bucket": "nasa",
        "detail": {
            "accesskey_ration": 74,
            "accesskey_tasks_queued": 0,
            "bucket_ration": 24,
            "bucket_tasks_queued": 0,
            "limit_reason": "",
            "rationing_engaged": 0,
            "rationing_level": 1399,
            "total_global_limit": 1799,
            "total_tasks_queued": 308
        },
        "over_limit": 1
    }"""

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//s3.us.archive.org',
                 body=test_body,
                 content_type='application/json')
        s = internetarchive.session.ArchiveSession(CONFIG)
        r = s.s3_is_overloaded('nasa')
        assert r is True


def test_user_agent_suffix():
    """Test that a custom user agent suffix is appended to the default UA."""
    custom_suffix = 'MyCustomApp/1.0 (test bot)'
    config = {
        's3': {
            'access': 'test_access',
            'secret': 'test_secret',
        },
        'general': {
            'user_agent_suffix': custom_suffix,
        },
    }
    s = internetarchive.session.ArchiveSession(config)
    # Verify the UA starts with the default and ends with the custom suffix
    assert s.headers['User-Agent'].startswith(f'internetarchive/{__version__}')
    assert s.headers['User-Agent'].endswith(custom_suffix)
    # Verify access key is present in the UA
    assert 'test_access' in s.headers['User-Agent']


def test_default_user_agent_when_not_specified():
    """Test that default user agent is used when custom is not specified."""
    config = {
        's3': {
            'access': 'test_access',
            'secret': 'test_secret',
        },
    }
    s = internetarchive.session.ArchiveSession(config)
    assert s.headers['user-agent'].startswith(f'internetarchive/{__version__}')


def test_user_agent_suffix_in_requests():
    """Test that the user agent suffix is appended and sent in requests."""
    custom_suffix = 'TestAgent/2.0'
    config = {
        's3': {
            'access': 'test_access',
            'secret': 'test_secret',
        },
        'general': {
            'user_agent_suffix': custom_suffix,
        },
    }

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//archive.org')
        s = internetarchive.session.ArchiveSession(config)
        r = s.get(f'{PROTOCOL}//archive.org')
        # Verify the UA starts with the default and ends with the custom suffix
        assert r.request.headers['User-Agent'].startswith(f'internetarchive/{__version__}')
        assert r.request.headers['User-Agent'].endswith(custom_suffix)
        # Verify access key is present in the UA
        assert 'test_access' in r.request.headers['User-Agent']


def test_access_key_always_in_user_agent():
    """Test that the access key is always present in the User-Agent."""
    config = {
        's3': {
            'access': 'MY_ACCESS_KEY',
            'secret': 'test_secret',
        },
    }
    s = internetarchive.session.ArchiveSession(config)
    assert 'MY_ACCESS_KEY' in s.headers['User-Agent']
    assert s.headers['User-Agent'].startswith(f'internetarchive/{__version__}')


def test_cookies():
    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//archive.org')
        s = internetarchive.session.ArchiveSession(CONFIG)
        r = s.get(f'{PROTOCOL}//archive.org')
        assert 'logged-in-sig' in r.request.headers['Cookie']
        assert 'logged-in-user' in r.request.headers['Cookie']
        for c in s.cookies:
            if c.name.startswith('logged-in-'):
                assert c.domain == '.archive.org'

    with responses.RequestsMock() as rsps:
        rsps.add(responses.GET, f'{PROTOCOL}//example.com')
        s = internetarchive.session.ArchiveSession(CONFIG)
        r = s.get(f'{PROTOCOL}//example.com')
        assert 'logged-in-sig' not in r.request.headers.get('Cookie', '')
        assert 'logged-in-user' not in r.request.headers.get('Cookie', '')
        assert '.archive.org' not in r.request.headers.get('Cookie', '')