forked from AdaGold/media-ranker-revisited
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Danielle - MediaRanker-Revisited #24
Open
danimetz
wants to merge
7
commits into
Ada-C10:master
Choose a base branch
from
danimetz:master
base: master
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.
+551
−64
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51de3dc
Updated Works_controller tests
danimetz d3d6e94
Added Oauth through github for login and logout
danimetz d00824a
Added session controller tests
danimetz 0f84577
Fixed before action for require_login in the session
danimetz 47d4122
Added tests for logged in users to works
danimetz ceaa573
Fixed tests for user and vote models
danimetz b193b8f
Added Users Controller tests
danimetz 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 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
/tmp/* | ||
!/log/.keep | ||
!/tmp/.keep | ||
.env | ||
|
||
# Ignore Byebug command history file. | ||
.byebug_history |
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 |
---|---|---|
@@ -1,34 +1,42 @@ | ||
class SessionsController < ApplicationController | ||
skip_before_action :require_login, only: [:create] | ||
def login_form | ||
end | ||
|
||
def login | ||
username = params[:username] | ||
if username and user = User.find_by(username: username) | ||
def create | ||
auth_hash = request.env['omniauth.auth'] | ||
|
||
user = User.find_by(uid: auth_hash[:uid], provider: 'github') | ||
|
||
if user | ||
# User was found in the database | ||
session[:user_id] = user.id | ||
flash[:status] = :success | ||
flash[:result_text] = "Successfully logged in as existing user #{user.username}" | ||
else | ||
user = User.new(username: username) | ||
user = User.build_from_github(auth_hash) | ||
|
||
if user.save | ||
session[:user_id] = user.id | ||
flash[:status] = :success | ||
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}" | ||
else | ||
flash.now[:status] = :failure | ||
flash.now[:result_text] = "Could not log in" | ||
flash.now[:messages] = user.errors.messages | ||
render "login_form", status: :bad_request | ||
flash[:status] = :failure | ||
flash[:result_text] = "Could not log in" | ||
flash[:messages] = user.errors.messages | ||
redirect_to root_path | ||
return | ||
end | ||
end | ||
|
||
session[:user_id] = user.id | ||
redirect_to root_path | ||
end | ||
|
||
def logout | ||
def destroy | ||
session[:user_id] = nil | ||
flash[:status] = :success | ||
flash[:result_text] = "Successfully logged out" | ||
flash[:success] = "Successfully logged out!" | ||
|
||
redirect_to root_path | ||
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
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,3 @@ | ||
Rails.application.config.middleware.use OmniAuth::Builder do | ||
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email" | ||
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Userfields < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :email, :string | ||
add_column :users, :uid, :integer, null: false | ||
add_column :users, :provider, :string, null: false | ||
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,5 +1,65 @@ | ||
require "test_helper" | ||
|
||
describe SessionsController do | ||
describe "Login" do | ||
it "logs in existing user and redirects to roote path" do | ||
|
||
start_count = User.count | ||
|
||
user = users(:grace) | ||
|
||
expect { | ||
perform_login(user) | ||
}.wont_change 'User.count' | ||
|
||
must_redirect_to root_path | ||
|
||
session[:user_id].must_equal user.id | ||
|
||
# Should *not* have created a new user | ||
User.count.must_equal start_count | ||
end | ||
|
||
it "creates a new user and redirects to root path" do | ||
user = users(:ada) | ||
user.destroy | ||
|
||
# Should have created a new user | ||
expect { | ||
perform_login(user) | ||
}.must_change 'User.count', 1 | ||
must_redirect_to root_path | ||
# The new user's ID should be set in the session | ||
session[:user_id].must_equal User.last.id | ||
end | ||
|
||
it "if given invalid user data redirects root path " do | ||
|
||
user = users(:ada) | ||
user.uid = nil | ||
#user.provider = nil | ||
|
||
expect { | ||
perform_login(user) | ||
}.wont_change 'User.count' | ||
|
||
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. You could also test a flash notice here. |
||
must_redirect_to root_path | ||
end | ||
end | ||
|
||
describe 'Logout' do | ||
it 'should logout user and redirect to root path' do | ||
user = users(:ada) | ||
|
||
perform_login(user) | ||
login_id = session[:user_id] | ||
delete logout_path | ||
logout_id = session[:user_id] | ||
|
||
expect(login_id).must_equal users(:ada).id | ||
expect(logout_id).must_be_nil | ||
|
||
must_redirect_to root_path | ||
end | ||
end | ||
end |
Oops, something went wrong.
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.
You also need to destroy the user's vote first, before destroying the user.