Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): implement unit tests #11

Closed
wants to merge 1 commit into from
Closed

chore(test): implement unit tests #11

wants to merge 1 commit into from

Conversation

hairmare
Copy link
Contributor

@hairmare hairmare commented Jul 4, 2024

⚠️ This draft unit testing PR contains a lot of noise that is not related to the tests at all, i plan on splitting the parts we agree on before undrafting this ⚠️

I verified that scrubbed does what it says on the box by writing comprehensive unit tests and the results look promising enough for this early draft PR.

I'm pretty sure this will fail fast and i'll try to follow up with some more details once i get the CI results i'm looking for :)

@hairmare hairmare self-assigned this Jul 4, 2024
r = requests.post(
URL,
json=alert,
headers=request.headers,
Copy link
Contributor Author

@hairmare hairmare Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i had a bad feeling when changing this just to make testing easier, but the longer i think about it, the more it seems like the two implementations achieve the exact same thing under the hood.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved to session exactly because headers=request.headers didn't work directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I know... and i'm pretty sure i've done this before as well but i can't seem to remember why it didn't work directly

Copy link
Collaborator

@alesz alesz Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
    return session.request(method=method, url=url, **kwargs)

is whole request function

post function is return request("post", url, data=data, json=json, **kwargs)

request headers is just plain dict, while Flask request is werzeug.EnvironHeaders inheriting from werkzeug.Headers, closest those classes do is implement __iter__, so headers=dict(request.headers)?

"Host": "localhost",
"Content-Type": "application/json",
"Content-Length": "451",
"Key": "SECRET", # TODO: redact this?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Key" header is a bit of a stand-in. Let's check if there are any sensitive headers that we want to scrub or potentially just implement an allow list to be on the really safe side.

name = "scrubbed"

[build-system]
build-backend = "flit_core.buildapi"
Copy link
Contributor Author

@hairmare hairmare Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually lean towards using poetry for everything to align myself with other efforts in the enterprise. flit is much more lightweight in comparison, and it doesn't really matter during runtime anyway.

I did not know about/remember pyinstaller and the only reason i touched these parts is because it helped me get pytest up and running without more complex folders whatnot.

For sure something we want to revisit before merging this, I'll gladly refactor back to requirement*txt if we want to stick with that route.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with pyproject lgtm too

},
],
}
assert upstream_request.last_request.headers == {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list doesn't reflect what probably should be happening...

>>> import requests
>>> requests.sessions.Session().headers
{'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate, br', 'Accept': '*/*', 'Connection': 'keep-alive'}

@alesz alesz closed this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants