Skip to content
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

Val - Edges - MediaRanker Revisited #28

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fd1bc5f
Create and migrate db
valgidzi Oct 15, 2018
d69490d
Add WorksController#root tests - passing
valgidzi Oct 15, 2018
807952b
Add WorksController#index and #new tests - passing
valgidzi Oct 15, 2018
0850bd7
Add 2 WorksController#create tests - passing
valgidzi Oct 16, 2018
4b4b0b4
Add OAuth github login/logout functionality
valgidzi Oct 16, 2018
65787e0
Update tests and test data for new required User data - uid and provi…
valgidzi Oct 16, 2018
7f31e6f
Add WorksController#create, #show, #edit tests
valgidzi Oct 16, 2018
2ea8a56
Add flash assertions to WorksController#create tests
valgidzi Oct 27, 2018
f22e20b
Fix show and edit test syntax
valgidzi Oct 27, 2018
68a5d6d
Add #update tests
valgidzi Oct 28, 2018
5155368
Refactor WorksController#create tests
valgidzi Oct 28, 2018
d9b40a2
Update WorksController#create tests for all valid and invalid categories
valgidzi Oct 28, 2018
04337e4
Refactor WorksController#update tests
valgidzi Oct 28, 2018
86136b9
Add WorksController#destroy tests, adjust bad_id variable
valgidzi Oct 28, 2018
69adcf1
Add path name for auth_callback route
valgidzi Oct 28, 2018
b17f024
Change username to GH nickname instead of email
valgidzi Oct 28, 2018
745bcbb
Refactor SessionsController
valgidzi Oct 28, 2018
bceff3b
Add helper methods for SessionsController tests
valgidzi Oct 28, 2018
89e5696
Fix mock_auth_hash
valgidzi Oct 28, 2018
c47b62e
Change provider of test user
valgidzi Oct 28, 2018
afb2658
Add SessionsController tests for logging in and out
valgidzi Oct 28, 2018
cfb4207
Fix WorksController#update method per slack/project notifications
valgidzi Oct 28, 2018
797f471
Fix WorksController#update test per slack change
valgidzi Oct 28, 2018
8508579
Add tests for WorksController#upvote
valgidzi Oct 28, 2018
8459e00
Add tests for UsersController
valgidzi Oct 28, 2018
0c87a71
Refactor WorksController tests
valgidzi Oct 28, 2018
a10574b
Add require_login method to ApplicationController
valgidzi Oct 30, 2018
1893999
Add UsersController#show test, add login method to all tests, fix tes…
valgidzi Oct 30, 2018
75cdf3d
Add controller filter, skip require_login before SessionsController#c…
valgidzi Oct 30, 2018
674e52f
Adjust provider name in user test data
valgidzi Oct 30, 2018
a96dcc9
Reorganize tests for logged in/guest user, test for all valid and inv…
valgidzi Oct 30, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@

# Ignore Byebug command history file.
.byebug_history

.env
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

