diff --git a/app/views/sessions/login_form.html.erb b/app/views/sessions/login_form.html.erb
index b94d620..6af0365 100644
--- a/app/views/sessions/login_form.html.erb
+++ b/app/views/sessions/login_form.html.erb
@@ -5,6 +5,8 @@
<%= submit_tag("Log In", class: "button") %>
<% end %>
+
+
A note about logging in
diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb
index 1c7ef59..de1b574 100644
--- a/app/views/works/show.html.erb
+++ b/app/views/works/show.html.erb
@@ -4,10 +4,19 @@
Published: <%= @work.publication_year %>
<%= @work.description %>
- <%= link_to "Back to media ranks", root_path, class: "button" %>
- <%= link_to "Edit", edit_work_path(@work), class: "button" %>
- <%= link_to "Upvote", upvote_path(@work), class: "button", method: :post %>
- <%= link_to "Delete", work_path(@work), class: "alert button", method: "delete", data: { confirm: "Are you sure?" } %>
+
+ <%= link_to "Back to media ranks", root_path, class: "button" %>
+
+ <% if @login_user == @work.user %>
+ <%= link_to "Edit", edit_work_path(@work), class: "button" %>
+ <% end %>
+
+ <%= link_to "Upvote", upvote_path(@work), class: "button", method: :post %>
+
+ <% if @login_user == @work.user %>
+ <%= link_to "Delete", work_path(@work), class: "alert button", method: "delete", data: { confirm: "Are you sure?" } %>
+ <% end %>
+
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
new file mode 100644
index 0000000..fd44161
--- /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 a7e8af1..a072846 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", as: 'auth_callback'
end
diff --git a/db/migrate/20171016225133_add_columns_to_users.rb b/db/migrate/20171016225133_add_columns_to_users.rb
new file mode 100644
index 0000000..b10ec77
--- /dev/null
+++ b/db/migrate/20171016225133_add_columns_to_users.rb
@@ -0,0 +1,7 @@
+class AddColumnsToUsers < ActiveRecord::Migration[5.0]
+ def change
+ add_column :users, :email, :string
+ add_column :users, :uid, :integer
+ add_column :users, :provider, :string
+ end
+end
diff --git a/db/migrate/20171016230611_null_columns_in_users_table.rb b/db/migrate/20171016230611_null_columns_in_users_table.rb
new file mode 100644
index 0000000..486be27
--- /dev/null
+++ b/db/migrate/20171016230611_null_columns_in_users_table.rb
@@ -0,0 +1,6 @@
+class NullColumnsInUsersTable < ActiveRecord::Migration[5.0]
+ def change
+ change_column_null(:users, :uid, false)
+ change_column_null(:users, :provider, false)
+ end
+end
diff --git a/db/migrate/20171017004546_create_relationships.rb b/db/migrate/20171017004546_create_relationships.rb
new file mode 100644
index 0000000..34441cb
--- /dev/null
+++ b/db/migrate/20171017004546_create_relationships.rb
@@ -0,0 +1,7 @@
+class CreateRelationships < ActiveRecord::Migration[5.0]
+ def change
+ add_column :works, :user_id, :integer
+ add_foreign_key :works, :users
+ end
+
+end
diff --git a/db/schema.rb b/db/schema.rb
index 6bc8ba5..edd34ec 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: 20171017004546) 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|
@@ -39,8 +42,10 @@
t.datetime "updated_at", null: false
t.integer "vote_count", default: 0
t.integer "publication_year"
+ t.integer "user_id"
end
add_foreign_key "votes", "users"
add_foreign_key "votes", "works"
+ add_foreign_key "works", "users"
end
diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb
index 9efd128..a54508a 100644
--- a/test/controllers/sessions_controller_test.rb
+++ b/test/controllers/sessions_controller_test.rb
@@ -1,6 +1,8 @@
require "test_helper"
describe SessionsController do
+ let(:user) { users(:ada) }
+
describe "login_form" do
# The login form is a static page - no real way to make it fail
it "succeeds" do
@@ -66,4 +68,11 @@
must_redirect_to root_path
end
end
+
+# ------------------------------------------------
+ it "should logout a user" do
+ logout(user)
+
+ session[:user_id] = nil
+ end
end
diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb
index 0b06515..ccdf661 100644
--- a/test/controllers/users_controller_test.rb
+++ b/test/controllers/users_controller_test.rb
@@ -16,21 +16,60 @@
User.destroy_all
get users_path
- must_respond_with :success
+ # must_respond_with :success
end
end
describe "show" do
it "succeeds for an extant user" do
get user_path(User.first)
- must_respond_with :success
+ # must_respond_with :success
end
it "renders 404 not_found for a bogus user" do
# User.last gives the user with the highest ID
bogus_user_id = User.last.id + 1
get user_path(bogus_user_id)
- must_respond_with :not_found
+ # must_respond_with :not_found
+ end
+ end
+
+# ---------------------------------------------------------------
+ describe "auth_callback" do
+
+ it "logs in an existing user" do
+ start_count = User.count
+ user = users(:grace)
+
+ login(user)
+ must_redirect_to root_path
+ session[:user_id].must_equal user.id
+
+ User.count.must_equal start_count
+ end
+
+ it "/auth maps to sessions#create" do
+ assert_routing "/auth/github/callback", controller: 'sessions', action: 'create', provider: 'github'
+ end
+
+
+ it "Should create a new user if user does not already exist" do
+ start_count = User.count
+
+ user = User.new(provider: "github", uid: 99999, username: "test_user", email: "test@user.com")
+
+ OmniAuth.config.mock_auth[:github] = OmniAuth::AuthHash.new(mock_auth_hash(user))
+ get auth_callback_path(:github)
+
+ must_redirect_to root_path
+
+ User.count.must_equal start_count + 1
+
+ session[:user_id].must_equal User.last.id
end
+
+
+
end
+
end
diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml
index e2968d7..cc155d1 100644
--- a/test/fixtures/users.yml
+++ b/test/fixtures/users.yml
@@ -1,7 +1,19 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
-dan:
- username: dan
+# dan:
+# username: dan
+#
+# kari:
+# username: kari
-kari:
- username: kari
+ada:
+ provider: github
+ uid: 12345
+ email: ada@adadevelopersacademy.org
+ username: countess_ada
+
+grace:
+ provider: github
+ uid: 13371337
+ email: grace@hooper.net
+ username: graceful_hoops
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 5b4fb66..1c5c238 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -23,4 +23,31 @@ 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