Skip to content

gh-103186: assert in tests that UnsafeMailcapInput warnings are provided - #103217

Merged
iritkatriel merged 9 commits into
python:mainfrom
TabLand:fix-issue-103186-test_mailcap
Apr 6, 2023
Merged

gh-103186: assert in tests that UnsafeMailcapInput warnings are provided#103217
iritkatriel merged 9 commits into
python:mainfrom
TabLand:fix-issue-103186-test_mailcap

Conversation

@TabLand

@TabLandTabLand commented Apr 3, 2023

Copy link
Copy Markdown
Contributor

These warnings are expected as the function result being asserted is None

@bedevere-bot

This comment was marked as outdated.

Comment threadLib/test/test_mailcap.py Outdated
with warnings_helper.check_warnings(('', tc[2]["warn_type"]), quiet=True):
self.assertEqual(mailcap.subst(*tc[0]), tc[1])
else:
self.assertEqual(mailcap.subst(*tc[0]), tc[1])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit over-engineered. I'd split this into a separate test function, which tests that the warning is issued in cases that deserve the warning. This exception is raised from 3 places in the Lib/mailcap.py, so shouldn't we have at least 3 test cases for it?

CC @encukou .

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Irit,

Thank you, I'll rework the affected cases into a separate test function and update the patch.
Will add 2x new test cases for unsafe filename and parameters, only the MIME type warnings are raised at present

Comment threadLib/test/test_mailcap.py Outdated
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to substitute MIME type.*into a shell'):
self.assertEqual(mailcap.subst("echo %t", "audio/*", "foo.txt"), None)

with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to use mailcap with filename.*Use a safe temporary filename.'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP 8: wrap lines at 80 chars, and have whitespace between the , and the next function arg.

Suggested change
withself.assertWarnsRegex(mailcap.UnsafeMailcapInput,'Refusing to use mailcap with filename.*Use a safe temporary filename.'):
withself.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to use mailcap with filename.*Use a safe temporary filename.'):

Comment threadLib/test/test_mailcap.py Outdated
self._run_cases(cases)

def test_unsafe_mailcap_input(self):
c = MAILCAPDICT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c=MAILCAPDICT
c=MAILCAPDICT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only used in one place, you could move it there.

Comment threadLib/test/test_mailcap.py Outdated
Comment threadLib/test/test_mailcap.py Outdated
Comment on lines +257 to +258
self.assertEqual(mailcap.findmatch(MAILCAPDICT,
"audio/wav", filename="foo*.txt"), (None, None))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to break the first arg into two lines and then continue with the second arg on the same line.

You can assign the return value of findmatch to a variable and then compare the variable to expected value.
Otherwise, add a newline before the (None, None) and indent it like "mailcap.findmatch".

Comment threadLib/test/test_mailcap.py Outdated

def test_unsafe_mailcap_input(self):
c = MAILCAPDICT
plist = ["total=*"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also only used in one place.

TabLandand others added 2 commits April 6, 2023 21:40
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Comment threadLib/test/test_mailcap.py Outdated
self.assertEqual(mailcap.subst("echo %t", "audio/*", "foo.txt"), None)

with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to use mailcap with filename.*Use a safe temporary filename.'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Refusing to use mailcap with filename.*Use a safe temporary filename.'):
'Refusing to use mailcap with filename.*'
'Use a safe temporary filename.'):

Comment threadLib/test/test_mailcap.py Outdated
plist = ["total=*"]
with self.assertWarnsRegex(mailcap.UnsafeMailcapInput,
'Refusing to substitute parameter.*into a shell command'):
self.assertEqual(mailcap.subst("echo %{total}", "audio/wav", "foo.txt", plist), None)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also > 80 chars

@iritkatrieliritkatriel changed the title gh-103186 Suppress UnsafeMailcapInput warningsgh-103186: assert in tests that UnsafeMailcapInput warnings are providedApr 6, 2023
@iritkatriel
iritkatriel merged commit 1724553 into python:mainApr 6, 2023
@iritkatriel

Copy link
Copy Markdown
Member

Thank you @TabLand, congrats on your first commit to cpython!

@TabLand

Copy link
Copy Markdown
ContributorAuthor

First of hopefully lots of more in the future!
Many thanks!

warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@serhiy-storchakaserhiy-storchaka added the needs backport to 3.11 only security fixes label Sep 2, 2023
@miss-islington

Copy link
Copy Markdown
Contributor

Thanks @TabLand for the PR, and @iritkatriel for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

@bedevere-bot

Copy link
Copy Markdown

GH-108800 is a backport of this pull request to the 3.11 branch.

@bedevere-botbedevere-bot removed the needs backport to 3.11 only security fixes label Sep 2, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 2, 2023
… provided (pythonGH-103217)
(cherry picked from commit 1724553)
Co-authored-by: Ijtaba Hussain <ijtabahussain@live.com>
serhiy-storchaka pushed a commit that referenced this pull request Sep 2, 2023
…e provided (GH-103217) (GH-108800)
(cherry picked from commit 1724553)
Co-authored-by: Ijtaba Hussain <ijtabahussain@live.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip newstestsTests in the Lib/test dirtopic-email

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@TabLand@bedevere-bot@iritkatriel@miss-islington@serhiy-storchaka@arhadthedev