-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add request specs generation to authentication generator #2863
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
7c7b2a5
c2191c9
41ffc24
1139850
5f7714c
b30775d
ebe6bc5
f26e3ac
045a4a2
8eea380
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Passwords", <%= type_metatag(:request) %> do | ||
fixtures :users | ||
|
||
let(:user) { users(:one) } | ||
|
||
describe "GET /password/new" do | ||
it "returns http success" do | ||
get new_password_path | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST /password" do | ||
it "sends password reset email for valid user" do | ||
expect { | ||
post passwords_path, params: { email_address: user.email_address } | ||
}.to have_enqueued_mail(PasswordsMailer, :reset).with(user) | ||
|
||
expect(response).to redirect_to(new_session_path) | ||
|
||
follow_redirect! | ||
expect(flash[:notice]).to eq("Password reset instructions sent (if user with that email address exists).") | ||
end | ||
|
||
it "handles invalid email gracefully" do | ||
expect { | ||
post passwords_path, params: { email_address: "[email protected]" } | ||
}.not_to have_enqueued_mail | ||
|
||
expect(response).to redirect_to(new_session_path) | ||
|
||
follow_redirect! | ||
expect(flash[:notice]).to eq("Password reset instructions sent (if user with that email address exists).") | ||
end | ||
end | ||
|
||
describe "GET /password/edit" do | ||
it "returns http success with valid token" do | ||
get edit_password_path(user.password_reset_token) | ||
expect(response).to have_http_status(:success) | ||
end | ||
|
||
it "redirects with invalid password reset token" do | ||
get edit_password_path("invalid token") | ||
expect(response).to redirect_to(new_password_path) | ||
|
||
follow_redirect! | ||
expect(flash[:alert]).to eq("Password reset link is invalid or has expired.") | ||
end | ||
end | ||
|
||
describe "PATCH /password" do | ||
it "updates password with valid token and password" do | ||
expect { | ||
patch password_path(user.password_reset_token), params: { password: "new", password_confirmation: "new" } | ||
}.to change { user.reload.password_digest } | ||
|
||
expect(response).to redirect_to(new_session_path) | ||
|
||
follow_redirect! | ||
expect(flash[:notice]).to eq("Password has been reset.") | ||
end | ||
|
||
it "rejects non matching passwords" do | ||
token = user.password_reset_token | ||
expect { | ||
patch password_path(token), params: { password: "no", password_confirmation: "match" } | ||
}.not_to change { user.reload.password_digest } | ||
|
||
expect(response).to redirect_to(edit_password_path(token)) | ||
|
||
follow_redirect! | ||
expect(flash[:alert]).to eq("Passwords did not match.") | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Sessions", <%= type_metatag(:request) %> do | ||
fixtures :users | ||
|
||
let(:user) { users(:one) } | ||
|
||
|
||
describe "GET /new_session" do | ||
it "returns http success" do | ||
get new_session_path | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST /session" do | ||
context "with valid credentials" do | ||
it "redirects to root path and sets session cookie" do | ||
post session_path, params: { email_address: user.email_address, password: "password" } | ||
|
||
expect(response).to redirect_to(root_path) | ||
expect(cookies[:session_id]).to be_present | ||
end | ||
end | ||
|
||
context "with invalid credentials" do | ||
it "redirects to new session path and does not set session cookie" do | ||
post session_path, params: { email_address: user.email_address, password: "wrong" } | ||
|
||
expect(response).to redirect_to(new_session_path) | ||
expect(cookies[:session_id]).to be_nil | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE /session" do | ||
it "logs out the current user and redirects to new session path" do | ||
# Simulate being signed in | ||
sign_in_as(user) | ||
|
||
delete session_path | ||
|
||
expect(response).to redirect_to(new_session_path) | ||
expect(cookies[:session_id]).to be_empty | ||
end | ||
end | ||
|
||
private | ||
|
||
def sign_in_as(user) | ||
|
||
Current.session = user.sessions.create! | ||
|
||
ActionDispatch::TestRequest.create.cookie_jar.tap do |cookie_jar| | ||
cookie_jar.signed[:session_id] = Current.session.id | ||
cookies[:session_id] = cookie_jar[:session_id] | ||
end | ||
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below