From 5e5ad1049d01113736adf2b5a4f825bbb9b696e6 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Mon, 15 Oct 2018 19:24:46 -0700 Subject: [PATCH 01/23] Create and migrate database. Controller test for works#root --- db/schema.rb | 34 ++-- test/controllers/works_controller_test.rb | 207 ++++++++++++---------- 2 files changed, 127 insertions(+), 114 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 6bc8ba5c..a35df558 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,35 +10,35 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170407164321) do +ActiveRecord::Schema.define(version: 2017_04_07_164321) 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 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" diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 0945ca47..4a894daa 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -5,112 +5,125 @@ it "succeeds with all media types" do # Precondition: there is at least one media of each category + get root_path + must_respond_with :success end it "succeeds with one media type absent" do # Precondition: there is at least one media in two of the categories - end - - it "succeeds with no media" do - - end - end - - CATEGORIES = %w(albums books movies) - INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] - - describe "index" do - it "succeeds when there are works" do - - end - - it "succeeds when there are no works" do + #destroy some from fixture so have one less type + work = works(:movie) + id = work.id + work.destroy + get root_path + must_respond_with :success end - end - - describe "new" do - it "succeeds" do - - end - end - - describe "create" do - it "creates a work with valid data for a real category" do - - end - - it "renders bad_request and does not update the DB for bogus data" do - - end - - it "renders 400 bad_request for bogus categories" do - - end - - end - - describe "show" do - it "succeeds for an extant work ID" do - - end - - it "renders 404 not_found for a bogus work ID" do - - end - end - - describe "edit" do - it "succeeds for an extant work ID" do - end - - it "renders 404 not_found for a bogus work ID" do - - end - end - - describe "update" do - it "succeeds for valid data and an extant work ID" do - - end - - it "renders bad_request for bogus data" do - - end - - it "renders 404 not_found for a bogus work ID" do - - end - end - - describe "destroy" do - it "succeeds for an extant work ID" do - - end - - it "renders 404 not_found and does not update the DB for a bogus work ID" do - - end - end - - describe "upvote" do - - it "redirects to the work page if no user is logged in" do - - end - - it "redirects to the work page after the user has logged out" do - - end - - it "succeeds for a logged-in user and a fresh user-vote pair" do - - end - - it "redirects to the work page if the user has already voted for that work" do + it "succeeds with no media" do + works = Work.all + works.destroy_all + get root_path + must_respond_with :success end end + # + # CATEGORIES = %w(albums books movies) + # INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] + # + # describe "index" do + # it "succeeds when there are works" do + # + # end + # + # it "succeeds when there are no works" do + # + # end + # end + # + # describe "new" do + # it "succeeds" do + # + # end + # end + # + # describe "create" do + # it "creates a work with valid data for a real category" do + # + # end + # + # it "renders bad_request and does not update the DB for bogus data" do + # + # end + # + # it "renders 400 bad_request for bogus categories" do + # + # end + # + # end + # + # describe "show" do + # it "succeeds for an extant work ID" do + # + # end + # + # it "renders 404 not_found for a bogus work ID" do + # + # end + # end + # + # describe "edit" do + # it "succeeds for an extant work ID" do + # + # end + # + # it "renders 404 not_found for a bogus work ID" do + # + # end + # end + # + # describe "update" do + # it "succeeds for valid data and an extant work ID" do + # + # end + # + # it "renders bad_request for bogus data" do + # + # end + # + # it "renders 404 not_found for a bogus work ID" do + # + # end + # end + # + # describe "destroy" do + # it "succeeds for an extant work ID" do + # + # end + # + # it "renders 404 not_found and does not update the DB for a bogus work ID" do + # + # end + # end + # + # describe "upvote" do + # + # it "redirects to the work page if no user is logged in" do + # + # end + # + # it "redirects to the work page after the user has logged out" do + # + # end + # + # it "succeeds for a logged-in user and a fresh user-vote pair" do + # + # end + # + # it "redirects to the work page if the user has already voted for that work" do + # + # end + # end end From 5b731a4697e9da4638c2abc72bef1ff2d8563825 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Mon, 15 Oct 2018 19:59:17 -0700 Subject: [PATCH 02/23] Add works controller tests for index, new, and create --- test/controllers/works_controller_test.rb | 116 ++++++++++++++++------ 1 file changed, 83 insertions(+), 33 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 4a894daa..f612120d 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -29,40 +29,90 @@ must_respond_with :success end end + + CATEGORIES = %w(albums books movies) + INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] + + describe "index" do + it "succeeds when there are works" do + + get works_path + must_respond_with :success + end # - # CATEGORIES = %w(albums books movies) - # INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] - # - # describe "index" do - # it "succeeds when there are works" do - # - # end - # - # it "succeeds when there are no works" do - # - # end - # end - # - # describe "new" do - # it "succeeds" do - # - # end - # end - # - # describe "create" do - # it "creates a work with valid data for a real category" do - # - # end - # - # it "renders bad_request and does not update the DB for bogus data" do - # - # end - # - # it "renders 400 bad_request for bogus categories" do - # - # end - # - # end + it "succeeds when there are no works" do + + works = Work.all + works.destroy_all + + get works_path + must_respond_with :success + end + end + + describe "new" do + it "succeeds" do + + get new_work_path + must_respond_with :success + end + end + + describe "create" do + it "creates a work with valid data for a real category" do + work_hash = { + work: { + title: "Bad Cherry", + creator: "Nuna the Doe", + description: "Southern Fried Pixie Trapp", + publication_year: 2018, + category: "album" + } + } + + expect { + post works_path, params: work_hash + }.must_change 'Work.count', 1 + + must_respond_with :redirect + + expect(Work.last.title).must_equal work_hash[:work][:title] + end + + it "renders bad_request and does not update the DB for bogus data" do + work_hash = { + work: { + title: "Old Title", + creator: "Stella", + category: "album" + } + } + + expect { + post works_path, params: work_hash + }.wont_change 'Work.count' + + must_respond_with :bad_request + end + + it "renders 400 bad_request for bogus categories" do + work_hash = { + work: { + title: "Bad Cherry", + creator: "Lucy", + category: "booky" + } + } + + # work_hash[:work][:category] = "rock" + expect { + post works_path, params: work_hash + }.wont_change 'Work.count' + + must_respond_with :bad_request + end + + end # # describe "show" do # it "succeeds for an extant work ID" do From 3a297ada26bc53e4f5e50ca859f53ff109cc434c Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 11:39:53 -0700 Subject: [PATCH 03/23] Add OAuth gems --- .gitignore | 3 +++ Gemfile | 5 +++++ Gemfile.lock | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/.gitignore b/.gitignore index 48fb168f..415d132a 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ # Ignore Byebug command history file. .byebug_history + +# Ignore .env +.env diff --git a/Gemfile b/Gemfile index 42f4bb2c..34333cc7 100644 --- a/Gemfile +++ b/Gemfile @@ -62,7 +62,12 @@ 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] + +# Omni OAuth +gem "omniauth" +gem "omniauth-github" diff --git a/Gemfile.lock b/Gemfile.lock index 5b407e7e..f66ae74f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -207,6 +232,7 @@ DEPENDENCIES bootstrap (~> 4.1.3) byebug coffee-rails (~> 4.2) + dotenv-rails jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) @@ -214,6 +240,8 @@ DEPENDENCIES minitest-reporters minitest-skip minitest-spec-rails + omniauth + omniauth-github pg (~> 0.18) pry-rails puma (~> 3.0) From 0f4039586dcc0e63405a890d3beed43b2f046528 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 11:42:52 -0700 Subject: [PATCH 04/23] Add config initializer file for authorization --- config/initializers/omniauth.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 config/initializers/omniauth.rb diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 00000000..ef8c60c3 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,4 @@ +# config/initializers/omniauth.rb +Rails.application.config.middleware.use OmniAuth::Builder do + provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email" +end From f2a7247191cd3d688d0e660e13c5c9d6157f8716 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 12:16:10 -0700 Subject: [PATCH 05/23] Migrate to add columns for github auth --- app/controllers/sessions_controller.rb | 64 +++++++++++--------- config/routes.rb | 7 ++- db/migrate/20181016190723_add_authto_user.rb | 11 ++++ db/schema.rb | 6 +- 4 files changed, 55 insertions(+), 33 deletions(-) create mode 100644 db/migrate/20181016190723_add_authto_user.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5bce99e6..e9804a62 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,34 +1,40 @@ class SessionsController < ApplicationController - def login_form - end + # def login_form + # end + # + # 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}" + # else + # user = User.new(username: username) + # 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 + # return + # end + # end + # redirect_to root_path + # end + # + # def logout + # session[:user_id] = nil + # flash[:status] = :success + # flash[:result_text] = "Successfully logged out" + # redirect_to root_path + # end - 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}" - else - user = User.new(username: username) - 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 - return - end - end - redirect_to root_path + def create + auth_hash = request.env['omniauth.auth'] + # raise end - def logout - session[:user_id] = nil - flash[:status] = :success - flash[:result_text] = "Successfully logged out" - redirect_to root_path - end end diff --git a/config/routes.rb b/config/routes.rb index a7e8af1d..c1a5ebb4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,10 @@ 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' + # get '/login', to: 'sessions#login_form', as: 'login' + # post '/login', to: 'sessions#login' + # post '/logout', to: 'sessions#logout', as: 'logout' + get "/auth/:provider/callback", to: "sessions#create" resources :works post '/works/:id/upvote', to: 'works#upvote', as: 'upvote' diff --git a/db/migrate/20181016190723_add_authto_user.rb b/db/migrate/20181016190723_add_authto_user.rb new file mode 100644 index 00000000..b972fad6 --- /dev/null +++ b/db/migrate/20181016190723_add_authto_user.rb @@ -0,0 +1,11 @@ +class AddAuthtoUser < ActiveRecord::Migration[5.2] + def change + # Rails loves both symbols and implicit parens + add_column :users, :name, :string + add_column :users, :email, :string + add_column :users, :uid, :integer, null:false + # this is the identifier provided by GitHub + add_column :users, :provider, :string, null:false + # this tells us who provided the identifier + end +end diff --git a/db/schema.rb b/db/schema.rb index a35df558..0b5ad3a2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2017_04_07_164321) do +ActiveRecord::Schema.define(version: 2018_10_16_190723) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -19,6 +19,10 @@ 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", id: :serial, force: :cascade do |t| From 30e56239b5a60c4049582300863669b44c66a203 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 14:21:06 -0700 Subject: [PATCH 06/23] Comment out old login method in sessions. Make new sessions#create method that uses OAuth --- app/controllers/sessions_controller.rb | 95 ++++++++++++++++---------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e9804a62..75d3e6e9 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,40 +1,63 @@ class SessionsController < ApplicationController - # def login_form - # end - # - # 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}" - # else - # user = User.new(username: username) - # 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 - # return - # end - # end - # redirect_to root_path - # end - # - # def logout - # session[:user_id] = nil - # flash[:status] = :success - # flash[:result_text] = "Successfully logged out" - # redirect_to root_path - # end - - def create + def create auth_hash = request.env['omniauth.auth'] - # raise - end + user = User.find_by(uid: auth_hash[:uid], provider: 'github') + + if user + flash[:success] = "Logged in as returning user #{user.name}" + else + flash[:notice] = "Log in not successful." + # User doesn't match anything in the DB + # TODO: Attempt to create a new user + # user = User.new(uid: auth_hash[:uid] provider: 'github') + + user = User.add_user_from_github(auth_hash) + if user.save + flash[:success] = "Logged in as a new user #{user.username}" + else + 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 end + + +# Code for login and logout w/o OAuth +# def login_form +# end +# +# 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}" +# else +# user = User.new(username: username) +# 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 +# return +# end +# end +# redirect_to root_path +# end +# +# def logout +# session[:user_id] = nil +# flash[:status] = :success +# flash[:result_text] = "Successfully logged out" +# redirect_to root_path +# end From b3fd9e778e6e3f888add7b61fdff4b3f57c04fef Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 14:22:05 -0700 Subject: [PATCH 07/23] Add user model self method add_user_from_github --- app/models/user.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4cac8fe0..58af47a3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,18 @@ class User < ApplicationRecord has_many :ranked_works, through: :votes, source: :work validates :username, uniqueness: true, presence: true + + def self.add_user_from_github(auth_hash) + user = User.new + user.uid = auth_hash[:uid] + user.provider = 'github' + user.name = auth_hash['info']['name'] + user.email = auth_hash['info']['email'] + # use github nickname for something? + # do I need to have had it in db? or no? + user.username = auth_hash['info']['nickname'] + # Note that the user has not been saved + return user + end + end From af9118311b8d6794a61d762603ff70a7593c4f0c Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 14:59:06 -0700 Subject: [PATCH 08/23] Edit flash messages. Uncomment logout method --- app/controllers/sessions_controller.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 75d3e6e9..158a2431 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,10 +1,10 @@ class SessionsController < ApplicationController def create auth_hash = request.env['omniauth.auth'] - user = User.find_by(uid: auth_hash[:uid], provider: 'github') + user = User.find_by(uid: auth_hash[:uid], provider: auth_hash[:provider]) if user - flash[:success] = "Logged in as returning user #{user.name}" + flash[:success] = flash[:success] = "Logged in as returning user #{user.name}" else flash[:notice] = "Log in not successful." # User doesn't match anything in the DB @@ -14,21 +14,28 @@ def create user = User.add_user_from_github(auth_hash) if user.save - flash[:success] = "Logged in as a new user #{user.username}" + flash[:success] = "Logged in as a new user" else flash[:error] = "Could not create new user account #{user.errors.messages}" redirect_to root_path return - end + end end session[:user_id] = user.id redirect_to root_path end + + def logout + session[:user_id] = nil + flash[:status] = :success + flash[:result_text] = "Successfully logged out" + redirect_to root_path + end end -# Code for login and logout w/o OAuth +## Code for login and logout w/o OAuth # def login_form # end # From 5a4ffa543362b114c6681f7512a1b9c01c0b8296 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 15:00:25 -0700 Subject: [PATCH 09/23] Edit login link on app view to Login with Github --- app/views/layouts/application.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e7b07ce4..a5a06bde 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -45,7 +45,7 @@ <% else %> <% end %> From 9730b3c8cdebddbca959cbc8440aec0cbc4a1c5c Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 15:02:00 -0700 Subject: [PATCH 10/23] Uncomment sessions#logout route --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index c1a5ebb4..38d95c72 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ root 'works#root' # get '/login', to: 'sessions#login_form', as: 'login' # post '/login', to: 'sessions#login' - # post '/logout', to: 'sessions#logout', as: 'logout' + post '/logout', to: 'sessions#logout', as: 'logout' get "/auth/:provider/callback", to: "sessions#create" resources :works From 076567a40cfd4514283a4d237e7e37cf25f4b098 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 15:04:20 -0700 Subject: [PATCH 11/23] Edit auth_hash uid in user model --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 58af47a3..6f56eec3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,7 @@ class User < ApplicationRecord def self.add_user_from_github(auth_hash) user = User.new - user.uid = auth_hash[:uid] + user.uid = auth_hash['uid'] user.provider = 'github' user.name = auth_hash['info']['name'] user.email = auth_hash['info']['email'] From 7695011a361160c4d84b6c87df09035950b6f42e Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 16 Oct 2018 15:21:58 -0700 Subject: [PATCH 12/23] Edit model tests and fixtures to mirror OAuth user requirements --- test/fixtures/users.yml | 4 ++++ test/models/user_test.rb | 4 ++-- test/models/vote_test.rb | 4 ++-- test/models/work_test.rb | 4 ++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index e2968d78..0e8a7d8a 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -2,6 +2,10 @@ dan: username: dan + uid: 1 + provider: github kari: username: kari + uid: 2 + provider: github diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 793ce7e6..eccd7cac 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -28,12 +28,12 @@ it "requires a unique username" do username = "test username" - user1 = User.new(username: username) + user1 = User.new(username: username, provider: 'github', uid: 1) # This must go through, so we use create! user1.save! - user2 = User.new(username: username) + user2 = User.new(username: username, provider: 'github', uid: 2) result = user2.save result.must_equal false user2.errors.messages.must_include :username diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index f2615aa1..5a664804 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -16,8 +16,8 @@ end describe "validations" do - let (:user1) { User.new(username: 'chris') } - let (:user2) { User.new(username: 'chris') } + let (:user1) { User.new(username: 'chris', provider: 'github', uid: 1) } + let (:user2) { User.new(username: 'chris', provider: 'github', uid: 1) } let (:work1) { Work.new(category: 'book', title: 'House of Leaves') } let (:work2) { Work.new(category: 'book', title: 'For Whom the Bell Tolls') } diff --git a/test/models/work_test.rb b/test/models/work_test.rb index d9c00073..f8ba735a 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -83,7 +83,7 @@ it "tracks the number of votes" do work = Work.create!(title: "test title", category: "movie") 4.times do |i| - user = User.create!(username: "user#{i}") + user = User.create!(username: "user#{i}", provider: 'github', uid: i) Vote.create!(user: user, work: work) end work.vote_count.must_equal 4 @@ -97,7 +97,7 @@ # Create users to do the voting test_users = [] 20.times do |i| - test_users << User.create!(username: "user#{i}") + test_users << User.create!(username: "user#{i}", provider: "github", uid: i) end # Create media to vote upon From 4bd007e9348b7105d98135333bf21cb8c33f2701 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sat, 27 Oct 2018 13:03:50 -0700 Subject: [PATCH 13/23] Fix typo in sessions controller flash success --- app/controllers/sessions_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 158a2431..d8dcd4bd 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,7 +4,7 @@ def create user = User.find_by(uid: auth_hash[:uid], provider: auth_hash[:provider]) if user - flash[:success] = flash[:success] = "Logged in as returning user #{user.name}" + flash[:success] = "Logged in as returning user #{user.name}" else flash[:notice] = "Log in not successful." # User doesn't match anything in the DB From a11f184a398ffa133cde52c7b68365e792898a2e Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sat, 27 Oct 2018 14:20:24 -0700 Subject: [PATCH 14/23] Add tests for works_controller show action --- test/controllers/works_controller_test.rb | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index f612120d..6d854ff4 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -113,17 +113,24 @@ end end - # - # describe "show" do - # it "succeeds for an extant work ID" do - # - # end - # - # it "renders 404 not_found for a bogus work ID" do - # - # end - # end - # + + describe "show" do + it "succeeds for an extant work ID" do + existing_work = works(:album) + get work_path(existing_work.id) + + must_respond_with :success + end + + it "renders 404 not_found for a bogus work ID" do + deleted_work = works(:poodr) + deleted_work.destroy + get work_path(deleted_work.id) + + must_respond_with :missing + end + end + # describe "edit" do # it "succeeds for an extant work ID" do # From e71a258a75b61035284e1fafac2aac75c96f7366 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sat, 27 Oct 2018 16:00:16 -0700 Subject: [PATCH 15/23] Correct typo in works controller update action --- app/controllers/works_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 2020bee4..6084055c 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -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 From 5b4a6f2d8e3e22b7589d8c77e94cec88d38eeb23 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sat, 27 Oct 2018 16:00:54 -0700 Subject: [PATCH 16/23] Add tests for works_controller update action --- test/controllers/works_controller_test.rb | 93 +++++++++++++++++------ 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 6d854ff4..b63e31c8 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -131,29 +131,76 @@ end end - # describe "edit" do - # it "succeeds for an extant work ID" do - # - # end - # - # it "renders 404 not_found for a bogus work ID" do - # - # end - # end - # - # describe "update" do - # it "succeeds for valid data and an extant work ID" do - # - # end - # - # it "renders bad_request for bogus data" do - # - # end - # - # it "renders 404 not_found for a bogus work ID" do - # - # end - # end + describe "edit" do + it "succeeds for an extant work ID" do + existing_work = works(:album) + get edit_work_path(existing_work.id) + + must_respond_with :success + end + + it "renders 404 not_found for a bogus work ID" do + deleted_work = works(:poodr) + deleted_work.destroy + get edit_work_path(deleted_work.id) + + must_respond_with :missing + end + end + + describe "update" do + let (:work_hash) { + { + work: { + title: "Binti", + creator: "Nnedi Orokafor", + category: "book" + } + } + } + + it "succeeds for valid data and an extant work ID" do + existing_work = works(:another_album) + existing_id = existing_work.id + + expect { + patch work_path(existing_id), params: work_hash + }.wont_change 'Work.count' + + must_respond_with :redirect + + updated_work = Work.find_by(id: existing_id) + + expect(updated_work.title).must_equal work_hash[:work][:title] + expect(updated_work.creator).must_equal work_hash[:work][:creator] + expect(updated_work.category).must_equal work_hash[:work][:category] + end + + it "renders bad_request for bogus data" do + original_id = works(:movie).id + original_work = works(:movie) + work_hash[:work][:category] = "invalid category" + + expect { + patch work_path(original_id), params: work_hash + }.wont_change 'Work.count' + + must_respond_with :bad_request + + failed_update = Work.find_by(id: original_id) + expect(failed_update.title).must_equal original_work.title + expect(failed_update.category).must_equal original_work.category + end + + it "renders 404 not_found for a bogus work ID" do + id = 0 + expect { + patch work_path(id), params: work_hash + }.wont_change 'Work.count' + + must_respond_with :not_found + end + end # # describe "destroy" do # it "succeeds for an extant work ID" do From 6ca945fe634eded391cfc3aa95f7ee5d9a5eac08 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sun, 28 Oct 2018 17:59:46 -0700 Subject: [PATCH 17/23] Adds test for works controller destroy action --- test/controllers/works_controller_test.rb | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index b63e31c8..d54e6181 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -201,17 +201,24 @@ must_respond_with :not_found end end - # - # describe "destroy" do - # it "succeeds for an extant work ID" do - # - # end - # - # it "renders 404 not_found and does not update the DB for a bogus work ID" do - # - # end - # end - # + + describe "destroy" do + it "succeeds for an extant work ID" do + existing_id = works(:movie).id + + expect { + delete work_path(existing_id) + }.must_change 'Work.count', -1 + + must_respond_with :redirect + must_redirect_to root_path + end + + it "renders 404 not_found and does not update the DB for a bogus work ID" do + + end + end + # describe "upvote" do # # it "redirects to the work page if no user is logged in" do From fbb7586933b46d897f04331fe892366817e292e6 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sun, 28 Oct 2018 18:22:23 -0700 Subject: [PATCH 18/23] Add first test for works controller upvote action --- test/controllers/works_controller_test.rb | 34 +++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index d54e6181..873c2cba 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -215,20 +215,32 @@ end it "renders 404 not_found and does not update the DB for a bogus work ID" do + bogus_id = 0 + expect { + delete work_path(bogus_id) + }.wont_change 'Work.count' + + must_respond_with :not_found end end - # describe "upvote" do - # - # it "redirects to the work page if no user is logged in" do - # - # end - # - # it "redirects to the work page after the user has logged out" do - # - # end - # + describe "upvote" do + + it "redirects to the work page if no user is logged in" do + work = Work.first + + expect { + post upvote_path(work.id) + }.wont_change 'Vote.count' + + must_redirect_to work_path(work) + end + + # it "redirects to the work page after the user has logged out" do + # + # end + # it "succeeds for a logged-in user and a fresh user-vote pair" do # # end @@ -236,5 +248,5 @@ # it "redirects to the work page if the user has already voted for that work" do # # end - # end + end end From 775f735e485afb769af3491bbfa17dd3437a34f4 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sun, 28 Oct 2018 18:28:30 -0700 Subject: [PATCH 19/23] Delete Logged in as button in app layout page --- app/views/layouts/application.html.erb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a5a06bde..ca0afa82 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -34,19 +34,13 @@ From fe6f5c99ee032a673b0b5d0190d8b221bb007c05 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Sun, 28 Oct 2018 18:45:58 -0700 Subject: [PATCH 20/23] Add route helper to sessions create path --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 38d95c72..2d8519b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ # get '/login', to: 'sessions#login_form', as: 'login' # post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' - get "/auth/:provider/callback", to: "sessions#create" + get "/auth/:provider/callback", to: "sessions#create", as: 'login' resources :works post '/works/:id/upvote', to: 'works#upvote', as: 'upvote' From 42f6c8f4fc998b5ace156e8060ed6113b55b3686 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Mon, 29 Oct 2018 21:33:29 -0700 Subject: [PATCH 21/23] delete unused debugging statements --- app/controllers/sessions_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index d8dcd4bd..398e1521 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,7 +8,7 @@ def create else flash[:notice] = "Log in not successful." # User doesn't match anything in the DB - # TODO: Attempt to create a new user + # create a new user # user = User.new(uid: auth_hash[:uid] provider: 'github') user = User.add_user_from_github(auth_hash) From 4278334f0e461c8b599631783dcd0fdfe1007c1f Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Mon, 29 Oct 2018 21:35:42 -0700 Subject: [PATCH 22/23] Add to upvote works controller test. 1 failure --- test/controllers/works_controller_test.rb | 23 ++++++++++++++++++---- test/test_helper.rb | 24 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 873c2cba..c2aab187 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -238,12 +238,27 @@ end # it "redirects to the work page after the user has logged out" do - # + # logged_in_user = users(:dan) + # post login_path, params: user_params + # expect (:session[:user_id]).must_equal logged_in_user.id + # + # user # end - # it "succeeds for a logged-in user and a fresh user-vote pair" do - # - # end + it "succeeds for a logged-in user and a fresh user-vote pair" do + user = users(:dan) + + user_hash = { + username: user.username, + provider: user.provider, + uid: user.uid + } + + count = Vote.all.count + get login_path('github'), params: user_hash + + expect(session[:user_id]).must_equal user.id + end # # it "redirects to the work page if the user has already voted for that work" do # diff --git a/test/test_helper.rb b/test/test_helper.rb index 5b4fb667..99628bd7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,4 +23,28 @@ class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all # Add more helper methods to be used by all tests here... + def setup + OmniAuth.config.test_mode = true + end + + def mock_merchant_hash(user) + return { + uid: user.uid, + provider: "github", + info: { + nickname: user.username, + email: user.email + } + } + end + + def perform_login(user) + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new( mock_merchant_hash( user ) ) + + get auth_callback_path(:github) + end + + def perform_logout + post logout_path + end end From efb9f4036132d1a45b99e62c02ddfce754f19263 Mon Sep 17 00:00:00 2001 From: Katricia Smith Date: Tue, 30 Oct 2018 16:54:38 -0700 Subject: [PATCH 23/23] Edit test login path calls for upvote works controller --- test/controllers/works_controller_test.rb | 4 +++- test/test_helper.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index c2aab187..5da4f02f 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -255,7 +255,9 @@ } count = Vote.all.count - get login_path('github'), params: user_hash + perform_login(user) + + # get login_path('github'), params: user_hash expect(session[:user_id]).must_equal user.id end diff --git a/test/test_helper.rb b/test/test_helper.rb index 99628bd7..35274f9a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -41,7 +41,7 @@ def mock_merchant_hash(user) def perform_login(user) OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new( mock_merchant_hash( user ) ) - get auth_callback_path(:github) + get login_path(:github) end def perform_logout