Commit 1fa218ff authored by azul's avatar azul

cleanup: remove skipped survey tests

parent 09d43878
require 'test_helper'
class SurveyPageControllerTest < ActionController::TestCase
def setup
skip 'skipping survey tests for now'
end
def test_create_survey
login_as :blue
post :create, page: { title: 'a little survey for you' }, id: 'survey'
page = assigns(:page)
post :edit, page_id: page.id, survey: { description: 'description' }
assert assigns(:survey).valid?
assert assigns(:page).valid?
assert_not_nil Page.find(assigns(:page).id).data
end
def test_save_survey
login_as :blue
get :edit, page_id: pages('survey_blank').id
assert_response :success
assert_active_tab 'Edit Survey'
assert_not_nil assigns(:survey)
# this will create two new questions
save_survey_to_blank_page('new_questions_attributes' => {
'new_0.4083156603636907' => { 'newline_delimited_choices' => "sweet\r\nsour\r\npork-flavored\r\nstale", 'private' => '1', 'type' => 'SelectOneQuestion', 'label' => 'chips', 'position' => '1' },
'new_0.19274031862237884' => { 'private' => '0', 'type' => 'ShortTextQuestion', 'label' => 'how can we help you?', 'position' => '2' }
},
'edit_may_rate' => '0',
'admin_may_rate' => '1',
'edit_may_create' => '0')
assert_redirected_to @controller.page_url(pages('survey_blank'), action: 'edit'), 'save action should redirect to edit'
# assert_redirected_to "_page_action" => "edit"
get :edit, page_id: pages('survey_blank').id
assert_active_tab 'Edit Survey'
assert_response :success
survey = assigns(:survey)
assert_equal 'chips', survey.questions[0].label
assert_equal ['sweet', 'sour', 'pork-flavored', 'stale'], survey.questions[0].choices
assert_equal 'how can we help you?', survey.questions[1].label
assert_equal true, survey.admin_may_rate?
assert_equal false, survey.edit_may_rate?
assert_equal false, survey.edit_may_create?
end
def test_create_same_name
login_as :gerrard
data_ids = []
page_ids = []
page_urls = []
3.times do
post 'create', page: { title: 'dupe', summary: '' }, id: SurveyPage.param_id
page = assigns(:page)
assert_equal 'dupe', page.title
assert_not_nil page.id
# check that we have:
# a new page
assert !page_ids.include?(page.id)
# a new url
assert !page_urls.include?(page.name_url)
# remember the values we saw
page_ids << page.id
page_urls << page.name_url
end
end
protected
def assert_active_tab(tab_text)
assert_select '.nav-tabs' do
assert_select 'a.active', text: tab_text
end
end
def assert_tabs(tabs)
assert_select '.nav-tabs' do
assert_select '.tab a', count: tabs.size do |tab_links|
links_text = tab_links.collect { |link| link.children.first.content }
assert_equal tabs, links_text
end
end
end
def save_survey_to_blank_page(params)
post :edit, page_id: pages('survey_blank').id, survey: params
end
end
require 'test_helper'
class SurveyPageResponseControllerTest < ActionController::TestCase
def setup
skip 'skipping survey page tests for now'
end
def test_new_response
login_as :dolphin
# the user should exist and have access level :edit
@user = users(:dolphin)
assert @user, 'the user should exist'
@page = pages(:survey_blank)
assert @page, 'the page should exist'
assert @user.may?(:edit, @page), 'the user should have the right to edit the page'
assert !@user.may?(:admin, @page), 'the user should not have the right to admin the page'
assert users(:blue).may?(:admin, @page)
get :new, page_id: @page.id
assert_response :success
assert_difference 'SurveyResponse.count' do
post :make, :page_id => @page.id, 'response' => {
'answers_attributes' => {
'1' => { 'question_id' => '1', 'value' => 'a1' },
'2' => { 'question_id' => '2', 'value' => 'a2' },
'3' => { 'question_id' => '3', 'value' => 'a3' }
}
}
end
# assert_equal 1, assigns(:survey).responses_count
# ^^ i can't get the counter cache working
assert_equal 'success', flash[:type]
assert_equal @user.id, assigns('response').user_id
assert_equal %w[a1 a2 a3], assigns('response').answers.map(&:value)
# check the listing - for only admins can see this, we have to login as blue again
login_as :blue
get :list, page_id: @page.id
assert_response :success
assert_equal 1, assigns(:survey).responses(true).size
assert_active_tab 'List Responses'
response = assigns('responses').detect { |r| r.user_id == @user.id }
assert_equal %w[a1 a2 a3], response.answers.map(&:value)
end
def test_edit_response
blue_id = users(:blue).id
login_as :blue
survey = pages('survey1').data
blue_response = users(:blue).response_for_survey(survey)
page = pages('survey1')
get :show, page_id: page.id, id: blue_response.id
assert_response :success
assert_active_tab 'My Response'
# save new reponse
post :update, :page_id => pages('survey1').id, :id => blue_response.id,
'response' => {
'answers_attributes' => {
'1' => { 'question_id' => '1', 'value' => 'ba1' },
'2' => { 'question_id' => '2', 'value' => 'ba2' },
'3' => { 'question_id' => '3', 'value' => 'ba3' }
}
}
assert_equal blue_id, assigns('response').user_id
assert_equal %w[ba1 ba2 ba3], assigns('response').answers.map(&:value)
# check the listing
get :list, page_id: pages('survey1').id
assert_response :success
assert_active_tab 'List Responses'
response = assigns('responses').detect { |r| r.user_id == blue_id }
assert_equal %w[ba1 ba2 ba3], response.answers.map(&:value)
end
def test_delete_own_response
login_as :red
user = users(:red)
page = pages(:survey1)
response = user.response_for_survey(page.data)
assert !user.may?(:admin, page)
assert_difference 'SurveyResponse.count', -1 do
post :destroy, page_id: pages('survey1').id, id: response.id
end
end
def test_delete_others_response
login_as :blue
user = users(:blue)
page = pages('survey1')
response = survey_responses(:resp1)
assert user.may?(:admin, page)
assert response.user_id != user.id
assert_difference 'SurveyResponse.count', -1 do
post :destroy, page_id: page.id, id: response.id
end
end
def test_rate_answers
# first, we enable ratings
login_as :blue
page = pages(:survey1)
survey = page.data
survey.admin_may_rate = '1'
survey.save
get :rate, page_id: page.id
assert_active_tab 'Rate Responses'
assert_equal survey.responses[0].id, assigns('response').id
first_rated_response_id = assigns('response').id
# rate it
post :rate, page_id: page.id, id: first_rated_response_id, rating: '10'
assert_response :success
# rate it as different user
login_as :orange
# rate again
post :rate, page_id: page.id, id: first_rated_response_id, rating: '2'
assert_response :success
# check that the average rating is listed
get :list, page_id: page.id
assert_response :success
rated_response = assigns('responses').detect { |r| r.id == first_rated_response_id }
assert_not_nil rated_response
assert_equal 6.0, rated_response.rating
end
def test_public
page = pages('survey1')
assert page, 'the page should exist'
get :show, page_id: page.id
assert_response :success
end
def test_overwrite_rating
login_as :blue
page = pages('survey1')
survey = page.data
survey.admin_may_rate = '1'
survey.save
# do some ratings
survey = surveys('1')
get :rate, page_id: page.id
first_rated_response_id = assigns('response').id
# rate the first response
post :rate, page_id: page.id, id: first_rated_response_id, rating: '10'
assert_response :success
# check that the rating is recorder
get :list, page_id: page.id
assert_response :success
rated_response = assigns('responses').detect { |r| r.id == first_rated_response_id }
assert_not_nil rated_response
assert_equal 10.0, rated_response.rating
# re-rate everything
survey.responses.each do |response|
unless response.user_id == users(:blue).id
post :rate, page_id: page.id, id: response.id, rating: '7'
assert_response :success
end
end
# check that the rating is over written
get :list, page_id: page.id
assert_response :success
rated_response = assigns('responses').detect { |r| r.id == first_rated_response_id }
assert_not_nil rated_response
assert_equal 7.0, rated_response.rating
end
def test_private_questions
# make a question private
page = pages(:survey1)
question = page.data.questions.find_by_label('Another Question')
question.update_attribute(:private, true)
login_as :blue
get :show, page_id: page.id, id: 5
assert_select 'h2.question_label', /Another Question/, 'blue should see private question'
login_as :dolphin
get :show, page_id: page.id, id: 5
assert_raise(Test::Unit::AssertionFailedError, 'dolphin should not see private Qs') do
assert_select 'h2.question_label', /Another Question/
end
end
protected
def assert_active_tab(tab_text)
assert_select '.nav-tabs' do
assert_select 'a.active', text: tab_text
end
end
end
require 'test_helper'
class SurveyTest < ActiveSupport::TestCase
def setup
setup_assets
end
def teardown
teardown_assets
end
def test_reorder_and_append_complex_survey
survey = surveys('2')
# convert survey questions to a hash in a style
# that we get when a form is submitted
params_hash = make_params(survey.questions)
sorted_params = params_hash.collect { |id, attrs| [id, attrs] }
sorted_params.sort! { |a, b| a.first <=> b.first }
# delete few things
params_hash['11'].replace('deleted' => 'true')
params_hash['12'].replace('deleted' => 'true')
params_hash['15'].replace('deleted' => 'true')
# reorder few things
sorted_params[0], sorted_params[4], sorted_params[6] = sorted_params[4], sorted_params[6], sorted_params[0]
# add some new ones
sorted_params << ['new_' + rand.to_s, { 'private' => '0', 'type' => 'ShortTextQuestion', 'position' => 0, 'label' => 'new_q1' }]
sorted_params.unshift ['new_' + rand.to_s, { 'private' => '0', 'type' => 'ShortTextQuestion', 'position' => 0, 'label' => 'new_q2' }]
# update positions
params_hash = {}
labels = []
current_position = 1
sorted_params.each do |id, attrs|
# deleted stuff should not be given a position
unless attrs['deleted']
# store the sorted labels
labels << attrs['label']
attrs['position'] = current_position
current_position += 1
end
params_hash[id] = attrs
end
skip 'mass assignment protection prevents updating survey this way'
# We're disabling survey pages for the time being anyway.
# Also we will use strong_params to fix mass assignments -
# so no use in fixing the test now.
survey.new_questions_attributes = params_hash
survey.save!
survey.reload
# check every question
current_position = 1
survey.questions.each do |question|
assert_equal current_position, question.position, 'questions should be sorted'
assert_equal labels[current_position - 1], question.label
current_position += 1
end
end
def test_destruction
survey = Survey.create!
SurveyQuestion # loads the source file for ImageUploadQuestion
......@@ -82,19 +21,4 @@ class SurveyTest < ActiveSupport::TestCase
survey.destroy
end
end
def make_params(questions)
important_attributes = %w[private type label position]
choices_attribute = 'newline_delimited_choices'
params = {}
questions.each do |question|
question_params = {}
important_attributes.each { |attr| question_params[attr] = question.attributes[attr] }
if question.type =~ /Select/
question_params[choices_attribute] = question.attributes[choices_attribute]
end
params[question.id.to_s] = question_params
end
params
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