|
| 1 | +# Copyright The IETF Trust 2020, All Rights Reserved |
| 2 | + |
| 3 | +import debug # pyflakes:ignore |
| 4 | +import factory |
| 5 | + |
| 6 | +from hashlib import sha224 |
| 7 | +from random import randint |
| 8 | +from uuid import uuid4 |
| 9 | + |
| 10 | +from oidc_provider.models import Client as OidClientRecord, ResponseType |
| 11 | + |
| 12 | +from ietf.person.factories import UserFactory, PersonFactory |
| 13 | + |
| 14 | +class OidClientRecordFactory(factory.DjangoModelFactory): |
| 15 | + class Meta: |
| 16 | + model = OidClientRecord |
| 17 | + |
| 18 | + name = factory.Faker('company') |
| 19 | + owner = factory.SubFactory(UserFactory) |
| 20 | + client_type = 'confidential' |
| 21 | + client_id = str(randint(1, 999999)).zfill(6) |
| 22 | + |
| 23 | + @factory.lazy_attribute |
| 24 | + def client_secret(self): |
| 25 | + if self.client_type == 'confidential': |
| 26 | + secret = sha224(uuid4().hex.encode()).hexdigest() |
| 27 | + else: |
| 28 | + secret = '' |
| 29 | + return secret |
| 30 | + |
| 31 | + @factory.post_generation |
| 32 | + def response_types(self, create, extracted, **kwargs): |
| 33 | + if not create: |
| 34 | + # Simple build, do nothing. |
| 35 | + return |
| 36 | + |
| 37 | + if not extracted: |
| 38 | + extracted = ['code', ] |
| 39 | + |
| 40 | + # A list of groups were passed in, use them |
| 41 | + for value in extracted: |
| 42 | + type, _ = ResponseType.objects.get_or_create(value=value) |
| 43 | + self.response_types.add(type) |
| 44 | + |
| 45 | + @factory.post_generation |
| 46 | + def person(self, create, extracted, **kwargs): |
| 47 | + if not create: |
| 48 | + # Simple build, do nothing. |
| 49 | + return |
| 50 | + |
| 51 | + user = self.owner |
| 52 | + if extracted: |
| 53 | + extracted.user = user |
| 54 | + extracted.save() |
| 55 | + else: |
| 56 | + PersonFactory(name='%s %s' % (user.first_name, user.last_name), user=user) |
0 commit comments