gem "omniauth"
gem "omniauth-github"
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,18 @@ GEM
concurrent-ruby (1.0.5)
crass (1.0.4)
debug_inspector (0.0.3)
dotenv (2.5.0)
dotenv-rails (2.5.0)
dotenv (= 2.5.0)
railties (>= 3.2, < 6.0)
erubi (1.7.1)
execjs (2.7.0)
faraday (0.15.3)
multipart-post (>= 1.2, < 3)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashie (3.5.7)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
Expand All @@ -84,6 +91,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (2.1.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -113,9 +121,26 @@ GEM
minitest (~> 5.0)
rails (>= 4.1)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.3.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.8.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-github (1.3.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-oauth2 (1.5.0)
oauth2 (~> 1.1)
omniauth (~> 1.2)
pg (0.21.0)
popper_js (1.14.3)
pry (0.11.3)
Expand Down Expand Up @@ -207,13 +232,16 @@ DEPENDENCIES
bootstrap (~> 4.1.3)
byebug
coffee-rails (~> 4.2)
dotenv-rails
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
minitest-rails
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
before_action :require_login, except: [:root]

def render_404
# DPR: this will actually render a 404 page in production
raise ActionController::RoutingError.new('Not Found')
end


private
def find_user
if session[:user_id]
@login_user = User.find_by(id: session[:user_id])
end
end
def require_login
if @login_user.nil?
flash[:status] = :failure
flash[:result_text] = "You must be logged in to do that."
redirect_back(fallback_location: root_path)
end
end
end
30 changes: 14 additions & 16 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
class SessionsController < ApplicationController
def login_form
end
skip_before_action :require_login, only: [:create]

def create
auth_hash = request.env['omniauth.auth']
user = User.find_by(uid: auth_hash[:uid], provider: 'github')

def login
username = params[:username]
if username and user = User.find_by(username: username)
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully logged in as existing user #{user.username}"
if user
flash[:success] = "Logged in as returning user #{user.name}"
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}"
flash[:success] = "Logged in as new user #{user.name}"
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[:error] = "Could not create new user account: #{user.errors.messages}"
redirect_to root_path
return
end
end

session[:user_id] = user.id
redirect_to root_path
end

Expand All @@ -31,4 +28,5 @@ def logout
flash[:result_text] = "Successfully logged out"
redirect_to root_path
end

end
2 changes: 1 addition & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def update
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :not_found
render :edit, status: :bad_request
end
end

Expand Down
13 changes: 13 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ class User < ApplicationRecord
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true

def self.build_from_github(auth_hash)
user = User.new
user.username = auth_hash['info']['nickname']
user.uid = auth_hash[:uid]
user.provider = 'github'
user.name = auth_hash['info']['name']
user.email = auth_hash['info']['email']

# Note that the user has not been saved
user.save
return user
end
end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "btn btn-primary" %>
</li>
<li class="nav-item app-header__nav_item">
<%= link_to "Log Out", logout_path, method: :post, class: "btn btn-primary" %>
<%= link_to "Log Out", logout_path, method: :delete, class: "btn btn-primary" %>
</li>

<% else %>

<li class="nav-item app-header__nav_item">
<%= link_to "Log In", login_path, class: "btn btn-primary" %>
<%= link_to "Login with Github", "/auth/github", class: "btn btn-primary" %>
</li>
<% end %>

Expand Down
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
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
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'works#root'
get '/login', to: 'sessions#login_form', as: 'login'
post '/login', to: 'sessions#login'
post '/logout', to: 'sessions#logout', as: 'logout'

resources :works
post '/works/:id/upvote', to: 'works#upvote', as: 'upvote'

resources :users, only: [:index, :show]

get "/auth/:provider/callback", to: "sessions#create", as: 'auth_callback'
delete '/logout', to: 'sessions#logout', as: 'logout'
end
8 changes: 8 additions & 0 deletions db/migrate/20181016184222_add_auth_columns_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddAuthColumnsToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :name, :string
add_column :users, :email, :string
add_column :users, :uid, :integer, null: false
add_column :users, :provider, :string, null: false
end
end
38 changes: 21 additions & 17 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,39 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170407164321) do
ActiveRecord::Schema.define(version: 2018_10_16_184222) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
t.string "username"
create_table "users", id: :serial, force: :cascade do |t|
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name"
t.string "email"
t.integer "uid", null: false
t.string "provider", null: false
end

create_table "votes", force: :cascade do |t|
t.integer "user_id"
t.integer "work_id"
create_table "votes", id: :serial, force: :cascade do |t|
t.integer "user_id"
t.integer "work_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_votes_on_user_id", using: :btree
t.index ["work_id"], name: "index_votes_on_work_id", using: :btree
t.index ["user_id"], name: "index_votes_on_user_id"
t.index ["work_id"], name: "index_votes_on_work_id"
end

create_table "works", force: :cascade do |t|
t.string "title"
t.string "creator"
t.string "description"
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "vote_count", default: 0
t.integer "publication_year"
create_table "works", id: :serial, force: :cascade do |t|
t.string "title"
t.string "creator"
t.string "description"
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "vote_count", default: 0
t.integer "publication_year"
end

add_foreign_key "votes", "users"
Expand Down
50 changes: 50 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
require "test_helper"

describe SessionsController do
let(:user) {users(:dan)}

describe "create" do

it "can successfully log in with github as an existing user" do
perform_login(user)

expect(flash[:success]).must_equal "Logged in as returning user #{user.name}"
expect(session[:user_id]).must_equal user.id
must_redirect_to root_path
end

it "can successfully create a new user when logging in with valid data" do
new_user = User.new(username: 'new user', uid: 1, provider: 'github')
new_user.must_be :valid?, "Invalid user data in test."

expect {
perform_login(new_user)
}.must_change('User.count', +1)

expect(flash[:success]).must_equal "Logged in as new user #{user.name}"
expect(session[:user_id]).must_equal User.last.id
must_redirect_to root_path
end

it "does not create a new user when logging in with invalid data" do
invalid_user = User.new()
invalid_user.wont_be :valid?, "Valid user data in test."

expect {
perform_login(invalid_user)
}.wont_change('User.count')

expect(session[:user_id]).must_equal nil
expect(flash[:error]).must_include "Could not create new user account"
must_redirect_to root_path
end
end

describe "logout" do
it "can successfully log out a logged in user" do
perform_login(user)

delete logout_path(user)

expect(session[:user_id]).must_equal nil
expect(flash[:status]).must_equal :success
must_redirect_to root_path
end

end
end
26 changes: 26 additions & 0 deletions test/controllers/users_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
require 'test_helper'

describe UsersController do
describe 'logged in users' do
let (:dan) {users(:dan)}
let (:kari) {users(:kari)}

describe 'index' do
it 'succeeds when there are users' do
perform_login(dan)
get users_path
must_respond_with :success
end
end


describe 'show' do
it 'succeeds when user exists' do
perform_login(dan)
get user_path(kari)
must_respond_with :success
end

it 'renders 404 if user does not exist' do
perform_login(dan)
get user_path(User.last.id + 1)
must_respond_with :not_found
end
end
end
end
Loading