Skip to content

Commit

Permalink
Add tests for iframes option
Browse files Browse the repository at this point in the history
Why is this change needed?
--------------------------
Just checking to make sure one of the new options works as expected.

How does it address the issue?
------------------------------
Add tests for iframes option.

Any links to any relevant tickets, articles, or other resources?
---------------------------------------------------------------
https://3.basecamp.com/3093825/buckets/29007795/todos/6362751178

Did you complete all of the following?
--------------------------------------
- Run test suite?
- Add new tests?
- Consider security implications and practices?
  • Loading branch information
jason-o-matic committed Jul 19, 2023
1 parent c7efba8 commit 6fb6401
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion script/inside_container/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cd "$(dirname "$0")/../.."

cd test

apk add qpdf
apk add qpdf poppler-utils

pip install --upgrade ../

Expand Down
44 changes: 44 additions & 0 deletions test/iframes_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import docraptor
import os
import platform
import re

doc_api = docraptor.DocApi()
doc_api.api_client.configuration.username = 'YOUR_API_KEY_HERE'
# doc_api.api_client.configuration.debug = True

pdf_data = doc_api.create_doc({
"test": True,
"document_content": """
<html><body>
<p>Baseline text</p>
<iframe src="https://docraptor-test-harness.herokuapp.com/public/index.html"></iframe>
</body></html>
""",
"name": "python-sync.pdf",
"document_type": "pdf",
# "prince_options": {
# "iframes": True,
# },
})

file_path = "/app/tmp/test_output/" + os.path.basename(__file__) + "_python_" + platform.python_version() + ".pdf"

with open(file_path, "wb") as f:
f.write(pdf_data)

stream = os.popen("qpdf --check " + file_path + " 2>&1")
output = stream.read()
close_value = stream.close()
if close_value is not None:
with open(file_path, "rb") as f:
decoded_response = f.read().decode('cp437')
message = "Invalid PDF expected: %PDF-1.5 recieved:\n"
message += decoded_response[0:8]
message += "\n\nqpdf --check:\n" + output
raise ValueError(message)

stream = os.popen("pdftotext " + file_path + " -")
output = stream.read()
if re.search("Test", output) is None:
raise ValueError("Output should load iframe")
44 changes: 44 additions & 0 deletions test/iframes_false.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import docraptor
import os
import platform
import re

doc_api = docraptor.DocApi()
doc_api.api_client.configuration.username = 'YOUR_API_KEY_HERE'
# doc_api.api_client.configuration.debug = True

pdf_data = doc_api.create_doc({
"test": True,
"document_content": """
<html><body>
<p>Baseline text</p>
<iframe src="https://docraptor-test-harness.herokuapp.com/public/index.html"></iframe>
</body></html>
""",
"name": "python-sync.pdf",
"document_type": "pdf",
"prince_options": {
"iframes": False,
},
})

file_path = "/app/tmp/test_output/" + os.path.basename(__file__) + "_python_" + platform.python_version() + ".pdf"

with open(file_path, "wb") as f:
f.write(pdf_data)

stream = os.popen("qpdf --check " + file_path + " 2>&1")
output = stream.read()
close_value = stream.close()
if close_value is not None:
with open(file_path, "rb") as f:
decoded_response = f.read().decode('cp437')
message = "Invalid PDF expected: %PDF-1.5 recieved:\n"
message += decoded_response[0:8]
message += "\n\nqpdf --check:\n" + output
raise ValueError(message)

stream = os.popen("pdftotext " + file_path + " -")
output = stream.read()
if re.search("Test", output) is not None:
raise ValueError("Output shouldn't load iframe")
44 changes: 44 additions & 0 deletions test/iframes_true.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import docraptor
import os
import platform
import re

doc_api = docraptor.DocApi()
doc_api.api_client.configuration.username = 'YOUR_API_KEY_HERE'
# doc_api.api_client.configuration.debug = True

pdf_data = doc_api.create_doc({
"test": True,
"document_content": """
<html><body>
<p>Baseline text</p>
<iframe src="https://docraptor-test-harness.herokuapp.com/public/index.html"></iframe>
</body></html>
""",
"name": "python-sync.pdf",
"document_type": "pdf",
"prince_options": {
"iframes": True,
},
})

file_path = "/app/tmp/test_output/" + os.path.basename(__file__) + "_python_" + platform.python_version() + ".pdf"

with open(file_path, "wb") as f:
f.write(pdf_data)

stream = os.popen("qpdf --check " + file_path + " 2>&1")
output = stream.read()
close_value = stream.close()
if close_value is not None:
with open(file_path, "rb") as f:
decoded_response = f.read().decode('cp437')
message = "Invalid PDF expected: %PDF-1.5 recieved:\n"
message += decoded_response[0:8]
message += "\n\nqpdf --check:\n" + output
raise ValueError(message)

stream = os.popen("pdftotext " + file_path + " -")
output = stream.read()
if re.search("Test", output) is None:
raise ValueError("Output should load iframe")

0 comments on commit 6fb6401

Please sign in to comment.