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

Test helpers #71

Closed
dankimio opened this issue Dec 29, 2019 · 9 comments · Fixed by #90
Closed

Test helpers #71

dankimio opened this issue Dec 29, 2019 · 9 comments · Fixed by #90

Comments

@dankimio
Copy link

Something like Devise's sign_in @user helper.

See also #29.

@mikker
Copy link
Owner

mikker commented Jan 2, 2020

Hi @dankimio!

I think this could be a nice addition. What kinds of tests would you expect to use this in? request, controller, system?

@dankimio
Copy link
Author

dankimio commented Jan 2, 2020

Hello!

I think both controller (integration) and system would be nice to have for starters, and would cover most cases.

Devise has IntegrationHelpers.

@Uysim
Copy link
Contributor

Uysim commented Feb 20, 2020

Since there is no help in this yet i do like this

  def passwordless_sign_in(resource)
    session = Passwordless::Session.create!(
      authenticatable: resource,
      user_agent: 'Command Line',
      remote_addr: 'unknown'
    )

    visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
  end

@ryenski
Copy link

ryenski commented Mar 16, 2020

How would you do this in controller tests?

@mikker
Copy link
Owner

mikker commented Mar 17, 2020

You could just mock it. If you're using RSpec, something like

def sign_in(user)
  allow_any_instance_of(ApplicationController).to receive(:current_user) { user }
end

@scomma
Copy link

scomma commented Apr 11, 2020

@crispinheneise you could use @Uysim's code and substitute get for visit

@ryenski
Copy link

ryenski commented Apr 15, 2020

@scomma yes, I did use @Uysim's suggestion.

Here's a module that can be included in test_helper.rb or dropped in test/support/

module PasswordlessSupport
  module TestCase
    def passwordless_sign_out
      delete Passwordless::Engine.routes.url_helpers.sign_out_path
    end

    def passwordless_sign_in(resource)
      session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
      get Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
    end
  end

  module SystemTestCase
    def passwordless_sign_in(resource)
      session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
      visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
    end
  end
end

class ActiveSupport::TestCase
  include ::PasswordlessSupport::TestCase
end
class ActionDispatch::SystemTestCase
  include ::PasswordlessSupport::SystemTestCase
end

Any recommendations on DRY-ing up this module?

@mikker
Copy link
Owner

mikker commented Apr 16, 2020

Looks good, @crispinheneise! I don't see a reason to DRY anything up. Very short and succinct 👍

@fa11enangel
Copy link

fa11enangel commented Sep 2, 2020

@scomma yes, I did use @Uysim's suggestion.

Here's a module that can be included in test_helper.rb or dropped in test/support/

module PasswordlessSupport
 ...
end

Any recommendations on DRY-ing up this module?

I've tried to use it. It works, but there is follow_redirect! missing for get and delete. If you'll sign_in or sign_out and try to access another url afterwards, it will be broken, as the redirect after login was not finished yet.

module PasswordlessSupport
  module TestCase
    def passwordless_sign_out
      delete Passwordless::Engine.routes.url_helpers.sign_out_path
      follow_redirect!
    end

    def passwordless_sign_in(resource)
      session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
      get Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
      follow_redirect!
    end
  end

  module SystemTestCase
    def passwordless_sign_in(resource)
      session = Passwordless::Session.create!(authenticatable: resource, user_agent: 'TestUnit', remote_addr: 'unknown')
      visit Passwordless::Engine.routes.url_helpers.token_sign_in_path(session.token)
    end
  end
end

class ActiveSupport::TestCase
  include ::PasswordlessSupport::TestCase
end
class ActionDispatch::SystemTestCase
  include ::PasswordlessSupport::SystemTestCase
end

This was referenced Oct 27, 2020
@mikker mikker closed this as completed in #90 Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants