Skip to content

Commit

Permalink
ged rid of unicode sandwiches, .format
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Kudrin authored and Yaroslav Kudrin committed Aug 5, 2021
1 parent c782379 commit 18554a8
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 409 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: [2.7, 3.5, 3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8]
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
8 changes: 3 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Generate temporary AWS credentials via Okta.
.. image:: https://github.com/dowjones/tokendito/workflows/Lint%20and%20Test/badge.svg
:target: https://github.com/dowjones/tokendito/actions

.. image:: https://img.shields.io/badge/python-2.7%2C%203.5%2C%203.6%2C%203.7%2C%203.8-blueviolet
.. image:: https://img.shields.io/badge/python-3.6%2C%203.7%2C%203.8-blueviolet
:target: https://pypi.org/project/tokendito/

.. image:: https://github.com/dowjones/tokendito/workflows/Woke/badge.svg
Expand All @@ -27,8 +27,6 @@ Generate temporary AWS credentials via Okta.
.. image:: https://raw.githubusercontent.com/dowjones/tokendito/master/docs/tokendito-scaled.gif


**WARNING: Python2.7 and Python3.5 support will be dropped in a near time, pin tokendito version to current if you want to keep it working in automation.**

NOTE: Advanced users may shorten the tokendito interaction to a `single command <https://github.com/dowjones/tokendito/tree/master/docs#single-command-usage>`_.

.. _STS: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html
Expand All @@ -38,10 +36,10 @@ Use tokendito to generate temporary AWS credentials via Okta for programmatic au
Requirements
------------

* Python 2.7.10+
* Python 3.6+
* Your AWS account is federated in Okta

tokendito is compatible with both python 2 and 3, and can be installed with either pip or pip3.
tokendito is compatible with python 3, and can be installed with either pip or pip3.

Getting started
---------------
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-r ./requirements.txt
black==20.8b1; python_version >= '3.6'
black==20.8b1
coveragepy-lcov; python_version >= '3.8'
docutils<0.16,>=0.10
flake8
flake8-black; python_version >= '3.6'
flake8-black
flake8-colors
flake8-docstrings
flake8-import-order>=0.9
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ requests>=2.19.0
configparser>=3.5.0
future>=0.16.0
pyOpenSSL>=18.0.0
cryptography==3.3.2; python_version == '2.7'
beautifulsoup4>=4.6.0
lxml>=4.3.0
pytz==2021.1
tzlocal==2.1
6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@
"Intended Audience :: End Users/Desktop",
"Intended Audience :: Developers",
"Intended Audience :: System Administrators",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
keywords=["okta", "aws", "sts"],
packages=find_packages(exclude=["contrib", "docs", "tests", ".tox"]),
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
python_requires=">=3.6",
license=about["__license__"],
zip_safe=False,
install_requires=[required],
Expand Down
5 changes: 0 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# vim: set filetype=python ts=4 sw=4
# -*- coding: utf-8 -*-
"""pytest configuration, hooks, and global fixtures."""
from __future__ import absolute_import, division, print_function, unicode_literals

from future import standard_library

standard_library.install_aliases()


def pytest_addoption(parser):
Expand Down
38 changes: 6 additions & 32 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
# vim: set filetype=python ts=4 sw=4
# -*- coding: utf-8 -*-
"""Functional tests, and local fixtures."""
from __future__ import absolute_import, division, print_function, unicode_literals

from builtins import ( # noqa: F401
ascii,
bytes,
chr,
dict,
filter,
hex,
input,
int,
list,
map,
next,
object,
oct,
open,
pow,
range,
round,
str,
super,
zip,
)
import datetime
from os import environ, path
import re
import subprocess
import sys
import time

from future import standard_library
import pytest

sys.path.insert(0, path.dirname(path.dirname(path.abspath(__file__))))
standard_library.install_aliases()


