-
Notifications
You must be signed in to change notification settings - Fork 0
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
23 changed files
with
336 additions
and
42 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
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,9 @@ | ||
class UsersController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def show | ||
@user = User.friendly.find(params[:id]) | ||
rescue ActiveRecord::RecordNotFound | ||
redirect_to root_path, alert: "User not found" | ||
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,2 @@ | ||
module UsersHelper | ||
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
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 @@ | ||
Welcome <%= @user.name %> |
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,6 @@ | ||
class AddSlugToUsers < ActiveRecord::Migration[8.0] | ||
def change | ||
add_column :users, :slug, :string | ||
add_index :users, :slug, unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,4 +1,10 @@ | ||
require "test_helper" | ||
|
||
class DashboardControllerTest < ActionDispatch::IntegrationTest | ||
test "should get show" do | ||
user = users(:john) | ||
sign_in(user) | ||
get dashboard_path | ||
assert_response :success | ||
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,120 @@ | ||
require "test_helper" | ||
|
||
class SessionControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
# without this omniauth.origin can be set to the one in previous test (tags_url) resulting in flaky tests | ||
OmniAuth.config.before_callback_phase do |env| | ||
env["omniauth.origin"] = nil | ||
end | ||
end | ||
|
||
test "guest should not be able to access dashboard" do | ||
get dashboard_path | ||
assert_redirected_to root_path | ||
|
||
get dashboard_url | ||
assert_response :redirect | ||
assert_redirected_to root_path | ||
assert_equal "Please sign in first.", flash[:alert] | ||
end | ||
|
||
test "authenticated github user should get dashboard" do | ||
login_with_github | ||
|
||
get dashboard_url | ||
assert_response :success | ||
|
||
delete sign_out_path | ||
assert_response :redirect | ||
assert_redirected_to root_url | ||
assert_equal "Signed out", flash[:notice] | ||
end | ||
|
||
|
||
test "successful github sign in" do | ||
login_with_github | ||
|
||
assert_response :redirect | ||
assert_redirected_to dashboard_url | ||
email = OmniAuth.config.mock_auth[:github][:info][:email] | ||
name = OmniAuth.config.mock_auth[:github][:info][:name] | ||
assert_equal "Logged in as #{name}", flash[:notice] | ||
assert User.pluck(:email).include?(email) | ||
assert_equal controller.current_user.email, email | ||
end | ||
|
||
test "github oauth failure" do | ||
silence_omniauth_logger do | ||
OmniAuth.config.test_mode = true | ||
OmniAuth.config.mock_auth[:github] = :invalid_credentials | ||
Rails.application.env_config["omniauth.auth"] = OmniAuth.config.mock_auth[:github] | ||
get "/auth/github/callback" | ||
follow_redirect! | ||
|
||
assert_response :redirect | ||
assert_redirected_to root_path | ||
assert_equal "Authentication failed", flash[:alert] | ||
assert_nil controller.current_user | ||
end | ||
end | ||
|
||
test "github auth with no email" do | ||
silence_omniauth_logger do | ||
auth_hash = OmniAuth::AuthHash.new({ | ||
provider: "github", | ||
uid: "123545", | ||
info: { | ||
nickname: "test nickname", | ||
name: "test name", | ||
email: nil, | ||
image: "https://avatars.githubusercontent.com/u/123545?v=3" | ||
} | ||
}) | ||
|
||
OmniAuth.config.mock_auth[:github] = auth_hash | ||
Rails.application.env_config["omniauth.auth"] = auth_hash | ||
|
||
get "/auth/github/callback" | ||
|
||
assert_redirected_to dashboard_path | ||
assert_match "test name", flash[:notice] | ||
end | ||
end | ||
|
||
test "redirect to previous page after login" do | ||
user = users(:john) | ||
|
||
|
||
OmniAuth.config.before_callback_phase do |env| | ||
env["omniauth.origin"] = user_path(user) | ||
end | ||
|
||
sign_in(user) | ||
assert_response :redirect | ||
assert_redirected_to user_path(user) | ||
end | ||
|
||
test "github auth fails when user cannot be persisted" do | ||
silence_omniauth_logger do | ||
auth_hash = OmniAuth::AuthHash.new({ | ||
provider: "github", | ||
uid: "", # Invalid uid (blank) will cause persistence to fail | ||
info: { | ||
nickname: "test nickname", | ||
name: "foo", | ||
email: "[email protected]", | ||
image: "https://avatars.githubusercontent.com/u/123545?v=3" | ||
} | ||
}) | ||
|
||
OmniAuth.config.mock_auth[:github] = auth_hash | ||
Rails.application.env_config["omniauth.auth"] = auth_hash | ||
|
||
get "/auth/github/callback" | ||
|
||
assert_redirected_to root_url | ||
assert_equal "Failure", flash[:alert] | ||
assert_nil session[:user_id] | ||
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,30 @@ | ||
require "test_helper" | ||
|
||
class UsersControllerTest < ActionDispatch::IntegrationTest | ||
test "should redierect for unauthenticated user" do | ||
user = users(:john) | ||
get user_path(user) | ||
|
||
assert_response :redirect | ||
assert_redirected_to root_path | ||
assert_equal "Please sign in first.", flash[:alert] | ||
end | ||
|
||
test "should get show for authenticated user" do | ||
user = users(:john) | ||
sign_in(user) | ||
get user_path(user) | ||
|
||
assert_response :success | ||
end | ||
|
||
test "should raise error for non-existent user" do | ||
user = users(:john) | ||
sign_in(user) | ||
get user_path("non-existent-user") | ||
|
||
assert_response :redirect | ||
assert_redirected_to root_path | ||
assert_equal "User not found", flash[:alert] | ||
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
Oops, something went wrong.