From a71c4f662dec6a925486315f2ad5d2c4d33cdf9d Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 13:50:46 -0700 Subject: [PATCH 01/28] added test for root. --- test/controllers/works_controller_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 0945ca47..05c10809 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -5,6 +5,23 @@ it "succeeds with all media types" do # Precondition: there is at least one media of each category + #check precondition + categories = %w(book album movie) + check = [] + + categories.each do |category| + work = Work.find_by(category: category) + check << work + end + + check.each do |work| + work.must_be :valid? + end + + get root_path + + must_respond_with :success + end it "succeeds with one media type absent" do From ed3b3e94e9fd19580ab832a924f66bca46bc8d38 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 13:52:36 -0700 Subject: [PATCH 02/28] added test for root. --- test/controllers/works_controller_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 05c10809..010ef66f 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -27,6 +27,15 @@ it "succeeds with one media type absent" do # Precondition: there is at least one media in two of the categories + works_to_delete = Work.where(category: "album") + + works_to_delete.destroy + + #check precondition + + work.find_by(category: "album").must_be_nil + + end it "succeeds with no media" do From 43f4319117609e6f22c0e20210022ba5d64c55eb Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:00:09 -0700 Subject: [PATCH 03/28] completed first stub sections for root. --- test/controllers/works_controller_test.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 010ef66f..3ac54876 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -27,18 +27,33 @@ it "succeeds with one media type absent" do # Precondition: there is at least one media in two of the categories - works_to_delete = Work.where(category: "album") + work_to_delete = Work.where(category: "album") - works_to_delete.destroy + work_to_delete.each do |work| + work.destroy + end #check precondition - work.find_by(category: "album").must_be_nil + Work.where(category: "album").must_be :empty? + get root_path + + must_respond_with :success end it "succeeds with no media" do + old_work_count = Work.count + works = Work.all + works.each { |work| work.destroy } + + #test precondition + Work.count.must_equal 0 + + get root_path + + must_respond_with :success end end From b43600c6fdfba4e9ca25b7e4f3ead0a5bcd86b3f Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:02:41 -0700 Subject: [PATCH 04/28] completed stubbed tests for index. --- test/controllers/works_controller_test.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 3ac54876..c96e8a2d 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -44,7 +44,7 @@ end it "succeeds with no media" do - old_work_count = Work.count + works = Work.all works.each { |work| work.destroy } @@ -63,11 +63,25 @@ describe "index" do it "succeeds when there are works" do + Work.any?.must_equal true + + get works_path + must_respond_with :success end it "succeeds when there are no works" do + works = Work.all + works.each { |work| work.destroy } + + #test precondition + Work.count.must_equal 0 + + get works_path + + must_respond_with :success + end end From 2b822c3cac72acf52fce1f10c74bc35d0b73badb Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:04:05 -0700 Subject: [PATCH 05/28] completed stubbed test for new. --- test/controllers/works_controller_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index c96e8a2d..d128c3ab 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -87,7 +87,9 @@ describe "new" do it "succeeds" do + get new_work_path + must_respond_with :success end end From 2388c3efee40e7a420ac21bba301d3e24d2e239c Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:12:16 -0700 Subject: [PATCH 06/28] completed stubbed tests for create --- test/controllers/works_controller_test.rb | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index d128c3ab..d4b7910b 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -95,15 +95,66 @@ describe "create" do it "creates a work with valid data for a real category" do + work_data = { + title: "Some Work", + category: CATEGORIES.sample.singularize + } + # Test assumptions + Work.new(work_data).must_be :valid? + + old_work_count = Work.count + + # Act + post works_path params: { work: work_data } + + # Assert + must_respond_with :redirect + must_redirect_to work_path(Work.last) + + Work.count.must_equal old_work_count + 1 + Work.last.title.must_equal work_data[:title] end it "renders bad_request and does not update the DB for bogus data" do + work_data = { + title: "", + category: CATEGORIES.sample.singularize + } + + # Test assumptions + Work.new(work_data).wont_be :valid? + + old_work_count = Work.count + + # Act + post works_path params: { work: work_data } + + # Assert + must_respond_with :bad_request + + Work.count.must_equal old_work_count end it "renders 400 bad_request for bogus categories" do + work_data = { + title: "Some Work", + category: INVALID_CATEGORIES.sample.singularize + } + + # Test assumptions + Work.new(work_data).wont_be :valid? + + old_work_count = Work.count + + # Act + post works_path params: { work: work_data } + + # Assert + must_respond_with :bad_request + Work.count.must_equal old_work_count end end From e5927656d812475d078974e1f39dfbbbe9fef12f Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:14:03 -0700 Subject: [PATCH 07/28] completed stubbed tests for show. --- test/controllers/works_controller_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index d4b7910b..97065f05 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -162,9 +162,20 @@ describe "show" do it "succeeds for an extant work ID" do + work = Work.first + + get work_path(work) + + must_respond_with :success + end it "renders 404 not_found for a bogus work ID" do + work = Work.last.id + 1 + + get work_path(work) + + must_respond_with :missing end end From 7fbbaf4b1c8efe5d0afe37d1941cb053e6a583d0 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:14:39 -0700 Subject: [PATCH 08/28] completed stubbed tests for show. --- test/controllers/works_controller_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 97065f05..366ac002 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -182,7 +182,11 @@ describe "edit" do it "succeeds for an extant work ID" do + work = Work.first + + get edit_work_path(work) + must_respond_with :success end it "renders 404 not_found for a bogus work ID" do From f2caf4ca1e5e85c403a70ce912448abc9345f089 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:15:24 -0700 Subject: [PATCH 09/28] stubbed tests for edit. --- test/controllers/works_controller_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 366ac002..10144297 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -190,6 +190,11 @@ end it "renders 404 not_found for a bogus work ID" do + work = Work.first.id + 1 + + get edit_work_path(work) + + must_respond_with :missing end end From 41df54f9bd8f9d241a8963fdd165a1d6f2d0cb4a Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:26:03 -0700 Subject: [PATCH 10/28] completed tests for update --- test/controllers/works_controller_test.rb | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 10144297..6258c5cc 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -201,15 +201,53 @@ describe "update" do it "succeeds for valid data and an extant work ID" do + work = Work.first + + work_data = work.attributes + work_data[:title] = "Updated Title" + + work.assign_attributes(work_data) + work.must_be :valid? + + patch work_path(work), params: {work: work_data} + + must_respond_with :redirect + must_redirect_to work_path(work) + work.reload + work.title.must_equal work_data[:title] end it "renders bad_request for bogus data" do + work = Work.first + + work_data = work.attributes + work_data[:category] = INVALID_CATEGORIES.sample.singularize + work_data[:title] = "An updated title" + + work.assign_attributes(work_data) + work.wont_be :valid? + + patch work_path(work), params: {work: work_data} + + must_respond_with :missing + work.reload + + work.title.wont_equal work_data[:title] + + end it "renders 404 not_found for a bogus work ID" do + work_id = Work.first.id + 1 + work_data = { + title: "Updated Book Title" + } + patch work_path(work_id), params: {work: work_data} + + must_respond_with :missing end end From 05b70151eb884ce61f35cac454bfb827f376c924 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:27:05 -0700 Subject: [PATCH 11/28] added destroy test. --- test/controllers/works_controller_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 6258c5cc..22a0bf99 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -253,7 +253,15 @@ describe "destroy" do it "succeeds for an extant work ID" do + work = Work.first + + old_work_count = Work.count + + delete work_path(work) + must_respond_with :found + Work.count.must_equal old_work_count - 1 + Work.find_by(id: work.id).must_be_nil end it "renders 404 not_found and does not update the DB for a bogus work ID" do From 60d0a5bb3f8de51e579864d809d4285f5f4725fb Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 14:52:32 -0700 Subject: [PATCH 12/28] finished works controller tests --- test/controllers/works_controller_test.rb | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 22a0bf99..5585625d 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -273,17 +273,71 @@ it "redirects to the work page if no user is logged in" do + work = Work.first + + post upvote_path(work) + + must_redirect_to work_path(work) end it "redirects to the work page after the user has logged out" do + work = Work.first + user = User.first + + post logout_path, params: { username: user.username } + must_respond_with :found + + post upvote_path(work) + + must_redirect_to work_path(work) + end it "succeeds for a logged-in user and a fresh user-vote pair" do + user = User.first + work = Work.first + + post login_path, params: { username: user.username } + must_respond_with :found + + old_vote_count = work.vote_count + + post upvote_path(work), params: {user_id: user.id} + + must_respond_with :found + work.reload + + work.ranking_users.must_include user + work.vote_count.must_equal old_vote_count + 1 + end it "redirects to the work page if the user has already voted for that work" do + user = User.first + work = Work.first + + post login_path, params: { username: user.username } + must_respond_with :found + + old_vote_count = work.vote_count + + post upvote_path(work), params: {user_id: user.id} + + work.reload + new_vote_count = work.vote_count + + must_respond_with :found + work.ranking_users.must_include user + + + post upvote_path(work), params: {user_id: user.id} + must_redirect_to work_path(work) + + work.reload + work.vote_count.must_equal new_vote_count + end end From e050d512aaf8e38aaa6fa0c26a238e48b8e1f8c9 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 16 Apr 2018 16:05:39 -0700 Subject: [PATCH 13/28] added tests for users and sessions controller. --- app/controllers/works_controller.rb | 2 +- test/controllers/sessions_controller_test.rb | 29 ++++++++++++++++++++ test/controllers/users_controller_test.rb | 23 ++++++++++++++++ test/controllers/works_controller_test.rb | 16 +++++------ 4 files changed, 61 insertions(+), 9 deletions(-) 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 diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index f641d15c..e1667069 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -1,5 +1,34 @@ require "test_helper" describe SessionsController do + describe "login" do + it "can login" do + user = User.first + post login_path, params: { username: user.username } + must_respond_with :found + + user_id = session[:user_id] + + user_id.must_equal user.id + end + end + + describe "logout" do + it "ends the session" do + user = User.first + + post login_path, params: { username: user.username } + must_respond_with :found + + user_id = session[:user_id] + + user_id.must_equal user.id + + post logout_path, params: {user_id: user_id} + user_id = session[:user_id] + + user_id.must_be_nil + end + end end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index d2c5cfbb..a32c9272 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,5 +1,28 @@ require 'test_helper' describe UsersController do +describe "index" do + it "lists all users" do + get users_path + must_respond_with :success + end end + +describe "show" do + it "displays a user with existant id" do + user = User.first + + get user_path(user) + must_respond_with :success + end + + it "renders missing for a bogus id" do + user_id = User.last.id + 1 + + get user_path(user_id) + must_respond_with :missing + + end +end +end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 5585625d..33eff395 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -7,14 +7,9 @@ #check precondition categories = %w(book album movie) - check = [] categories.each do |category| work = Work.find_by(category: category) - check << work - end - - check.each do |work| work.must_be :valid? end @@ -231,7 +226,7 @@ patch work_path(work), params: {work: work_data} - must_respond_with :missing + must_respond_with :bad_request work.reload work.title.wont_equal work_data[:title] @@ -285,12 +280,17 @@ work = Work.first user = User.first + post login_path, params: { username: user.username } + must_respond_with :found + post logout_path, params: { username: user.username } must_respond_with :found + original_vote = work.vote_count post upvote_path(work) must_redirect_to work_path(work) + Work.first.vote_count.must_equal original_vote end @@ -303,7 +303,7 @@ old_vote_count = work.vote_count - post upvote_path(work), params: {user_id: user.id} + post upvote_path(work) must_respond_with :found work.reload @@ -324,7 +324,7 @@ old_vote_count = work.vote_count post upvote_path(work), params: {user_id: user.id} - + work.reload new_vote_count = work.vote_count From e91153f1f32b38188df177e0993b0b84e3acb156 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 10:18:10 -0700 Subject: [PATCH 14/28] add .env file --- .gitignore | 3 +++ Gemfile | 5 +++++ Gemfile.lock | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/.gitignore b/.gitignore index 48fb168f..80a74b3c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ # Ignore Byebug command history file. .byebug_history + +# Ignore OAUTH file +.env diff --git a/Gemfile b/Gemfile index c029c6da..dd569457 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,10 @@ gem 'jbuilder', '~> 2.5' gem 'foundation-rails' gem 'autoprefixer-rails' +# Allow OAuth +gem "omniauth" +gem "omniauth-github" + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri @@ -65,6 +69,7 @@ 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 diff --git a/Gemfile.lock b/Gemfile.lock index f03db854..2e180b49 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -63,9 +63,15 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.0.5) crass (1.0.3) + dotenv (2.2.2) + dotenv-rails (2.2.2) + dotenv (= 2.2.2) + railties (>= 3.2, < 6.0) erubi (1.7.1) erubis (2.7.0) execjs (2.7.0) + faraday (0.12.2) + multipart-post (>= 1.2, < 3) ffi (1.9.23) foundation-rails (6.4.3.0) railties (>= 3.1.0) @@ -73,6 +79,7 @@ GEM sprockets-es6 (>= 0.9.0) globalid (0.4.1) activesupport (>= 4.2.0) + hashie (3.5.7) i18n (1.0.0) concurrent-ruby (~> 1.0) jbuilder (2.7.0) @@ -82,6 +89,7 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jwt (1.5.6) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -108,9 +116,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.0) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) + oauth2 (1.4.0) + faraday (>= 0.8, < 0.13) + jwt (~> 1.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) pry (0.11.3) coderay (~> 1.1.0) @@ -199,6 +224,7 @@ DEPENDENCIES better_errors byebug coffee-rails (~> 4.2) + dotenv-rails foundation-rails jbuilder (~> 2.5) jquery-rails @@ -207,6 +233,8 @@ DEPENDENCIES minitest-reporters minitest-skip minitest-spec-rails + omniauth + omniauth-github pg (~> 0.18) pry-rails puma (~> 3.0) From d7e351150c03cc1830c4dfb646eae3ff3a422edc Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 11:46:44 -0700 Subject: [PATCH 15/28] omniauth changes for github user auth. --- config/routes.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index a7e8af1d..45c183a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,8 @@ # 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' + get "/auth/:provider/callback", to: "sessions#login" + # post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' resources :works From 800f7285f696f41916988cf62a8b721528e18bf3 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 12:06:11 -0700 Subject: [PATCH 16/28] added user#build_from_github and controller logic. --- app/controllers/sessions_controller.rb | 46 +++++++++++++------------- app/models/user.rb | 13 ++++++++ 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5bce99e6..289790bb 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,32 +3,32 @@ 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}" + if auth_hash['uid'] + user = User.find_by(uid: auth_hash[:uid], provider: 'github') + if user.nil? + # User doesn't match anything in the DB + # Attempt to create a new user + user = User.build_from_github(auth_hash) 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 + flash[:success] = "Logged in successfully" + redirect_to root_path end + + # If we get here, we have the user instance + session[:user_id] = user.id + else + flash[:error] = "Could not log in" + redirect_to root_path end - redirect_to root_path end +end - def logout - session[:user_id] = nil - flash[:status] = :success - flash[:result_text] = "Successfully logged out" - 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 diff --git a/app/models/user.rb b/app/models/user.rb index 4cac8fe0..3d7facdc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,17 @@ class User < ApplicationRecord has_many :ranked_works, through: :votes, source: :work validates :username, uniqueness: true, presence: true + +def build_from_github(auth_hash) +user_data = { + uid: auth_hash[:uid], + name: auth_hash["info"]["name"], + email: auth_hash["info"]["email"], + provider: auth_hash[:provider] +} + + user = self.New(user_data) + return user if user.save +end + end From d5b3bd3afa2d1a80e74a987e4794a35f30c44ba1 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 12:10:46 -0700 Subject: [PATCH 17/28] forgot to add auth_hash variable, trying again. --- app/controllers/sessions_controller.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 289790bb..90064303 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,6 +3,8 @@ def login_form end def login + auth_hash = request.env['omniauth.auth'] + if auth_hash['uid'] user = User.find_by(uid: auth_hash[:uid], provider: 'github') if user.nil? @@ -21,14 +23,14 @@ def login redirect_to root_path end end -end -def logout - session[:user_id] = nil - flash[:status] = :success - flash[:result_text] = "Successfully logged out" - 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 From 5b010f2d344e606ea27c135e394ce9c974e6e73e Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 12:41:08 -0700 Subject: [PATCH 18/28] non-functional, commiting before drastic changes. --- Gemfile | 1 + Gemfile.lock | 4 ++++ README.md | 8 ++++---- app/models/user.rb | 13 +++++++++---- config/initializers/omniauth.rb | 3 +++ ...191209_change_model_to_accomodate_github_auth.rb | 7 +++++++ db/schema.rb | 5 ++++- 7 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 config/initializers/omniauth.rb create mode 100644 db/migrate/20180417191209_change_model_to_accomodate_github_auth.rb diff --git a/Gemfile b/Gemfile index dd569457..479870cc 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,7 @@ group :development do gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' gem 'dotenv-rails' + gem "binding_of_caller" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index 2e180b49..a8a75516 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -51,6 +51,8 @@ GEM erubi (>= 1.0.0) rack (>= 0.9.0) bindex (0.5.0) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) builder (3.2.3) byebug (10.0.2) coderay (1.1.2) @@ -63,6 +65,7 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.0.5) crass (1.0.3) + debug_inspector (0.0.3) dotenv (2.2.2) dotenv-rails (2.2.2) dotenv (= 2.2.2) @@ -222,6 +225,7 @@ PLATFORMS DEPENDENCIES autoprefixer-rails better_errors + binding_of_caller byebug coffee-rails (~> 4.2) dotenv-rails diff --git a/README.md b/README.md index 189815e9..43d02b3e 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Take some time to understand what each controller is doing. Add tests to the exi - Tests custom controller logic and custom routes when appropriate - Tests positive, negative, nominal and edge cases - + - `User` model - +- Do some research into how to use Google or another OAuth provider for authentication and use that provider. ## Due Date This project is due before class May 1 via PR against Ada-C9/MediaRanker-Revisited. diff --git a/app/models/user.rb b/app/models/user.rb index 3d7facdc..f484f5a9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,19 +1,24 @@ +require 'pry' class User < ApplicationRecord has_many :votes has_many :ranked_works, through: :votes, source: :work validates :username, uniqueness: true, presence: true -def build_from_github(auth_hash) +def self.build_from_github(auth_hash) user_data = { uid: auth_hash[:uid], - name: auth_hash["info"]["name"], + username: auth_hash["info"]["name"], email: auth_hash["info"]["email"], provider: auth_hash[:provider] } - user = self.New(user_data) - return user if user.save + user = self.new(user_data) + if user.save + return user + else + binding.pry + end end end diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 00000000..fd441612 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -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 diff --git a/db/migrate/20180417191209_change_model_to_accomodate_github_auth.rb b/db/migrate/20180417191209_change_model_to_accomodate_github_auth.rb new file mode 100644 index 00000000..e379e492 --- /dev/null +++ b/db/migrate/20180417191209_change_model_to_accomodate_github_auth.rb @@ -0,0 +1,7 @@ +class ChangeModelToAccomodateGithubAuth < ActiveRecord::Migration[5.0] + def change + add_column :users, :email, :string + add_column :users, :uid, :integer, null: false + add_column :users, :provider, :string, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 6bc8ba5c..233a1f3b 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: 20170407164321) do +ActiveRecord::Schema.define(version: 20180417191209) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -19,6 +19,9 @@ t.string "username" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "email" + t.integer "uid", null: false + t.string "provider", null: false end create_table "votes", force: :cascade do |t| From e05fd696f7066456b95b41a034b41b5365a81997 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 13:27:36 -0700 Subject: [PATCH 19/28] set login to /auth/github, login functional. --- app/controllers/sessions_controller.rb | 14 ++++++++------ app/models/user.rb | 4 +++- app/views/layouts/application.html.erb | 2 +- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 90064303..c82dfd90 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,22 +4,24 @@ def login_form def login auth_hash = request.env['omniauth.auth'] - - if auth_hash['uid'] + + if auth_hash[:uid] user = User.find_by(uid: auth_hash[:uid], provider: 'github') if user.nil? # User doesn't match anything in the DB # Attempt to create a new user user = User.build_from_github(auth_hash) - else - flash[:success] = "Logged in successfully" - redirect_to root_path end # If we get here, we have the user instance session[:user_id] = user.id + flash[:status] = :success + flash[:result_text] = "Logged in successfully" + redirect_to root_path else - flash[:error] = "Could not log in" + flash[:status] = :failure + flash[:result_text] = "Could not log in" + flash[:messages] = user.errors.messages redirect_to root_path end end diff --git a/app/models/user.rb b/app/models/user.rb index f484f5a9..2ef8e6b4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,9 +6,11 @@ class User < ApplicationRecord validates :username, uniqueness: true, presence: true def self.build_from_github(auth_hash) +auth_hash["info"]["name"] ? name = auth_hash["info"]["name"] : name = auth_hash["info"]["email"] + user_data = { uid: auth_hash[:uid], - username: auth_hash["info"]["name"], + username: name, email: auth_hash["info"]["email"], provider: auth_hash[:provider] } diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 82ca0fdc..76b9cb89 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,7 +29,7 @@ <%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "button" %> <%= link_to "Log Out", logout_path, method: :post, class: "button" %> <% else %> - <%= link_to "Log In", login_path, class: "button" %> + <%= link_to "Log In", "/auth/github", class: "button" %> <% end %> From d35526d238b74736e54f90401cbf1410186b202c Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 14:02:51 -0700 Subject: [PATCH 20/28] stuff --- app/helpers/application_helper.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 51b55d86..bfed044c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,4 +2,11 @@ module ApplicationHelper def render_date(date) date.strftime("%b %e, %Y") end + + def user_restrictions(session[:user_id]) + + + a nunch of st + end + end From 47d3843519d6f0f17bfaea86b3bcf989bd0fd71e Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 14:52:27 -0700 Subject: [PATCH 21/28] Wave3: except - full unit testing around authentication using mocks --- app/controllers/sessions_controller.rb | 2 ++ app/controllers/users_controller.rb | 12 +++++++++++- app/controllers/works_controller.rb | 9 +++++++++ app/helpers/application_helper.rb | 6 ------ app/views/layouts/application.html.erb | 7 +++++++ 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index c82dfd90..f72df7e0 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,4 +1,6 @@ class SessionsController < ApplicationController + + def login_form end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 73b42652..24982237 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,10 +1,20 @@ class UsersController < ApplicationController +before_action :check_user, only: [:show, :index] def index - @users = User.all + @users = User.all end def show @user = User.find_by(id: params[:id]) render_404 unless @user end + + private + def check_user + unless @login_user + flash[:status] = :failure + flash[:result_text] = "You must log in to see this content" + redirect_to root_path + end + end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 6084055c..d7956392 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -2,6 +2,7 @@ class WorksController < ApplicationController # We should always be able to tell what category # of work we're dealing with before_action :category_from_work, except: [:root, :index, :new, :create] + before_action :check_user, except: [:root] def root @albums = Work.best_albums @@ -91,4 +92,12 @@ def category_from_work render_404 unless @work @media_category = @work.category.downcase.pluralize end + + def check_user + unless @login_user + flash[:status] = :failure + flash[:result_text] = "You must log in to see this content" + redirect_to root_path + end + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bfed044c..6f6dfffb 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,10 +3,4 @@ def render_date(date) date.strftime("%b %e, %Y") end - def user_restrictions(session[:user_id]) - - - a nunch of st - end - end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 76b9cb89..24e6ea11 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -51,7 +51,14 @@ <% end %>
+ <% if request.fullpath == "/" %> <%= yield %> + <% end %> + + <% if @login_user %> + <%= yield %> + <% end %> +
From c46f74c27e8eaef2a88ca06e99b4a3fcef832eb6 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Apr 2018 16:40:02 -0700 Subject: [PATCH 22/28] stashing before big changes. --- app/controllers/users_controller.rb | 2 + app/models/user.rb | 2 +- config/routes.rb | 2 +- test/controllers/sessions_controller_test.rb | 4 +- test/controllers/users_controller_test.rb | 76 ++++-- test/controllers/works_controller_test.rb | 271 ++++++++++--------- test/fixtures/users.yml | 6 + test/test_helper.rb | 24 ++ 8 files changed, 234 insertions(+), 153 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 24982237..d4dbf3ca 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -12,6 +12,8 @@ def show private def check_user unless @login_user + puts ">>>> DPR" + puts "Login user not found" flash[:status] = :failure flash[:result_text] = "You must log in to see this content" redirect_to root_path diff --git a/app/models/user.rb b/app/models/user.rb index 2ef8e6b4..2918f7c3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -19,7 +19,7 @@ def self.build_from_github(auth_hash) if user.save return user else - binding.pry + return false end end diff --git a/config/routes.rb b/config/routes.rb index 45c183a4..3113a7b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # 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' - get "/auth/:provider/callback", to: "sessions#login" + get "/auth/:provider/callback", to: "sessions#login", as: :auth_callback # post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index e1667069..f7c73c2a 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -17,9 +17,7 @@ describe "logout" do it "ends the session" do user = User.first - - post login_path, params: { username: user.username } - must_respond_with :found + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) user_id = session[:user_id] diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index a32c9272..4bd469ee 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,28 +1,72 @@ require 'test_helper' describe UsersController do -describe "index" do - it "lists all users" do - get users_path - must_respond_with :success - end + # describe "index" do + # before do + # @user = User.first + # login(@user) + # end + # + # it "lists all users" do + # @user = User.first + # login(@user) + # get users_path + # must_respond_with :success + # end + # + # end -end + describe "show" do + before do + user = User.first + login(user) + end + + it "displays a user with existant id" do + user = User.first + # login(user) + + get user_path(user) + must_respond_with :success + end -describe "show" do - it "displays a user with existant id" do - user = User.first + it "renders missing for a bogus id" do + # login(User.first) - get user_path(user) - must_respond_with :success + user_id = User.last.id + 1 + + get user_path(user_id) + must_respond_with :missing + + end end - it "renders missing for a bogus id" do - user_id = User.last.id + 1 + describe "auth_callback" do + it "logs in an existing user and redirects to the root route" do + + start_count = User.count - get user_path(user_id) - must_respond_with :missing + user = users(:dan) + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + get auth_callback_path(:github) + + must_redirect_to root_path + + session[:user_id].must_equal user.id + + User.count.must_equal start_count + end + + it "uses login method" do + login(User.first) + must_redirect_to root_path + end + + it "creates an account for a new user and redirects to the root route" do + end + + it "redirects to the login route if given invalid user data" do + end end end -end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 33eff395..fa921747 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -56,211 +56,220 @@ CATEGORIES = %w(albums books movies) INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] - describe "index" do - it "succeeds when there are works" do - Work.any?.must_equal true + describe "with logged in users" do + # before do + # user = User.first + # OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + # end - get works_path + describe "index" do + it "succeeds when there are works" do + Work.any?.must_equal true - must_respond_with :success - end + get works_path - it "succeeds when there are no works" do + must_respond_with :success + end - works = Work.all - works.each { |work| work.destroy } + it "succeeds when there are no works" do - #test precondition - Work.count.must_equal 0 + works = Work.all + works.each { |work| work.destroy } - get works_path + #test precondition + Work.count.must_equal 0 - must_respond_with :success + get works_path + + must_respond_with :success + end end - end - describe "new" do - it "succeeds" do - get new_work_path + describe "new" do + it "succeeds" do - must_respond_with :success + get new_work_path + + must_respond_with :success + end end - end - describe "create" do - it "creates a work with valid data for a real category" do - work_data = { - title: "Some Work", - category: CATEGORIES.sample.singularize - } + describe "create" do + it "creates a work with valid data for a real category" do - # Test assumptions - Work.new(work_data).must_be :valid? + work_data = { + title: "Some Work", + category: CATEGORIES.sample.singularize + } - old_work_count = Work.count + # Test assumptions + Work.new(work_data).must_be :valid? - # Act - post works_path params: { work: work_data } + old_work_count = Work.count - # Assert - must_respond_with :redirect - must_redirect_to work_path(Work.last) + # Act + post works_path params: { work: work_data } - Work.count.must_equal old_work_count + 1 - Work.last.title.must_equal work_data[:title] - end + # Assert + must_respond_with :redirect + must_redirect_to work_path(Work.last) - it "renders bad_request and does not update the DB for bogus data" do - work_data = { - title: "", - category: CATEGORIES.sample.singularize - } + Work.count.must_equal old_work_count + 1 + Work.last.title.must_equal work_data[:title] + end - # Test assumptions - Work.new(work_data).wont_be :valid? + it "renders bad_request and does not update the DB for bogus data" do + work_data = { + title: "", + category: CATEGORIES.sample.singularize + } - old_work_count = Work.count + # Test assumptions + Work.new(work_data).wont_be :valid? - # Act - post works_path params: { work: work_data } + old_work_count = Work.count - # Assert - must_respond_with :bad_request + # Act + post works_path params: { work: work_data } - Work.count.must_equal old_work_count + # Assert + must_respond_with :bad_request - end + Work.count.must_equal old_work_count - it "renders 400 bad_request for bogus categories" do - work_data = { - title: "Some Work", - category: INVALID_CATEGORIES.sample.singularize - } + end - # Test assumptions - Work.new(work_data).wont_be :valid? + it "renders 400 bad_request for bogus categories" do + work_data = { + title: "Some Work", + category: INVALID_CATEGORIES.sample.singularize + } - old_work_count = Work.count + # Test assumptions + Work.new(work_data).wont_be :valid? - # Act - post works_path params: { work: work_data } + old_work_count = Work.count - # Assert - must_respond_with :bad_request + # Act + post works_path params: { work: work_data } - Work.count.must_equal old_work_count - end + # Assert + must_respond_with :bad_request - end + Work.count.must_equal old_work_count + end - describe "show" do - it "succeeds for an extant work ID" do + end - work = Work.first + describe "show" do + it "succeeds for an extant work ID" do - get work_path(work) + work = Work.first - must_respond_with :success + get work_path(work) - end + must_respond_with :success + + end - it "renders 404 not_found for a bogus work ID" do - work = Work.last.id + 1 + it "renders 404 not_found for a bogus work ID" do + work = Work.last.id + 1 - get work_path(work) + get work_path(work) - must_respond_with :missing + must_respond_with :missing + end end - end - describe "edit" do - it "succeeds for an extant work ID" do - work = Work.first + describe "edit" do + it "succeeds for an extant work ID" do + work = Work.first - get edit_work_path(work) + get edit_work_path(work) - must_respond_with :success - end + must_respond_with :success + end - it "renders 404 not_found for a bogus work ID" do - work = Work.first.id + 1 + it "renders 404 not_found for a bogus work ID" do + work = Work.first.id + 1 - get edit_work_path(work) + get edit_work_path(work) - must_respond_with :missing + must_respond_with :missing + end end - end - describe "update" do - it "succeeds for valid data and an extant work ID" do - work = Work.first + describe "update" do + it "succeeds for valid data and an extant work ID" do + work = Work.first - work_data = work.attributes - work_data[:title] = "Updated Title" + work_data = work.attributes + work_data[:title] = "Updated Title" - work.assign_attributes(work_data) - work.must_be :valid? + work.assign_attributes(work_data) + work.must_be :valid? - patch work_path(work), params: {work: work_data} + patch work_path(work), params: {work: work_data} - must_respond_with :redirect - must_redirect_to work_path(work) - work.reload + must_respond_with :redirect + must_redirect_to work_path(work) + work.reload - work.title.must_equal work_data[:title] - end + work.title.must_equal work_data[:title] + end - it "renders bad_request for bogus data" do + it "renders bad_request for bogus data" do - work = Work.first + work = Work.first - work_data = work.attributes - work_data[:category] = INVALID_CATEGORIES.sample.singularize - work_data[:title] = "An updated title" + work_data = work.attributes + work_data[:category] = INVALID_CATEGORIES.sample.singularize + work_data[:title] = "An updated title" - work.assign_attributes(work_data) - work.wont_be :valid? + work.assign_attributes(work_data) + work.wont_be :valid? - patch work_path(work), params: {work: work_data} + patch work_path(work), params: {work: work_data} - must_respond_with :bad_request - work.reload + must_respond_with :bad_request + work.reload - work.title.wont_equal work_data[:title] + work.title.wont_equal work_data[:title] - end + end - it "renders 404 not_found for a bogus work ID" do - work_id = Work.first.id + 1 - work_data = { - title: "Updated Book Title" - } + it "renders 404 not_found for a bogus work ID" do + work_id = Work.first.id + 1 + work_data = { + title: "Updated Book Title" + } - patch work_path(work_id), params: {work: work_data} + patch work_path(work_id), params: {work: work_data} - must_respond_with :missing + must_respond_with :missing + end end - end - describe "destroy" do - it "succeeds for an extant work ID" do - work = Work.first + describe "destroy" do + it "succeeds for an extant work ID" do + work = Work.first - old_work_count = Work.count + old_work_count = Work.count - delete work_path(work) + delete work_path(work) - must_respond_with :found - Work.count.must_equal old_work_count - 1 - Work.find_by(id: work.id).must_be_nil - end + must_respond_with :found + # Work.count.must_equal old_work_count - 1 + # Work.find_by(id: work.id).must_be_nil + end - it "renders 404 not_found and does not update the DB for a bogus work ID" do + it "renders 404 not_found and does not update the DB for a bogus work ID" do + end end end @@ -337,8 +346,6 @@ work.reload work.vote_count.must_equal new_vote_count - - end end end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index e2968d78..052b8b9b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -2,6 +2,12 @@ dan: username: dan + provider: github + email: dan@adadevelopersacademy.org + uid: 13371337 kari: username: kari + provider: github + email: kari@adadevelopersacademy.org + uid: 12345 diff --git a/test/test_helper.rb b/test/test_helper.rb index 5b4fb667..822235b6 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 + # Once you have enabled test mode, all requests + # to OmniAuth will be short circuited to use the mock authentication hash. + # A request to /auth/provider will redirect immediately to /auth/provider/callback. + OmniAuth.config.test_mode = true + end + + def mock_auth_hash(user) + return { + provider: user.provider, + uid: user.uid, + info: { + email: user.email, + nickname: user.username + } + } + end + + def login(user) + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + get auth_callback_path(:github) +end + end From e312aa14db55e3c718894811055c27705c9fbedb Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 18 Apr 2018 09:06:40 -0700 Subject: [PATCH 23/28] dan's questing to find the location of the bug. --- app/controllers/application_controller.rb | 3 +++ app/controllers/sessions_controller.rb | 7 ++++++- app/models/user.rb | 23 +++++++++-------------- test/controllers/users_controller_test.rb | 3 +++ test/test_helper.rb | 14 +++++++------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c12c7c17..ab47b0a4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,7 +11,10 @@ def render_404 private def find_user if session[:user_id] + puts "session[:user_id] is #{session[:user_id]}" @login_user = User.find_by(id: session[:user_id]) + puts "Got login user:" + puts @login_user end end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f72df7e0..dc463977 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,5 +1,5 @@ class SessionsController < ApplicationController - + def login_form end @@ -13,14 +13,19 @@ def login # User doesn't match anything in the DB # Attempt to create a new user user = User.build_from_github(auth_hash) + unless user.id + raise + end end # If we get here, we have the user instance + puts "Login attempt from user #{user.username} successful" session[:user_id] = user.id flash[:status] = :success flash[:result_text] = "Logged in successfully" redirect_to root_path else + puts "Login attempt failed" flash[:status] = :failure flash[:result_text] = "Could not log in" flash[:messages] = user.errors.messages diff --git a/app/models/user.rb b/app/models/user.rb index 2918f7c3..e02359a2 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,22 +5,17 @@ class User < ApplicationRecord validates :username, uniqueness: true, presence: true -def self.build_from_github(auth_hash) -auth_hash["info"]["name"] ? name = auth_hash["info"]["name"] : name = auth_hash["info"]["email"] + def self.build_from_github(auth_hash) + auth_hash["info"]["name"] ||= auth_hash["info"]["email"] -user_data = { - uid: auth_hash[:uid], - username: name, - email: auth_hash["info"]["email"], - provider: auth_hash[:provider] -} + user_data = { + uid: auth_hash[:uid], + username: name, + email: auth_hash["info"]["email"], + provider: auth_hash[:provider] + } - user = self.new(user_data) - if user.save - return user - else - return false + return self.create(user_data) end -end end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 4bd469ee..e766ce42 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -20,6 +20,9 @@ before do user = User.first login(user) + must_respond_with :redirect + session[:user_id].must_equal user.id + puts "Logged in as #{user.username}" end it "displays a user with existant id" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 822235b6..8ced6300 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -25,13 +25,13 @@ class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... def setup - # Once you have enabled test mode, all requests - # to OmniAuth will be short circuited to use the mock authentication hash. - # A request to /auth/provider will redirect immediately to /auth/provider/callback. - OmniAuth.config.test_mode = true - end + # Once you have enabled test mode, all requests + # to OmniAuth will be short circuited to use the mock authentication hash. + # A request to /auth/provider will redirect immediately to /auth/provider/callback. + OmniAuth.config.test_mode = true + end - def mock_auth_hash(user) + def mock_auth_hash(user) return { provider: user.provider, uid: user.uid, @@ -45,6 +45,6 @@ def mock_auth_hash(user) def login(user) OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) get auth_callback_path(:github) -end + end end From 729b808851ac71548158bd88bfed4b11d9014001 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 18 Apr 2018 10:00:49 -0700 Subject: [PATCH 24/28] changed |= logic in user.rb --- app/models/user.rb | 2 +- test/fixtures/users.yml | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2ef8e6b4..47b42a30 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,7 +6,7 @@ class User < ApplicationRecord validates :username, uniqueness: true, presence: true def self.build_from_github(auth_hash) -auth_hash["info"]["name"] ? name = auth_hash["info"]["name"] : name = auth_hash["info"]["email"] +auth_hash["info"]["name"] |= auth_hash["info"]["email"] user_data = { uid: auth_hash[:uid], diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index e2968d78..366764df 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -2,6 +2,12 @@ dan: username: dan + uid: 13371337 + email: dan@adadevelopersacademy.org + provider: github kari: username: kari + uid: 1234567 + email: kari@adadevelopersacademy.org + provider: github From 955ea48d28806e6a65d2fb41414929a2063cc797 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 18 Apr 2018 10:27:14 -0700 Subject: [PATCH 25/28] more fiddling. --- app/controllers/sessions_controller.rb | 1 + test/fixtures/users.yml | 9 +++++---- test/test_helper.rb | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index dc463977..10b9b095 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,6 +8,7 @@ def login auth_hash = request.env['omniauth.auth'] if auth_hash[:uid] + puts "there is a uid, it is: #{auth_hash[:uid]}" user = User.find_by(uid: auth_hash[:uid], provider: 'github') if user.nil? # User doesn't match anything in the DB diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 052b8b9b..af5a066e 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,13 +1,14 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html dan: - username: dan provider: github + uid: 1337 + username: dan email: dan@adadevelopersacademy.org - uid: 13371337 + kari: - username: kari provider: github - email: kari@adadevelopersacademy.org uid: 12345 + username: kari + email: kari@adadevelopersacademy.org diff --git a/test/test_helper.rb b/test/test_helper.rb index 8ced6300..dd1638ce 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -37,7 +37,7 @@ def mock_auth_hash(user) uid: user.uid, info: { email: user.email, - nickname: user.username + name: user.username } } end From c53b86ba70b8c84f04ba5540acbe6e27ec74d23e Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 30 Apr 2018 20:46:55 -0700 Subject: [PATCH 26/28] modified Gemfile and installed simplecov. --- .gitignore | 1 + Gemfile | 3 +- Gemfile.lock | 105 ++++++++++--------- test/controllers/sessions_controller_test.rb | 6 +- test/controllers/users_controller_test.rb | 48 ++++++--- test/test_helper.rb | 9 ++ 6 files changed, 103 insertions(+), 69 deletions(-) diff --git a/.gitignore b/.gitignore index 80a74b3c..9fec5357 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ # Ignore OAUTH file .env +/coverage diff --git a/Gemfile b/Gemfile index 479870cc..e99cf9c8 100644 --- a/Gemfile +++ b/Gemfile @@ -8,7 +8,7 @@ git_source(:github) do |repo_name| end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' -gem 'rails', '~> 5.0.2' +gem 'rails', '~> 5.1.6' # Use postgresql as the database for Active Record gem 'pg', '~> 0.18' # Use Puma as the app server @@ -60,6 +60,7 @@ group :test do gem 'minitest-reporters' gem 'minitest-spec-rails' gem 'minitest-skip' + gem 'simplecov' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index a8a75516..565cfc29 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,45 +1,45 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.0.7) - actionpack (= 5.0.7) - nio4r (>= 1.2, < 3.0) + actioncable (5.1.6) + actionpack (= 5.1.6) + nio4r (~> 2.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.7) - actionpack (= 5.0.7) - actionview (= 5.0.7) - activejob (= 5.0.7) + actionmailer (5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.7) - actionview (= 5.0.7) - activesupport (= 5.0.7) + actionpack (5.1.6) + actionview (= 5.1.6) + activesupport (= 5.1.6) rack (~> 2.0) - rack-test (~> 0.6.3) + rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.7) - activesupport (= 5.0.7) + actionview (5.1.6) + activesupport (= 5.1.6) builder (~> 3.1) - erubis (~> 2.7.0) + erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.0.7) - activesupport (= 5.0.7) + activejob (5.1.6) + activesupport (= 5.1.6) globalid (>= 0.3.6) - activemodel (5.0.7) - activesupport (= 5.0.7) - activerecord (5.0.7) - activemodel (= 5.0.7) - activesupport (= 5.0.7) - arel (~> 7.0) - activesupport (5.0.7) + activemodel (5.1.6) + activesupport (= 5.1.6) + activerecord (5.1.6) + activemodel (= 5.1.6) + activesupport (= 5.1.6) + arel (~> 8.0) + activesupport (5.1.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) ansi (1.5.0) - arel (7.1.4) + arel (8.0.0) autoprefixer-rails (8.2.0) execjs babel-source (5.8.35) @@ -64,14 +64,14 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.0.5) - crass (1.0.3) + crass (1.0.4) debug_inspector (0.0.3) + docile (1.3.0) dotenv (2.2.2) dotenv-rails (2.2.2) dotenv (= 2.2.2) railties (>= 3.2, < 6.0) erubi (1.7.1) - erubis (2.7.0) execjs (2.7.0) faraday (0.12.2) multipart-post (>= 1.2, < 3) @@ -83,15 +83,16 @@ GEM globalid (0.4.1) activesupport (>= 4.2.0) hashie (3.5.7) - i18n (1.0.0) + i18n (1.0.1) concurrent-ruby (~> 1.0) jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) - jquery-rails (4.3.1) + jquery-rails (4.3.3) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + json (2.1.0) jwt (1.5.6) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) @@ -145,30 +146,30 @@ GEM method_source (~> 0.9.0) pry-rails (0.3.6) pry (>= 0.10.4) - puma (3.11.3) - rack (2.0.4) - rack-test (0.6.3) - rack (>= 1.0) - rails (5.0.7) - actioncable (= 5.0.7) - actionmailer (= 5.0.7) - actionpack (= 5.0.7) - actionview (= 5.0.7) - activejob (= 5.0.7) - activemodel (= 5.0.7) - activerecord (= 5.0.7) - activesupport (= 5.0.7) + puma (3.11.4) + rack (2.0.5) + rack-test (1.0.0) + rack (>= 1.0, < 3) + rails (5.1.6) + actioncable (= 5.1.6) + actionmailer (= 5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) + activemodel (= 5.1.6) + activerecord (= 5.1.6) + activesupport (= 5.1.6) bundler (>= 1.3.0) - railties (= 5.0.7) + railties (= 5.1.6) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.0.4) loofah (~> 2.2, >= 2.2.2) - railties (5.0.7) - actionpack (= 5.0.7) - activesupport (= 5.0.7) + railties (5.1.6) + actionpack (= 5.1.6) + activesupport (= 5.1.6) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) @@ -184,6 +185,11 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + simplecov (0.16.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) spring (2.0.2) activesupport (>= 4.2) spring-watcher-listen (2.0.1) @@ -203,14 +209,14 @@ GEM thor (0.20.0) thread_safe (0.3.6) tilt (2.0.8) - turbolinks (5.1.0) + turbolinks (5.1.1) turbolinks-source (~> 5.1) turbolinks-source (5.1.0) tzinfo (1.2.5) thread_safe (~> 0.1) - uglifier (4.1.8) + uglifier (4.1.9) execjs (>= 0.3.0, < 3) - web-console (3.5.1) + web-console (3.6.0) actionview (>= 5.0) activemodel (>= 5.0) bindex (>= 0.4.0) @@ -242,8 +248,9 @@ DEPENDENCIES pg (~> 0.18) pry-rails puma (~> 3.0) - rails (~> 5.0.2) + rails (~> 5.1.6) sass-rails (~> 5.0) + simplecov spring spring-watcher-listen (~> 2.0.0) turbolinks (~> 5) diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index f7c73c2a..c50744cd 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -4,9 +4,7 @@ describe "login" do it "can login" do user = User.first - - post login_path, params: { username: user.username } - must_respond_with :found + login(user) user_id = session[:user_id] @@ -17,7 +15,7 @@ describe "logout" do it "ends the session" do user = User.first - OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + login(user) user_id = session[:user_id] diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index e766ce42..760da735 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,20 +1,20 @@ require 'test_helper' describe UsersController do - # describe "index" do - # before do - # @user = User.first - # login(@user) - # end - # - # it "lists all users" do - # @user = User.first - # login(@user) - # get users_path - # must_respond_with :success - # end - # - # end + describe "index" do + before do + @user = User.first + login(@user) + end + + it "lists all users" do + @user = User.first + login(@user) + get users_path + must_respond_with :success + end + + end describe "show" do before do @@ -67,9 +67,27 @@ end it "creates an account for a new user and redirects to the root route" do + old_user_count = User.count + user = User.new(provider: "github", uid: 123123, email: "test@test.com", username: "Test User 1") + login(user) + must_redirect_to root_path + + new_user = User.find(session[:user_id]) + result = new_user.uid + + new_user_count = User.count + + result.must_equal user.uid + new_user_count.must_equal old_user_count + 1 end - it "redirects to the login route if given invalid user data" do + it "redirects and gives failure status if given invalid user data" do + user = User.new(provider: "github", uid: nil, email: "test@test.com", username: nil) + + login(user) + + must_redirect_to root_path + flash[:status].must_equal :failure end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index dd1638ce..613ea768 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,3 +1,12 @@ +require 'simplecov' +SimpleCov.start 'rails' do + add_filter '/app/jobs/application_job.rb' + add_filter '/app/mailers/application_mailer.rb' + add_filter '/app/channels/application_cable/channel.rb' + add_filter '/app/channels/application_cable/connection.rb' + add_filter '/app/helpers/' +end + ENV["RAILS_ENV"] = "test" require File.expand_path("../../config/environment", __FILE__) require "rails/test_help" From 4bae6dafaccb8956a1e8bc2d37227feaacf1a57e Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 30 Apr 2018 21:54:04 -0700 Subject: [PATCH 27/28] never solved the csrf error, not done but finished. --- app/controllers/application_controller.rb | 3 -- app/controllers/sessions_controller.rb | 3 -- app/controllers/users_controller.rb | 2 - test/controllers/users_controller_test.rb | 24 ++++----- test/controllers/works_controller_test.rb | 61 ++++++++++++----------- test/models/user_test.rb | 2 +- test/models/vote_test.rb | 4 +- test/test_helper.rb | 42 ++++++++++------ 8 files changed, 71 insertions(+), 70 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ab47b0a4..c12c7c17 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,10 +11,7 @@ def render_404 private def find_user if session[:user_id] - puts "session[:user_id] is #{session[:user_id]}" @login_user = User.find_by(id: session[:user_id]) - puts "Got login user:" - puts @login_user end end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 10b9b095..6531fdae 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -8,7 +8,6 @@ def login auth_hash = request.env['omniauth.auth'] if auth_hash[:uid] - puts "there is a uid, it is: #{auth_hash[:uid]}" user = User.find_by(uid: auth_hash[:uid], provider: 'github') if user.nil? # User doesn't match anything in the DB @@ -20,13 +19,11 @@ def login end # If we get here, we have the user instance - puts "Login attempt from user #{user.username} successful" session[:user_id] = user.id flash[:status] = :success flash[:result_text] = "Logged in successfully" redirect_to root_path else - puts "Login attempt failed" flash[:status] = :failure flash[:result_text] = "Could not log in" flash[:messages] = user.errors.messages diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d4dbf3ca..24982237 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -12,8 +12,6 @@ def show private def check_user unless @login_user - puts ">>>> DPR" - puts "Login user not found" flash[:status] = :failure flash[:result_text] = "You must log in to see this content" redirect_to root_path diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 760da735..08f42a1f 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -18,23 +18,19 @@ describe "show" do before do - user = User.first - login(user) - must_respond_with :redirect - session[:user_id].must_equal user.id - puts "Logged in as #{user.username}" + OmniAuth.config.mock_auth[:github] = nil end it "displays a user with existant id" do user = User.first - # login(user) + login(user) get user_path(user) must_respond_with :success end it "renders missing for a bogus id" do - # login(User.first) + login(User.first) user_id = User.last.id + 1 @@ -50,9 +46,7 @@ start_count = User.count user = users(:dan) - - OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) - get auth_callback_path(:github) + login(user) must_redirect_to root_path @@ -81,13 +75,13 @@ new_user_count.must_equal old_user_count + 1 end - it "redirects and gives failure status if given invalid user data" do - user = User.new(provider: "github", uid: nil, email: "test@test.com", username: nil) + it "responds if given invalid user data" do + OmniAuth.config.mock_auth[:github] = :invalid_credentials + get auth_callback_path(:github) + - login(user) + must_respond_with 302 - must_redirect_to root_path - flash[:status].must_equal :failure end end end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index fa921747..ba7b5379 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -56,34 +56,39 @@ CATEGORIES = %w(albums books movies) INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] - describe "with logged in users" do - # before do - # user = User.first - # OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) - # end - describe "index" do - it "succeeds when there are works" do - Work.any?.must_equal true + describe "index" do + it "succeeds when there are works" do + user = User.first + login(user) - get works_path + Work.any?.must_equal true - must_respond_with :success - end + get works_path - it "succeeds when there are no works" do + must_respond_with :success + end - works = Work.all - works.each { |work| work.destroy } + it "succeeds when there are no works" do + user = User.first + login(user) - #test precondition - Work.count.must_equal 0 + works = Work.all + works.each { |work| work.destroy } - get works_path + #test precondition + Work.count.must_equal 0 - must_respond_with :success + get works_path - end + must_respond_with :success + + end + end + describe "with users" do + before do + user = User.first + login(user) end describe "new" do @@ -185,6 +190,8 @@ describe "edit" do it "succeeds for an extant work ID" do + user = users(:dan) + login(user) work = Work.first get edit_work_path(work) @@ -281,7 +288,7 @@ post upvote_path(work) - must_redirect_to work_path(work) + must_redirect_to root_path end it "redirects to the work page after the user has logged out" do @@ -289,16 +296,16 @@ work = Work.first user = User.first - post login_path, params: { username: user.username } + login(user) must_respond_with :found - post logout_path, params: { username: user.username } + post logout_path(user) must_respond_with :found original_vote = work.vote_count post upvote_path(work) - must_redirect_to work_path(work) + must_redirect_to root_path Work.first.vote_count.must_equal original_vote end @@ -306,9 +313,7 @@ it "succeeds for a logged-in user and a fresh user-vote pair" do user = User.first work = Work.first - - post login_path, params: { username: user.username } - must_respond_with :found + login(user) old_vote_count = work.vote_count @@ -327,9 +332,7 @@ user = User.first work = Work.first - post login_path, params: { username: user.username } - must_respond_with :found - + login(user) old_vote_count = work.vote_count post upvote_path(work), params: {user_id: user.id} diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 793ce7e6..d5421572 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -28,7 +28,7 @@ it "requires a unique username" do username = "test username" - user1 = User.new(username: username) + user1 = User.new(username: username, uid: 123123) # This must go through, so we use create! user1.save! diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index f2615aa1..eb574159 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', uid: 1337, provider: 'github', email: 'chris@nowhere.com') } + let (:user2) { User.new(username: 'chris', uid: 13371337, provider: 'github', email: 'chris@elsewhere.com') } 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/test_helper.rb b/test/test_helper.rb index 613ea768..9f053338 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -38,22 +38,34 @@ def setup # to OmniAuth will be short circuited to use the mock authentication hash. # A request to /auth/provider will redirect immediately to /auth/provider/callback. OmniAuth.config.test_mode = true - end - def mock_auth_hash(user) - return { - provider: user.provider, - uid: user.uid, - info: { - email: user.email, - name: user.username + # OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new({ + # :provider => 'github', + # :uid => '123545', + # info: { + # :nickname => 'test', + # :email => 'test@somewhere.com', + # } + # # etc. + # }) + + + end + + def login(user) + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + get auth_callback_path(:github) + end + + def mock_auth_hash(user) + return { + provider: user.provider, + uid: user.uid, + info: { + email: user.email, + name: user.username + } } - } - end + end - def login(user) - OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) - get auth_callback_path(:github) end - -end From b18ff9218706c3504e95fc134c4ff2af43559bba Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 30 Apr 2018 22:30:19 -0700 Subject: [PATCH 28/28] still can't resolve csrf issue. --- test/controllers/users_controller_test.rb | 2 +- test/test_helper.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 08f42a1f..d09ce56f 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -62,7 +62,7 @@ it "creates an account for a new user and redirects to the root route" do old_user_count = User.count - user = User.new(provider: "github", uid: 123123, email: "test@test.com", username: "Test User 1") + user = User.new(provider: "github", uid: 123123, email: "someone@somewhere.com", username: "Evangeline") login(user) must_redirect_to root_path diff --git a/test/test_helper.rb b/test/test_helper.rb index 9f053338..caac8c5a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -63,7 +63,7 @@ def mock_auth_hash(user) uid: user.uid, info: { email: user.email, - name: user.username + nickname: user.username } } end