Skip to content

Commit

Permalink
Add test for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
snehalbaghel committed Oct 18, 2020
1 parent 5583401 commit 5e6dd48
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
TOKEN_URL = "http://localhost:8000/o/token"
TEST_IMAGE_PATH_PNG = str(Path("tests/test_image.png"))
TEST_IMAGE_PATH_SVG = str(Path("tests/test_image.svg"))
TEST_USER = "test"
TEST_PASSWORD = "test_pass"


def get_badgeclass_data(**kwargs):
Expand Down Expand Up @@ -48,23 +50,37 @@ def client(requests_mock):
requests_mock.post(TOKEN_URL, text=get_mock_auth_text())

client = BadgrClient(
username="test",
password="test_pass",
username=TEST_USER,
password=TEST_PASSWORD,
client_id="kewl_client",
scope="rw:profile rw:issuer rw:backpack",
)

return client


def test_client_init(client):
def test_client_init(client, mocker):
"""Test client instance is correctly created"""

assert client.scope == "rw:profile rw:issuer rw:backpack"
assert client.refresh_token == "mock_refresh_token"
assert client.base_url == "http://localhost:8000"
assert client.header == {"Authorization": "Bearer mock_token"}


def test_client_credentials(mocker):
"""Test username password"""

mocker.patch("badgrclient.BadgrClient._get_auth_token")
BadgrClient(
username=TEST_USER,
password=TEST_PASSWORD,
client_id="kewl_client",
scope="rw:profile rw:issuer rw:backpack",
)
BadgrClient._get_auth_token.assert_called_once_with(TEST_USER, TEST_PASSWORD)


def test_token_refresh(requests_mock):
"""Test token is refreshed after expiry"""

Expand All @@ -77,8 +93,8 @@ def test_token_refresh(requests_mock):
)

client = BadgrClient(
username="test",
password="test_pass",
username=TEST_USER,
password=TEST_PASSWORD,
client_id="kewl_client",
scope="rw:profile rw:issuer rw:backpack",
)
Expand All @@ -89,7 +105,7 @@ def test_token_refresh(requests_mock):
requests_mock.post(
TOKEN_URL, text=get_mock_auth_text(token="refreshed_token")
)
# Call api to rigger refresh
# Call api to trigger refresh
client.fetch_assertion()

assert client.header == {"Authorization": "Bearer refreshed_token"}
Expand Down Expand Up @@ -294,8 +310,8 @@ def unique_badge_client(requests_mock):
requests_mock.post(TOKEN_URL, text=get_mock_auth_text())

client = BadgrClient(
username="test",
password="test_pass",
username=TEST_USER,
password=TEST_PASSWORD,
client_id="kewl_client",
scope="rw:profile rw:issuer rw:backpack",
unique_badge_names=True,
Expand Down

0 comments on commit 5e6dd48

Please sign in to comment.