Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2012 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4418869 to your computer and use it in GitHub Desktop.
Save anonymous/4418869 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe "Clients Pages" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
visit new_user_session_path
fill_in "user_email", with: user.email
fill_in "user_password", with: user.password
click_button "Sign in"
end
after(:all) do
# User.delete_all
end
subject { page }
describe "index" do
before(:all) { 30.times { FactoryGirl.create(:client, user: user) } }
before(:each) do
visit clients_path
end
it { should have_selector('title', text: 'All clients') }
it { should have_selector('h1', text: 'All clients') }
it { should have_link('New Client', href: new_client_path) }
describe "lists clients" do
it "should list each user" do
Client.all.each do |client|
page.should have_selector('li>a', text: client.name)
end
end
end
end
describe "client creation" do
before(:each) do
visit new_client_path
end
it { should have_selector('title', text: 'New client') }
it { should have_selector('h1', text: 'New client') }
describe "with invalid information" do
it "should not create a client" do
expect { click_button "Create Client" }.not_to change(Client, :count)
end
describe "error messages" do
before { click_button "Create Client" }
it { should have_content('Error') }
end
end
describe "with valid information" do
let(:client) { FactoryGirl.build(:client) }
before(:each) do
visit new_client_path
fill_in "client_name", with: client.name
fill_in "client_address1", with: client.address1
fill_in "client_city", with: client.city
fill_in "client_state", with: client.state
select client.country, from: "client_country"
fill_in "client_zipcode", with: client.zipcode
fill_in "client_contactname", with: client.contactname
fill_in "client_email", with: client.email
end
it "should create a client" do
expect { click_button "Create Client" }.to change(Client, :count)
end
describe "success messages" do
before { click_button "Create Client" }
it { should have_content('created') }
end
end
end
describe "client page" do
let(:client) { FactoryGirl.create(:client, user: user) }
before(:each) do
visit client_path(client)
end
it { should have_content(client.name) }
it { should have_link('Edit', href: edit_client_path(client))}
it { should have_link('Delete', href: client_path(client))}
end
describe "edit client page" do
let(:client) { FactoryGirl.create(:client, user: user) }
before(:each) do
visit edit_client_path(client)
end
it { should have_content('Edit client')}
it { should have_field('client_name', value: client.name )}
describe "with invalid information" do
before(:each) { fill_in "client_name", with: "" }
it "should not update a client" do
#TODO expect { click_button "Update Client" }.not_to change(Client, :count)
end
describe "error messages" do
before do
click_button "Update Client"
end
it { should have_content('Error') }
end
end
describe "with valid information" do
let(:client) { FactoryGirl.create(:client, user: user) }
before(:each) do
visit edit_client_path(client)
fill_in "client_name", with: "Barotta kadai"
fill_in "client_address1", with: client.address1
fill_in "client_city", with: client.city
fill_in "client_state", with: client.state
select client.country, from: "client_country"
fill_in "client_zipcode", with: client.zipcode
fill_in "client_contactname", with: client.contactname
fill_in "client_email", with: client.email
end
it "should create a client" do
click_button "Update Client"
should have_content("Barotta kadai")
end
describe "success messages" do
before { click_button "Update Client" }
it { should have_content('Updated') }
end
end
end
describe "destroy client" do
describe "as correct client" do
let(:client) { FactoryGirl.create(:client, user: user) }
before { visit client_path(client) }
it "should delete client" do
expect { click_link "Delete" }.to change(Client, :count).by(-1)
end
end
end
describe "only owner should see client" do
before do
5.times { FactoryGirl.create(:client, name: "Old user clt", user: user) }
click_link "Sign Out"
new_user = FactoryGirl.create(:user)
visit new_user_session_path
fill_in "user_email", with: new_user.email
fill_in "user_password", with: new_user.password
click_button "Sign in"
visit clients_path
end
it "should not have any other user clients" do
should_not have_content("Old user clt")
end
end
describe "should not access other users client" do
before do
@vul_client = FactoryGirl.create(:client)
click_link "Sign Out"
@mal_user = FactoryGirl.create(:user)
visit new_user_session_path
fill_in "user_email", with: @mal_user.email
fill_in "user_password", with: @mal_user.password
click_button "Sign in"
end
it "show page" do
visit client_path(@vul_client)
should have_selector('h1', text: "All clients")
end
it "edit page" do
visit edit_client_path(@vul_client)
should have_selector('h1', text: "All clients")
end
pending "update action should have correct user"
pending "destroy action should have correct user"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment