Например, есть представление, которое обрабатывает запрос на регистрацию пользователя. Представление принимает номер телефона и отправляет на него СМС с кодом. Код генерируется дополнительной функцией. Вот как можно имитировать эту функцию, чтобы отправить нужный мне код?
Это мое мнение
Код: Выделить всё
def post(self, request):
serializer = ReviewerSignUpSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
phone = serializer.validated_data.get("phone")
confirmation_code = get_confirmation_code()
cache.set(
key=str(phone),
value=confirmation_code,
timeout=settings.CONFIRMATION_CODE_LIFETIME
)
send_sms_task.apply_async([confirmation_code, str(phone)])
Код: Выделить всё
def get_confirmation_code() -> str:
seq = list(map(lambda x: str(x), range(10)))
shuffle(seq)
code = choices(seq, k=settings.INVITE_CODE_LENGTH)
return "".join(code)
Код: Выделить всё
def test_saving_confirmation_code_in_cache(self, client, cache):
phone = "+79389999999"
generated_code = "9999"
with patch("api.v1.authentication.utils.get_confirmation_code", return_value=generated_code):
client.post(reverse('api:v1:signup'), data={'phone': phone})
confirmation_code = cache.get(str(phone))
assert confirmation_code is not None
assert confirmation_code == generated_code
Подробнее здесь: https://stackoverflow.com/questions/790 ... some-value