@ -3,6 +3,8 @@ import unittest
from datetime import datetime
from datetime import datetime
from unittest . mock import Mock , patch
from unittest . mock import Mock , patch
import pytest
from models . account import Account , AccountMFASettings
from models . account import Account , AccountMFASettings
from services . mfa_service import MFAService
from services . mfa_service import MFAService
@ -25,18 +27,18 @@ class TestMFAService(unittest.TestCase):
def test_generate_secret ( self ) :
def test_generate_secret ( self ) :
""" Test secret generation. """
""" Test secret generation. """
secret = MFAService . generate_secret ( )
secret = MFAService . generate_secret ( )
self . assertIsI nstance( secret , str )
assert isi nstance( secret , str )
self . assertEqual ( len ( secret ) , 32 ) # Base32 length
assert len ( secret ) == 32 # Base32 length
def test_generate_backup_codes ( self ) :
def test_generate_backup_codes ( self ) :
""" Test backup codes generation. """
""" Test backup codes generation. """
codes = MFAService . generate_backup_codes ( )
codes = MFAService . generate_backup_codes ( )
self . assertEqual ( len ( codes ) , 8 )
assert len ( codes ) == 8
for code in codes :
for code in codes :
self . assertIsI nstance( code , str )
assert isi nstance( code , str )
self . assertEqual ( len ( code ) , 8 ) # 4 hex bytes = 8 chars
assert len ( code ) == 8 # 4 hex bytes = 8 chars
@patch ( ' pyotp.TOTP ' )
@patch ( " pyotp.TOTP " )
def test_verify_totp_valid ( self , mock_totp_class ) :
def test_verify_totp_valid ( self , mock_totp_class ) :
""" Test TOTP verification with valid token. """
""" Test TOTP verification with valid token. """
mock_totp = Mock ( )
mock_totp = Mock ( )
@ -45,10 +47,10 @@ class TestMFAService(unittest.TestCase):
result = MFAService . verify_totp ( " test_secret " , " 123456 " )
result = MFAService . verify_totp ( " test_secret " , " 123456 " )
self . assertTrue ( result )
assert result
mock_totp . verify . assert_called_once_with ( " 123456 " , valid_window = 1 )
mock_totp . verify . assert_called_once_with ( " 123456 " , valid_window = 1 )
@patch ( ' pyotp.TOTP ' )
@patch ( " pyotp.TOTP " )
def test_verify_totp_invalid ( self , mock_totp_class ) :
def test_verify_totp_invalid ( self , mock_totp_class ) :
""" Test TOTP verification with invalid token. """
""" Test TOTP verification with invalid token. """
mock_totp = Mock ( )
mock_totp = Mock ( )
@ -57,24 +59,24 @@ class TestMFAService(unittest.TestCase):
result = MFAService . verify_totp ( " test_secret " , " invalid " )
result = MFAService . verify_totp ( " test_secret " , " invalid " )
self . assertFalse ( result )
assert not result
def test_verify_totp_no_secret ( self ) :
def test_verify_totp_no_secret ( self ) :
""" Test TOTP verification with no secret. """
""" Test TOTP verification with no secret. """
result = MFAService . verify_totp ( None , " 123456 " )
result = MFAService . verify_totp ( None , " 123456 " )
self . assertFalse ( result )
assert not result
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_get_or_create_mfa_settings_existing ( self , mock_session ) :
def test_get_or_create_mfa_settings_existing ( self , mock_session ) :
""" Test getting existing MFA settings. """
""" Test getting existing MFA settings. """
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
result = MFAService . get_or_create_mfa_settings ( self . account )
result = MFAService . get_or_create_mfa_settings ( self . account )
self . assertEqual ( result , self . mfa_settings )
assert result == self . mfa_settings
mock_session . query . assert_called_once ( )
mock_session . query . assert_called_once ( )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_get_or_create_mfa_settings_new ( self , mock_session ) :
def test_get_or_create_mfa_settings_new ( self , mock_session ) :
""" Test creating new MFA settings. """
""" Test creating new MFA settings. """
mock_session . query . return_value . filter_by . return_value . first . return_value = None
mock_session . query . return_value . filter_by . return_value . first . return_value = None
@ -82,23 +84,23 @@ class TestMFAService(unittest.TestCase):
result = MFAService . get_or_create_mfa_settings ( self . account )
result = MFAService . get_or_create_mfa_settings ( self . account )
# Check that new settings were created
# Check that new settings were created
self . assertIsI nstance( result , AccountMFASettings )
assert isi nstance( result , AccountMFASettings )
self . assertEqual ( result . account_id , self . account . id )
assert result . account_id == self . account . id
mock_session . add . assert_called_once ( )
mock_session . add . assert_called_once ( )
mock_session . commit . assert_called_once ( )
mock_session . commit . assert_called_once ( )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_verify_backup_code_valid ( self , mock_session ) :
def test_verify_backup_code_valid ( self , mock_session ) :
""" Test backup code verification with valid code. """
""" Test backup code verification with valid code. """
self . mfa_settings . backup_codes = json . dumps ( [ " ABCD1234 " , " EFGH5678 " ] )
self . mfa_settings . backup_codes = json . dumps ( [ " ABCD1234 " , " EFGH5678 " ] )
result = MFAService . verify_backup_code ( self . mfa_settings , " abcd1234 " ) # Test case insensitive
result = MFAService . verify_backup_code ( self . mfa_settings , " abcd1234 " ) # Test case insensitive
self . assertTrue ( result )
assert result
# Check that the code was removed
# Check that the code was removed
remaining_codes = json . loads ( self . mfa_settings . backup_codes )
remaining_codes = json . loads ( self . mfa_settings . backup_codes )
self . assertNotIn ( " ABCD1234 " , remaining_codes )
assert " ABCD1234 " not in remaining_codes
self . assertIn ( " EFGH5678 " , remaining_codes )
assert " EFGH5678 " in remaining_codes
mock_session . commit . assert_called_once ( )
mock_session . commit . assert_called_once ( )
def test_verify_backup_code_invalid ( self ) :
def test_verify_backup_code_invalid ( self ) :
@ -107,7 +109,7 @@ class TestMFAService(unittest.TestCase):
result = MFAService . verify_backup_code ( self . mfa_settings , " INVALID " )
result = MFAService . verify_backup_code ( self . mfa_settings , " INVALID " )
self . assertFalse ( result )
assert not result
def test_verify_backup_code_no_codes ( self ) :
def test_verify_backup_code_no_codes ( self ) :
""" Test backup code verification with no backup codes. """
""" Test backup code verification with no backup codes. """
@ -115,12 +117,12 @@ class TestMFAService(unittest.TestCase):
result = MFAService . verify_backup_code ( self . mfa_settings , " ABCD1234 " )
result = MFAService . verify_backup_code ( self . mfa_settings , " ABCD1234 " )
self . assertFalse ( result )
assert not result
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
@patch ( ' services.mfa_service.MFAService.verify_totp ' )
@patch ( " services.mfa_service.MFAService.verify_totp " )
@patch ( ' services.mfa_service.MFAService.generate_backup_codes ' )
@patch ( " services.mfa_service.MFAService.generate_backup_codes " )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_setup_mfa_success ( self , mock_session , mock_gen_codes , mock_verify , mock_get_settings ) :
def test_setup_mfa_success ( self , mock_session , mock_gen_codes , mock_verify , mock_get_settings ) :
""" Test successful MFA setup. """
""" Test successful MFA setup. """
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
@ -130,46 +132,46 @@ class TestMFAService(unittest.TestCase):
result = MFAService . setup_mfa ( self . account , " 123456 " )
result = MFAService . setup_mfa ( self . account , " 123456 " )
self . assertTrue ( self . mfa_settings . enabled )
assert self . mfa_settings . enabled
self . assertEqual ( self . mfa_settings . backup_codes , json . dumps ( [ " CODE1 " , " CODE2 " ] ) )
assert self . mfa_settings . backup_codes == json . dumps ( [ " CODE1 " , " CODE2 " ] )
self . assertIsNotNone ( self . mfa_settings . setup_at )
assert self . mfa_settings . setup_at is not None
self . assertEqual ( result [ " backup_codes " ] , [ " CODE1 " , " CODE2 " ] )
assert result [ " backup_codes " ] == [ " CODE1 " , " CODE2 " ]
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
def test_setup_mfa_already_enabled ( self , mock_get_settings ) :
def test_setup_mfa_already_enabled ( self , mock_get_settings ) :
""" Test MFA setup when already enabled. """
""" Test MFA setup when already enabled. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
with self . assertR aises( ValueError ) as context :
with pytest . r aises( ValueError ) as context :
MFAService . setup_mfa ( self . account , " 123456 " )
MFAService . setup_mfa ( self . account , " 123456 " )
self . assertIn ( " already enabled " , str ( context . exception ) )
assert " already enabled " in str ( context . value )
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
def test_setup_mfa_no_secret ( self , mock_get_settings ) :
def test_setup_mfa_no_secret ( self , mock_get_settings ) :
""" Test MFA setup without secret. """
""" Test MFA setup without secret. """
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
with self . assertR aises( ValueError ) as context :
with pytest . r aises( ValueError ) as context :
MFAService . setup_mfa ( self . account , " 123456 " )
MFAService . setup_mfa ( self . account , " 123456 " )
self . assertIn ( " secret not generated " , str ( context . exception ) )
assert " secret not generated " in str ( context . value )
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
@patch ( ' services.mfa_service.MFAService.verify_totp ' )
@patch ( " services.mfa_service.MFAService.verify_totp " )
def test_setup_mfa_invalid_token ( self , mock_verify , mock_get_settings ) :
def test_setup_mfa_invalid_token ( self , mock_verify , mock_get_settings ) :
""" Test MFA setup with invalid TOTP token. """
""" Test MFA setup with invalid TOTP token. """
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
self . mfa_settings . secret = " test_secret "
self . mfa_settings . secret = " test_secret "
mock_verify . return_value = False
mock_verify . return_value = False
with self . assertR aises( ValueError ) as context :
with pytest . r aises( ValueError ) as context :
MFAService . setup_mfa ( self . account , " invalid " )
MFAService . setup_mfa ( self . account , " invalid " )
self . assertIn ( " Invalid TOTP token " , str ( context . exception ) )
assert " Invalid TOTP token " in str ( context . value )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_is_mfa_required_enabled ( self , mock_session ) :
def test_is_mfa_required_enabled ( self , mock_session ) :
""" Test MFA requirement check when enabled. """
""" Test MFA requirement check when enabled. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
@ -178,29 +180,29 @@ class TestMFAService(unittest.TestCase):
result = MFAService . is_mfa_required ( self . account )
result = MFAService . is_mfa_required ( self . account )
self . assertTrue ( result )
assert result
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_is_mfa_required_disabled ( self , mock_session ) :
def test_is_mfa_required_disabled ( self , mock_session ) :
""" Test MFA requirement check when disabled. """
""" Test MFA requirement check when disabled. """
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
result = MFAService . is_mfa_required ( self . account )
result = MFAService . is_mfa_required ( self . account )
self . assertFalse ( result )
assert not result
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_is_mfa_required_no_settings ( self , mock_session ) :
def test_is_mfa_required_no_settings ( self , mock_session ) :
""" Test MFA requirement check with no settings. """
""" Test MFA requirement check with no settings. """
mock_session . query . return_value . filter_by . return_value . first . return_value = None
mock_session . query . return_value . filter_by . return_value . first . return_value = None
result = MFAService . is_mfa_required ( self . account )
result = MFAService . is_mfa_required ( self . account )
self . assertFalse ( result )
assert not result
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
@patch ( ' services.mfa_service.MFAService.verify_totp ' )
@patch ( " services.mfa_service.MFAService.verify_totp " )
@patch ( ' services.mfa_service.MFAService.verify_backup_code ' )
@patch ( " services.mfa_service.MFAService.verify_backup_code " )
def test_authenticate_with_mfa_totp_success ( self , mock_verify_backup , mock_verify_totp , mock_session ) :
def test_authenticate_with_mfa_totp_success ( self , mock_verify_backup , mock_verify_totp , mock_session ) :
""" Test MFA authentication with valid TOTP. """
""" Test MFA authentication with valid TOTP. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
@ -210,13 +212,13 @@ class TestMFAService(unittest.TestCase):
result = MFAService . authenticate_with_mfa ( self . account , " 123456 " )
result = MFAService . authenticate_with_mfa ( self . account , " 123456 " )
self . assertTrue ( result )
assert result
mock_verify_totp . assert_called_once_with ( " test_secret " , " 123456 " )
mock_verify_totp . assert_called_once_with ( " test_secret " , " 123456 " )
mock_verify_backup . assert_not_called ( )
mock_verify_backup . assert_not_called ( )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
@patch ( ' services.mfa_service.MFAService.verify_totp ' )
@patch ( " services.mfa_service.MFAService.verify_totp " )
@patch ( ' services.mfa_service.MFAService.verify_backup_code ' )
@patch ( " services.mfa_service.MFAService.verify_backup_code " )
def test_authenticate_with_mfa_backup_success ( self , mock_verify_backup , mock_verify_totp , mock_session ) :
def test_authenticate_with_mfa_backup_success ( self , mock_verify_backup , mock_verify_totp , mock_session ) :
""" Test MFA authentication with valid backup code. """
""" Test MFA authentication with valid backup code. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
@ -227,20 +229,20 @@ class TestMFAService(unittest.TestCase):
result = MFAService . authenticate_with_mfa ( self . account , " BACKUP123 " )
result = MFAService . authenticate_with_mfa ( self . account , " BACKUP123 " )
self . assertTrue ( result )
assert result
mock_verify_totp . assert_called_once_with ( " test_secret " , " BACKUP123 " )
mock_verify_totp . assert_called_once_with ( " test_secret " , " BACKUP123 " )
mock_verify_backup . assert_called_once_with ( self . mfa_settings , " BACKUP123 " )
mock_verify_backup . assert_called_once_with ( self . mfa_settings , " BACKUP123 " )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_authenticate_with_mfa_disabled ( self , mock_session ) :
def test_authenticate_with_mfa_disabled ( self , mock_session ) :
""" Test MFA authentication when disabled. """
""" Test MFA authentication when disabled. """
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
mock_session . query . return_value . filter_by . return_value . first . return_value = self . mfa_settings
result = MFAService . authenticate_with_mfa ( self . account , " 123456 " )
result = MFAService . authenticate_with_mfa ( self . account , " 123456 " )
self . assertTrue ( result )
assert result
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_get_mfa_status_enabled ( self , mock_session ) :
def test_get_mfa_status_enabled ( self , mock_session ) :
""" Test getting MFA status when enabled. """
""" Test getting MFA status when enabled. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
@ -250,29 +252,21 @@ class TestMFAService(unittest.TestCase):
result = MFAService . get_mfa_status ( self . account )
result = MFAService . get_mfa_status ( self . account )
expected = {
expected = { " enabled " : True , " setup_at " : " 2025-01-01T12:00:00 " , " has_backup_codes " : True }
" enabled " : True ,
assert result == expected
" setup_at " : " 2025-01-01T12:00:00 " ,
" has_backup_codes " : True
}
self . assertEqual ( result , expected )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_get_mfa_status_no_settings ( self , mock_session ) :
def test_get_mfa_status_no_settings ( self , mock_session ) :
""" Test getting MFA status with no settings. """
""" Test getting MFA status with no settings. """
mock_session . query . return_value . filter_by . return_value . first . return_value = None
mock_session . query . return_value . filter_by . return_value . first . return_value = None
result = MFAService . get_mfa_status ( self . account )
result = MFAService . get_mfa_status ( self . account )
expected = {
expected = { " enabled " : False , " setup_at " : None , " has_backup_codes " : False }
" enabled " : False ,
assert result == expected
" setup_at " : None ,
" has_backup_codes " : False
}
self . assertEqual ( result , expected )
@patch ( ' qrcode.QRCode ' )
@patch ( " qrcode.QRCode " )
@patch ( ' pyotp.TOTP ' )
@patch ( " pyotp.TOTP " )
def test_generate_qr_code ( self , mock_totp_class , mock_qr_class ) :
def test_generate_qr_code ( self , mock_totp_class , mock_qr_class ) :
""" Test QR code generation. """
""" Test QR code generation. """
# Mock TOTP
# Mock TOTP
@ -287,20 +281,16 @@ class TestMFAService(unittest.TestCase):
mock_qr_class . return_value = mock_qr
mock_qr_class . return_value = mock_qr
# Mock image buffer
# Mock image buffer
with patch ( ' io.BytesIO ' ) as mock_buffer , \
with patch ( " io.BytesIO " ) as mock_buffer , patch ( " base64.b64encode " ) as mock_b64 :
patch ( ' base64.b64encode ' ) as mock_b64 :
mock_b64 . return_value . decode . return_value = " base64data "
mock_b64 . return_value . decode . return_value = " base64data "
result = MFAService . generate_qr_code ( self . account , " test_secret " )
result = MFAService . generate_qr_code ( self . account , " test_secret " )
self . assertEqual ( result , " data:image/png;base64,base64data " )
assert result == " data:image/png;base64,base64data "
mock_totp . provisioning_uri . assert_called_once_with (
mock_totp . provisioning_uri . assert_called_once_with ( name = self . account . email , issuer_name = " Dify " )
name = self . account . email ,
issuer_name = " Dify "
)
@patch ( ' libs.password.compare_password ' )
@patch ( " libs.password.compare_password " )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_disable_mfa_success ( self , mock_session , mock_compare_password ) :
def test_disable_mfa_success ( self , mock_session , mock_compare_password ) :
""" Test successful MFA disable. """
""" Test successful MFA disable. """
mock_compare_password . return_value = True
mock_compare_password . return_value = True
@ -308,24 +298,24 @@ class TestMFAService(unittest.TestCase):
result = MFAService . disable_mfa ( self . account , " correct_password " )
result = MFAService . disable_mfa ( self . account , " correct_password " )
self . assertTrue ( result )
assert result
self . assertFalse ( self . mfa_settings . enabled )
assert not self . mfa_settings . enabled
self . assertIsNone ( self . mfa_settings . secret )
assert self . mfa_settings . secret is None
self . assertIsNone ( self . mfa_settings . backup_codes )
assert self . mfa_settings . backup_codes is None
self . assertIsNone ( self . mfa_settings . setup_at )
assert self . mfa_settings . setup_at is None
mock_session . commit . assert_called_once ( )
mock_session . commit . assert_called_once ( )
@patch ( ' libs.password.compare_password ' )
@patch ( " libs.password.compare_password " )
def test_disable_mfa_wrong_password ( self , mock_compare_password ) :
def test_disable_mfa_wrong_password ( self , mock_compare_password ) :
""" Test MFA disable with wrong password. """
""" Test MFA disable with wrong password. """
mock_compare_password . return_value = False
mock_compare_password . return_value = False
result = MFAService . disable_mfa ( self . account , " wrong_password " )
result = MFAService . disable_mfa ( self . account , " wrong_password " )
self . assertFalse ( result )
assert not result
@patch ( ' libs.password.compare_password ' )
@patch ( " libs.password.compare_password " )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_disable_mfa_no_settings ( self , mock_session , mock_compare_password ) :
def test_disable_mfa_no_settings ( self , mock_session , mock_compare_password ) :
""" Test MFA disable when no settings exist. """
""" Test MFA disable when no settings exist. """
mock_compare_password . return_value = True
mock_compare_password . return_value = True
@ -333,12 +323,12 @@ class TestMFAService(unittest.TestCase):
result = MFAService . disable_mfa ( self . account , " correct_password " )
result = MFAService . disable_mfa ( self . account , " correct_password " )
self . assertTrue ( result ) # Already disabled
assert result # Already disabled
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
@patch ( ' services.mfa_service.MFAService.generate_secret ' )
@patch ( " services.mfa_service.MFAService.generate_secret " )
@patch ( ' services.mfa_service.MFAService.generate_qr_code ' )
@patch ( " services.mfa_service.MFAService.generate_qr_code " )
@patch ( ' services.mfa_service.db.session ' )
@patch ( " services.mfa_service.db.session " )
def test_generate_mfa_setup_data_success ( self , mock_session , mock_gen_qr , mock_gen_secret , mock_get_settings ) :
def test_generate_mfa_setup_data_success ( self , mock_session , mock_gen_qr , mock_gen_secret , mock_get_settings ) :
""" Test successful MFA setup data generation. """
""" Test successful MFA setup data generation. """
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
@ -347,22 +337,22 @@ class TestMFAService(unittest.TestCase):
result = MFAService . generate_mfa_setup_data ( self . account )
result = MFAService . generate_mfa_setup_data ( self . account )
self . assertEqual ( result [ " secret " ] , " NEWSECRET123 " )
assert result [ " secret " ] == " NEWSECRET123 "
self . assertEqual ( result [ " qr_code " ] , " data:image/png;base64,qrdata " )
assert result [ " qr_code " ] == " data:image/png;base64,qrdata "
self . assertEqual ( self . mfa_settings . secret , " NEWSECRET123 " )
assert self . mfa_settings . secret == " NEWSECRET123 "
mock_session . commit . assert_called_once ( )
mock_session . commit . assert_called_once ( )
@patch ( ' services.mfa_service.MFAService.get_or_create_mfa_settings ' )
@patch ( " services.mfa_service.MFAService.get_or_create_mfa_settings " )
def test_generate_mfa_setup_data_already_enabled ( self , mock_get_settings ) :
def test_generate_mfa_setup_data_already_enabled ( self , mock_get_settings ) :
""" Test MFA setup data generation when already enabled. """
""" Test MFA setup data generation when already enabled. """
self . mfa_settings . enabled = True
self . mfa_settings . enabled = True
mock_get_settings . return_value = self . mfa_settings
mock_get_settings . return_value = self . mfa_settings
with self . assertR aises( ValueError ) as context :
with pytest . r aises( ValueError ) as context :
MFAService . generate_mfa_setup_data ( self . account )
MFAService . generate_mfa_setup_data ( self . account )
self . assertIn ( " already enabled " , str ( context . exception ) )
assert " already enabled " in str ( context . value )
if __name__ == ' __main__ ' :
if __name__ == " __main__ " :
unittest . main ( )
unittest . main ( )