-
-
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
Open
DiegoPisa2003
wants to merge
10
commits into
rspec:main
Choose a base branch
from
cedarcode:authentication-request-specs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c7b2a5
chore: Add request_specs option to authentication generator
DiegoPisa2003 c2191c9
chore: Add session request spec template and generator
DiegoPisa2003 41ffc24
chore: Add password request spec template and generator
DiegoPisa2003 1139850
test: add request specs tests for authentication generator
DiegoPisa2003 5f7714c
fix: Some small changes in new requests spec.
DiegoPisa2003 b30775d
chore: Unify unit tests from authentication_generator_spec.rb
DiegoPisa2003 ebe6bc5
chore: Change the sign_in_as method to the rails way
DiegoPisa2003 f26e3ac
feat: add authentication support module for request specs
DiegoPisa2003 045a4a2
chore: Remove fixture dependency from authentication generator specs
DiegoPisa2003 8eea380
test: assert uncommented support glob line in authentication generator
DiegoPisa2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
lib/generators/rspec/authentication/templates/authentication_support.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module AuthenticationSupport | ||
# Helper method to sign in a user for testing purposes | ||
# Uses the actual authentication flow via POST request | ||
def sign_in_as(user) | ||
post session_path, params: { | ||
email_address: user.email_address, | ||
password: "password" | ||
} | ||
end | ||
end |
79 changes: 79 additions & 0 deletions
79
lib/generators/rspec/authentication/templates/password_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Passwords", <%= type_metatag(:request) %> do | ||
# TODO: Replace with your factory or model creation method | ||
# For example, with FactoryBot: let(:user) { create(:user) } | ||
# or with fixtures: let(:user) { users(:one) } | ||
let(:user) { User.create!(email_address: "[email protected]", password: "password") } | ||
|
||
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 |
47 changes: 47 additions & 0 deletions
47
lib/generators/rspec/authentication/templates/session_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Sessions", <%= type_metatag(:request) %> do | ||
# TODO: Replace with your factory or model creation method | ||
# For example, with FactoryBot: let(:user) { create(:user) } | ||
# or with fixtures: let(:user) { users(:one) } | ||
let(:user) { User.create!(email_address: "[email protected]", password: "password") } | ||
|
||
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 | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Being pedantic, this only asserts the line was removed, I think prehaps match on the line without the #
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.
Oh yeah you are right, thanks. I will apply your suggestion