Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing not finished........ #44

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/tmp/*
!/log/.keep
!/tmp/.keep

.env

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

def create
@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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 11 additions & 17 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,18 @@
</li>
</ul>
<ul class="nav app-header__user-nav-container">
<% if @login_user %>

<li class="nav-item app-header__nav_item">
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "btn btn-primary" %>
</li>
<li class="nav-item app-header__nav_item">
<%= link_to "Log Out", logout_path, method: :post, class: "btn btn-primary" %>
</li>

<% else %>

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

</ul>
</nav>
<ul class="nav app-header__user-nav-container>"
<li class="nav-item app-header__nav_item">
<% if @current_user %>
<%= link_to "Log out, #{@current_user.handle}", logout_path, method: :delete, class: "btn btn-primary" %> </li>
<li class="nav-item app-header__nav_item"> <%= link_to "My account", user_path(session[:user_id]), class: "btn btn-primary" %> </li>
<% else %>
<%= link_to "Log In", auth_callback_path, class: "btn btn-primary" %> </li>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auth_callback_path is not what you use to access the site, it's what the site uses to get back to you about the request. You want something more like <%= link_to "Login with Github", "/auth/github" %>

<% end %>
</li>
</ul>
</nav>
</header>

<% if flash[:result_text] or flash[:messages] %>
Expand Down
1 change: 1 addition & 0 deletions app/views/sessions/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
create here
8 changes: 8 additions & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% if logged_in? %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much more efficient to use a controller filter to redirect those who aren't logged in. This is not very DRY as a solution.


<h2>List of Users</h2>
<table class="table">
<thead>
Expand All @@ -19,3 +21,9 @@
</table>

<%= link_to "Back to Media List", root_path, class: "btn btn-secondary" %>

<% else %>

<h1> FORBIDDEN </h1>

<%end%>
7 changes: 7 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% if logged_in? %>

<h2>User Summary: <%= link_to @user.username, user_path(@user) %></h2>
<p>Joined site <%= render_date @user.created_at %></p>

Expand Down Expand Up @@ -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 %>

<h1> FORBIDDEN </h1>

<%end%>
8 changes: 8 additions & 0 deletions app/views/works/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
<% if logged_in? %>

<h2>Edit This <%= @media_category.singularize.capitalize %></h2>
<%= render partial: "form" %>

<% else %>

<h1> FORBIDDEN </h1>

<%end%>
8 changes: 8 additions & 0 deletions app/views/works/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% if logged_in? %>
<h2>List of Works</h2>

<% @works_by_category.each do |category, works| %>
Expand Down Expand Up @@ -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 %>

<h1> FORBIDDEN </h1>

<%end%>
8 changes: 8 additions & 0 deletions app/views/works/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
<% if logged_in? %>

<h2>Add a new work</h2>
<%= render partial: "form" %>

<% else %>

<h1> FORBIDDEN </h1>

<%end%>
8 changes: 8 additions & 0 deletions app/views/works/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<% if logged_in? %>

<section>
<h2><%= @work.title %></h2>
<p>Created by: <%= @work.creator %></p>
Expand Down Expand Up @@ -29,3 +31,9 @@
<% end %>
</ul>
</section>

<% else %>

<h1> FORBIDDEN </h1>

<%end%>
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email"
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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
37 changes: 20 additions & 17 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@
#
# It's strongly recommended that you check this file into your version control system.

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

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

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

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

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

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

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

it 'logs a user out' do
perform_login(user)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if you try to log out when nobody is logged in?


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
Loading