import json
import time
import uuid

import pytest
from jose import jwt

from helusers.oidc import ApiTokenAuthentication

from .keys import rsa_key

ISSUER = "test_issuer"


class _TestableApiTokenAuthentication(ApiTokenAuthentication):
    @property
    def oidc_config(self):
        return {
            "issuer": ISSUER,
        }

    def jwks_data(self):
        return json.dumps({"keys": [rsa_key.public_key_jwk]})


@pytest.mark.django_db
def test_valid_jwt_is_accepted(rf):
    sut = _TestableApiTokenAuthentication()

    unix_timestamp_now = int(time.time())

    user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")
    jwt_data = {
        "iss": ISSUER,
        "aud": "test_audience",
        "iat": unix_timestamp_now - 10,
        "exp": unix_timestamp_now + 1000,
        "sub": str(user_uuid),
    }

    encoded_jwt = jwt.encode(
        jwt_data, key=rsa_key.private_key_pem, algorithm=rsa_key.jose_algorithm
    )

    request = rf.get("/path", HTTP_AUTHORIZATION=f"Bearer {encoded_jwt}")

    (user, auth) = sut.authenticate(request)

    assert user.uuid == user_uuid
    assert auth.user == user

import json
import uuid

import pytest

from helusers.oidc import ApiTokenAuthentication

from .conftest import encoded_jwt_factory, ISSUER1
from .keys import rsa_key


class _TestableApiTokenAuthentication(ApiTokenAuthentication):
    @property
    def oidc_config(self):
        return {
            "issuer": ISSUER1,
        }

    def jwks_data(self):
        return json.dumps({"keys": [rsa_key.public_key_jwk]})


@pytest.mark.django_db
def test_valid_jwt_is_accepted(rf, unix_timestamp_now):
    sut = _TestableApiTokenAuthentication()

    user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")

    encoded_jwt = encoded_jwt_factory(
        iss=ISSUER1,
        aud="test_audience",
        iat=unix_timestamp_now - 10,
        exp=unix_timestamp_now + 1000,
        sub=str(user_uuid),
    )

    request = rf.get("/path", HTTP_AUTHORIZATION=f"Bearer {encoded_jwt}")

    (user, auth) = sut.authenticate(request)

    assert user.uuid == user_uuid
    assert auth.user == user

Use common test helpers in a test
Use common test helpers in a test

  import json
- import time
  import uuid
  
  import pytest
- from jose import jwt
  
  from helusers.oidc import ApiTokenAuthentication
  
+ from .conftest import encoded_jwt_factory, ISSUER1
  from .keys import rsa_key
- 
- ISSUER = "test_issuer"
  
  
  class _TestableApiTokenAuthentication(ApiTokenAuthentication):
      @property
      def oidc_config(self):
          return {
-             "issuer": ISSUER,
+             "issuer": ISSUER1,
          }
  
      def jwks_data(self):
          return json.dumps({"keys": [rsa_key.public_key_jwk]})
  
  
  @pytest.mark.django_db
- def test_valid_jwt_is_accepted(rf):
+ def test_valid_jwt_is_accepted(rf, unix_timestamp_now):
      sut = _TestableApiTokenAuthentication()
  
-     unix_timestamp_now = int(time.time())
+     user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")
  
+     encoded_jwt = encoded_jwt_factory(
-     user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")
-     jwt_data = {
-         "iss": ISSUER,
+         iss=ISSUER1,
-         "aud": "test_audience",
+         aud="test_audience",
-         "iat": unix_timestamp_now - 10,
+         iat=unix_timestamp_now - 10,
-         "exp": unix_timestamp_now + 1000,
+         exp=unix_timestamp_now + 1000,
-         "sub": str(user_uuid),
+         sub=str(user_uuid),
-     }
- 
-     encoded_jwt = jwt.encode(
-         jwt_data, key=rsa_key.private_key_pem, algorithm=rsa_key.jose_algorithm
      )
  
      request = rf.get("/path", HTTP_AUTHORIZATION=f"Bearer {encoded_jwt}")
  
      (user, auth) = sut.authenticate(request)
  
      assert user.uuid == user_uuid
      assert auth.user == user
  
Use common test helpers in a test
## Code Before:
import json
import time
import uuid

import pytest
from jose import jwt

from helusers.oidc import ApiTokenAuthentication

from .keys import rsa_key

ISSUER = "test_issuer"


class _TestableApiTokenAuthentication(ApiTokenAuthentication):
    @property
    def oidc_config(self):
        return {
            "issuer": ISSUER,
        }

    def jwks_data(self):
        return json.dumps({"keys": [rsa_key.public_key_jwk]})


@pytest.mark.django_db
def test_valid_jwt_is_accepted(rf):
    sut = _TestableApiTokenAuthentication()

    unix_timestamp_now = int(time.time())

    user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")
    jwt_data = {
        "iss": ISSUER,
        "aud": "test_audience",
        "iat": unix_timestamp_now - 10,
        "exp": unix_timestamp_now + 1000,
        "sub": str(user_uuid),
    }

    encoded_jwt = jwt.encode(
        jwt_data, key=rsa_key.private_key_pem, algorithm=rsa_key.jose_algorithm
    )

    request = rf.get("/path", HTTP_AUTHORIZATION=f"Bearer {encoded_jwt}")

    (user, auth) = sut.authenticate(request)

    assert user.uuid == user_uuid
    assert auth.user == user

## Instruction:
Use common test helpers in a test
## Code After:
import json
import uuid

import pytest

from helusers.oidc import ApiTokenAuthentication

from .conftest import encoded_jwt_factory, ISSUER1
from .keys import rsa_key


class _TestableApiTokenAuthentication(ApiTokenAuthentication):
    @property
    def oidc_config(self):
        return {
            "issuer": ISSUER1,
        }

    def jwks_data(self):
        return json.dumps({"keys": [rsa_key.public_key_jwk]})


@pytest.mark.django_db
def test_valid_jwt_is_accepted(rf, unix_timestamp_now):
    sut = _TestableApiTokenAuthentication()

    user_uuid = uuid.UUID("b7a35517-eb1f-46c9-88bf-3206fb659c3c")

    encoded_jwt = encoded_jwt_factory(
        iss=ISSUER1,
        aud="test_audience",
        iat=unix_timestamp_now - 10,
        exp=unix_timestamp_now + 1000,
        sub=str(user_uuid),
    )

    request = rf.get("/path", HTTP_AUTHORIZATION=f"Bearer {encoded_jwt}")

    (user, auth) = sut.authenticate(request)

    assert user.uuid == user_uuid
    assert auth.user == user
