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 tests for devise #225

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/locales/nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ nb:
remember_me: "Husk meg"
forgot_password: "Glemt passordet ditt"
submit_login: "Logg inn"
password_reset: "Send meg passord instruksjoner"
password_reset: "Send instruksjoner for nytt passord"
change_password: "Endre ditt passord"
account: Din konto
edit_account: Rediger konto
Expand Down
3 changes: 3 additions & 0 deletions spec/fabricators/user_fabricator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

Fabricator(:user) do
email { "[email protected]" }
password { "password" }

first_name { Faker::Superhero.prefix + Faker::Superhero.descriptor }
last_name { Faker::Superhero.suffix }
email { Faker::Internet.email }
Expand Down
53 changes: 53 additions & 0 deletions spec/features/user_resets_password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

require "rails_helper"
require "support/devise_helpers"

RSpec.describe "User resets a password" do
include DeviseHelpers

it "user enters a valid email" do
user = Fabricate :user

visit new_user_password_path

fill_in_email user.email

expect do
click_reset_password_button
end.to change { ActionMailer::Base.deliveries.size }.by(1)

expect(page).to have_text I18n.t("devise.passwords.send_instructions")
expect(page).to have_current_path new_user_session_path
end

it "user enters an invalid email" do
visit new_user_password_path

fill_in_email "[email protected]"
click_reset_password_button

# Translation of Email not found
expect(page).to have_text I18n.t("errors.messages.not_found", attribute: "Email")
end

it "user changes password" do
token = Fabricate(:user).send_reset_password_instructions

visit edit_user_password_path(reset_password_token: token)

fill_in_and_ask_for_new_password

expect(page).to have_text I18n.t("devise.passwords.updated")
expect(page).to have_current_path root_path
end

it "password reset token is invalid" do
visit edit_user_password_path(reset_password_token: "fake_token")

fill_in_and_ask_for_new_password

expect(page).to have_no_text I18n.t("devise.passwords.updated")
expect(page).to have_no_current_path root_path
end
end
37 changes: 37 additions & 0 deletions spec/features/user_signs_in_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "rails_helper"
require "support/devise_helpers"

RSpec.describe "User signs in" do
include DeviseHelpers

it "with valid credentials" do
user = Fabricate :user

visit new_user_session_path

within ".new_user" do
fill_in_email user.email
fill_in_password user.password
click_login_button
end

expect(page).to have_text I18n.t("simple_form.labels.user.signed_in")
expect(page).to have_current_path root_path
end

it "with invalid credentials" do
user = Fabricate.build :user

visit new_user_session_path

within ".new_user" do
fill_in_email user.email
fill_in_password "wrong_password"
click_login_button
end

expect(page).to have_text I18n.t("devise.failure.invalid", authentication_keys: "E-post")
end
end
20 changes: 20 additions & 0 deletions spec/features/user_signs_out_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "rails_helper"
require "support/devise_helpers"

RSpec.describe "User signs out" do
include DeviseHelpers
it "user signed in" do
user = Fabricate :user

sign_in user

visit edit_session_path

click_on I18n.t("simple_form.labels.user.signout")

expect(page).to have_text I18n.t("simple_form.labels.user.signed_out")
expect(page).to have_current_path root_path
end
end
29 changes: 29 additions & 0 deletions spec/features/user_signs_up_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require "rails_helper"
require "support/devise_helpers"

RSpec.describe "User signs up" do
include DeviseHelpers

it "with valid data" do
visit new_user_registration_path

fill_in_email "[email protected]"
fill_in_password "password123456"
fill_in_password_confirmation "password123456"
click_on I18n.t("simple_form.labels.user.submit_registration")

expect(page).to have_text I18n.t("devise.registrations.signed_up")
expect(page).to have_current_path root_path
end

it "with invalid data" do
visit new_user_registration_path

click_on I18n.t("simple_form.labels.user.submit_registration")

expect(page).to have_text I18n.t("errors.messages.blank", attribute: "E-post")
expect(page).to have_text I18n.t("errors.messages.blank", attribute: "Passord")
end
end
5 changes: 5 additions & 0 deletions spec/support/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.include Devise::Test::IntegrationHelpers, type: :feature
end
33 changes: 33 additions & 0 deletions spec/support/devise_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module DeviseHelpers
def fill_in_email(email)
fill_in I18n.t("devise.registrations.user.email"), with: email
end

def click_reset_password_button
click_on I18n.t("simple_form.labels.user.password_reset")
end

def fill_in_password(password)
fill_in I18n.t("simple_form.labels.user.password"), with: password
end

def fill_in_password_confirmation(password)
fill_in I18n.t("simple_form.labels.user.password_confirmation"), with: password
end

def click_change_password_button
click_on I18n.t("simple_form.labels.user.change_password")
end

def fill_in_and_ask_for_new_password
fill_in_password "p4ssw0rd"
fill_in_password_confirmation "p4ssw0rd"
click_change_password_button
end

def click_login_button
click_on I18n.t("simple_form.labels.user.submit_login")
end
end
Loading