aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/test_ingest.py
blob: 46346b76da0f595f77b548dbe970a505b798da5b (plain)
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

import json
import pytest
import responses

from sandcrawler import *
from test_wayback import *
from test_savepagenow import *
from test_grobid import REAL_TEI_XML


@pytest.fixture
def ingest_worker(wayback_client, spn_client):
    grobid_client = GrobidClient(
        host_url="http://dummy-grobid",
    )
    worker = IngestFileWorker(
        wayback_client=wayback_client,
        spn_client=spn_client,
        grobid_client=grobid_client,
    )
    return worker

@pytest.fixture
def ingest_worker_pdf(wayback_client_pdf, spn_client):
    grobid_client = GrobidClient(
        host_url="http://dummy-grobid",
    )
    pgrest_client = SandcrawlerPostgrestClient(
        api_url="http://dummy-postgrest",
    )
    worker = IngestFileWorker(
        wayback_client=wayback_client_pdf,
        spn_client=spn_client,
        grobid_client=grobid_client,
        pgrest_client=pgrest_client,
    )
    return worker


@responses.activate
def test_ingest_success(ingest_worker_pdf):

    with open('tests/files/dummy.pdf', 'rb') as f:
        pdf_bytes = f.read()

    request = {
        'ingest_type': 'pdf',
        'base_url': "http://dummy-host/",
    }
    responses.add(responses.POST,
        'http://dummy-spnv2/save',
        status=200,
        body=json.dumps({"url": TARGET, "job_id": JOB_ID}))
    responses.add(responses.GET,
        'http://dummy-spnv2/save/status/' + JOB_ID,
        status=200,
        body=json.dumps(PENDING_BODY))
    responses.add(responses.GET,
        'http://dummy-spnv2/save/status/' + JOB_ID,
        status=200,
        body=json.dumps(SUCCESS_BODY))
    responses.add(responses.GET,
        'http://dummy-cdx/cdx',
        status=200,
        body=json.dumps(CDX_SPN_HIT))
    responses.add(responses.GET,
        'https://web.archive.org/web/{}id_/{}'.format("20180326070330", TARGET + "/redirect"),
        status=200,
        headers={"X-Archive-Src": "liveweb-whatever.warc.gz"},
        body=pdf_bytes)
    responses.add(responses.GET,
        'http://dummy-postgrest/grobid?sha1hex=eq.{}'.format("90ffd2359008d82298821d16b21778c5c39aec36"),
        status=200,
        body=json.dumps([]))
    responses.add(responses.GET,
        'http://dummy-postgrest/pdf_meta?sha1hex=eq.{}'.format("90ffd2359008d82298821d16b21778c5c39aec36"),
        status=200,
        body=json.dumps([]))
    responses.add(responses.POST,
        'http://dummy-grobid/api/processFulltextDocument', status=200,
        body=REAL_TEI_XML, content_type='text/xml')

    resp = ingest_worker_pdf.process(request)

    print(resp)
    assert resp['hit'] == True
    assert resp['status'] == "success"
    assert resp['request'] == request
    assert resp['terminal']['terminal_sha1hex'] == resp['file_meta']['sha1hex']
    assert type(resp['terminal']['terminal_dt']) == str
    assert resp['terminal']['terminal_url'] == TARGET + "/redirect"
    assert resp['terminal']['terminal_status_code']
    assert type(resp['file_meta']['size_bytes']) == int
    assert resp['file_meta']['mimetype'] == "application/pdf"
    assert resp['cdx']['url'] == TARGET + "/redirect"
    assert 'warc_path' not in resp['cdx']
    assert 'revisit_cdx' not in resp
    assert resp['grobid']['status'] == "success"
    assert resp['grobid']['status_code'] == 200
    assert resp['grobid']['grobid_version']
    assert 'fatcat_release' in resp['grobid']
    assert 'grobid_version' not in resp['grobid']['metadata']
    assert 'fatcat_release' not in resp['grobid']['metadata']
    assert not 'tei_xml' in resp['grobid']
    assert resp['pdf_meta']['status'] == "success"
    assert resp['pdf_meta']['pdf_extra']['page_count'] == 1
    assert resp['pdf_meta'].get('text') is None

@responses.activate
def test_ingest_landing(ingest_worker):

    request = {
        'ingest_type': 'pdf',
        'base_url': "http://dummy-host/",
    }
    responses.add(responses.POST,
        'http://dummy-spnv2/save',
        status=200,
        body=json.dumps({"url": TARGET, "job_id": JOB_ID}))
    responses.add(responses.GET,
        'http://dummy-spnv2/save/status/' + JOB_ID,
        status=200,
        body=json.dumps(PENDING_BODY))
    responses.add(responses.GET,
        'http://dummy-spnv2/save/status/' + JOB_ID,
        status=200,
        body=json.dumps(SUCCESS_BODY))
    responses.add(responses.GET,
        'http://dummy-cdx/cdx',
        status=200,
        body=json.dumps(CDX_SPN_HIT))
    responses.add(responses.GET,
        'https://web.archive.org/web/{}id_/{}'.format("20180326070330", TARGET + "/redirect"),
        status=200,
        headers={"X-Archive-Src": "liveweb-whatever.warc.gz"},
        body=WARC_BODY)

    # this is for second time around; don't want to fetch same landing page
    # HTML again and result in a loop
    responses.add(responses.GET,
        'https://web.archive.org/web/{}id_/{}'.format("20180326070330", TARGET + "/redirect"),
        status=200,
        headers={"X-Archive-Src": "liveweb-whatever.warc.gz"},
        body="<html></html>")

    resp = ingest_worker.process(request)

    print(resp)
    assert resp['hit'] == False
    assert resp['status'] == "no-pdf-link"
    assert resp['request'] == request
    assert 'terminal' in resp
    assert 'file_meta' not in resp
    assert 'cdx' not in resp
    assert 'revisit_cdx' not in resp
    assert 'grobid' not in resp

@responses.activate
def test_ingest_blocklist(ingest_worker):

    ingest_worker.base_url_blocklist = [
        '://test.fatcat.wiki/',
    ]
    request = {
        'ingest_type': 'pdf',
        'base_url': "https://test.fatcat.wiki/asdfasdf.pdf",
    }

    resp = ingest_worker.process(request)

    assert resp['hit'] == False
    assert resp['status'] == "skip-url-blocklist"
    assert resp['request'] == request


@responses.activate
def test_ingest_wall_blocklist(ingest_worker):

    ingest_worker.wall_blocklist = [
        '://test.fatcat.wiki/',
    ]
    request = {
        'ingest_type': 'pdf',
        'base_url': "https://test.fatcat.wiki/asdfasdf.pdf",
    }

    resp = ingest_worker.process(request)

    assert resp['hit'] == False
    assert resp['status'] == "skip-wall"
    assert resp['request'] == request