Skip to content

Commit

Permalink
get all phonedevices of the user (fixes #659)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaniagualaconich committed Sep 10, 2023
1 parent d6d8b63 commit 52c9815
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions two_factor/plugins/phonenumber/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from .forms import PhoneNumberForm
from .models import PhoneDevice
from .utils import backup_phones, format_phone_number, mask_phone_number
from .utils import format_phone_number, mask_phone_number


class PhoneMethodBase(MethodBase):
def get_devices(self, user):
return [device for device in backup_phones(user) if device.method == self.code]
return PhoneDevice.objects.filter(user=user, method=self.code)

def recognize_device(self, device):
return isinstance(device, PhoneDevice)
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions two_factor/plugins/phonenumber/tests/test_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.test import TestCase

from tests.utils import UserMixin
from two_factor.plugins.phonenumber.method import PhoneCallMethod, SMSMethod

class PhoneMethodBaseTestMixin(UserMixin):
def test_get_devices(self):
user = self.create_user()
backup_device = user.phonedevice_set.create(name='backup', number='+12024561111', method=self.method.code)
default_device = user.phonedevice_set.create(name='default', number='+12024561111', method=self.method.code)

method_device_pks = [device.pk for device in self.method.get_devices(user)]
self.assertEqual(method_device_pks, [backup_device.pk, default_device.pk])


class PhoneCallMethodTest(PhoneMethodBaseTestMixin, TestCase):
method = PhoneCallMethod()


class SMSMethodTest(PhoneMethodBaseTestMixin, TestCase):
method = SMSMethod()

0 comments on commit 52c9815

Please sign in to comment.