21a397861a
Reviewed-on: #7
99 lines
2.8 KiB
Python
99 lines
2.8 KiB
Python
import json
|
|
|
|
from fastapi import HTTPException
|
|
from starlette import status
|
|
|
|
from client import SheerkaClient, parse_arguments
|
|
from mockserver import MockServer
|
|
|
|
|
|
def test_i_can_start_with_a_default_hostname():
|
|
parsed = parse_arguments([])
|
|
|
|
assert parsed.hostname == "http://localhost"
|
|
assert parsed.port == 56356
|
|
|
|
|
|
def test_i_can_override_hostname_and_port():
|
|
parsed = parse_arguments(["new_host", "--port", "1515"])
|
|
|
|
assert parsed.hostname == "new_host"
|
|
assert parsed.port == 1515
|
|
|
|
parsed = parse_arguments(["new_host", "-p", "1515"])
|
|
|
|
assert parsed.hostname == "new_host"
|
|
assert parsed.port == 1515
|
|
|
|
|
|
def test_i_can_provide_user_and_password():
|
|
parsed = parse_arguments(["--username", "my_user", "--password", "my_password"])
|
|
assert parsed.username == "my_user"
|
|
assert parsed.password == "my_password"
|
|
|
|
parsed = parse_arguments(["-u", "my_user", "-P", "my_password"])
|
|
assert parsed.username == "my_user"
|
|
assert parsed.password == "my_password"
|
|
|
|
|
|
def test_i_can_manage_when_no_server():
|
|
client = SheerkaClient("http://localhost", 80)
|
|
res = client.check_url()
|
|
|
|
assert res.status is False
|
|
assert res.message == "Connection refused."
|
|
|
|
|
|
def test_i_can_manage_when_resource_is_not_found():
|
|
with MockServer([]):
|
|
client = SheerkaClient("http://localhost", 5000)
|
|
res = client.check_url()
|
|
|
|
assert not res.status
|
|
assert res.message == '{"detail":"Not Found"}'
|
|
|
|
|
|
def test_i_can_connect_to_a_server():
|
|
with MockServer([{
|
|
"path": "/",
|
|
"response": "Hello world"
|
|
}]):
|
|
client = SheerkaClient("http://localhost", 5000)
|
|
res = client.check_url()
|
|
assert res.status
|
|
assert res.message == '"Hello world"'
|
|
|
|
|
|
def test_i_can_authenticate_with_valid_credentials():
|
|
with MockServer([{
|
|
"path": "/",
|
|
"response": "Hello world"
|
|
}, {
|
|
"method": "post",
|
|
"path": "/token",
|
|
"response": {"access_token": "xxxx", "token_type": "bearer"}
|
|
}]):
|
|
client = SheerkaClient("http://localhost", 5000)
|
|
res = client.connect("valid_username", "valid_password")
|
|
assert res.status
|
|
assert res.message == "Connected as valid_username"
|
|
|
|
|
|
def test_i_can_manage_when_authentication_fails():
|
|
with MockServer([{
|
|
"path": "/",
|
|
"response": "Hello world"
|
|
}, {
|
|
"method": "post",
|
|
"path": "/token",
|
|
"exception": HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect username or password",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
}]):
|
|
client = SheerkaClient("http://localhost", 5000)
|
|
res = client.connect("username", "wrong_password")
|
|
assert not res.status
|
|
assert res.message == 'Incorrect username or password'
|