Commit 54846764 by vulehuan

rspec for user features

parent 4a7b53ad
......@@ -35,11 +35,11 @@
</nav>
<div class="clearfix"></div>
<% if flash.any? %>
<div id="block-message" class="hidden">
<!-- <div id="block-message" class="hidden"> -->
<% flash.each do |key, value| %>
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
<% end %>
</div>
<!-- </div> -->
<% end %>
<%= yield %>
</div>
......
<% provide(:title, "Sign in") %>
<% provide(:title, "Login") %>
<div class="body-box">
<h2 class="sprite-2">Sign in<span class="sprite-2"></span></h2>
<h2 class="sprite-2">Login<span class="sprite-2"></span></h2>
<div class="text-justify">
<div id="block-message-visible" class="hidden"></div>
<%= form_for(:session, url: sessions_path) do |f| %>
......@@ -11,7 +11,7 @@
<%= f.label :password %>
<%= f.password_field :password, class: "form-control" %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary btn-submit" %>
<%= f.submit "Login", class: "btn btn-large btn-primary btn-submit" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
......
<% provide(:title, 'Sign up') %>
<% provide(:title, 'Register') %>
<div class="body-box">
<h2 class="sprite-2">Sign up<span class="sprite-2"></span></h2>
<div class="text-justify">
......
FactoryGirl.define do
factory :user do
# name "Michael Hartl"
# email "michael@example.com"
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
def sign_in(user, options={})
if options[:no_capybara]
# Sign in when not using Capybara
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Login"
end
end
require "spec_helper"
describe UserMailer do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'spec_helper'
describe MyCard do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'spec_helper'
describe ProductCategories do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'spec_helper'
describe Product do
pending "add some examples to (or delete) #{__FILE__}"
end
require 'spec_helper'
describe User do
pending "add some examples to (or delete) #{__FILE__}"
before do
@user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should respond_to(:admin) }
it { should be_valid }
it { should_not be_admin }
# Tests for an admin attribute.
describe "with admin attribute set to 'true'" do
before do
@user.save!
@user.toggle!(:admin)
end
it { should be_admin }
end
it { should be_valid }
describe "when name is not present" do
before { @user.name = " "}
it { should_not be_valid}
end
describe "when email is not present" do
before { @user.email = " "}
it { should_not be_valid}
end
describe "when name is too long" do
before { @user.name = "a" * 51}
it { should_not be_valid}
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+baz.com]
addresses.each do |invalid_address|
@user.email = invalid_address
expect(@user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
addresses.each do |valid_address|
@user.email = valid_address
expect(@user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { @user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { @user.password = @user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by(email: @user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
describe "remember token" do
before { @user.save }
its(:remember_token) { should_not be_blank }
end
end
require 'spec_helper'
describe "AuthenticatePages" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_content('Login') }
it { should have_title('Login') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Login" }
it { should have_title('Login') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_title(user.name) }
it { should have_link('Logout'), href: signout_path }
it { should_not have_link('Login', href: signin_path) }
describe "followed by signout" do
before { click_link "Logout" }
it { should have_link("Login") }
end
end
end
describe "authorization" do
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 "Login"
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('Login') }
end
describe "submitting to the update action" do
before { patch user_path(user) }
specify { expect(response).to redirect_to(signin_path) }
end
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 Users#update action" do
before { patch user_path(wrong_user) }
specify { expect(response).to redirect_to(root_url) }
end
end
end
end
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "GET /register" do
before { visit signup_path }
it { should have_content('Register') }
it { should have_title(full_title('Register')) }
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 "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_title('Register') }
it { should have_content('error') }
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: "123456"
fill_in "Confirm Password", with: "123456"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: "user@example.com") }
it { should have_link('Logout') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
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
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('Logout', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
# Testing that the admin attribute is forbidden.
describe "forbidden attributes" do
let(:params) do
{ user: { admin: true, password: user.password, password_confirmation: user.password } }
end
before do
sign_in user, no_capybara: true
patch user_path(user), params
end
specify { expect(user.reload).not_to be_admin }
end
end
end
......@@ -39,4 +39,5 @@ RSpec.configure do |config|
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
end
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Venshop"
if page_title.empty?
base_title
else
"#{page_title} | #{base_title}"
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment