From 3562d9b44149addaa5c85ae55d4c7d3ae08fb442 Mon Sep 17 00:00:00 2001 From: Leanne Rivera Date: Mon, 29 Oct 2018 17:17:52 -0700 Subject: [PATCH 1/5] dl-d req'd implementation of MR and seeded DB --- db/schema.rb | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 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" From 0f34805bd2820f32b23ce5642362f76d14779ea9 Mon Sep 17 00:00:00 2001 From: Leanne Rivera Date: Mon, 29 Oct 2018 17:20:47 -0700 Subject: [PATCH 2/5] omniauth added, .env added to gitignore --- .gitignore | 2 +- Gemfile | 2 ++ Gemfile.lock | 23 +++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 48fb168f..f225c0de 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,6 @@ /tmp/* !/log/.keep !/tmp/.keep - +.env # Ignore Byebug command history file. .byebug_history diff --git a/Gemfile b/Gemfile index 42f4bb2c..d617d204 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,8 @@ gem 'jbuilder', '~> 2.5' # gem 'redis', '~> 3.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' +gem 'omniauth' +gem 'omniauth-github' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 5b407e7e..b4b8ef23 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,9 +72,12 @@ GEM debug_inspector (0.0.3) 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 +87,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 +117,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) @@ -214,6 +235,8 @@ DEPENDENCIES minitest-reporters minitest-skip minitest-spec-rails + omniauth + omniauth-github pg (~> 0.18) pry-rails puma (~> 3.0) From 52f20a31ac3c204b9b8d98bae3e4f277897a3649 Mon Sep 17 00:00:00 2001 From: Leanne Rivera Date: Mon, 29 Oct 2018 17:45:34 -0700 Subject: [PATCH 3/5] added omniauth columns to user model --- .gitignore | 1 + app/controllers/sessions_controller.rb | 5 +++++ config/routes.rb | 2 ++ .../20181030002623_add_omniauth_columns_to_user_models.rb | 7 +++++++ db/schema.rb | 5 ++++- 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20181030002623_add_omniauth_columns_to_user_models.rb diff --git a/.gitignore b/.gitignore index f225c0de..1ae86a92 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,6 @@ !/log/.keep !/tmp/.keep .env + # Ignore Byebug command history file. .byebug_history diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5bce99e6..878ee874 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,6 +2,11 @@ class SessionsController < ApplicationController def login_form end + def create + auth_hash = request.env['omniauth.auth'] + + end + def login username = params[:username] if username and user = User.find_by(username: username) diff --git a/config/routes.rb b/config/routes.rb index a7e8af1d..b2839c13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,4 +9,6 @@ post '/works/:id/upvote', to: 'works#upvote', as: 'upvote' resources :users, only: [:index, :show] + + get "/auth/:provider/callback", to: "sessions#create" end diff --git a/db/migrate/20181030002623_add_omniauth_columns_to_user_models.rb b/db/migrate/20181030002623_add_omniauth_columns_to_user_models.rb new file mode 100644 index 00000000..b4469cb4 --- /dev/null +++ b/db/migrate/20181030002623_add_omniauth_columns_to_user_models.rb @@ -0,0 +1,7 @@ +class AddOmniauthColumnsToUserModels < ActiveRecord::Migration[5.2] + def change + add_column :users, :email, :string + add_column :users, :provider, :string + add_column :users, :uid, :integer, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index a35df558..223fb9dd 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_30_002623) 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.string "provider" + t.integer "uid", null: false end create_table "votes", id: :serial, force: :cascade do |t| From b40a986dd8e2e237c2900730ceaec3e85593bf13 Mon Sep 17 00:00:00 2001 From: Leanne Rivera Date: Tue, 30 Oct 2018 18:50:49 -0700 Subject: [PATCH 4/5] transferred code over to revisited --- app/controllers/sessions_controller.rb | 19 ++- app/helpers/application_helper.rb | 8 ++ app/models/user.rb | 3 +- app/views/layouts/application.html.erb | 28 ++--- app/views/sessions/create.html.erb | 1 + app/views/users/index.html.erb | 8 ++ app/views/users/show.html.erb | 7 ++ app/views/works/edit.html.erb | 8 ++ app/views/works/index.html.erb | 8 ++ app/views/works/new.html.erb | 8 ++ app/views/works/show.html.erb | 8 ++ config/initializers/omniauth.rb | 3 + config/routes.rb | 4 +- test/controllers/works_controller_test.rb | 147 +++++++++++++++++++++- test/fixtures/users.yml | 2 + 15 files changed, 230 insertions(+), 32 deletions(-) create mode 100644 app/views/sessions/create.html.erb create mode 100644 config/initializers/omniauth.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 878ee874..68d931e5 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,22 +1,21 @@ class SessionsController < ApplicationController - def login_form - end def create - auth_hash = request.env['omniauth.auth'] - + @auth_hash = request.env['omniauth.auth'] end def login - username = params[:username] - if username and user = User.find_by(username: username) - session[:user_id] = user.id + current_user = User.find_by(uid: @auth_hash[:uid], provider: 'github') + + if current_user + # session[:user_id] = current_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 + #try to make a new user + current_user = User.build_from_github(auth_hash) + + if current_user.save flash[:status] = :success flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}" else diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 51b55d86..f35f4413 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -2,4 +2,12 @@ module ApplicationHelper def render_date(date) date.strftime("%b %e, %Y") end + + def logged_in? + return false if session[:id] == nil + + current_user = User.find_by(id: session[:id], uid: @auth_hash[:uid], provider: @auth_hash[:provider]) + + return current_user ? true : false + end end diff --git a/app/models/user.rb b/app/models/user.rb index 4cac8fe0..f0ef3175 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,5 +2,6 @@ class User < ApplicationRecord has_many :votes has_many :ranked_works, through: :votes, source: :work - validates :username, uniqueness: true, presence: true + validates :uid, uniqueness: true, presence: true + validates :provider, presence: true end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e7b07ce4..a6fc8de4 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -33,24 +33,18 @@ - + + <% if flash[:result_text] or flash[:messages] %> diff --git a/app/views/sessions/create.html.erb b/app/views/sessions/create.html.erb new file mode 100644 index 00000000..62c2e86e --- /dev/null +++ b/app/views/sessions/create.html.erb @@ -0,0 +1 @@ +create here diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index c27f0d34..f84722e7 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,3 +1,5 @@ +<% if logged_in? %> +

