-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Optionally generate RSpec specs instead of Test::Unit tests #88
base: main
Are you sure you want to change the base?
Changes from all commits
08bcab9
bdd3c78
0f8c9c8
24353b4
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,36 @@ | ||
module Helpers | ||
module Authentication | ||
module Request | ||
<%- if options.api? -%> | ||
def sign_in_as(user) | ||
post(sign_in_url, params: { email: user.email, password: "Secret1*3*5*" }) | ||
response.headers["X-Session-Token"] | ||
end | ||
<%- else -%> | ||
def sign_in_as(user) | ||
post(sign_in_url, params: { email: user.email, password: "Secret1*3*5*" }) | ||
end | ||
<%- end -%> | ||
end | ||
|
||
module System | ||
def sign_in_as(user) | ||
visit sign_in_url | ||
fill_in :email, with: user.email | ||
fill_in :password, with: "Secret1*3*5*" | ||
click_on "Sign in" | ||
|
||
expect(current_path).to eq(root_url) | ||
user | ||
end | ||
end | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include Helpers::Authentication::Request, type: :request | ||
config.include Helpers::Authentication::System, type: :system | ||
|
||
config.include Capybara::RSpecMatchers, type: :request | ||
config.include ActiveSupport::Testing::TimeHelpers | ||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||
require "rails_helper" | ||||||
|
||||||
RSpec.describe "Sessions", type: :request do | ||||||
let(:user) { users(:lazaro_nixon) } | ||||||
let(:token) { sign_in_as(user) } | ||||||
let(:default_headers) { { "Authorization" => "Bearer #{token}" } } | ||||||
|
||||||
describe "GET #index" do | ||||||
it "returns HTTP success" do | ||||||
get sessions_url, headers: default_headers | ||||||
|
||||||
expect(response).to have_http_status(:success) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the expects can be like this:
Suggested change
|
||||||
end | ||||||
end | ||||||
|
||||||
describe "GET #show" do | ||||||
it "returns HTTP success" do | ||||||
get session_url(user.sessions.last), headers: default_headers | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here |
||||||
|
||||||
expect(response).to have_http_status(:success) | ||||||
end | ||||||
end | ||||||
|
||||||
describe "POST #create" do | ||||||
context "with valid credentials" do | ||||||
it "returns HTTP created" do | ||||||
post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" } | ||||||
|
||||||
expect(response).to have_http_status(:created) | ||||||
end | ||||||
end | ||||||
|
||||||
context "with invalid credentials" do | ||||||
it "returns HTTP unauthorized" do | ||||||
post sign_in_url, params: { email: user.email, password: "SecretWrong1*3" } | ||||||
|
||||||
expect(response).to have_http_status(:unauthorized) | ||||||
end | ||||||
end | ||||||
end | ||||||
|
||||||
describe "DELETE #destroy" do | ||||||
it "returns HTTP no content" do | ||||||
delete session_url(user.sessions.last), headers: default_headers | ||||||
|
||||||
expect(response).to have_http_status(:no_content) | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Identity::PasswordResetsController, type: :request do | ||
fixtures :users | ||
let(:user) { users(:lazaro_nixon) } | ||
|
||
describe "GET #new" do | ||
it "returns HTTP success" do | ||
get new_identity_password_reset_url | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "GET #edit" do | ||
let(:sid) { user.generate_token_for(:password_reset) } | ||
|
||
it "returns HTTP success" do | ||
get edit_identity_password_reset_url(sid: sid) | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "with a valid email" do | ||
it "sends a password reset email" do | ||
expect { | ||
post identity_password_reset_url, params: { email: user.email } | ||
}.to have_enqueued_mail(UserMailer, :password_reset) | ||
|
||
expect(response).to redirect_to(sign_in_url) | ||
end | ||
end | ||
|
||
context "with a nonexistent email" do | ||
it "does not send a password reset email" do | ||
expect { | ||
post identity_password_reset_url, params: { email: "[email protected]" } | ||
}.to_not have_enqueued_mail(UserMailer, :password_reset) | ||
|
||
expect(response).to redirect_to(new_identity_password_reset_url) | ||
expect(flash[:alert]).to eq("You can't reset your password until you verify your email") | ||
end | ||
end | ||
|
||
context "with an unverified email" do | ||
it "does not send a password reset email" do | ||
user.update!(verified: false) | ||
|
||
expect { | ||
post identity_password_reset_url, params: { email: user.email } | ||
}.to_not have_enqueued_mail(UserMailer, :password_reset) | ||
|
||
expect(response).to redirect_to(new_identity_password_reset_url) | ||
expect(flash[:alert]).to eq("You can't reset your password until you verify your email") | ||
end | ||
end | ||
end | ||
|
||
describe "PATCH #update" do | ||
let!(:sid) { user.generate_token_for(:password_reset) } | ||
|
||
context "with a valid token" do | ||
it "updates the password" do | ||
patch identity_password_reset_url, params: { sid: sid, password: "Secret6*4*2*", password_confirmation: "Secret6*4*2*" } | ||
|
||
expect(response).to redirect_to(sign_in_url) | ||
end | ||
end | ||
|
||
context "with an expired token" do | ||
it "does not update the password" do | ||
travel 30.minutes | ||
|
||
patch identity_password_reset_url, params: { sid: sid, password: "Secret6*4*2*", password_confirmation: "Secret6*4*2*" } | ||
|
||
expect(response).to redirect_to(new_identity_password_reset_url) | ||
expect(flash[:alert]).to eq("That password reset link is invalid") | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe PasswordsController, type: :request do | ||
fixtures :users | ||
let(:user) { users(:lazaro_nixon) } | ||
|
||
before do | ||
sign_in_as(user) | ||
end | ||
|
||
describe "GET #edit" do | ||
it "returns HTTP success" do | ||
get edit_password_url | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "PATCH #update" do | ||
context "with correct password challenge" do | ||
it "updates the password" do | ||
patch password_url, params: { password_challenge: "Secret1*3*5*", password: "Secret6*4*2*", password_confirmation: "Secret6*4*2*" } | ||
|
||
expect(response).to redirect_to(root_url) | ||
end | ||
end | ||
|
||
context "with wrong password challenge" do | ||
it "returns an error" do | ||
patch password_url, params: { password_challenge: "SecretWrong1*3", password: "Secret6*4*2*", password_confirmation: "Secret6*4*2*" } | ||
|
||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(response.body).to have_selector("li", text: /Password challenge is invalid/) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe RegistrationsController, type: :request do | ||
describe "GET #new" do | ||
it "returns HTTP success" do | ||
get sign_up_url | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
it "creates a new user" do | ||
expect { | ||
post sign_up_url, params: { email: "[email protected]", password: "Secret1*3*5*", password_confirmation: "Secret1*3*5*" } | ||
}.to change{ User.count }.by(1) | ||
|
||
expect(response).to redirect_to(root_url) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe SessionsController, type: :request do | ||
fixtures :users | ||
let(:user) { users(:lazaro_nixon) } | ||
|
||
describe "GET #index" do | ||
it "returns HTTP success" do | ||
sign_in_as(user) | ||
|
||
get sessions_url | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "GET #new" do | ||
it "returns HTTP success" do | ||
get sign_in_url | ||
|
||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
describe "POST #create" do | ||
context "with valid credentials" do | ||
it "signs the user in" do | ||
post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" } | ||
expect(response).to redirect_to(root_url) | ||
|
||
get root_url | ||
expect(response).to have_http_status(:success) | ||
end | ||
end | ||
|
||
context "with invalid credentials" do | ||
it "does not sign the user in" do | ||
post sign_in_url, params: { email: user.email, password: "SecretWrong1*3" } | ||
expect(response).to redirect_to(sign_in_url(email_hint: user.email)) | ||
|
||
get root_url | ||
expect(response).to redirect_to(sign_in_url) | ||
end | ||
end | ||
end | ||
|
||
describe "DELETE #destroy" do | ||
it "signs the user out" do | ||
sign_in_as(user) | ||
|
||
delete session_url(user.sessions.last) | ||
expect(response).to redirect_to(sessions_url) | ||
|
||
follow_redirect! | ||
expect(response).to redirect_to(sign_in_url) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
lazaro_nixon: | ||
email: [email protected] | ||
password_digest: <%= BCrypt::Password.create("Secret1*3*5*") %> | ||
verified: true |
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.
If you put a subject here, the call will be with is_expected and that is so good to read, personally.