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

Fix register fails on channles requests. #137 #519

Merged
merged 12 commits into from
Apr 10, 2024
Merged

Conversation

shmoon-kr
Copy link
Contributor

Fixed register mutation not working in channels environment. Instead of the get_current_site() function, we implemented it by getting the required values from the request.headers object and the request.consumer.scope object and returning them. There are a few things that need to be reviewed.

  1. the content of the request object has changed - this is included in the context used when rendering html mail. If they want to include the contents of the request in the email, they will need to customize it.
  2. I've tested and found that the site_name and domain returned by get_mail_context() actually contain the same values, so it should work the same in a channels environment. However, if your site_name and domain are different, you may run into issues.
  3. I used the port number by parsing request.header["host"]. if the request.header contains anything other than a valid "host:port" form of the value, an error may occur.

@gqlauth
Copy link
Collaborator

gqlauth commented Apr 8, 2024

Release Notes

Just wanted to say

success

Release type: patch

Fix register fails on channels requests. #137
Do not call get_current_site() from get_email_context()
Extract values from channels context.

Copy link

codecov bot commented Apr 8, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 92.93%. Comparing base (9c760b6) to head (7f26b06).
Report is 24 commits behind head on main.

Files Patch % Lines
gqlauth/models.py 66.66% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #519      +/-   ##
==========================================
+ Coverage   92.82%   92.93%   +0.10%     
==========================================
  Files          33       33              
  Lines        1478     1501      +23     
==========================================
+ Hits         1372     1395      +23     
  Misses        106      106              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@nrbnlulu nrbnlulu left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, left some notes...

Also please add tests

if isinstance(info.context, dict):
request = info.context["request"]
return {
"user": self.user,
Copy link
Owner

Choose a reason for hiding this comment

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

try to do something like

is_channels = isinstance(info.context, dict)

if is_channels:
	domain, port =  request.headers["host"].split(":")
else:
	domain = site.domain
	# yada yada
return {
	"domain" domain,
	# yada yada

@shmoon-kr
Copy link
Contributor Author

shmoon-kr commented Apr 8, 2024

I've been trying to write test code, but I'm not sure how to do it. I've thought of two ways to do it

  1. run the schema.execute() function in the channels context: I haven't seen this done, if you know how, please let me know.

  2. create a UserStatus object and then call the get_mail_context() function: I think it would be something similar to the code below, but I'm not sure how to make the parameter, do you have any ideas?

from strawberry.types.info import Info
from gqlauth.models import UserStatus
from where import User

info = Info(_raw_info="How can I make this?", _field="Is there any example?")

user = User.objects.create(blahblah)
user_status = UserStatus.objects.create(blahblah)
context = user_status.get_email_context(info=info, path=path, action=what)

assert context['domain'] == 'test.com' and context['port'] == '8000'

port = request.get_port()
site_name = site.name
domain = site.domain
protocol = ("https" if request.is_secure() else "http",)
Copy link
Owner

Choose a reason for hiding this comment

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

that's a tuple 🙃

Copy link
Contributor Author

@shmoon-kr shmoon-kr Apr 8, 2024

Choose a reason for hiding this comment

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

image

I don't think protocol was originally a tuple, is it really a tuple? It's confusing.

Copy link
Owner

@nrbnlulu nrbnlulu Apr 8, 2024

Choose a reason for hiding this comment

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

No it wasn't. but you what you wrote is a tuple
("https" if request.is_secure() else "http",)
`

Suggested change
protocol = ("https" if request.is_secure() else "http",)
protocol = "https" if request.is_secure() else "http"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems to have been automatically fixed by [pre-commit-ci[bot]. I'd like you to take a look at this bot.

@nrbnlulu
Copy link
Owner

nrbnlulu commented Apr 8, 2024

run the schema.execute() function in the channels context: I haven't seen this done, if you know how, please let me know.

You can try to use the tests consumer

db_verified_user_status, verified_channels_app_communicator

BTW TBH I am not sure that you should use channels for mutation / queries... Do you have any reason to do so?

@shmoon-kr
Copy link
Contributor Author

shmoon-kr commented Apr 8, 2024

Following your suggestion, I wrote a test code to call register mutation in channels context using verified_user_status_type and verified_channels_app_communicator. However, it seems to fail to run. The strange thing is that result.data["register"]["success"] is False, but result.errors is None when I print it out. When I ran the same code in sync context, it worked fine, but this doesn't cover the newly added code. Do you have any ideas on how to fix this?

@nrbnlulu
Copy link
Owner

nrbnlulu commented Apr 8, 2024

I'll try to run it myself later 🤞

@shmoon-kr
Copy link
Contributor Author

shmoon-kr commented Apr 8, 2024

The problem is that it's trying to register a new user, and the username and phone_number already exist. So I fixed it, however, it doesn't seem to cover the newly added code still. I've republished the PR, so please review it.

Copy link
Owner

@nrbnlulu nrbnlulu left a comment

Choose a reason for hiding this comment

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

Generally I accept the changes though I want to test this manually because the consumer is having problems ATM.

@@ -50,6 +50,17 @@ def test_register_invalid_password_validation(verified_user_status_type, anonymo
assert executed["errors"]


async def test_channels_register(
verified_user_status_type, captcha, verified_channels_app_communicator
Copy link
Owner

Choose a reason for hiding this comment

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

You don't need a verified consumer here I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I replaced it with unverified_channels_app_communicator, as it doesn't require authentication when registering.

@nrbnlulu nrbnlulu merged commit 36a230b into nrbnlulu:main Apr 10, 2024
23 of 24 checks passed
@nrbnlulu nrbnlulu linked an issue Apr 10, 2024 that may be closed by this pull request
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.

register fails on channles requests.
3 participants