/app.views.users._user.html.erb Secret
Last active
January 2, 2016 22:59
Failing Rspec tests at the end of Rails Tutorial ch 9 for unknown reason
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li> | |
<%= gravatar_for user, size: 52 %> | |
<%= link_to user.name, user %> | |
<% if current_user.admin? && !current_user?(user) %> | |
| <%= link_to "delete", user, method: :delete, | |
data: { confirm: "You sure?" } %> | |
<% end %> | |
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "Authentication" do | |
subject { page } | |
# verify that non-signed-in users attempting to access either action | |
# are simply sent to the signin page | |
describe "authorization" do | |
describe "as non-admin user" do | |
let(:user) { FactoryGirl.create(:user) } | |
let(:non_admin) { FactoryGirl.create(:user) } | |
before { sign_in non_admin, no_capybara: true } | |
describe "submitting a DELETE request to the Users#destroy action" do | |
before { delete user_path(user) } | |
specify { expect(response).to redirect_to(root_url) } | |
end | |
end | |
describe "as wrong user" do | |
let(:user) { FactoryGirl.create(:user) } | |
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") } | |
before { sign_in user, no_capybara: true } | |
describe "submitting a GET request to the Users#edit action" do | |
before { get edit_user_path(wrong_user) } | |
specify { expect(response.body).not_to match(full_title('Edit user')) } | |
specify { expect(response).to redirect_to(root_url) } | |
end | |
describe "submitting a PATCH request to the User#update action" do | |
before { patch user_path(wrong_user) } | |
specify { expect(response).to redirect_to(root_url) } | |
end | |
end | |
describe "for non-signed-in users" do | |
let(:user) { FactoryGirl.create(:user) } | |
describe "when attempting to visit a protected page" do | |
before do | |
visit edit_user_path(user) | |
fill_in "Email", with: user.email | |
fill_in "Password", with: user.password | |
click_button "Sign in" | |
end | |
describe "after signing in" do | |
it "should render the desired protected page" do | |
expect(page).to have_title('Edit user') | |
end | |
end | |
end | |
describe "in the Users controller" do | |
describe "visiting the edit page" do | |
before { visit edit_user_path(user) } | |
it { should have_title('Sign in') } | |
end | |
describe "submitting to the update action" do | |
before { patch user_path(user) } | |
specify { expect(response).to redirect_to(signin_path) } | |
# tests response of the server | |
end | |
describe "visiting the user index" do | |
before { visit users_path } | |
it { should have_title('Sign in') } | |
end | |
end | |
end | |
end | |
describe "signin page" do | |
before { visit signin_path } | |
it { should have_content('Sign in') } | |
it { should have_title('Sign in') } | |
end | |
describe "signin" do | |
before { visit signin_path } | |
describe "with invalid information" do | |
before { click_button "Sign in" } | |
it { should have_title('Sign in') } | |
it { should have_selector('div.alert.alert-error') } | |
describe "after visiting another page" do | |
before { click_link "Home" } | |
it { should_not have_selector('div.alert.alert-error') } | |
end | |
end | |
describe "with valid information" do | |
let(:user) { FactoryGirl.create(:user) } | |
# before { sign_in user } | |
before do | |
fill_in "Email", with: user.email.upcase | |
fill_in "Password", with: user.password | |
click_button "Sign in" | |
end | |
it { should have_title(user.name) } | |
it { should have_link('Users', href: users_path) } | |
it { should have_link('Profile', href: user_path(user)) } | |
it { should have_link('Settings', href: edit_user_path(user)) } | |
it { should have_link('Sign out', href: signout_path) } | |
it { should_not have_link('Sign in', href: signin_path) } | |
describe "followed by signout" do | |
before { click_link "Sign out" } | |
it { should have_link('Sign in') } | |
#it { should_not have_link('Settings') } | |
end | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
desc "Fill database with sample data" | |
task populate: :environment do | |
admin = User.create!(name: "Example User", | |
email: "example@awesome.org", | |
password: "foobar", | |
password_confirmation: "foobar", | |
admin: true) | |
99.times do |n| | |
name = Faker::Name.name | |
email = "example-#{n+1}@railstutorial.org" | |
password = "password" | |
User.create!(name: name, | |
email: email, | |
password: password, | |
password_confirmation: password) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :user do | |
sequence(:name) { |n| "Person #{n}" } | |
sequence(:email) { |n| "person_#{n}@example.com" } | |
password "foobar" | |
password_confirmation "foobar" | |
factory :admin do | |
admin true | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "User_pages" do | |
subject { page } | |
describe "index" do | |
let(:user) { FactoryGirl.create(:user) } | |
before(:each) do | |
sign_in user | |
visit users_path | |
end | |
it { should have_title('All users') } | |
it { should have_content('All users') } | |
describe "pagination" do | |
before(:all) { 30.times { FactoryGirl.create(:user) } } | |
after(:all) { User.delete_all } | |
it { should have_selector('div.pagination') } | |
it "should list each user" do | |
User.paginate(page: 1).each do |user| | |
expect(page).to have_selector('li', text: user.name) | |
end | |
end | |
end | |
end | |
describe "delete links" do | |
it { should_not have_link('delete') } | |
describe "as an admin user" do | |
let(:admin) { FactoryGirl.create(:admin) } | |
before do | |
sign_in admin | |
visit users_path | |
end | |
it { should have_link('delete', href: user_path(User.first)) } | |
it "should be able to delete another user" do | |
expect do | |
click_link('delete', match: :first) | |
end.to change(User, :count).by(-1) | |
end | |
it { should_not have_link('delete', href: user_path(admin)) } | |
end | |
end | |
describe "profile page" do | |
let(:user) { FactoryGirl.create(:user) } | |
before { visit user_path(user) } | |
it { should have_content(user.name) } | |
it { should have_title(user.name) } | |
end | |
describe "edit" do | |
let(:user) { FactoryGirl.create(:user) } | |
before do | |
sign_in user | |
visit edit_user_path(user) | |
end | |
describe "page" do | |
it { should have_content("Update your profile") } | |
it { should have_title("Edit user") } | |
it { should have_link('change', href: 'http://gravatar.com/emails') } | |
end | |
describe "with invalid information" do | |
before { click_button "Save changes" } | |
it { should have_content('error') } | |
end | |
describe "with valid information" do | |
let(:new_name) { "New Name" } | |
let(:new_email) { "new@example.com" } | |
before do | |
fill_in "Name", with: new_name | |
fill_in "Email", with: new_email | |
fill_in "Password", with: user.password | |
fill_in "Confirm Password", with: user.password | |
click_button "Save changes" | |
end | |
it { should have_title(new_name) } | |
it { should have_selector('div.alert.alert-success') } | |
it { should have_link('Sign out', href: signout_path) } | |
specify { expect(user.reload.name).to eq new_name } | |
specify { expect(user.reload.email).to eq new_email } | |
end | |
end | |
describe "signup page" do | |
before { visit signup_path } | |
it { should have_content('Sign up') } | |
it { should have_title(full_title('Sign up')) } | |
let(:submit) { "Create my account" } | |
describe "with invalid information" do | |
describe "after submission" do | |
before { click_button submit } | |
it { should have_title('Sign up') } | |
it { should have_content('error') } | |
end | |
it "should not create a user" do | |
expect { click_button submit }.not_to change(User, :count) | |
end | |
end | |
describe "with valid information" do | |
before do | |
fill_in "Name", with: "Example User" | |
fill_in "Email", with: "user@example.com" | |
fill_in "Password", with: "foobar" | |
fill_in "Confirmation", with: "foobar" | |
end | |
describe "after saving the user" do | |
before { click_button submit } | |
let(:user) { User.find_by(email: 'user@example.com') } | |
it { should have_link("Sign out") } | |
it { should have_title(user.name) } | |
it { should have_selector('div.alert.alert-success', text: 'Welcome') } | |
end | |
it "should create a user" do | |
expect { click_button submit }.to change(User, :count).by(1) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
before_action :signed_in_user, only: [:index, :edit, :update, :destroy] | |
before_action :correct_user, only: [:edit, :update] | |
before_action :admin_user, only: :destroy | |
def index | |
@users = User.paginate(page: params[:page]) | |
end | |
def destroy | |
User.find(params[:id]).destroy | |
flash[:success] = "User destroyed." | |
redirect_to users_url | |
end | |
def show | |
@user = User.find(params[:id]) | |
#find can smartly turn params[:id] from a str to int | |
end | |
def new | |
@user = User.new | |
end | |
def create | |
@user = User.new(user_params) #not final | |
if @user.save | |
sign_in @user | |
flash[:success] = "Welcome to the Twitter Clone App!" | |
redirect_to @user | |
else | |
render 'new' | |
end | |
end | |
def edit | |
# @user = User.find(params[:id]) | |
# made obselete by the correct_user attribute above | |
end | |
def update | |
# @user = User.find(params[:id]) | |
# see edit above | |
if @user.update_attributes(user_params) | |
flash[:success] = "Profile updated" | |
redirect_to @user | |
else | |
render 'edit' | |
end | |
end | |
private | |
def user_params | |
params.require(:user).permit(:name, :email, :password, :password_confirmation) | |
end | |
# Before Filters | |
def signed_in_user | |
unless signed_in? | |
store_location | |
redirect_to signin_url, notice: "Please sign in." | |
end | |
end | |
def correct_user | |
@user = User.find(params[:id]) | |
redirect_to(root_url) unless current_user?(@user) | |
end | |
def admin_user | |
redirect_to(root_url) unless current_user.admin? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment