diff options
Diffstat (limited to 'service/test')
-rw-r--r-- | service/test/unit/resources/test_backup_account_resource.py | 10 | ||||
-rw-r--r-- | service/test/unit/resources/test_base_resource.py | 8 | ||||
-rw-r--r-- | service/test/unit/test_account_recovery.py | 53 |
3 files changed, 59 insertions, 12 deletions
diff --git a/service/test/unit/resources/test_backup_account_resource.py b/service/test/unit/resources/test_backup_account_resource.py index 2b68dd1b..b8654203 100644 --- a/service/test/unit/resources/test_backup_account_resource.py +++ b/service/test/unit/resources/test_backup_account_resource.py @@ -50,12 +50,16 @@ class TestBackupAccountResource(unittest.TestCase): mock_account_recovery.update_recovery_code.return_value = defer.succeed("Success") request = DummyRequest(['/backup-account']) request.method = 'POST' + request.content = MagicMock() + request.content.getvalue.return_value = '{"email": "test@test.com"}' d = self.web.get(request) def assert_update_recovery_code_called(_): mock_account_recovery_init.assert_called_with( self.resource._authenticator.bonafide_session, - self.resource.soledad(request)) + self.resource.soledad(request), + self.resource._service(request, '_leap_session').smtp_config, + self.resource.get_backup_email(request)) mock_account_recovery.update_recovery_code.assert_called() d.addCallback(assert_update_recovery_code_called) @@ -66,6 +70,8 @@ class TestBackupAccountResource(unittest.TestCase): mock_update_recovery_code.return_value = defer.succeed("Success") request = DummyRequest(['/backup-account']) request.method = 'POST' + request.content = MagicMock() + request.content.getvalue.return_value = '{"email": "test@test.com"}' d = self.web.get(request) def assert_successful_response(_): @@ -79,6 +85,8 @@ class TestBackupAccountResource(unittest.TestCase): mock_update_recovery_code.return_value = defer.fail(Exception) request = DummyRequest(['/backup-account']) request.method = 'POST' + request.content = MagicMock() + request.content.getvalue.return_value = '{"email": "test@test.com"}' d = self.web.get(request) def assert_successful_response(_): diff --git a/service/test/unit/resources/test_base_resource.py b/service/test/unit/resources/test_base_resource.py index 35ae67c7..d7110917 100644 --- a/service/test/unit/resources/test_base_resource.py +++ b/service/test/unit/resources/test_base_resource.py @@ -25,3 +25,11 @@ class TestBaseResource(unittest.TestCase): base_resource = BaseResource(mock_services_factory) self.assertEqual(base_resource.soledad('request'), mock_services_factory.services()._leap_session.soledad) + + def test_get_backup_email_from_request(self): + mock_services_factory = MagicMock() + request = MagicMock() + request.content.getvalue.return_value = '{"backupEmail": "test@test.com"}' + base_resource = BaseResource(mock_services_factory) + + self.assertEqual(base_resource.get_backup_email(request), 'test@test.com') diff --git a/service/test/unit/test_account_recovery.py b/service/test/unit/test_account_recovery.py index af14814a..f113169a 100644 --- a/service/test/unit/test_account_recovery.py +++ b/service/test/unit/test_account_recovery.py @@ -13,24 +13,55 @@ # # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. +from email.mime.text import MIMEText -from twisted.internet.defer import inlineCallbacks +from twisted.internet import defer from twisted.trial import unittest +from twisted.mail import smtp -from mock import patch, Mock +from mock import patch, Mock, MagicMock +from mockito import mock, when, any as ANY from pixelated.account_recovery import AccountRecovery class AccountRecoveryTest(unittest.TestCase): + def setUp(self): + self.generated_code = '4645a2f8997e5d0d' + self.mock_bonafide_session = Mock() + self.mock_soledad = Mock() + self.mock_smtp_config = Mock() + self.keymanager = Mock() + self.mock_smtp_config.remote_smtp_host = 'test.com' + self.mock_soledad.create_recovery_code.return_value = self.generated_code + self.backup_email = 'test@test.com' + self.account_recovery = AccountRecovery( + self.mock_bonafide_session, + self.mock_soledad, + self.mock_smtp_config, + self.backup_email) + self.mock_smtp = Mock() - @inlineCallbacks + @defer.inlineCallbacks def test_update_recovery_code(self): - generated_code = '4645a2f8997e5d0d' - mock_bonafide_session = Mock() - mock_soledad = Mock() - mock_soledad.create_recovery_code.return_value = generated_code - account_recovery = AccountRecovery(mock_bonafide_session, mock_soledad) - - yield account_recovery.update_recovery_code() - mock_bonafide_session.update_recovery_code.assert_called_once_with(generated_code) + when(self.account_recovery)._send_mail(ANY).thenReturn(defer.succeed(None)) + response = yield self.account_recovery.update_recovery_code() + self.mock_bonafide_session.update_recovery_code.assert_called_once_with(self.generated_code) + + @defer.inlineCallbacks + def test_send_recovery_code_by_email(self): + msg = MIMEText('Your code %s' % self.generated_code) + msg['Subject'] = 'Recovery Code' + msg['From'] = 'team@pixelated-project.org' + msg['To'] = self.backup_email + + result = MagicMock() + deferred_sendmail = defer.succeed(result) + with patch.object(smtp, 'sendmail', return_value=deferred_sendmail) as mock_sendmail: + response = yield self.account_recovery._send_mail(self.generated_code, self.backup_email) + + mock_sendmail.assert_called_with( + 'test.com', + 'team@pixelated-project.org', + [self.backup_email], + msg.as_string()) |