List of Users

@@ -19,3 +21,9 @@
<%= link_to "Back to Media List", root_path, class: "btn btn-secondary" %> + +<% else %> + +

FORBIDDEN

+ +<%end%> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index c58d9205..4e35a231 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,3 +1,5 @@ +<% if logged_in? %> +

User Summary: <%= link_to @user.username, user_path(@user) %>

Joined site <%= render_date @user.created_at %>

@@ -29,3 +31,8 @@ <%= link_to "See all Users", users_path, class: "btn btn-secondary" %> <%= link_to "Back to Media List", root_path, class: "btn btn-primary" %> +<% else %> + +

FORBIDDEN

+ +<%end%> diff --git a/app/views/works/edit.html.erb b/app/views/works/edit.html.erb index e7f63402..905c641c 100644 --- a/app/views/works/edit.html.erb +++ b/app/views/works/edit.html.erb @@ -1,2 +1,10 @@ +<% if logged_in? %> +

Edit This <%= @media_category.singularize.capitalize %>

<%= render partial: "form" %> + +<% else %> + +

FORBIDDEN

+ +<%end%> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index aa1531eb..eb91d5a8 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,3 +1,4 @@ +<% if logged_in? %>

List of Works

<% @works_by_category.each do |category, works| %> @@ -30,3 +31,10 @@ <%= link_to "View top media", root_path, class: "btn btn-secondary" %> <%= link_to "Add a new work", new_work_path, class: "btn btn-primary" %> + + +<% else %> + +

FORBIDDEN

+ +<%end%> diff --git a/app/views/works/new.html.erb b/app/views/works/new.html.erb index 02c89da0..3746bf70 100644 --- a/app/views/works/new.html.erb +++ b/app/views/works/new.html.erb @@ -1,2 +1,10 @@ +<% if logged_in? %> +

Add a new work

<%= render partial: "form" %> + +<% else %> + +

FORBIDDEN

+ +<%end%> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 8bd6d3b2..7dcbff39 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,3 +1,5 @@ +<% if logged_in? %> +

<%= @work.title %>

Created by: <%= @work.creator %>

@@ -29,3 +31,9 @@ <% end %>
+ +<% else %> + +

FORBIDDEN

