File: test_notify_security_update_totp.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (60 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download
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
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from datetime import datetime, timedelta

from odoo.addons.auth_totp.controllers.home import TRUSTED_DEVICE_AGE
from odoo.addons.mail.tests.test_res_users import TestNotifySecurityUpdate
from odoo.tests import users


class TestNotifySecurityUpdateTotp(TestNotifySecurityUpdate):
    @users('employee')
    def test_security_update_totp_enabled_disabled(self):
        recipients = [self.env.user.email_formatted]
        with self.mock_mail_gateway():
            self.env.user.write({'totp_secret': 'test'})

        self.assertMailMailWEmails(recipients, 'outgoing', fields_values={
            'subject': 'Security Update: 2FA Activated',
        })

        with self.mock_mail_gateway():
            self.env.user.write({'totp_secret': False})

        self.assertMailMailWEmails(recipients, 'outgoing', fields_values={
            'subject': 'Security Update: 2FA Deactivated',
        })

    @users('employee')
    def test_security_update_trusted_device_added_removed(self):
        """ Make sure we notify the user when TOTP trusted devices are added/removed on his account. """
        recipients = [self.env.user.email_formatted]
        with self.mock_mail_gateway():
            self.env['auth_totp.device'].sudo()._generate(
                'trusted_device_chrome',
                'Chrome on Windows',
                datetime.now() + timedelta(seconds=TRUSTED_DEVICE_AGE)
            )

        self.assertMailMailWEmails(recipients, 'outgoing', fields_values={
            'subject': 'Security Update: Device Added',
        })

        # generating a key outside of the 'auth_totp.device' model should however not notify
        with self.mock_mail_gateway():
            self.env['res.users.apikeys']._generate(
                'new_api_key',
                'New Key',
                datetime.now() + timedelta(days=1)
            )
        self.assertNotSentEmail(recipients)

        # now remove the key using the user's relationship
        with self.mock_mail_gateway():
            self.env['auth_totp.device'].flush_model()
            self.env.user.sudo(False)._revoke_all_devices()

        self.assertMailMailWEmails(recipients, 'outgoing', fields_values={
            'subject': 'Security Update: Device Removed',
        })