Skip to content

Commit 33438e0

Browse files
jpaniagualaconichclaudep
authored andcommitted
get all phonedevices of the user (fixes #659)
1 parent 0c1273a commit 33438e0

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

two_factor/plugins/phonenumber/method.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
from .forms import PhoneNumberForm
66
from .models import PhoneDevice
7-
from .utils import backup_phones, format_phone_number, mask_phone_number
7+
from .utils import format_phone_number, mask_phone_number
88

99

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

1414
def recognize_device(self, device):
1515
return isinstance(device, PhoneDevice) and device.method == self.code

two_factor/plugins/phonenumber/tests/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.test import TestCase
2+
3+
from tests.utils import UserMixin
4+
from two_factor.plugins.phonenumber.method import PhoneCallMethod, SMSMethod
5+
6+
7+
class PhoneMethodBaseTestMixin(UserMixin):
8+
def test_get_devices(self):
9+
other_method_code = PhoneCallMethod.code if isinstance(self.method, SMSMethod) else SMSMethod.code
10+
user = self.create_user()
11+
backup_device = user.phonedevice_set.create(name='backup', number='+12024561111', method=self.method.code)
12+
default_device = user.phonedevice_set.create(name='default', number='+12024561111', method=self.method.code)
13+
user.phonedevice_set.create(name='default', number='+12024561111', method=other_method_code)
14+
15+
method_device_pks = [device.pk for device in self.method.get_devices(user)]
16+
self.assertEqual(method_device_pks, [backup_device.pk, default_device.pk])
17+
18+
19+
class PhoneCallMethodTest(PhoneMethodBaseTestMixin, TestCase):
20+
method = PhoneCallMethod()
21+
22+
23+
class SMSMethodTest(PhoneMethodBaseTestMixin, TestCase):
24+
method = SMSMethod()

0 commit comments

Comments
 (0)