+ +<%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/config/routes.rb b/config/routes.rb index b2839c13..9975d9c8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + get "/auth/github/callback", to: "sessions#create", as: 'auth_callback' root 'works#root' - get '/login', to: 'sessions#login_form', as: 'login' post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' @@ -9,6 +9,4 @@ post '/works/:id/upvote', to: 'works#upvote', as: 'upvote' resources :users, only: [:index, :show] - - get "/auth/:provider/callback", to: "sessions#create" end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index 0945ca47..e86a6e12 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -4,59 +4,150 @@ describe "root" do 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 + works(:poodr).destroy + + get root_path + + must_respond_with :success end it "succeeds with no media" do + works(:album).destroy + works(:another_album).destroy + works(:poodr).destroy + works(:movie).destroy + + get root_path + + must_respond_with :success end end CATEGORIES = %w(albums books movies) - INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext"] + INVALID_CATEGORIES = ["nope", "42", "", " ", "albumstrailingtext", "video games"] describe "index" do it "succeeds when there are works" do + get works_path + + must_respond_with :success end it "succeeds when there are no works" do + works(:album).destroy + works(:another_album).destroy + works(:poodr).destroy + works(:movie).destroy + + get works_path + + must_respond_with :success end end describe "new" do + let (:new_work) + { + creator: "leanne", + publication_year: 2018, + category: "book", + title: "i am is awesome and so am i", + description: "pullitzer prize-winning autobiography" + } + + it "succeeds" do + id = works(:album).id + + get new_work_path(id) + + must_respond_with :success end end describe "create" do + + it "creates a work with valid data for a real category" do + new_hash = { book: { + creator: "frank herbert", + publication_year: 1965, + category: "book", + title: "Dune", + description: "classic science-fiction adventure" + } } + + + expect { + post works_path, params: new_hash + }.must_change 'Work.count', 1 + + must_respond_with :success end it "renders bad_request and does not update the DB for bogus data" do + update_this = bogus_work + update_this.title = nil + + expect { + post works_path, params: bogus_work + }.wont_change 'Work.count' + + expect(update_this.save?).must_equal false + must_respond_with :bad_request end it "renders 400 bad_request for bogus categories" do + expect { + post works_path, params: bogus_work + }.wont_change 'Work.count' + + must_respond_with :bad_request + end + + it "should create a new work" do + + expect { + post works_path, params: new_work + }.must_change 'Work.count', 1 + end end describe "show" do + let (:work) {works(:album)} it "succeeds for an extant work ID" do + id = work.id + + get work_path(id) + + must_respond_with :success end it "renders 404 not_found for a bogus work ID" do + id = -1 + + get work_path(id) + + must_respond_with :not_found end end @@ -64,34 +155,88 @@ describe "edit" do it "succeeds for an extant work ID" do + id = works(:another_album).id + + get edit_work_path(id) + + must_respond_with :success + end it "renders 404 not_found for a bogus work ID" do + id = -1 + + get edit_work_path(id) + + must_respond_with :not_found end end describe "update" do + let (:work) {works(:poodr)} + it "succeeds for valid data and an extant work ID" do + expect { + patch work_path(id), params: work + }.wont_change 'Work.count' + + must_respond_with :success end it "renders bad_request for bogus data" do + edit = work + id = edit.id + edit.title = "" + + expect { + patch work_path(id), params: edit + }.wont_change 'Work.count' + + must_respond_with :bad_request end it "renders 404 not_found for a bogus work ID" do + id = -1 + + expect { + patch work_path(id), params: work + }.wont_change 'Work.count' + + must_respond_with :not_found end end describe "destroy" do + let (:work) {works(:poodr)} it "succeeds for an extant work ID" do + # Arrange + id = work.id + + # Act - Assert + expect { + delete work_path(id) + }.must_change 'Work.count', -1 + + must_respond_with :redirect + must_redirect_to root_path + assert_nil(Work.find_by(id: id)) end it "renders 404 not_found and does not update the DB for a bogus work ID" do + id = -1 + + expect { + delete work_path(id) + }.wont_change 'Work.count' + + must_respond_with :not_found + end end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index e2968d78..bea6d44b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -2,6 +2,8 @@ dan: username: dan + uid: 98 kari: username: kari + uid: 90 From 075b2fe4ac9f19e0d22934b6212ff9e6690e7c5b Mon Sep 17 00:00:00 2001 From: Leanne Rivera Date: Wed, 31 Oct 2018 13:05:56 -0700 Subject: [PATCH 5/5] most works controller failing, not update and not upvote --- Gemfile | 1 + Gemfile.lock | 5 ++ app/controllers/users_controller.rb | 2 +- test/controllers/sessions_controller_test.rb | 23 ++++++ test/controllers/users_controller_test.rb | 22 +++++ test/controllers/works_controller_test.rb | 87 ++++++++++++-------- test/test_helper.rb | 16 ++++ 7 files changed, 120 insertions(+), 36 deletions(-) diff --git a/Gemfile b/Gemfile index d617d204..4315e5bc 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,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 b4b8ef23..f66ae74f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,10 @@ 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) @@ -228,6 +232,7 @@ DEPENDENCIES bootstrap (~> 4.1.3) byebug coffee-rails (~> 4.2) + dotenv-rails jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 73b42652..b152dfc9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,7 +4,7 @@ def index end def show - @user = User.find_by(id: params[:id]) + @user = User.find_by(id: params[:id], uid: params[:uid]) render_404 unless @user end end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index f641d15c..bacd45e2 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -1,5 +1,28 @@ require "test_helper" describe SessionsController do + let(:user) { users(:kari) } + + it 'logs a user out' do + perform_login(user) + + expect{ + post logout_path + }.must_route_to root_path + end + + it 'logs in an existing user' do + expect{ + perform_login(user) + }.wont_change 'User.count' + end + + it 'builds new users for first time log-ins' do + new_user = User.new(username: 'name') + + expect{ + perform_login(new_user) + }.must_change 'User.count', 1 + end end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index d2c5cfbb..8b896f54 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,5 +1,27 @@ require 'test_helper' describe UsersController do + let (:user) { users(:dan) } + it 'finds a valid user' do + get users_path(user.id) + + must_respond_with :success + end + + it 'renders error if invalid user' do + id = -1 + + get users_path(id) + + must_respond_with :not_found + end + + it 'shows user index for logged in users' do + perform_login(user) + + get users_path + + must_respond_with :success + end end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index e86a6e12..193338fa 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -58,20 +58,9 @@ end describe "new" do - let (:new_work) - { - creator: "leanne", - publication_year: 2018, - category: "book", - title: "i am is awesome and so am i", - description: "pullitzer prize-winning autobiography" - } - - it "succeeds" do - id = works(:album).id - get new_work_path(id) + get new_work_path must_respond_with :success @@ -79,40 +68,52 @@ end describe "create" do - - - it "creates a work with valid data for a real category" do - new_hash = { book: { - creator: "frank herbert", - publication_year: 1965, + let (:new_work) do + { work: { + creator: "leanne", + publication_year: 2018, category: "book", - title: "Dune", - description: "classic science-fiction adventure" - } } - + title: "i am is awesome and so am i", + description: "pullitzer prize-winning autobiography" + } + } + end + it "creates a work with valid data for a real category" do expect { - post works_path, params: new_hash + post works_path, params: new_work }.must_change 'Work.count', 1 - - must_respond_with :success - end it "renders bad_request and does not update the DB for bogus data" do - update_this = bogus_work - update_this.title = nil + bogus_work = { work: { + creator: "leanne", + publication_year: 2018, + category: "book", + title: nil, + description: "pullitzer prize-winning autobiography" + } + } + + assert_nil(bogus_work['title']) expect { post works_path, params: bogus_work }.wont_change 'Work.count' - expect(update_this.save?).must_equal false must_respond_with :bad_request end it "renders 400 bad_request for bogus categories" do + bogus_work = { work: { + creator: "leanne", + publication_year: 2018, + category: "video games", + title: nil, + description: "pullitzer prize-winning autobiography" + } + } expect { post works_path, params: bogus_work @@ -120,7 +121,7 @@ must_respond_with :bad_request end - + # it "should create a new work" do expect { @@ -174,11 +175,15 @@ end describe "update" do - let (:work) {works(:poodr)} + let (:work) { works(:poodr) } it "succeeds for valid data and an extant work ID" do + update_work = work + update_work.title = "POODR THE SEQUEL" + + expect { - patch work_path(id), params: work + patch work_path(update_work.id) }.wont_change 'Work.count' must_respond_with :success @@ -241,21 +246,33 @@ end describe "upvote" do - + let (:user) { users(:dan) } it "redirects to the work page if no user is logged in" do + work = works(:album) - end + post upvote_path(work.id) - it "redirects to the work page after the user has logged out" do + must_redirect_to work_path(work.id) end it "succeeds for a logged-in user and a fresh user-vote pair" do + perform_login(user) + id = works(:poodr) + + expect { + post upvote_path(id) + }.must_change 'user.votes.count', 1 end it "redirects to the work page if the user has already voted for that work" do + perform_login(user) + id = works(:another_album).id + expect{ + post upvote_path(id) + }.wont_change 'user.votes.count' end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5b4fb667..19d4cbf6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -24,3 +24,19 @@ class ActiveSupport::TestCase fixtures :all # Add more helper methods to be used by all tests here... end + +def mock_auth_hash(user) + return { + provider: user.provider, + uid: user.uid, + info: { + email: user.email, + # username: user.name + } + } +end + +def perform_login(user) + OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user)) + get auth_callback_path(:github) +end