Permalink
Browse files

Add tests for user who visite draft article or draft tag

  • Loading branch information...
1 parent cc3a15a commit 6615a21ac19e16ab5c169a9a0e923d8b03893c9e @reyesyang committed Jan 3, 2014
View
2 Gemfile
@@ -63,6 +63,7 @@ end
group :development, :test do
gem 'quiet_assets'
gem 'pry-rails'
+ gem 'pry-debugger'
gem 'rspec-rails'
gem 'factory_girl_rails'
@@ -78,5 +79,6 @@ group :test do
gem "selenium-webdriver"
gem "launchy"
gem "database_cleaner"
+ gem "show_me_the_cookies"
gem "simplecov", require: false
end
View
14 Gemfile.lock
@@ -58,9 +58,16 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
+ columnize (0.3.6)
commonjs (0.2.7)
dalli (2.6.4)
database_cleaner (1.2.0)
+ debugger (1.6.5)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.2.0)
+ debugger-ruby_core_source (~> 1.3.1)
+ debugger-linecache (1.2.0)
+ debugger-ruby_core_source (1.3.1)
diff-lcs (1.2.5)
docile (1.1.1)
erubis (2.7.0)
@@ -120,6 +127,9 @@ GEM
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
+ pry-debugger (0.2.2)
+ debugger (~> 1.3)
+ pry (~> 0.9.10)
pry-rails (0.3.2)
pry (>= 0.9.10)
quiet_assets (1.0.2)
@@ -171,6 +181,8 @@ GEM
websocket (~> 1.0.4)
shoulda-matchers (2.4.0)
activesupport (>= 3.0.0)
+ show_me_the_cookies (2.1.0)
+ capybara (~> 2.0)
simplecov (0.8.2)
docile (~> 1.1.0)
multi_json
@@ -247,6 +259,7 @@ DEPENDENCIES
less-rails
mysql2
newrelic_rpm
+ pry-debugger
pry-rails
quiet_assets
rails (~> 4.0.0)
@@ -256,6 +269,7 @@ DEPENDENCIES
sass-rails (~> 4.0.0)
selenium-webdriver
shoulda-matchers
+ show_me_the_cookies
simplecov
therubyracer
turbolinks
View
4 app/assets/javascripts/global.js.coffee
@@ -1,3 +1,7 @@
$(document).on 'page:fetch', -> NProgress.start()
$(document).on 'page:change', -> NProgress.done()
$(document).on 'page:restore', -> NProgress.remove()
+
+$(document).on 'page:update', ->
+ if $.cookie('admin') isnt "true"
+ $('.admin').remove()
View
2 app/controllers/application_controller.rb
@@ -15,6 +15,6 @@ def logged_in?
private
def require_admin
- redirect_to root_path if !(logged_in? && current_user.admin?)
+ redirect_to root_path if !current_user.try(:admin?)
end
end
View
8 app/controllers/articles_controller.rb
@@ -29,12 +29,13 @@ def create
end
def show
- @page_title = "文章 - #{@article.title}"
+ raise ActiveRecord::RecordNotFound if !current_user.try(:admin?) && @article.draft?
+ @page_title = "#{@article.title}"
@page_description = @article.title
end
def edit
- @page_title = "编辑文章 - #{@article.title}"
+ @page_title = "#{@article.title}"
render layout: "wide"
end
@@ -56,7 +57,10 @@ def destroy
def tagging
tag = Tag.find_by_name! params[:tag]
+ raise ActiveRecord::RecordNotFound if tag.draft? && !current_user.try(:admin?)
+
@articles = tag.articles.includes(:tags).paginate(page: params[:page])
+ @page_title = "#{tag.name} 相关文章"
render :index
end
View
4 app/controllers/sessions_controller.rb
@@ -18,16 +18,18 @@ def create
if logged_in
session[:email] = result[:email]
cookies[:email] = result[:email]
+ cookies[:admin] = current_user.admin?
end
respond_to do |format|
format.json { render json: result, status: logged_in ? 200 : 403 }
end
end
- def destroy
+ def destroy
session[:email] = nil
cookies.delete :email
+ cookies.delete :admin
respond_to do |format|
format.json { render nothing: true, status: 204 }
View
2 app/views/layouts/application.html.haml
@@ -2,7 +2,7 @@
%html
%head
%meta{charset: 'utf-8'}
- %title= "#{@page_title + ' - ' if @page_title}" + APP_CONFIG['default_title']
+ %title= "#{APP_CONFIG['default_title']}#{' | ' + @page_title if @page_title}"
%meta{name: 'description', content: "#{@page_description || APP_CONFIG['default_description']}"}
%meta{:'http-equiv' => 'X-UA-Compativle', content: 'IE=Edge'}
= csrf_meta_tag
View
101 spec/controllers/articles_controller_spec.rb
@@ -1,6 +1,7 @@
require 'spec_helper'
describe ArticlesController do
+ let(:admin_email) { APP_CONFIG[:admin_email] }
let(:normal_user_email) { "normal_user@example.com" }
shared_examples "permission control" do
@@ -11,7 +12,7 @@
describe "GET #index" do
let!(:normal_article) { create :article }
- let!(:draft_article) { create :article, tag_list: "draft" }
+ let!(:draft_article) { create :draft_article }
shared_examples "for all user" do
it "page title set to '首页'" do
@@ -149,26 +150,36 @@
end
describe "GET #show" do
+ let(:article) { create :article }
+ let(:draft_article) { create :draft_article }
+
shared_examples "for all user" do
it "get @article" do
expect(assigns[:article]).to eq article
end
it "set page title" do
- expect(assigns[:page_title]).to eq "文章 - #{assigns[:article].title}"
+ expect(assigns[:page_title]).to eq assigns[:article].title
end
it "render show page" do
expect(response).to render_template :show
end
end
- let(:article) { create :article }
+ shared_examples "for no or normal user login" do
+ it "raise ActiveRecord::RecordNotFound error when show draft article" do
+ expect do
+ get :show, id: draft_article
+ end.to raise_error ActiveRecord::RecordNotFound
+ end
+ end
context "when no user sign in" do
before { get :show, id: article }
it_behaves_like "for all user"
+ it_behaves_like "for no or normal user login"
end
context "when sign in with normal user" do
@@ -178,15 +189,29 @@
end
it_behaves_like "for all user"
+ it_behaves_like "for no or normal user login"
end
context "when sign in with admin" do
- before do
- set_user_session APP_CONFIG[:admin_email]
- get :show, id: article
+ context "show normal article" do
+ before do
+ set_user_session APP_CONFIG[:admin_email]
+ get :show, id: article
+ end
+
+ it_behaves_like "for all user"
end
- it_behaves_like "for all user"
+ context "show draft article" do
+ before do
+ set_user_session APP_CONFIG[:admin_email]
+ get :show, id: draft_article
+ end
+
+ it_behaves_like "for all user" do
+ let(:article) { draft_article }
+ end
+ end
end
end
@@ -219,7 +244,7 @@
end
it "set page title" do
- expect(assigns[:page_title]).to eq "编辑文章 - #{assigns[:article].title}"
+ expect(assigns[:page_title]).to eq assigns[:article].title
end
it "render edit page" do
@@ -325,4 +350,64 @@
end
end
end
+
+ describe "GET #tagging" do
+ let!(:article) { create :article }
+ let!(:draft_article) { create :draft_article }
+
+ shared_examples "for all user" do
+ it "set page title" do
+ get :tagging, tag: tag_name
+ expect(assigns[:page_title]).to eq "#{tag_name} 相关文章"
+ end
+
+ it "set @articles when tag is not 'draft'" do
+ get :tagging, tag: tag_name
+ expect(assigns[:articles]).to eq [article]
+ end
+
+ it "render index" do
+ get :tagging, tag: tag_name
+ expect(response).to render_template :index
+ end
+ end
+
+ shared_examples "for no or normal user login" do
+ it "set @articles to empty array when tag is 'draft'" do
+ expect do
+ get :tagging, tag: 'draft'
+ end.to raise_error ActiveRecord::RecordNotFound
+ end
+ end
+
+ context "when no user login" do
+ it_behaves_like "for all user" do
+ let(:tag_name) { article.tags[0].name }
+ end
+ it_behaves_like "for no or normal user login"
+ end
+
+ context "when sign in with normal user" do
+ before { set_user_session normal_user_email }
+
+ it_behaves_like "for all user" do
+ let(:tag_name) { article.tags[0].name }
+ end
+ it_behaves_like "for no or normal user login"
+ end
+
+ context "when sign in with admin" do
+ before { set_user_session admin_email }
+
+ it_behaves_like "for all user" do
+ let(:tag_name) { article.tags[0].name }
+ end
+
+ it "set @articles when tag is 'draft'" do
+ get :tagging, tag: 'draft'
+
+ expect(assigns[:articles]).to eq [draft_article]
+ end
+ end
+ end
end
View
4 spec/factories/articles.rb
@@ -5,5 +5,9 @@
sequence(:title) { |n| "artile title #{n}" }
content { 'a' * 20 }
tag_list 'tag1,tag2'
+
+ factory :draft_article do
+ tag_list "draft"
+ end
end
end
View
115 spec/features/articles_spec.rb
@@ -1,7 +1,109 @@
require 'spec_helper'
feature "Articles" do
- scenario "Post a new article", js: true do
+ given(:admin_email) { APP_CONFIG[:admin_email] }
+ given(:normal_user_email) { 'normal_user@example.com' }
+
+ scenario "Anonymous user should not see draft articles", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ visit root_path
+
+ expect(page).to have_content article.title
+ article.tags.each do |tag|
+ expect(page).to have_link tag.name
+ end
+
+ expect(page).to_not have_content draft_article.title
+ expect(page).to_not have_link 'draft'
+
+ click_link article.title
+ expect(page).to_not have_link 'draft'
+ end
+
+ scenario "Normal user should not see draft articles", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ sign_in normal_user_email
+
+ expect(page).to have_content article.title
+ article.tags.each do |tag|
+ expect(page).to have_link tag.name
+ end
+
+ expect(page).to_not have_content draft_article.title
+ expect(page).to_not have_link 'draft'
+
+ click_link article.title
+ expect(page).to_not have_link 'draft'
+ end
+
+ scenario "Admin user should see draft articles", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ sign_in admin_email
+
+ expect(page).to have_content article.title
+ article.tags.each do |tag|
+ expect(page).to have_link tag.name
+ end
+
+ expect(page).to have_content draft_article.title
+ expect(page).to have_link 'draft'
+
+ click_link article.title
+ expect(page).to have_link 'draft'
+ end
+
+ scenario "Anonymous user should not see article operation links", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ visit root_path
+
+ expect(page).to_not have_link '现在就发表一篇'
+ expect(page).to_not have_link '编辑'
+ expect(page).to_not have_link '删除'
+
+ click_link article.title
+ expect(page).to_not have_link '编辑'
+ expect(page).to_not have_link '删除'
+ end
+
+ scenario "Normal user should not see article operation links", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ sign_in normal_user_email
+
+ expect(page).to_not have_link '现在就发表一篇'
+ expect(page).to_not have_link '编辑'
+ expect(page).to_not have_link '删除'
+
+ click_link article.title
+ expect(page).to_not have_link '编辑'
+ expect(page).to_not have_link '删除'
+ end
+
+ scenario "Admin user should see article operation links", js: true do
+ article = create :article
+ draft_article = create :draft_article
+
+ sign_in admin_email
+
+ expect(page).to have_link '现在就发表一篇'
+ expect(page).to have_link '编辑'
+ expect(page).to have_link '删除'
+
+ click_link article.title
+ expect(page).to have_link '编辑'
+ expect(page).to have_link '删除'
+ end
+
+ scenario "Admin post a new article", js: true do
sign_in APP_CONFIG[:admin_email]
click_link "现在就发表一篇"
@@ -23,12 +125,9 @@
expect(page).to have_content tag_name
end
expect(page).to have_content article.content
-
- sign_out
end
-
- scenario "Edit a exist article", js: true do
+ scenario "Admin edit a exist article", js: true do
article = create :article
sign_in APP_CONFIG[:admin_email]
@@ -53,11 +152,9 @@
expect(page).to have_content tag_name
end
expect(page).to have_content article.content
-
- sign_out
end
- scenario "Delete a exist article", js: true do
+ scenario "Admin delete a exist article", js: true do
article = create :article
sign_in APP_CONFIG[:admin_email]
@@ -71,7 +168,5 @@
confirm.accept
expect(current_path).to eq articles_path
-
- sign_out
end
end
View
8 spec/features/persona_spec.rb
@@ -4,10 +4,10 @@
scenario "sign in and sign out", js: true do
visit root_path
sign_in APP_CONFIG["admin_email"]
- expect(page).to have_content "退出"
+ expect(page).to have_link "退出"
sign_out
- expect(page).to have_content "登录"
+ expect(page).to have_link "登录"
end
scenario "cancel sign out", js: true do
@@ -18,8 +18,6 @@
alert = page.driver.browser.switch_to.alert
alert.dismiss
- expect(page).to have_content "退出"
-
- sign_out
+ expect(page).to have_link "退出"
end
end
View
2 spec/spec_helper.rb
@@ -28,6 +28,7 @@
# config.mock_with :rr
config.include FactoryGirl::Syntax::Methods
+ config.include ShowMeTheCookies, type: :feature
config.include PersonaMacros
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
@@ -44,6 +45,7 @@
end
config.after(:each) do
+ page.execute_script('navigator.id.logout()') if example.metadata[:js]
DatabaseCleaner.clean
end
View
3 spec/support/persona_macros.rb
@@ -16,15 +16,12 @@ def sign_in(email)
alert = page.driver.browser.switch_to.alert
alert.send_keys email
alert.accept
- #page.driver.js_prompt_input = email
- #page.driver.accept_js_prompts!
end
def sign_out
click_link "退出"
alert = page.driver.browser.switch_to.alert
alert.accept
- # page.driver.accept_js_comfirms!
end
end

0 comments on commit 6615a21

Please sign in to comment.