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(sentry_apps): Move bases file for sentryapps to sentry_apps #78096

Merged
merged 17 commits into from
Oct 2, 2024

Conversation

Christinarlong
Copy link
Contributor

@Christinarlong Christinarlong commented Sep 25, 2024

Move the bases file for sentry apps to sentry_apps!
[X] base file
[X] tests
[x] typing - yes mostly asserts on the sentryapps.py
[X] getsentry shim

issue ref(#73857)

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Sep 25, 2024
Copy link

codecov bot commented Sep 25, 2024

Codecov Report

Attention: Patch coverage is 90.77491% with 25 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/sentry_apps/api/bases/sentryapps.py 90.00% 13 Missing and 12 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #78096      +/-   ##
==========================================
+ Coverage   78.08%   78.10%   +0.01%     
==========================================
  Files        7067     7074       +7     
  Lines      311797   312014     +217     
  Branches    50958    50985      +27     
==========================================
+ Hits       243464   243685     +221     
+ Misses      61988    61971      -17     
- Partials     6345     6358      +13     

Comment on lines 383 to 385
assert isinstance(
request.user, (RpcUser, User)
), "user must be authenticated to check if they're a sentry app"
Copy link
Member

Choose a reason for hiding this comment

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

We should be able to start using user.is_authenticated to discriminate the anonymous user part of the union. See #78057 (comment)

Our main utils func for making requets builds HttpRequests but our inner endpoint functions
should be dealing with Request objects (most of the time). Unforutnately, converting with Request()
leads to various authentication issues. This change creates a helper method that uses the _force_auth_user/token
in the rest_framework Request constructor to add the ForcedAuthenticator() which tells any authenticate() calls in Request
to just trust the user/token we are sending and skip other authentication steps.

Unforunately this flag is not present on the HttpRequest object but used in rest_framework so it'll fail typing :(
Comment on lines 35 to 38
request._force_auth_user = user

if token:
request._force_auth_token = token
Copy link
Contributor Author

@Christinarlong Christinarlong Sep 27, 2024

Choose a reason for hiding this comment

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

The 'flag' in rest_framework
Kinda jank.99 because this method will fail typing as the attributes (_force_auth_X) don't exist on the base HttpRequest object, so RIP. This option is 'cleaner', since we're skipping all the auth stuff and only testing the classes/functions related to the module. But it fails typing sooooooo

There are a few other options we could take too...

  • Create a dummy Endpoint object add the tested permission to its permission_classes property and then pass the HttpRequest to the endpoints initialize_request function which is more similar to how it works in reality but we also introduce a 'new variable' in the endpoint so not sure if that's like straying away/not really a unit test.

  • Pass in some fun authenticators to the Request object, most of the test cases are provided a user that would work with SessionAuthentication. Only exception is the test which takes in no user and uses ApiToken to authenticate, we could use a different authentication and set the header. Kinda similar concern to the previous point where we have to work around another system/tie in another 'variable' (not sure if that's just the norm tho ?)

  • Make another make_request utils func that returns a Request obj ??? ( no clue what this would look like)

  • something else ?????????? 👀

Copy link
Member

Choose a reason for hiding this comment

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

Make another make_request utils func that returns a Request obj ??? ( no clue what this would look like)

This approach could work well and be done in way that satisfies mypy. We could construct an instance of a relevant endpoint and use that endpoint to wrap HttpRequest with Request.

Something like

 drf_request: Request = DiscordInteractionsEndpoint().initialize_request(self.request)

is how the discord integration is handling this scenario.

@Christinarlong Christinarlong marked this pull request as ready for review October 2, 2024 16:52
@Christinarlong Christinarlong requested review from a team as code owners October 2, 2024 16:52
@Christinarlong Christinarlong enabled auto-merge (squash) October 2, 2024 16:53
@Christinarlong Christinarlong enabled auto-merge (squash) October 2, 2024 17:30
@Christinarlong Christinarlong merged commit cc1aea3 into master Oct 2, 2024
50 checks passed
@Christinarlong Christinarlong deleted the christinarlong/move-sentryapps-bases branch October 2, 2024 18:29
@github-actions github-actions bot locked and limited conversation to collaborators Oct 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants