Skip to content

Commit

Permalink
🎨 Move shared RSA key fixtures to conftest
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Jan 13, 2020
1 parent adc1a2e commit 74698d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
39 changes: 39 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Shared fixtures for tests."""
import pytest

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PrivateFormat,
)


@pytest.fixture
def rsa_private_key():
"""Generate an RSA private key."""
return generate_private_key(
public_exponent=65537,
key_size=4096,
backend=default_backend(),
)


@pytest.fixture
# pylint: disable=redefined-outer-name
def rsa_private_key_bytes(rsa_private_key) -> bytes:
r"""Generate an unencrypted PKCS#1 formatted RSA private key.
Encoded as PEM-bytes.
This is what the GitHub-downloaded PEM files contain.
Ref: https://developer.github.com/apps/building-github-apps/\
authenticating-with-github-apps/
"""
return rsa_private_key.private_bytes(
encoding=Encoding.PEM,
format=PrivateFormat.TraditionalOpenSSL, # A.K.A. PKCS#1
encryption_algorithm=NoEncryption(),
)
32 changes: 0 additions & 32 deletions tests/github/models/private_key_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,21 @@

import pytest

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PublicFormat,
PrivateFormat,
)
from jwt import decode as parse_jwt

from octomachinery.github.models.private_key import GitHubPrivateKey


@pytest.fixture
def rsa_private_key():
"""Generate an RSA private key."""
return generate_private_key(
public_exponent=65537,
key_size=4096,
backend=default_backend(),
)


@pytest.fixture
def rsa_public_key(rsa_private_key):
"""Extract a public key out of private one."""
return rsa_private_key.public_key()


@pytest.fixture
def rsa_private_key_bytes(rsa_private_key) -> bytes:
r"""Generate an unencrypted PKCS#1 formatted RSA private key.
Encoded as PEM-bytes.
This is what the GitHub-downloaded PEM files contain.
Ref: https://developer.github.com/apps/building-github-apps/\
authenticating-with-github-apps/
"""
return rsa_private_key.private_bytes(
encoding=Encoding.PEM,
format=PrivateFormat.TraditionalOpenSSL, # A.K.A. PKCS#1
encryption_algorithm=NoEncryption(),
)


@pytest.fixture
def rsa_public_key_bytes(rsa_public_key) -> bytes:
"""Return a PKCS#1 formatted RSA public key encoded as PEM."""
Expand Down

0 comments on commit 74698d1

Please sign in to comment.