Coverage for tests/factories.py: 100%

41 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-06-23 20:51 +0000

1import factory 

2import factory.fuzzy 

3from factory.random import randgen as factory_randgen 

4 

5from django_otp_webauthn.models import WebAuthnAttestation, WebAuthnCredential 

6 

7from .fuzzy import FuzzyBytes 

8 

9 

10class UserFactory(factory.django.DjangoModelFactory): 

11 class Meta: 

12 model = "auth.User" 

13 django_get_or_create = ["username"] 

14 

15 username = factory.Faker("user_name") 

16 email = factory.Faker("email") 

17 first_name = factory.Faker("first_name") 

18 last_name = factory.Faker("last_name") 

19 

20 

21class WebAuthnCredentialFactory(factory.django.DjangoModelFactory): 

22 class Meta: 

23 model = WebAuthnCredential 

24 django_get_or_create = ["credential_id"] 

25 

26 class Params: 

27 attested = factory.Trait(attestation=factory.SubFactory("tests.factories.WebAuthnAttestationFactory")) 

28 

29 @factory.lazy_attribute 

30 def name(self): 

31 return f"{self.user.username}'s credential" 

32 

33 user = factory.SubFactory("tests.factories.UserFactory") 

34 credential_id = FuzzyBytes() 

35 sign_count = 0 

36 discoverable = factory.fuzzy.FuzzyChoice([True, False, None]) 

37 aaguid = factory.Faker("uuid4") 

38 public_key = b"\00" 

39 transports = [] 

40 backup_eligible = factory.fuzzy.FuzzyChoice([True, False]) 

41 

42 @factory.lazy_attribute 

43 def backup_state(self): 

44 # If backup_eligible is True, backup_state could be True or False. 

45 # If backup_eligible is False, backup_state should also always be False. 

46 return factory_randgen.choice([True, False]) if self.backup_eligible else False 

47 

48 

49class WebAuthnAttestationFactory(factory.django.DjangoModelFactory): 

50 class Meta: 

51 model = WebAuthnAttestation 

52 django_get_or_create = ["credential"] 

53 

54 credential = factory.SubFactory("tests.factories.WebAuthnCredentialFactory") 

55 

56 # We can't easily generate these fields, so we just set them to empty values. 

57 fmt = "none" 

58 data = b"\00" 

59 client_data_json = b"\00"