-
-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
210 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,46 @@ | ||
require "clearance/testing/utils" | ||
|
||
module Clearance | ||
module Testing | ||
# Provides helpers to your request specs. | ||
# These are typically used in tests by requiring `clearance/rspec` or | ||
# `clearance/test_unit` as appropriate in your `rails_helper.rb` or | ||
# `test_helper.rb` files. | ||
module RequestHelpers | ||
include Clearance::Testing::Utils | ||
|
||
# Signs in a user that is created using FactoryBot or FactoryGirl. | ||
# The factory name is derrived from your `user_class` Clearance | ||
# configuration. | ||
# | ||
# @raise [RuntimeError] if FactoryBot or FactoryGirl is not defined. | ||
def sign_in | ||
sign_in_as create_user(password: "password"), password: "password" | ||
end | ||
|
||
# Signs in the provided user. | ||
# | ||
# @param [User class] user | ||
# @param [String] password | ||
# @param [String] path | ||
|
||
# @return user | ||
def sign_in_as(user, password:, path: session_path) | ||
post path, params: { | ||
session: { email: user.email, password: password } | ||
} | ||
|
||
user | ||
end | ||
|
||
# Signs out a user that may be signed in. | ||
# | ||
# @param [String] path | ||
# | ||
# @return [void] | ||
def sign_out(path: sign_out_path) | ||
delete path | ||
end | ||
end | ||
end | ||
end |
This file contains 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,35 @@ | ||
module Clearance | ||
module Testing | ||
module Utils | ||
private | ||
|
||
# Creates a user using FactoryBot or FactoryGirl. | ||
# The factory name is derrived from `user_class` Clearance | ||
# configuration. | ||
# | ||
# @api private | ||
# @raise [RuntimeError] if FactoryBot or FactoryGirl is not defined. | ||
def create_user(**factory_options) | ||
constructor = factory_module("sign_in") | ||
|
||
factory = Clearance.configuration.user_model.to_s.underscore.to_sym | ||
constructor.create(factory, **factory_options) | ||
end | ||
|
||
# Determines the appropriate factory library | ||
# | ||
# @api private | ||
# @raise [RuntimeError] if both FactoryGirl and FactoryBot are not | ||
# defined. | ||
def factory_module(provider) | ||
if defined?(FactoryBot) | ||
FactoryBot | ||
elsif defined?(FactoryGirl) | ||
FactoryGirl | ||
else | ||
raise("Clearance's `#{provider}` helper requires factory_bot") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,80 @@ | ||
require "spec_helper" | ||
|
||
class SecretsController < ActionController::Base | ||
include Clearance::Controller | ||
|
||
before_action :require_login | ||
|
||
def index | ||
render json: { status: :ok } | ||
end | ||
end | ||
|
||
describe "Secrets managament", type: :request do | ||
before do | ||
Rails.application.routes.draw do | ||
clearance_routes_file = File.read(Rails.root.join("config", "routes.rb")) | ||
instance_eval(clearance_routes_file) # drawing clearance routes | ||
post "/my_sign_in" => "clearance/sessions#create", as: "my_sign_in" | ||
resources :secrets, only: :index | ||
end | ||
end | ||
|
||
describe "GET #index" do | ||
context "with default authenticated user" do | ||
it "renders action response" do | ||
sign_in | ||
|
||
get secrets_path | ||
|
||
expect(response.body).to eq({ status: :ok }.to_json) | ||
end | ||
end | ||
|
||
context "with custom authenticated user" do | ||
it "renders action response" do | ||
user = create(:user, password: "my-password") | ||
sign_in_as(user, password: "my-password") | ||
|
||
get secrets_path | ||
|
||
expect(response.body).to eq({ status: :ok }.to_json) | ||
end | ||
end | ||
|
||
context "with custom sign in path" do | ||
it "renders action response" do | ||
user = create(:user, password: "my-password") | ||
sign_in_as(user, password: "my-password", path: my_sign_in_path) | ||
|
||
get secrets_path | ||
|
||
expect(response.body).to eq({ status: :ok }.to_json) | ||
end | ||
end | ||
|
||
context "without authenticated user" do | ||
it "redirects to sign in" do | ||
get secrets_path | ||
|
||
expect(response).to redirect_to(sign_in_path) | ||
end | ||
end | ||
|
||
context "with signed out user" do | ||
it "redirects to sign in" do | ||
sign_in | ||
|
||
get secrets_path | ||
|
||
expect(response.body).to eq({ status: :ok }.to_json) | ||
|
||
sign_out | ||
|
||
get secrets_path | ||
|
||
expect(response).to redirect_to(sign_in_path) | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
This file contains 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