summaryrefslogtreecommitdiff
path: root/service/test/unit/resources/test_backup_account_resource.py
blob: e16fa0e14eb6b1aec7358efdbdf0d04639a86540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Copyright (c) 2017 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# 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 mock import MagicMock, patch
from twisted.trial import unittest
from twisted.web.test.requesthelper import DummyRequest
from twisted.internet import defer

from pixelated.resources.backup_account_resource import BackupAccountResource
from test.unit.resources import DummySite


class TestBackupAccountResource(unittest.TestCase):
    def setUp(self):
        self.services_factory = MagicMock()
        self.authenticator = MagicMock()
        self.leap_provider = MagicMock()
        self.leap_provider.server_name = 'test.com'
        self.resource = BackupAccountResource(self.services_factory, self.authenticator, self.leap_provider)
        self.web = DummySite(self.resource)

    def test_get(self):
        request = DummyRequest(['/backup-account'])
        request.method = 'GET'
        d = self.web.get(request)

        def assert_200_when_user_logged_in(_):
            self.assertEqual(200, request.responseCode)
            self.assertIn("DOCTYPE html", request.written[0])

        d.addCallback(assert_200_when_user_logged_in)
        return d

    @patch('pixelated.resources.backup_account_resource.parse_accept_language')
    @patch('pixelated.resources.backup_account_resource.AccountRecovery')
    def test_post_updates_recovery_code(self, mock_account_recovery_init, mock_language):
        mock_language.return_value = 'pt-BR'
        mock_account_recovery = MagicMock()
        mock_account_recovery_init.return_value = mock_account_recovery
        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._service(request, '_leap_session').smtp_config,
                self.resource._get_backup_email(request),
                self.leap_provider.server_name,
                language='pt-BR')
            mock_account_recovery.update_recovery_code.assert_called()

        d.addCallback(assert_update_recovery_code_called)
        return d

    @patch('pixelated.resources.backup_account_resource.AccountRecovery.update_recovery_code')
    def test_post_returns_successfully(self, mock_update_recovery_code):
        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(_):
            self.assertEqual(204, request.responseCode)

        d.addCallback(assert_successful_response)
        return d

    @patch('pixelated.resources.backup_account_resource.AccountRecovery.update_recovery_code')
    def test_post_returns_internal_server_error(self, mock_update_recovery_code):
        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(_):
            self.assertEqual(500, request.responseCode)

        d.addCallback(assert_successful_response)
        return d

    def test_get_backup_email_from_request(self):
        request = MagicMock()
        request.content.getvalue.return_value = '{"backupEmail": "test@test.com"}'

        self.assertEqual(self.resource._get_backup_email(request), 'test@test.com')