Commit c8450e044f3455fb548c495096ddd3f6884aa032
1 parent
c5eecbcc
Exists in
master
and in
1 other branch
Added rspec integration tests
Showing
9 changed files
with
634 additions
and
2 deletions
Show diff stats
app/controllers/questions_controller.rb
| ... | ... | @@ -117,7 +117,7 @@ class QuestionsController < InheritedResources::Base |
| 117 | 117 | |
| 118 | 118 | @question = current_user.questions.find(params[:id]) |
| 119 | 119 | |
| 120 | - puts "redis key is::::: #{redis_key}" | |
| 120 | + # puts "redis key is::::: #{redis_key}" | |
| 121 | 121 | |
| 122 | 122 | @question.send_later :export_and_delete, type, |
| 123 | 123 | :response_type => response_type, :redis_key => redis_key, :delete_at => 3.days.from_now | ... | ... |
spec/integration/choices_spec.rb
| 1 | 1 | require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') |
| 2 | 2 | |
| 3 | 3 | describe "Choices" do |
| 4 | + include IntegrationSupport | |
| 5 | + | |
| 6 | + describe "POST 'create'" do | |
| 7 | + before(:each) do | |
| 8 | + @question = Factory.create(:aoi_question, :site => @api_user) | |
| 9 | + @visitor = Factory.create(:visitor, :site => @api_user) | |
| 10 | + end | |
| 11 | + | |
| 12 | + describe "succeeds and returns a new choice" do | |
| 13 | + | |
| 14 | + specify "given no optional arguments"# do @params = nil end | |
| 15 | + | |
| 16 | + specify "given only data" do | |
| 17 | + @params = { :choice => { :data => "hey"} } | |
| 18 | + end | |
| 19 | + | |
| 20 | + specify "given only the visitor identifier" do | |
| 21 | + @params = { :choice => { :visitor_identifier => @visitor.identifier } } | |
| 22 | + end | |
| 23 | + | |
| 24 | + after do | |
| 25 | + post_auth question_choices_path(@question, :format => 'xml'), @params | |
| 26 | + response.should be_success | |
| 27 | + response.should have_tag "choice" | |
| 28 | + end | |
| 29 | + end | |
| 30 | + | |
| 31 | + it "correctly sets the supplied attributes" do | |
| 32 | + @params = { | |
| 33 | + :choice => { | |
| 34 | + :visitor_identifier => @visitor.identifier, | |
| 35 | + :data => "foo", | |
| 36 | + :local_identifier => "bar" } } | |
| 37 | + | |
| 38 | + post_auth question_choices_path(@question, :format => 'xml'), @params | |
| 39 | + | |
| 40 | + response.should be_success | |
| 41 | + response.should have_tag "choice creator-id", @visitor.id.to_s | |
| 42 | + response.should have_tag "choice data", "foo" | |
| 43 | + response.should have_tag "choice local-identifier", "bar" | |
| 44 | + end | |
| 45 | + end | |
| 46 | + | |
| 47 | + describe "PUT 'flag'" do | |
| 48 | + before do | |
| 49 | + @question = Factory.create(:aoi_question, :site => @api_user) | |
| 50 | + @choice = Factory.create(:choice, :question => @question) | |
| 51 | + @choice.activate! | |
| 52 | + end | |
| 53 | + | |
| 54 | + it "should return the deactivated choice given no arguments" do | |
| 55 | + put_auth flag_question_choice_path(@question, @choice, :format => 'xml') | |
| 56 | + | |
| 57 | + response.should be_success | |
| 58 | + response.should have_tag "choice active", "false" | |
| 59 | + end | |
| 60 | + | |
| 61 | + it "should return the deactivated choice given an explanation" do | |
| 62 | + put_auth flag_question_choice_path(@question, @choice, :format => 'xml'), :explanation => "foo" | |
| 63 | + | |
| 64 | + response.should be_success | |
| 65 | + response.should have_tag "choice active", "false" | |
| 66 | + end | |
| 67 | + | |
| 68 | + context "when trying to flag another site's choices" do | |
| 69 | + before do | |
| 70 | + # this is ugly | |
| 71 | + @orig_user = @api_user | |
| 72 | + @api_user = Factory(:email_confirmed_user) | |
| 73 | + end | |
| 74 | + | |
| 75 | + it "should fail" do | |
| 76 | + put_auth flag_question_choice_path(@question, @choice, :format => 'xml'), :explanation => "foo" | |
| 77 | + response.should_not be_success | |
| 78 | + end | |
| 79 | + | |
| 80 | + after { @api_user = @orig_user } | |
| 81 | + end | |
| 82 | + end | |
| 83 | + | |
| 84 | + describe "GET 'index'" do | |
| 85 | + before(:each) do | |
| 86 | + @question = Factory.create(:aoi_question, :site => @api_user, :choices => [], :prompts => []) | |
| 87 | + 5.times{ Factory.create(:choice, :question => @question).deactivate! } | |
| 88 | + 5.times{ Factory.create(:choice, :question => @question).activate! } | |
| 89 | + end | |
| 90 | + | |
| 91 | + it "should return all active choices given no optional parameters" do | |
| 92 | + get_auth question_choices_path(@question, :format => 'xml') | |
| 93 | + | |
| 94 | + response.should be_success | |
| 95 | + response.should have_tag "choices choice", 5 | |
| 96 | + end | |
| 97 | + | |
| 98 | + it "should return all choices if include_inactive is set" do | |
| 99 | + get_auth question_choices_path(@question, :format => 'xml'), :include_inactive => true | |
| 100 | + | |
| 101 | + response.should be_success | |
| 102 | + response.should have_tag "choices choice", 10 | |
| 103 | + response.should have_tag "choices choice active", "false" | |
| 104 | + end | |
| 105 | + | |
| 106 | + | |
| 107 | + it "should return 3 choices when limt is set to 3" do | |
| 108 | + get_auth question_choices_path(@question, :format => 'xml'), :limit => 3 | |
| 109 | + | |
| 110 | + response.should be_success | |
| 111 | + response.should have_tag "choices choice", 3 | |
| 112 | + end | |
| 113 | + | |
| 114 | + it "should return the remaining choices when offset is provided" do | |
| 115 | + get_auth question_choices_path(@question, :format => 'xml'), :offset => 2, :limit => 4 | |
| 116 | + | |
| 117 | + response.should be_success | |
| 118 | + response.should have_tag "choices choice", 3 | |
| 119 | + end | |
| 120 | + | |
| 121 | + context "when trying to access another site's choices" do | |
| 122 | + before do | |
| 123 | + @other_user = Factory(:email_confirmed_user) | |
| 124 | + @other_question = Factory.create(:aoi_question, :site => @other_user) | |
| 125 | + 5.times{ Factory.create(:choice, :question => @other_question) } | |
| 126 | + end | |
| 127 | + | |
| 128 | + it "should fail" do | |
| 129 | + pending("user scope") do | |
| 130 | + get_auth question_choices_path(@question, :format => 'xml'), :offset => 2, :limit => 4 | |
| 131 | + response.should_not be_success | |
| 132 | + end | |
| 133 | + end | |
| 134 | + end | |
| 135 | + | |
| 136 | + end | |
| 137 | + | |
| 138 | + describe "GET 'show'" do | |
| 139 | + before do | |
| 140 | + @question = Factory.create(:aoi_question, :site => @api_user) | |
| 141 | + @choice = Factory.create(:choice, :question => @question) | |
| 142 | + end | |
| 143 | + | |
| 144 | + it "should return a choice" do | |
| 145 | + get_auth question_choice_path(@question, @choice, :format => 'xml') | |
| 146 | + | |
| 147 | + response.should be_success | |
| 148 | + response.should have_tag "choice", 1 | |
| 149 | + end | |
| 150 | + | |
| 151 | + context "when requesting a choice from another site" do | |
| 152 | + before do | |
| 153 | + @other_user = Factory(:email_confirmed_user) | |
| 154 | + @other_question = Factory.create(:aoi_question, :site => @other_user) | |
| 155 | + @other_choice = Factory.create(:choice, :question => @other_question) | |
| 156 | + end | |
| 157 | + | |
| 158 | + it "should fail" do | |
| 159 | + pending("user scope") do | |
| 160 | + get_auth question_choice_path(@other_question, @other_choice, :format => 'xml') | |
| 161 | + response.should_not be_success | |
| 162 | + end | |
| 163 | + end | |
| 164 | + end | |
| 165 | + | |
| 166 | + end | |
| 167 | + | |
| 168 | + describe "PUT 'update'" do | |
| 169 | + before do | |
| 170 | + @question = Factory.create(:aoi_question, :site => @api_user) | |
| 171 | + @choice = Factory.create(:choice, :question => @question) | |
| 172 | + @choice.activate! | |
| 173 | + end | |
| 174 | + | |
| 175 | + it "should succeed given valid attributes" do | |
| 176 | + params = { :choice => { :data => "foo" } } | |
| 177 | + put_auth question_choice_path(@question, @choice, :format => 'xml'), params | |
| 178 | + response.should be_success | |
| 179 | + end | |
| 180 | + | |
| 181 | + context "when updatng another site's choice" do | |
| 182 | + before do | |
| 183 | + @orig_user = @api_user | |
| 184 | + @api_user = Factory(:email_confirmed_user) | |
| 185 | + end | |
| 186 | + | |
| 187 | + it "should fail" do | |
| 188 | + pending("user scope") do | |
| 189 | + params = { :choice => { :data => "foo" } } | |
| 190 | + put_auth question_choice_path(@question, @choice, :format => 'xml'), params | |
| 191 | + response.should_not be_success | |
| 192 | + end | |
| 193 | + end | |
| 194 | + | |
| 195 | + after { @api_user = @orig_user } | |
| 196 | + end | |
| 197 | + end | |
| 198 | + | |
| 4 | 199 | end | ... | ... |
spec/integration/prompts_spec.rb
| 1 | 1 | require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') |
| 2 | 2 | |
| 3 | +# to do: figure out future-prompts | |
| 4 | +# clean up repeated code | |
| 5 | + | |
| 3 | 6 | describe "Prompts" do |
| 7 | + include IntegrationSupport | |
| 8 | + | |
| 9 | + describe "GET 'show'" do | |
| 10 | + before do | |
| 11 | + @question = Factory.create(:aoi_question, :site => @api_user) | |
| 12 | + @prompt = @question.prompts.first | |
| 13 | + end | |
| 14 | + | |
| 15 | + it "returns a prompt object" do | |
| 16 | + get_auth question_prompt_path(@question, @prompt, :format => 'xml') | |
| 17 | + response.should be_success | |
| 18 | + response.should have_tag "prompt", 1 | |
| 19 | + end | |
| 20 | + end | |
| 21 | + | |
| 22 | + describe "POST 'skip'" do | |
| 23 | + before do | |
| 24 | + @visitor = Factory.create(:visitor, :site => @api_user, :identifier => "foo") | |
| 25 | + @question = Factory.create(:aoi_question, | |
| 26 | + :site => @api_user, | |
| 27 | + :choices => [], | |
| 28 | + :prompts => []) | |
| 29 | + 3.times{ Factory.create(:choice, :question => @question).activate! } | |
| 30 | + info = @question.reload.get_optional_information(:with_appearance => true, | |
| 31 | + :with_prompt => true, | |
| 32 | + :visitor_identifier => @visitor.identifier ) | |
| 33 | + @appearance_id = info[:appearance_id] | |
| 34 | + @prompt_id = info[:picked_prompt_id] | |
| 35 | + end | |
| 36 | + | |
| 37 | + it "should return a new skip object given no optional parameters" do | |
| 38 | + post_auth skip_question_prompt_path(@question.id, @prompt_id, :format => 'xml') | |
| 39 | + response.should be_success | |
| 40 | + response.should have_tag "skip", 1 | |
| 41 | + end | |
| 42 | + | |
| 43 | + it "should correctly set the optional attributes of the skip object" do | |
| 44 | + pending("shouldn\'t this set appearance_id?") do | |
| 45 | + params = { | |
| 46 | + :skip => { | |
| 47 | + :visitor_identifier => @visitor.identifier, | |
| 48 | + :skip_reason => "bar", | |
| 49 | + :appearance_lookup => @appearance_id, | |
| 50 | + :time_viewed => 47 } } | |
| 51 | + post_auth skip_question_prompt_path(@question, @prompt_id, :format => 'xml'), params | |
| 52 | + response.should be_success | |
| 53 | + response.should have_tag "skip", 1 | |
| 54 | + response.should have_tag "skip appearance-id", @appearance_id.to_s | |
| 55 | + response.should have_tag "skip skip-reason", "bar" | |
| 56 | + response.should have_tag "skip time-viewed", "47" | |
| 57 | + response.should have_tag "skip skipper-id", @visitor.id.to_s | |
| 58 | + end | |
| 59 | + end | |
| 60 | + | |
| 61 | + it "should return a prompt object if next_prompt is set" do | |
| 62 | + params = { | |
| 63 | + :next_prompt => { | |
| 64 | + :visitor_identifier => @visitor.identifier, | |
| 65 | + :with_appearance => true, | |
| 66 | + :algorithm => "catchup", | |
| 67 | + :with_visitor_stats => true } } | |
| 68 | + post_auth skip_question_prompt_path(@question, @prompt_id, :format => 'xml'), params | |
| 69 | + response.should be_success | |
| 70 | + response.should have_tag "prompt", 1 | |
| 71 | + response.should have_tag "prompt appearance_id", /.+/ | |
| 72 | + response.should have_tag "prompt visitor_votes", /\d+/ | |
| 73 | + response.should have_tag "prompt visitor_ideas", /\d+/ | |
| 74 | + end | |
| 75 | + | |
| 76 | + context "when trying to skip another site's questions" do | |
| 77 | + before do | |
| 78 | + @orig_user = @api_user | |
| 79 | + @api_user = Factory(:email_confirmed_user) | |
| 80 | + end | |
| 81 | + | |
| 82 | + it "should fail" do | |
| 83 | + post_auth skip_question_prompt_path(@question.id, @prompt_id, :format => 'xml') | |
| 84 | + response.should_not be_success | |
| 85 | + end | |
| 86 | + | |
| 87 | + after { @api_user = @orig_user } | |
| 88 | + end | |
| 89 | + end | |
| 90 | + | |
| 91 | + describe "POST 'vote'" do | |
| 92 | + before do | |
| 93 | + # dry this up | |
| 94 | + @visitor = Factory.create(:visitor, :site => @api_user, :identifier => "foo") | |
| 95 | + @question = Factory.create(:aoi_question, | |
| 96 | + :site => @api_user, | |
| 97 | + :choices => [], | |
| 98 | + :prompts => []) | |
| 99 | + 3.times{ Factory.create(:choice, :question => @question).activate! } | |
| 100 | + info = @question.reload.get_optional_information(:with_appearance => true, | |
| 101 | + :with_prompt => true, | |
| 102 | + :visitor_identifier => @visitor.identifier ) | |
| 103 | + @appearance_id = info[:appearance_id] | |
| 104 | + @prompt_id = info[:picked_prompt_id] | |
| 105 | + end | |
| 106 | + | |
| 107 | + it "should fail without the required 'direction' parameter" do | |
| 108 | + post_auth vote_question_prompt_path(@question.id, @prompt_id, :format => 'xml') | |
| 109 | + response.should_not be_success | |
| 110 | + end | |
| 111 | + | |
| 112 | + it "should return a new vote object given no optional parameters" do | |
| 113 | + params = { :vote => { :direction => "left" } } | |
| 114 | + post_auth vote_question_prompt_path(@question.id, @prompt_id, :format => 'xml'), params | |
| 115 | + response.should be_success | |
| 116 | + response.should have_tag "vote", 1 | |
| 117 | + end | |
| 118 | + | |
| 119 | + it "should correctly set the optional attributes of the vote object" do | |
| 120 | + pending("also has nil appearance id") do | |
| 121 | + params = { | |
| 122 | + :vote => { | |
| 123 | + :visitor_identifier => @visitor.identifier, | |
| 124 | + :direction => "right", | |
| 125 | + :appearance_lookup => @appearance_id, | |
| 126 | + :time_viewed => 47 } } | |
| 127 | + post_auth vote_question_prompt_path(@question, @prompt_id, :format => 'xml'), params | |
| 128 | + response.should be_success | |
| 129 | + response.should have_tag "vote", 1 | |
| 130 | + response.should have_tag "vote appearance-id", @appearance_id.to_s | |
| 131 | + response.should have_tag "vote time-viewed", "47" | |
| 132 | + response.should have_tag "vote voter-id", @visitor.id.to_s | |
| 133 | + end | |
| 134 | + end | |
| 135 | + | |
| 136 | + # copy-paste from vote --> shared behavior? | |
| 137 | + it "should return a prompt object if next_prompt is set" do | |
| 138 | + params = { | |
| 139 | + :vote => { | |
| 140 | + :direction => "left" }, | |
| 141 | + :next_prompt => { | |
| 142 | + :visitor_identifier => @visitor.identifier, | |
| 143 | + :with_appearance => true, | |
| 144 | + :algorithm => "catchup", | |
| 145 | + :with_visitor_stats => true } } | |
| 146 | + post_auth vote_question_prompt_path(@question, @prompt_id, :format => 'xml'), params | |
| 147 | + response.should be_success | |
| 148 | + response.should have_tag "prompt", 1 | |
| 149 | + response.should have_tag "prompt appearance_id", /.+/ | |
| 150 | + response.should have_tag "prompt visitor_votes", /\d+/ | |
| 151 | + response.should have_tag "prompt visitor_ideas", /\d+/ | |
| 152 | + end | |
| 153 | + | |
| 154 | + context "when trying to vote on another site's questions" do | |
| 155 | + before do | |
| 156 | + @orig_user = @api_user | |
| 157 | + @api_user = Factory(:email_confirmed_user) | |
| 158 | + end | |
| 159 | + | |
| 160 | + it "should fail" do | |
| 161 | + params = { :vote => { :direction => "left" } } | |
| 162 | + post_auth vote_question_prompt_path(@question.id, @prompt_id, :format => 'xml'), params | |
| 163 | + response.should_not be_success | |
| 164 | + end | |
| 165 | + | |
| 166 | + after { @api_user = @orig_user } | |
| 167 | + end | |
| 168 | + | |
| 169 | + end | |
| 4 | 170 | end | ... | ... |
spec/integration/questions_spec.rb
| 1 | 1 | require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') |
| 2 | 2 | |
| 3 | 3 | describe "Questions" do |
| 4 | + include IntegrationSupport | |
| 5 | + before do | |
| 6 | + 3.times{ Factory.create(:aoi_question, :site => @api_user) } | |
| 7 | + end | |
| 8 | + | |
| 9 | + describe "GET 'index'" do | |
| 10 | + it "should return an array of questions" do | |
| 11 | + get_auth questions_path(:format => 'xml') | |
| 12 | + response.body.should have_tag("questions question", 3) | |
| 13 | + response.should be_success | |
| 14 | + end | |
| 15 | + | |
| 16 | + it "should not return the questions of other api users" do | |
| 17 | + pending ("doesn't scope to the level of the user") do | |
| 18 | + other_user = Factory(:email_confirmed_user) | |
| 19 | + Factory.create(:aoi_question, :site => other_user) | |
| 20 | + get_auth questions_path | |
| 21 | + response.should be_success | |
| 22 | + response.body.should_not have_tag("question") | |
| 23 | + end | |
| 24 | + end | |
| 25 | + end | |
| 26 | + | |
| 27 | + describe "GET 'new'" do | |
| 28 | + it "should return an empty question object" do | |
| 29 | + get_auth new_question_path(:format => 'xml') | |
| 30 | + response.should be_success | |
| 31 | + response.body.should have_tag "question", 1 | |
| 32 | + response.body.should have_tag "question name", "" | |
| 33 | + response.body.should have_tag "question creator-id", "" | |
| 34 | + response.body.should have_tag "question created-at", "" | |
| 35 | + end | |
| 36 | + end | |
| 37 | + | |
| 38 | + describe "POST 'create'" do | |
| 39 | + before { @visitor = Factory.create(:visitor, :site => @api_user) } | |
| 40 | + | |
| 41 | + it "should fail when required parameters are omitted" do | |
| 42 | + post_auth questions_path(:format => 'xml') | |
| 43 | + response.should_not be_success | |
| 44 | + end | |
| 45 | + | |
| 46 | + it "should return a question object given no optional parameters" do | |
| 47 | + pending("choice count doesn't reflect # seed ideas") do | |
| 48 | + params = { :question => { :visitor_identifier => @visitor.identifier, :ideas => "foo\r\nbar\r\nbaz" } } | |
| 49 | + | |
| 50 | + post_auth questions_path(:format => 'xml'), params | |
| 51 | + | |
| 52 | + response.should be_success | |
| 53 | + response.should have_tag "question", 1 | |
| 54 | + response.should have_tag "question creator-id", @visitor.id.to_s | |
| 55 | + response.should have_tag "question choices-count", 3 | |
| 56 | + end | |
| 57 | + end | |
| 58 | + | |
| 59 | + it "should correctly set optional attributes" do | |
| 60 | + params = { | |
| 61 | + :question => { | |
| 62 | + :visitor_identifier => @visitor.identifier, | |
| 63 | + :ideas => "foo\r\nbar\r\nbaz", | |
| 64 | + :name => "foo", | |
| 65 | + :local_identifier => "bar", | |
| 66 | + :information => "baz" } } | |
| 67 | + | |
| 68 | + post_auth questions_path(:format => 'xml'), params | |
| 69 | + response.should be_success | |
| 70 | + response.should have_tag "question", 1 | |
| 71 | + response.should have_tag "question creator-id", @visitor.id.to_s | |
| 72 | + # response.should have_tag "question choices-count", 3 | |
| 73 | + response.should have_tag "question name", "foo" | |
| 74 | + response.should have_tag "question local-identifier", "bar" | |
| 75 | + response.should have_tag "question information", "baz" | |
| 76 | + end | |
| 77 | + end | |
| 78 | + | |
| 79 | + describe "POST 'export'" do | |
| 80 | + before { @question = Factory.create(:aoi_question, :site => @api_user) } | |
| 81 | + | |
| 82 | + it "should fail without any of the required parameters" do | |
| 83 | + post_auth export_question_path(@question, :format => 'xml') | |
| 84 | + response.should be_success | |
| 85 | + response.body.should =~ /Error/ | |
| 86 | + end | |
| 87 | + | |
| 88 | + it "should fail given invalid parameters" do | |
| 89 | + params = { :type => "ideas", :response_type => "foo", :redisk_key => "bar" } | |
| 90 | + post_auth export_question_path(@question, :format => 'xml') | |
| 91 | + response.should be_success | |
| 92 | + response.body.should =~ /Error/ | |
| 93 | + end | |
| 94 | + | |
| 95 | + it "should succeed given valid parameters" do | |
| 96 | + params = { :type => "ideas", :response_type => "redis", :redis_key => "foo" } | |
| 97 | + post_auth export_question_path(@question, :format => 'xml'), params | |
| 98 | + response.should be_success | |
| 99 | + response.body.should =~ /Ok!/ | |
| 100 | + end | |
| 101 | + end | |
| 102 | + | |
| 103 | + describe "GET 'show'" do | |
| 104 | + before { @question = Factory.create(:aoi_question, :site => @api_user) } | |
| 105 | + | |
| 106 | + it "should succeed given no optional parameters" do | |
| 107 | + get_auth question_path(@question, :format => 'xml') | |
| 108 | + response.should be_success | |
| 109 | + response.should have_tag "question", 1 | |
| 110 | + response.should have_tag "question id", @question.id.to_s | |
| 111 | + end | |
| 112 | + | |
| 113 | + it "should correctly set optional parameters" do | |
| 114 | + @visitor = Factory.create(:visitor, :site => @api_user) | |
| 115 | + params = { | |
| 116 | + :visitor_identifier => @visitor.identifier, | |
| 117 | + :with_prompt => true, | |
| 118 | + :with_appearance => true, | |
| 119 | + :with_visitor_stats => true } | |
| 120 | + get_auth question_path(@question, :format => 'xml'), params | |
| 121 | + response.should be_success | |
| 122 | + response.should have_tag "question", 1 | |
| 123 | + response.should have_tag "question id", @question.id.to_s | |
| 124 | + response.should have_tag "question picked_prompt_id" | |
| 125 | + response.should have_tag "question appearance_id" | |
| 126 | + response.should have_tag "question visitor_votes" | |
| 127 | + response.should have_tag "question visitor_ideas" | |
| 128 | + end | |
| 129 | + | |
| 130 | + it "should fail if 'with_prompt' is set but 'visitor_identifier' not provided" do | |
| 131 | + pending("figure out argument dependencies") do | |
| 132 | + params = { :with_prompt => true } | |
| 133 | + get_auth question_path(@question, :format => 'xml'), params | |
| 134 | + response.should_not be_success | |
| 135 | + end | |
| 136 | + end | |
| 137 | + | |
| 138 | + context "GET 'show' trying to view others sites' questions" | |
| 139 | + before do | |
| 140 | + @orig_user = @api_user | |
| 141 | + @api_user = Factory(:email_confirmed_user) | |
| 142 | + end | |
| 143 | + | |
| 144 | + it "should fail" do | |
| 145 | + pending("user scope") do | |
| 146 | + get_auth question_path(@question, :format => 'xml') | |
| 147 | + response.should_not be_success | |
| 148 | + end | |
| 149 | + end | |
| 150 | + | |
| 151 | + after { @api_user = @orig_user } | |
| 152 | + end | |
| 153 | + | |
| 154 | + describe "PUT 'update'" do | |
| 155 | + before { @question = Factory.create(:aoi_question, :site => @api_user) } | |
| 156 | + | |
| 157 | + it "should succeed give valid attributes" do | |
| 158 | + params = { | |
| 159 | + :question => { | |
| 160 | + :active => false, | |
| 161 | + :information => "foo", | |
| 162 | + :name => "bar", | |
| 163 | + :local_identifier => "baz" } } | |
| 164 | + put_auth question_path(@question, :format => 'xml'), params | |
| 165 | + response.should be_success | |
| 166 | + end | |
| 167 | + | |
| 168 | + it "should not be able to change the site id" do | |
| 169 | + pending("needs attr_protected") do | |
| 170 | + original_site_id = @question.site_id | |
| 171 | + params = { :question => { :site_id => -1 } } | |
| 172 | + put_auth question_path(@question, :format => 'xml'), params | |
| 173 | + @question.reload.site_id.should == original_site_id | |
| 174 | + end | |
| 175 | + end | |
| 176 | + | |
| 177 | + it "should ignore protected attributes" do | |
| 178 | + params = { :question => { :votes_count => 999 } } | |
| 179 | + put_auth question_path(@question, :format => 'xml'), params | |
| 180 | + response.should be_success | |
| 181 | + @question.reload.site_id.should_not == 999 | |
| 182 | + end | |
| 183 | + | |
| 184 | + context "when updatng another site's question" do | |
| 185 | + before do | |
| 186 | + @orig_user = @api_user | |
| 187 | + @api_user = Factory(:email_confirmed_user) | |
| 188 | + end | |
| 189 | + | |
| 190 | + it "should fail" do | |
| 191 | + pending("user scope") do | |
| 192 | + params = { :question => { :name => "foo" } } | |
| 193 | + put_auth question_path(@question, :format => 'xml'), params | |
| 194 | + response.should_not be_success | |
| 195 | + end | |
| 196 | + end | |
| 197 | + | |
| 198 | + after { @api_user = @orig_user } | |
| 199 | + end | |
| 200 | + end | |
| 201 | + | |
| 202 | + describe "GET 'all_object_info_totals_by_date'" do | |
| 203 | + end | |
| 204 | + | |
| 205 | + describe "GET 'object_info_totals_by_date'" do | |
| 206 | + end | |
| 207 | + | |
| 4 | 208 | end | ... | ... |
spec/integration/visitors_spec.rb
spec/models/question_spec.rb
| 1 | 1 | require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') |
| 2 | 2 | |
| 3 | 3 | describe Question do |
| 4 | - | |
| 4 | + include DBSupport | |
| 5 | + | |
| 5 | 6 | it {should belong_to :creator} |
| 6 | 7 | it {should belong_to :site} |
| 7 | 8 | it {should have_many :choices} |
| ... | ... | @@ -292,6 +293,7 @@ describe Question do |
| 292 | 293 | prompt.left_choice.deactivate! |
| 293 | 294 | @catchup_q.choose_prompt.should_not == prompt1 |
| 294 | 295 | end |
| 296 | + after(:all) { truncate_all } | |
| 295 | 297 | end |
| 296 | 298 | |
| 297 | 299 | context "exporting data" do |
| ... | ... | @@ -416,6 +418,7 @@ describe Question do |
| 416 | 418 | |
| 417 | 419 | end |
| 418 | 420 | |
| 421 | + after(:all) { truncate_all } | |
| 419 | 422 | end |
| 420 | 423 | |
| 421 | 424 | end | ... | ... |
| ... | ... | @@ -0,0 +1,10 @@ |
| 1 | +module DBSupport | |
| 2 | + | |
| 3 | + # this is useful after a before :all block fills the db with stuff | |
| 4 | + def truncate_all | |
| 5 | + (ActiveRecord::Base.connection.tables - ["schema_migrations"]).each do |table| | |
| 6 | + ActiveRecord::Base.connection.execute("TRUNCATE TABLE #{table};") | |
| 7 | + end | |
| 8 | + end | |
| 9 | + | |
| 10 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,42 @@ |
| 1 | +module IntegrationSupport | |
| 2 | + | |
| 3 | + # todo: make automatically included in integration tests | |
| 4 | + Spec::Runner.configure do |config| | |
| 5 | + config.before(:each, :type => :integration) do | |
| 6 | + @api_user = Factory(:email_confirmed_user) | |
| 7 | + end | |
| 8 | + end | |
| 9 | + | |
| 10 | + def get_auth(path, parameters = {}, headers = {}) | |
| 11 | + auth_wrap(:get, path, parameters, headers) | |
| 12 | + end | |
| 13 | + | |
| 14 | + def put_auth(path, parameters = {}, headers = {}) | |
| 15 | + auth_wrap(:put, path, parameters, headers) | |
| 16 | + end | |
| 17 | + | |
| 18 | + def post_auth(path, parameters = {}, headers = {} ) | |
| 19 | + auth_wrap(:post, path, parameters, headers) | |
| 20 | + end | |
| 21 | + | |
| 22 | + def delete_auth(path, parameters = {}, headers = {}) | |
| 23 | + auth_wrap(:delete, path, parameters, headers) | |
| 24 | + end | |
| 25 | + | |
| 26 | + def head_auth(path, parameters = {}, headers = {}) | |
| 27 | + auth_wrap(:head, path, parameters, headers) | |
| 28 | + end | |
| 29 | + | |
| 30 | + private | |
| 31 | + def auth_wrap(method, path, parameters, headers) | |
| 32 | + return nil unless [:get, :put, :post, :delete, :head].include? method | |
| 33 | + | |
| 34 | + auth = ActionController::HttpAuthentication::Basic.encode_credentials(@api_user.email, @api_user.password) | |
| 35 | + headers.merge!(:authorization => auth) | |
| 36 | + # headers.merge!(:content_type => "application/xml", :authorization => auth) | |
| 37 | + # parameters.merge!(:format => 'xml') | |
| 38 | + | |
| 39 | + send(method, path, parameters, headers) | |
| 40 | + end | |
| 41 | +end | |
| 42 | + | ... | ... |