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

Add support for session in AppClient for tests #1165

Open
confact opened this issue Jun 1, 2020 · 4 comments
Open

Add support for session in AppClient for tests #1165

confact opened this issue Jun 1, 2020 · 4 comments

Comments

@confact
Copy link
Contributor

confact commented Jun 1, 2020

I want to test so the browser actions are return right status codes if logged in and not logged in.

Now the app client only set authorization header, so it is only made to work for the API. I would like to have some support to auth a user for browser action as well.

Been trying to something like:

class HtmlClient < Lucky::BaseHTTPClient
  def initialize
    super
    headers("Content-Type": "text/html")
  end

  def self.auth(user : User)
    session = Lucky::Session.new
    session.set(BrowserAction::SIGN_IN_KEY, user.id.to_s)
    cookie = HTTP::Cookie.new(Lucky::Session.settings.key, session.to_json)
    new.headers("cookie": cookie.to_cookie_header)
  end
end

But it won't work as the cookie is set incorrectly.

While looking into the code, lucky have backdoor test for the user. But it seems to be only for LuckyFlow?

@jwoertink
Copy link
Member

Related: luckyframework/lucky_cli#492

@jwoertink
Copy link
Member

@paulcsmith
Copy link
Member

Anyone can use the backdoor by setting the param backdoor_user_id when in the test environment. So if you append that to your request it'll sign you in!

https://github.com/luckyframework/lucky_cli/blob/1c42ed1b38ff7fe921ee83fa182e34dba1db277e/src/browser_authentication_app_skeleton/src/actions/mixins/auth/test_backdoor.cr#L7

Either way though, it'd be nice to allow setting the session is possible. Not sure how it'd work but should be doable!

@akadusei
Copy link
Contributor

@confact

This is how I do it:

client = ApiClient.new

response = client.exec(Some::Action)

client.headers("Cookie": response.headers["Set-Cookie"])
response = client.exec(Another::Action)

I set response cookies from the previous request as the cookies for the next request. It's worked OK so far.

@jwoertink jwoertink added the hacktoberfest Valid Issue for Hacktoberfest label Oct 1, 2022
@jwoertink jwoertink removed the hacktoberfest Valid Issue for Hacktoberfest label Nov 6, 2022
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

No branches or pull requests

4 participants