Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tests for Nonce model and logic #43

Open
payton opened this issue Aug 29, 2022 · 0 comments
Open

Create tests for Nonce model and logic #43

payton opened this issue Aug 29, 2022 · 0 comments
Labels

Comments

@payton
Copy link
Owner

payton commented Aug 29, 2022

Write unit tests for...

  1. nonce_is_valid function
    def _nonce_is_valid(nonce: str) -> bool:
    """
    Check if given nonce exists and has not yet expired.
    :param nonce: The nonce string to validate.
    :return: True if valid else False.
    """
    n = Nonce.objects.get(value=nonce)
    is_valid = False
    if n is not None and n.expiration > datetime.datetime.now(tz=pytz.UTC):
    is_valid = True
    n.delete()
    return is_valid
  2. Nonce model itself
    class Nonce(models.Model):
    value = models.CharField(max_length=24, primary_key=True)
    expiration = models.DateTimeField()
    def __str__(self):
    return self.value
  3. Nonce scrubbing logic (with freezegun?)
    @ratelimit(key='ip', rate='5/m')
    @require_http_methods(["GET"])
    def nonce(request):
    now = datetime.now(tz=pytz.UTC)
    _scrub_nonce()
    n = Nonce(value=secrets.token_hex(12), expiration=now + timedelta(hours=12))
    n.save()
    return JsonResponse({"nonce": n.value})
    def _scrub_nonce():
    # Delete all expired nonce's
    for n in Nonce.objects.filter(expiration__lte=datetime.now(tz=pytz.UTC)):
    n.delete()
@payton payton added good first issue Good for newcomers tech debt labels Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant