-
-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Specs: add tests for redirects in app.rb
Also made it so that auth check applies in test mode as well. Needed to make a couple of other changes to tests to get them working.
- Loading branch information
Showing
6 changed files
with
68 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "spec_helper" | ||
require "support/active_record" | ||
|
||
describe "App" do | ||
context "when user is not authenticated and page requires authentication" do | ||
it "sets the session redirect_to" do | ||
create_user(:setup_complete) | ||
|
||
get("/news") | ||
|
||
expect(session[:redirect_to]).to eq("/news") | ||
end | ||
|
||
it "redirects to /login" do | ||
create_user(:setup_complete) | ||
|
||
get("/news") | ||
|
||
expect(last_response).to be_redirect | ||
expect(last_response.headers["Location"]).to end_with("/login") | ||
end | ||
end | ||
|
||
it "does not redirect when page needs no authentication" do | ||
create_user(:setup_complete) | ||
|
||
get("/login") | ||
|
||
expect(last_response).not_to be_redirect | ||
end | ||
|
||
it "does not redirect when user is authenticated" do | ||
user = create_user(:setup_complete) | ||
|
||
get("/news", {}, "rack.session" => { user_id: user.id }) | ||
|
||
expect(last_response).not_to be_redirect | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
module Factories | ||
def create_user(params = {}) | ||
build_user(params).tap(&:save!) | ||
USER_TRAITS = { | ||
setup_complete: -> { { setup_complete: true } } | ||
}.freeze | ||
|
||
def create_user(*traits, **params) | ||
build_user(*traits, **params).tap(&:save!) | ||
end | ||
|
||
def build_user(params = {}) | ||
def build_user(*traits, **params) | ||
traits.each { |trait| params.merge!(USER_TRAITS.fetch(trait).call) } | ||
|
||
User.new(password: "super-secret", **params) | ||
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