Skip to content

Commit

Permalink
feat: user_agent rework
Browse files Browse the repository at this point in the history
- Removed user_agent and _user_agent from HyScoresClient as excessive.
Now user agent can only be set via init arg of HyScoresClient.
- Added test to cover client's initialization.
  • Loading branch information
moonburnt committed Nov 13, 2021
1 parent 4368352 commit c88a8ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
13 changes: 3 additions & 10 deletions hscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,10 @@ def __init__(
self.timeout = max(timeout, 0)
self.app = app

self.user_agent = user_agent
self._token = None

@property
def user_agent(self):
return self._user_agent
if user_agent:
self.session.headers["user-agent"] = user_agent

@user_agent.setter
def user_agent(self, val: str):
self._user_agent = val
self.session.headers.update({"user-agent": self._user_agent})
self._token = None

@property
def token(self):
Expand Down
17 changes: 16 additions & 1 deletion tests/test_hscp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ def authorized_client(client):
client.token = token
return client

def test_client():
client = hscp.HyScoresClient(
url=url,
app=app,
)
assert client.url == url
assert client.app == app

user_agent = "pytest_client"
client = hscp.HyScoresClient(
url=url,
app=app,
user_agent=user_agent
)
assert client.session.headers["user-agent"] == user_agent

def test_token_fail(client):
with pytest.raises(hscp.TokenUnavailable):
Expand All @@ -36,6 +51,7 @@ def test_register(requests_mock, client):
def test_login(requests_mock, client):
requests_mock.post(url + "/login", json={"result": {"token": token}})
client.login(login, pw)
assert client.token is not None


def test_scores(requests_mock, authorized_client):
Expand All @@ -58,7 +74,6 @@ def test_score_uploader(requests_mock, authorized_client):
requests_mock.post(url + "/score", json={"result": True})
assert authorized_client.post_score("sadam", 69) is True


def test_logout(authorized_client):
authorized_client.logout()
assert authorized_client.token is None

0 comments on commit c88a8ce

Please sign in to comment.