-
Notifications
You must be signed in to change notification settings - Fork 2
Fix email validation, add tests #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
553e047
Refactor the jsonfield doctest to a simpler unit test.
mauritsvanrees 78ce5be
Add basic tests for the email field.
mauritsvanrees eb8db65
Fix email validation.
mauritsvanrees 0818901
Update classifiers.
mauritsvanrees 32c1086
fix typo in comment
mauritsvanrees 9627aad
Better check of domain part.
mauritsvanrees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Fix email validation: | ||
| * allow apostrophes | ||
| * allow accented characters | ||
| * allow ampersand in the user part | ||
| * do not allows spaces. | ||
| * accept TLDs with more than 4 characters | ||
| [maurits] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Refactor the ``jsonfield`` doctest to a simpler unit test. | ||
| [maurits] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add basic tests for the email field. | ||
| [maurits] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import unittest | ||
|
|
||
|
|
||
| class TestEmail(unittest.TestCase): | ||
|
|
||
| def test_fromUnicode(self): | ||
| from plone.schema.email import Email | ||
| from plone.schema.email import InvalidEmail | ||
|
|
||
| # Value must be string | ||
| self.assertEqual( | ||
| Email().fromUnicode("arthur@example.org"), | ||
| "arthur@example.org", | ||
| ) | ||
|
|
||
| # We strip spaces. | ||
| self.assertEqual( | ||
| Email().fromUnicode(" arthur@example.org "), | ||
| "arthur@example.org", | ||
| ) | ||
|
|
||
| # We do some validation | ||
| with self.assertRaises(InvalidEmail): | ||
| Email().fromUnicode("arthur") | ||
| with self.assertRaises(InvalidEmail): | ||
| Email().fromUnicode("arthur@") | ||
| with self.assertRaises(InvalidEmail): | ||
| Email().fromUnicode("arthur@one") | ||
|
mauritsvanrees marked this conversation as resolved.
|
||
| with self.assertRaises(InvalidEmail): | ||
| Email().fromUnicode("@one.two") | ||
|
|
||
| def test_fromBytes(self): | ||
| from plone.schema.email import Email | ||
| from plone.schema.email import InvalidEmail | ||
|
|
||
| # Value must be bytes | ||
| self.assertEqual( | ||
| Email().fromBytes(b"arthur@example.org"), | ||
| "arthur@example.org", | ||
| ) | ||
|
|
||
| # We strip spaces. | ||
| self.assertEqual( | ||
| Email().fromBytes(b" arthur@example.org "), | ||
| "arthur@example.org", | ||
| ) | ||
|
|
||
| # We do some validation | ||
| with self.assertRaises(InvalidEmail): | ||
| Email().fromBytes(b"arthur@one") | ||
|
|
||
| def test_validation(self): | ||
| # Let's test the email validation directly, without the field. | ||
| from plone.schema.email import _isemail | ||
|
|
||
| # Some good: | ||
| self.assertTrue(_isemail("arthur@example.org")) | ||
|
|
||
| # Some bad: | ||
| self.assertFalse(_isemail("")) | ||
| self.assertFalse(_isemail(" ")) | ||
| self.assertFalse(_isemail(" arthur@example.org")) | ||
| self.assertFalse(_isemail("arthur@example.org\n")) | ||
| self.assertFalse(_isemail("arthur\t@example.org")) | ||
| self.assertFalse(_isemail("arthur@one")) | ||
|
mauritsvanrees marked this conversation as resolved.
|
||
| self.assertFalse(_isemail("arthur@example@org")) | ||
| self.assertFalse(_isemail("me@.me")) | ||
| self.assertFalse(_isemail("x" * 254 + "@too.long")) | ||
|
|
||
| # Explicitly test some examples from the docstring, | ||
| # reported in the issue tracker. | ||
|
|
||
| # 1. allow apostrophes | ||
| self.assertTrue(_isemail("o'hara@example.org")) | ||
|
|
||
| # 2. allow accented characters | ||
| self.assertTrue(_isemail("jens@österreich.example.com")) | ||
|
|
||
| # 3. allow ampersand in the user part | ||
| self.assertTrue(_isemail("q&a@example.com")) | ||
|
|
||
| # 4. do not allows spaces. | ||
| self.assertFalse(_isemail("test@aha.ok hello")) | ||
|
|
||
| # 5. accept TLDs with more than 4 characters | ||
| self.assertTrue(_isemail("me@you.online")) | ||
| self.assertTrue(_isemail("me@example.museum")) | ||
|
mauritsvanrees marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import unittest | ||
|
|
||
|
|
||
| class TestJsonField(unittest.TestCase): | ||
|
|
||
| def test_fromUnicode(self): | ||
| from plone.schema.jsonfield import JSONField | ||
|
|
||
| # Value can be a valid JSON object: | ||
| self.assertDictEqual( | ||
| JSONField().fromUnicode('{"items": []}'), | ||
| {"items": []}, | ||
| ) | ||
|
|
||
| # or it can be a Python dict stored as string: | ||
| self.assertDictEqual(JSONField().fromUnicode("{'items': []}"), {"items": []}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.