def string_decode(bytestring):
Expand Down Expand Up @@ -202,17 +176,17 @@ def test_generate_credentials(custom_args):
# Rebuild argument list
args = [
"--role-arn",
"{}".format(settings.role_arn),
f"{settings.role_arn}",
"--okta-aws-app-url",
"{}".format(settings.okta_aws_app_url),
f"{settings.okta_aws_app_url}",
"--mfa-method",
"{}".format(settings.mfa_method),
f"{settings.mfa_method}",
"--mfa-response",
"{}".format(settings.mfa_response),
f"{settings.mfa_response}",
"--username",
"{}".format(settings.okta_username),
f"{settings.okta_username}",
"--password",
"{}".format(settings.okta_password),
f"{settings.okta_password}",
]
# run as a local module, as we can't guarantee that the binary is installed.
executable = [sys.executable, "-m", "tokendito"]
Expand Down
80 changes: 17 additions & 63 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
# vim: set filetype=python ts=4 sw=4
# -*- coding: utf-8 -*-
"""Unit tests, and local fixtures."""
from __future__ import absolute_import, division, print_function, unicode_literals

from builtins import ( # noqa: F401
ascii,
bytes,
chr,
dict,
filter,
hex,
input,
int,
list,
map,
next,
object,
oct,
open,
pow,
range,
round,
str,
super,
zip,
)
from datetime import datetime
import os
import sys

from future import standard_library
import pytest
import semver
from tokendito.settings import okta_status_dict


sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
standard_library.install_aliases()


@pytest.fixture
Expand All @@ -53,7 +27,6 @@ def valid_settings():
"__name__",
"__package__",
"__spec__",
"absolute_import",
"ascii",
"bytes",
"chr",
Expand All @@ -75,7 +48,6 @@ def valid_settings():
"range",
"role_arn",
"round",
"standard_library",
"str",
"super",
"sys",
Expand Down Expand Up @@ -158,20 +130,11 @@ def test__version__var_names():
assert item.endswith("__")


@pytest.mark.parametrize("string", [r"raw_string", "unicode_string", r"byte_string"])
def test_to_unicode(string):
"""Test whether to_unicode returns unicode strings."""
from tokendito import helpers

new_str = helpers.to_unicode(string)
assert isinstance(new_str, str)


def test_set_okta_username(monkeypatch):
def test_set_okta_username(mocker):
"""Test whether data sent is the same as data returned."""
from tokendito import helpers, settings

monkeypatch.setattr("tokendito.helpers.input", lambda _: "pytest_patched")
mocker.patch("tokendito.helpers.input", return_value="pytest_patched")
val = helpers.set_okta_username()

assert val == "pytest_patched"
Expand Down Expand Up @@ -258,20 +221,20 @@ def test_validate_input(mocker, test, limit, expected):
assert helpers.validate_input(test, limit) is expected


def test_get_input(monkeypatch):
def test_get_input(mocker):
"""Check if provided input is return unmodified."""
from tokendito import helpers

monkeypatch.setattr("tokendito.helpers.input", lambda _: "pytest_patched")
mocker.patch("tokendito.helpers.input", return_value="pytest_patched")
assert helpers.get_input() == "pytest_patched"


@pytest.mark.parametrize("value,expected", [("00", 0), ("01", 1), ("5", 5)])
def test_collect_integer(monkeypatch, value, expected):
def test_collect_integer(mocker, value, expected):
"""Check if a given digit or series of digits are properly casted to int."""
from tokendito import helpers

monkeypatch.setattr("tokendito.helpers.input", lambda _: value)
mocker.patch("tokendito.helpers.input", return_value=value)
assert helpers.collect_integer(10) == expected


Expand Down Expand Up @@ -301,11 +264,11 @@ def test_prepare_payload():
) == {"pytest_key1": "pytest_val1", "pytest_key2": "pytest_val2"}


def test_set_passcode(monkeypatch):
def test_set_passcode(mocker):
"""Check if numerical passcode can handle leading zero values."""
from tokendito import duo_helpers

