lucky
lucky copied to clipboard
Add support for session in AppClient for tests
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?
Related: https://github.com/luckyframework/lucky_cli/issues/492
Bump: https://gitter.im/luckyframework/Lobby?at=5f49256ca5788a3c29baeb6b
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!
@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.