Refactor unlock squeak method of squeak store (#1907)

This commit is contained in:
Jonathan Zernik 2021-12-27 19:28:14 -08:00 committed by GitHub
parent da457825a8
commit e827db2ef7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 15 deletions

View file

@ -137,15 +137,9 @@ class NetworkHandler:
return False
def unlock_squeak(self, squeak_hash: bytes, secret_key: bytes):
squeak = self.squeak_store.get_squeak(squeak_hash)
decrypted_content = self.squeak_core.get_decrypted_content(
squeak,
secret_key,
)
self.squeak_store.unlock_squeak(
squeak_hash,
secret_key,
decrypted_content,
)
def get_secret_key_reply(self, squeak_hash: bytes, peer_address: PeerAddress) -> Optional[SecretKeyReply]:

View file

@ -97,7 +97,6 @@ class SqueakController:
self.squeak_store.unlock_squeak(
inserted_squeak_hash,
secret_key,
content_str,
)
return inserted_squeak_hash
@ -115,15 +114,9 @@ class SqueakController:
self.squeak_store.mark_received_offer_paid(
sent_payment.payment_hash,
)
squeak = self.squeak_store.get_squeak(received_offer.squeak_hash)
decrypted_content = self.squeak_core.get_decrypted_content(
squeak,
sent_payment.secret_key,
)
self.squeak_store.unlock_squeak(
received_offer.squeak_hash,
sent_payment.secret_key,
decrypted_content,
)
return sent_payment_id

View file

@ -26,6 +26,7 @@ from typing import List
from typing import Optional
from squeak.core import CheckSqueak
from squeak.core import CheckSqueakSecretKey
from squeak.core import CSqueak
from squeak.core.keys import SqueakPrivateKey
from squeak.core.keys import SqueakPublicKey
@ -107,8 +108,13 @@ class SqueakStore:
self.new_squeak_listener.handle_new_item(squeak)
return inserted_squeak_hash
def unlock_squeak(self, squeak_hash: bytes, secret_key: bytes, decrypted_content: str):
def unlock_squeak(self, squeak_hash: bytes, secret_key: bytes):
squeak = self.squeak_db.get_squeak(squeak_hash)
CheckSqueakSecretKey(squeak, secret_key)
decrypted_content = self.squeak_core.get_decrypted_content(
squeak,
secret_key,
)
self.squeak_db.set_squeak_decryption_key(
squeak_hash,
secret_key,

View file

@ -183,5 +183,4 @@ class TwitterForwarderTask:
self.squeak_store.unlock_squeak(
inserted_squeak_hash,
secret_key,
content_str,
)

View file

@ -151,6 +151,20 @@ def test_save_squeak_above_max_per_pubkey(squeak_store, squeak_db, squeak_core,
assert mock_handle_new_squeak.call_count == 0
def test_unlock_squeak(squeak_store, squeak_db, squeak_core, squeak, squeak_hash, secret_key, squeak_content):
with mock.patch.object(squeak_db, 'get_squeak', autospec=True) as mock_get_squeak, \
mock.patch.object(squeak_db, 'set_squeak_decryption_key', autospec=True) as mock_set_squeak_decryption_key, \
mock.patch.object(squeak_store.new_secret_key_listener, 'handle_new_item', autospec=True) as mock_handle_new_secret_key, \
mock.patch.object(squeak_core, 'get_decrypted_content', autospec=True) as mock_get_decrypted_content:
mock_get_squeak.return_value = squeak
mock_get_decrypted_content.return_value = squeak_content
squeak_store.unlock_squeak(squeak_hash, secret_key)
mock_set_squeak_decryption_key.assert_called_once_with(
squeak_hash, secret_key, squeak_content)
mock_handle_new_secret_key.assert_called_once_with(squeak)
# @pytest.fixture
# def unlocked_squeak(squeak_store, saved_squeak, secret_key, squeak_content):
# saved_squeak_hash = get_hash(saved_squeak)