-
Notifications
You must be signed in to change notification settings - Fork 87
EncryptedDirectMessage class; simplify Event
#39
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
17 commits
Select commit
Hold shift + click to select a range
a146af3
Adds initial test framework; adds PrivateKey.from_nsec()
kdmukai 0c14cfc
Update setup.py
kdmukai 185477f
Merge remote-tracking branch 'upstream/main'
kdmukai 4d32774
Merge remote-tracking branch 'upstream/main'
kdmukai f7ed740
Interim commit
kdmukai babf120
Integrate `Event` and `RelayManager` w/validity checking
kdmukai 4f36e64
Merge branch 'relay_verify_check' into dm_class
kdmukai f926e7a
Update event.py
kdmukai eb165ce
Update key.py
kdmukai b99f930
Update key.py
kdmukai 606387b
Merge remote-tracking branch 'upstream/main' into dm_class
kdmukai d6ccdb5
tests; Key.sign inserts Event.public_key
kdmukai 72fee7e
Update test_key.py
kdmukai 6287207
Update test_event.py
kdmukai 9855ede
Merge remote-tracking branch 'upstream/main' into dm_class
kdmukai e0ea22f
Merge remote-tracking branch 'upstream/main' into dm_class
kdmukai 256c917
Improvements based on review
kdmukai 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
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 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 |
|---|---|---|
| @@ -1,14 +1,93 @@ | ||
| from nostr.event import Event | ||
| from nostr.key import PrivateKey | ||
| import pytest | ||
| import time | ||
| from nostr.event import Event, EncryptedDirectMessage | ||
| from nostr.key import PrivateKey | ||
|
|
||
|
|
||
|
|
||
| class TestEvent: | ||
| def test_event_default_time(self): | ||
| """ | ||
| ensure created_at default value reflects the time at Event object instantiation | ||
| see: https://github.com/jeffthibault/python-nostr/issues/23 | ||
| """ | ||
| event1 = Event(content='test event') | ||
| time.sleep(1.5) | ||
| event2 = Event(content='test event') | ||
| assert event1.created_at < event2.created_at | ||
|
|
||
|
|
||
| def test_content_only_instantiation(self): | ||
| """ should be able to create an Event by only specifying content without kwarg """ | ||
| event = Event("Hello, world!") | ||
| assert event.content is not None | ||
|
|
||
|
|
||
| def test_event_id_recomputes(self): | ||
| """ should recompute the Event.id to reflect the current Event attrs """ | ||
| event = Event(content="some event") | ||
|
|
||
| # id should be computed on the fly | ||
| event_id = event.id | ||
|
|
||
| event.created_at += 10 | ||
|
|
||
| # Recomputed id should now be different | ||
| assert event.id != event_id | ||
|
|
||
|
|
||
| def test_add_event_ref(self): | ||
| """ should add an 'e' tag for each event_ref added """ | ||
| some_event_id = "some_event_id" | ||
| event = Event(content="Adding an 'e' tag") | ||
| event.add_event_ref(some_event_id) | ||
| assert ['e', some_event_id] in event.tags | ||
|
|
||
|
|
||
| def test_add_pubkey_ref(self): | ||
| """ should add a 'p' tag for each pubkey_ref added """ | ||
| some_pubkey = "some_pubkey" | ||
| event = Event(content="Adding a 'p' tag") | ||
| event.add_pubkey_ref(some_pubkey) | ||
| assert ['p', some_pubkey] in event.tags | ||
|
|
||
|
|
||
|
|
||
| class TestEncryptedDirectMessage: | ||
| def setup_class(self): | ||
| self.sender_pk = PrivateKey() | ||
| self.sender_pubkey = self.sender_pk.public_key.hex() | ||
| self.recipient_pk = PrivateKey() | ||
| self.recipient_pubkey = self.recipient_pk.public_key.hex() | ||
|
|
||
|
|
||
| def test_content_field_moved_to_cleartext_content(self): | ||
| """ Should transfer `content` field data to `cleartext_content` """ | ||
| dm = EncryptedDirectMessage(content="My message!", recipient_pubkey=self.recipient_pubkey) | ||
| assert dm.content is None | ||
| assert dm.cleartext_content is not None | ||
|
|
||
|
|
||
| def test_nokwarg_content_allowed(self): | ||
| """ Should allow creating a new DM w/no `content` nor `cleartext_content` kwarg """ | ||
| dm = EncryptedDirectMessage("My message!", recipient_pubkey=self.recipient_pubkey) | ||
| assert dm.cleartext_content is not None | ||
|
|
||
|
|
||
| def test_recipient_p_tag(self): | ||
| """ Should generate recipient 'p' tag """ | ||
| dm = EncryptedDirectMessage(cleartext_content="Secret message!", recipient_pubkey=self.recipient_pubkey) | ||
| assert ['p', self.recipient_pubkey] in dm.tags | ||
|
|
||
|
|
||
| def test_unencrypted_dm_has_undefined_id(self): | ||
| """ Should raise Exception if `id` is requested before DM is encrypted """ | ||
| dm = EncryptedDirectMessage(cleartext_content="My message!", recipient_pubkey=self.recipient_pubkey) | ||
|
|
||
| with pytest.raises(Exception) as e: | ||
| dm.id | ||
| assert "undefined" in str(e) | ||
|
|
||
| def test_event_default_time(): | ||
| """ | ||
| ensure created_at default value reflects the time at Event object instantiation | ||
| see: https://github.com/jeffthibault/python-nostr/issues/23 | ||
| """ | ||
| public_key = PrivateKey().public_key.hex() | ||
| event1 = Event(public_key=public_key, content='test event') | ||
| time.sleep(1.5) | ||
| event2 = Event(public_key=public_key, content='test event') | ||
| assert event1.created_at < event2.created_at | ||
| # But once we encrypt it, we can request its id | ||
| self.sender_pk.encrypt_dm(dm) | ||
| assert dm.id is not None |
Oops, something went wrong.
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.