monkeypatch.setattr("tokendito.helpers.input", lambda _: "0123456")
mocker.patch("tokendito.helpers.input", return_value="0123456")
assert duo_helpers.set_passcode({"factor": "passcode"}) == "0123456"


Expand All @@ -317,9 +280,7 @@ def test_process_environment(monkeypatch, valid_settings, invalid_settings):
valid_keys = {key.upper(): val for (key, val) in valid_settings.items()}
invalid_keys = {key.upper(): val for (key, val) in invalid_settings.items()}

# Python 2.7 does not support {**dict1, **dict2} for concatenation
env_keys = valid_keys.copy()
env_keys.update(invalid_keys)
env_keys = {**valid_keys.copy(), **invalid_keys}

monkeypatch.setattr(os, "environ", env_keys)
helpers.process_environment()
Expand All @@ -336,9 +297,8 @@ def test_process_arguments(valid_settings, invalid_settings):
from tokendito import helpers, settings
from argparse import Namespace

# Python 2.7 does not support {**dict1, **dict2} for concatenation
args = valid_settings.copy()
args.update(invalid_settings)
args = {**valid_settings.copy(), **invalid_settings}
args.update()

helpers.process_arguments(Namespace(**args))

Expand All @@ -359,14 +319,10 @@ def test_process_ini_file(tmpdir, valid_settings, invalid_settings, mocker):

# Create a mock config file
data = "[default]\nokta_username = pytest\n\n[pytest]\n"
data += "".join("{} = {}\n".format(key, val) for key, val in valid_settings.items())
data += "".join(
"{} = {}\n".format(key, val) for key, val in invalid_settings.items()
)
data += "".join(f"{key} = {val}\n" for key, val in valid_settings.items())
data += "".join(f"{key} = {val}\n" for key, val in invalid_settings.items())
data += "\n[pytest_end]\n"
data += "".join(
"{} = {}\n".format(key, val) for key, val in invalid_settings.items()
)
data += "".join(f"{key} = {val}\n" for key, val in invalid_settings.items())

# Python 3.7 supports patching builtins.open(), which gives us the ability
# to bypass file creation with:
Expand Down Expand Up @@ -527,9 +483,7 @@ def test_login_error_code_parser(mocker):
== "Okta auth failed: " + value
)
unexpected_key = "UNEXPECTED_KEY"
value = "Okta auth failed: {}. Please verify your settings and try again.".format(
unexpected_key
)
value = f"Okta auth failed: {unexpected_key}. Please verify your settings and try again."
assert login_error_code_parser(unexpected_key, okta_status_dict) == value


Expand Down Expand Up @@ -612,8 +566,8 @@ def test_select_preferred_mfa_index_output(email, capsys, mocker, sample_json_re
correct_output = (
"\nSelect your preferred MFA method and press Enter:\n"
"[0] OKTA push Redmi 6 Pro Id: opfrar9yi4bKJNH2WEWQ0x8\n"
"[1] GOOGLE token:software:totp {0} Id: FfdskljfdsS1ljUT0r8\n"
"[2] OKTA token:software:totp {0} Id: fdsfsd6ewREr8\n".format(email)
f"[1] GOOGLE token:software:totp {email} Id: FfdskljfdsS1ljUT0r8\n"
f"[2] OKTA token:software:totp {email} Id: fdsfsd6ewREr8\n"
)

mocker.patch("tokendito.helpers.collect_integer", return_value=1)
Expand Down
7 changes: 0 additions & 7 deletions tokendito/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
# vim: set filetype=python ts=4 sw=4
# -*- coding: utf-8 -*-
"""tokendito module entry point."""

from __future__ import absolute_import, division, print_function, unicode_literals

import sys

from future import standard_library

standard_library.install_aliases()


def main(args=None): # needed for console script
"""Packge entry point."""
Expand Down
Loading

0 comments on commit 18554a8

Please sign in to comment.