limesurvey_rails 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +23 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +42 -0
  7. data/app/assets/images/limesurvey_rails/.gitkeep +0 -0
  8. data/app/assets/javascripts/limesurvey_rails/application.js +15 -0
  9. data/app/assets/javascripts/limesurvey_rails/surveys.js +2 -0
  10. data/app/assets/stylesheets/limesurvey_rails/application.css +13 -0
  11. data/app/assets/stylesheets/limesurvey_rails/surveys.css +4 -0
  12. data/app/assets/stylesheets/scaffold.css +56 -0
  13. data/app/controllers/limesurvey_rails/application_controller.rb +3 -0
  14. data/app/controllers/limesurvey_rails/surveys_controller.rb +51 -0
  15. data/app/helpers/limesurvey_rails/application_helper.rb +4 -0
  16. data/app/helpers/limesurvey_rails/surveys_helper.rb +4 -0
  17. data/app/models/limesurvey_rails/no_persistence_active_model.rb +33 -0
  18. data/app/models/limesurvey_rails/survey.rb +151 -0
  19. data/app/models/limesurvey_rails/survey_participation.rb +78 -0
  20. data/app/views/limesurvey_rails/surveys/_form.html.haml +16 -0
  21. data/app/views/limesurvey_rails/surveys/edit.html.haml +7 -0
  22. data/app/views/limesurvey_rails/surveys/index.html.haml +21 -0
  23. data/app/views/limesurvey_rails/surveys/new.html.haml +5 -0
  24. data/app/views/limesurvey_rails/surveys/show.html.haml +12 -0
  25. data/config/locales/en.yml +2 -0
  26. data/config/locales/it.yml +15 -0
  27. data/config/routes.rb +7 -0
  28. data/db/migrate/20140130110450_create_limesurvey_rails_survey_participations.rb +11 -0
  29. data/lib/limesurvey_rails.rb +10 -0
  30. data/lib/limesurvey_rails/configuration.rb +31 -0
  31. data/lib/limesurvey_rails/engine.rb +21 -0
  32. data/lib/limesurvey_rails/exceptions.rb +7 -0
  33. data/lib/limesurvey_rails/limesurvey_rails.rb +86 -0
  34. data/lib/limesurvey_rails/participant.rb +86 -0
  35. data/lib/limesurvey_rails/version.rb +3 -0
  36. data/limesurvey_rails.gemspec +30 -0
  37. data/script/rails +8 -0
  38. data/spec/dummy/README.rdoc +261 -0
  39. data/spec/dummy/Rakefile +7 -0
  40. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  41. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  42. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  43. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  44. data/spec/dummy/app/mailers/.gitkeep +0 -0
  45. data/spec/dummy/app/models/.gitkeep +0 -0
  46. data/spec/dummy/app/models/test_model.rb +7 -0
  47. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  48. data/spec/dummy/config.ru +4 -0
  49. data/spec/dummy/config/application.rb +65 -0
  50. data/spec/dummy/config/boot.rb +10 -0
  51. data/spec/dummy/config/database.yml +25 -0
  52. data/spec/dummy/config/environment.rb +7 -0
  53. data/spec/dummy/config/environments/development.rb +37 -0
  54. data/spec/dummy/config/environments/production.rb +67 -0
  55. data/spec/dummy/config/environments/test.rb +38 -0
  56. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  57. data/spec/dummy/config/initializers/inflections.rb +15 -0
  58. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  59. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  60. data/spec/dummy/config/initializers/session_store.rb +8 -0
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  62. data/spec/dummy/config/locales/en.yml +5 -0
  63. data/spec/dummy/config/routes.rb +4 -0
  64. data/spec/dummy/db/development.sqlite3 +0 -0
  65. data/spec/dummy/db/migrate/20140130103310_create_test_models.rb +12 -0
  66. data/spec/dummy/db/schema.rb +34 -0
  67. data/spec/dummy/lib/assets/.gitkeep +0 -0
  68. data/spec/dummy/log/.gitkeep +0 -0
  69. data/spec/dummy/public/404.html +26 -0
  70. data/spec/dummy/public/422.html +26 -0
  71. data/spec/dummy/public/500.html +25 -0
  72. data/spec/dummy/public/favicon.ico +0 -0
  73. data/spec/dummy/script/rails +6 -0
  74. data/spec/dummy/spec/factories/test_models.rb +10 -0
  75. data/spec/factories/limesurvey_rails_survey_participations.rb +9 -0
  76. data/spec/factories/limesurvey_rails_surveys.rb +8 -0
  77. data/spec/limesurvey_rails_spec.rb +97 -0
  78. data/spec/models/limesurvey_rails/participant_spec.rb +174 -0
  79. data/spec/models/limesurvey_rails/survey_participations_spec.rb +113 -0
  80. data/spec/models/limesurvey_rails/survey_spec.rb +222 -0
  81. data/spec/spec_helper.rb +77 -0
  82. data/spec/support/shared_examples.rb +56 -0
  83. data/spec/support/test_limesurvey_constants.rb +26 -0
  84. data/spec/support/utils.rb +47 -0
  85. metadata +286 -0
@@ -0,0 +1,8 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :limesurvey_rails_survey, :class => 'Survey' do
5
+ title "MyString"
6
+ lang "MyString"
7
+ end
8
+ end
@@ -0,0 +1,97 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe LimesurveyRails, :main => true do
4
+ after(:each) do
5
+ LimesurveyRails.reset
6
+ end
7
+
8
+ context "when it's not fully configured" do
9
+ before(:each) { LimesurveyRails.reset }
10
+ its(:configured?) { is_expected.to be false }
11
+ its(:connected?) { is_expected.to be false }
12
+ end
13
+
14
+ context "when it's configured with a wrong password" do
15
+ before(:each) do
16
+ LimesurveyRails.configure do |config|
17
+ config.api_url = LIMESURVEY_API_URL
18
+ config.username = LIMESURVEY_USERNAME
19
+ config.password = 'wrong_password'
20
+ end
21
+ end
22
+ describe ".connect" do
23
+ it "return false" do
24
+ expect(LimesurveyRails.connect).to be false
25
+ end
26
+ end
27
+ end
28
+
29
+ context "when it's fully configured" do
30
+ before(:each) do
31
+ LimesurveyRails.configure do |config|
32
+ config.api_url = LIMESURVEY_API_URL
33
+ config.username = LIMESURVEY_USERNAME
34
+ config.password = LIMESURVEY_PASSWORD
35
+ end
36
+ end
37
+ its(:configured?) {is_expected.to be true }
38
+ describe 'LimesurveyRails.configuration' do
39
+ subject { LimesurveyRails.configuration }
40
+ its(:username) {should eq('rails_user_test') }
41
+ its(:password) {should eq('limoncello') }
42
+ its(:base_url) {should eq(LIMESURVEY_BASE_URL) }
43
+ end
44
+ its(:connected?) { is_expected.to be false }
45
+ describe ".connect" do
46
+ it "return true" do
47
+ expect(LimesurveyRails.connect).to be true
48
+ end
49
+ end
50
+ describe ".disconnect" do
51
+ it "return true" do
52
+ expect(LimesurveyRails.disconnect).to be false
53
+ end
54
+ end
55
+ context "when it's connected" do
56
+ before(:each) do
57
+ LimesurveyRails.connect
58
+ end
59
+ describe ".connect" do
60
+ it "return false" do
61
+ expect(LimesurveyRails.connect).to be false
62
+ end
63
+ end
64
+ describe ".connect(true)" do
65
+ it "return true" do
66
+ expect(LimesurveyRails.connect(true)).to be true
67
+ end
68
+ end
69
+ its(:connected?) { is_expected.to be true }
70
+ describe ".disconnect" do
71
+ it "return true" do
72
+ expect(LimesurveyRails.disconnect).to be true
73
+ end
74
+ end
75
+ describe ".list_surveys" do
76
+ it "return an array" do
77
+ expect(LimesurveyRails.list_surveys).to be_an_instance_of Array
78
+ end
79
+ end
80
+ describe ".wrong_name_method" do
81
+ specify { expect { LimesurveyRails.wrong_name_method }.to raise_error }
82
+ end
83
+ end
84
+ context "when it's configurated with continuous connection" do
85
+ before(:each) do
86
+ LimesurveyRails.configure do |config|
87
+ config.auto_connection = true
88
+ end
89
+ end
90
+ its(:connected?) { is_expected.to be true }
91
+ end
92
+ end
93
+
94
+
95
+
96
+
97
+ end
@@ -0,0 +1,174 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ module LimesurveyRails
4
+ describe TestModel, :participant do
5
+
6
+ before(:all) do
7
+ configure_and_connect
8
+ remove_all_test_surveys
9
+ reset_models
10
+ @test_survey_id = get_brand_new_test_survey_id(:activate_tokens => true)
11
+ end
12
+ after(:all) { remove_all_test_surveys }
13
+
14
+ subject { TestModel }
15
+
16
+ let(:a_participant) { FactoryGirl.create(:test_model) }
17
+ let(:test_survey) { Survey.find(@test_survey_id) }
18
+
19
+ context "when not initialized for being a participant" do
20
+ before(:each) { reset_models }
21
+ its(:is_a_limesurvey_participant_class?) { is_expected.to be false }
22
+ end
23
+
24
+ describe ".is_a_limesurvey_participant" do
25
+ before(:each) { reset_models }
26
+ context "with no options" do
27
+ before(:each) do
28
+ TestModel.is_a_limesurvey_participant
29
+ end
30
+ it "has many survey_participations" do
31
+ expect(subject.reflect_on_association(:survey_participations).macro).to be :has_many
32
+ end
33
+ its(:is_a_limesurvey_participant_class?) { is_expected.to be true }
34
+ its(:limesurvey_participant_attribute_1_attr) { is_expected.to eq 'id' }
35
+ its(:limesurvey_participant_email_attr) { is_expected.to be_nil }
36
+ its(:limesurvey_participant_firstname_attr) { is_expected.to be_nil }
37
+ its(:limesurvey_participant_lastname_attr) { is_expected.to be_nil }
38
+ end
39
+ context "with options :attribute_1_attr => 'other_id' " do
40
+ before(:each) do
41
+ TestModel.is_a_limesurvey_participant(:attribute_1_attr => 'other_id')
42
+ end
43
+ its(:is_a_limesurvey_participant_class?) { is_expected.to be true }
44
+ its(:limesurvey_participant_attribute_1_attr) { is_expected.to eq 'other_id' }
45
+ end
46
+ context "with options :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname' " do
47
+ before(:each) do
48
+ TestModel.is_a_limesurvey_participant(:email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname')
49
+ end
50
+ its(:limesurvey_participant_email_attr) { is_expected.to eq 'email_address' }
51
+ its(:limesurvey_participant_firstname_attr) { is_expected.to eq 'name' }
52
+ its(:limesurvey_participant_lastname_attr) { is_expected.to eq 'surname' }
53
+ end
54
+ end
55
+
56
+ context "when participant model is initialized with options: :attribute_1_attr => 'extra_id', :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname'" do
57
+
58
+ before(:all) { TestModel.is_a_limesurvey_participant(:attribute_1_attr => "extra_id", :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname') }
59
+
60
+ let(:a_participant) { FactoryGirl.create(:test_model) }
61
+
62
+ describe "#add_to_survey(arg)" do
63
+ context "when arg is an existing survey object" do
64
+ it "returns true" do
65
+ expect(a_participant.add_to_survey(test_survey)).to be true
66
+ end
67
+ context "and #{described_class} instance is already registered" do
68
+ before(:each) do
69
+ a_participant.add_to_survey(test_survey)
70
+ end
71
+ it "returns false" do
72
+ expect(a_participant.add_to_survey(test_survey)).to be false
73
+ end
74
+ end
75
+ end
76
+ context "when arg is something wrong" do
77
+ it "raise WrongArgumentError" do
78
+ expect { a_participant.add_to_survey(Object.new) }.to raise_error(WrongArgumentError)
79
+ end
80
+ end
81
+ end
82
+ describe "#remove_from_survey(arg)" do
83
+ context "when arg is an existing survey object" do
84
+ context "when #{described_class} instance is already registered" do
85
+ before(:each) do
86
+ a_participant.add_to_survey(test_survey)
87
+ end
88
+ it "returns true" do
89
+ expect(a_participant.remove_from_survey(test_survey)).to be true
90
+ end
91
+ end
92
+ context "when #{described_class} instance is not registered" do
93
+ it "returns false" do
94
+ expect(a_participant.remove_from_survey(test_survey)).to be false
95
+ end
96
+ end
97
+ end
98
+ context "when arg is something wrong" do
99
+ it "raise WrongArgumentError" do
100
+ expect { a_participant.remove_from_survey(Object.new) }.to raise_error(WrongArgumentError)
101
+ end
102
+ end
103
+ end
104
+ describe "#surveys" do
105
+ before(:all) do
106
+ @another_test_survey_id = get_brand_new_test_survey_id(:activate_tokens => true)
107
+ @another_more_test_survey_id = get_brand_new_test_survey_id(:activate_tokens => true)
108
+ end
109
+ let(:another_test_survey) { Survey.find(@another_test_survey_id) }
110
+ let(:another_more_test_survey) { Survey.find(@another_more_test_survey_id) }
111
+ it "returns an empty array" do
112
+ expect(a_participant.surveys).to be_empty
113
+ end
114
+ context "when has added to a survey" do
115
+ before(:each) { SurveyParticipation.create(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
116
+ it "returns an array with one element" do
117
+ expect(a_participant.surveys.size).to eq 1
118
+ end
119
+ it "and that element containing a Survey object" do
120
+ expect(a_participant.surveys.first).to be_kind_of(Survey)
121
+ end
122
+ context "when has added to another survey" do
123
+ before(:each) { SurveyParticipation.create(:survey_id => another_test_survey.id, :participant_id => a_participant.id ) }
124
+ it "returns an array with two elements" do
125
+ expect(a_participant.surveys.size).to eq 2
126
+ end
127
+ end
128
+ describe "#available_surveys" do
129
+ it "returns an array with two elements" do
130
+ expect(a_participant.available_surveys.size).to eq 2
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+ describe "#destroy" do
138
+ before(:each) do
139
+ reset_models
140
+ TestModel.is_a_limesurvey_participant(opts)
141
+ test_survey.add_participant!(a_participant)
142
+ end
143
+
144
+ context "when participant model is initialized with no options" do
145
+ let(:opts){{}}
146
+ it "limesurvey token doesn't get destroyed" do
147
+ tid = a_participant.survey_participations.for_survey(test_survey.id).token_id
148
+ a_participant.destroy
149
+ expect(LimesurveyRails.list_participants(test_survey.id).map { |p| p["tid"] }).to include(tid)
150
+ end
151
+ it "participation remain but with no reference to the destroyed participant" do
152
+ tid = a_participant.survey_participations.for_survey(test_survey.id).token_id
153
+ a_participant.destroy
154
+ expect(SurveyParticipation.find_by_token_id(tid)).to be_present
155
+ end
156
+ end
157
+ context "when participant model is initialized with option: :dependent_participations => :destroy" do
158
+ let(:opts){{:dependent_participations => :destroy}}
159
+ it "also destroy token on limesurvey" do
160
+ tid = a_participant.survey_participations.for_survey(test_survey.id).token_id
161
+ a_participant.destroy
162
+ expect(LimesurveyRails.list_participants(test_survey.id).map { |p| p["tid"] }).to_not include(tid)
163
+ end
164
+ end
165
+ context "when participant model is initialized with option: :dependent_participations => :restrict" do
166
+ let(:opts){{:dependent_participations => :restrict}}
167
+ specify { expect { a_participant.destroy }.to raise_error(ActiveRecord::DeleteRestrictionError) }
168
+ end
169
+ end
170
+
171
+
172
+ end
173
+ end
174
+
@@ -0,0 +1,113 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ module LimesurveyRails
4
+ describe SurveyParticipation, :participation => true do
5
+
6
+ subject { SurveyParticipation }
7
+
8
+ before(:all) do
9
+ configure_and_connect
10
+ @test_survey_id = get_brand_new_test_survey_id(:activate_tokens => true)
11
+ reset_models
12
+ TestModel.is_a_limesurvey_participant :attribute_1_attr => "extra_id", :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname'
13
+ end
14
+ after(:all) { remove_all_test_surveys }
15
+
16
+ let(:a_participant) { FactoryGirl.create(:test_model) }
17
+ let(:test_survey) { Survey.find(@test_survey_id) }
18
+
19
+ describe ".new" do
20
+ describe "the new SurveyParticipation object" do
21
+ context "when another record with the same survey_id and participant_id exist" do
22
+ before(:each) { SurveyParticipation.create(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
23
+ subject { SurveyParticipation.new(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
24
+ specify do
25
+ subject.valid?
26
+ expect(subject.errors[:participant_id].size).to eq 1
27
+ end
28
+ end
29
+ [:participant_id,:survey_id].each do |a|
30
+ context "when trying to create a new instance without #{a}" do
31
+ subject { SurveyParticipation.new }
32
+ specify do
33
+ subject.valid?
34
+ expect(subject.errors[a].size).to eq 1
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ describe "callbacks beahvior" do
43
+ # before(:each) { LimesurveyRails.connect }
44
+ let!(:a_participant) { FactoryGirl.create(:test_model) }
45
+ let!(:test_survey) { Survey.find(@test_survey_id) }
46
+ describe "#create!" do
47
+ describe "returned value" do
48
+ subject(:ss) { SurveyParticipation.create!(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
49
+ its(:token){ is_expected.to be_kind_of String }
50
+ its(:token_id){ is_expected.to be_kind_of String }
51
+ its(:url){ is_expected.to eq(LIMESURVEY_BASE_URL + "/survey/index/sid/#{ss.survey_id}/token/#{ss.token}/lang//newtest/Y") }
52
+ end
53
+ context "when something goes wrong with limesurvey add_participants call during the callback" do
54
+ before(:each) { LimesurveyRails.stub(:add_participants).and_raise }
55
+ specify { expect { SurveyParticipation.create!(:survey_id => test_survey.id, :participant_id => a_participant.id ) }.to raise_error}
56
+ it "doesn't create the SurveyParticipation object" do
57
+ SurveyParticipation.create!(:survey_id => test_survey.id, :participant_id => a_participant.id ) rescue nil
58
+ expect(SurveyParticipation.all).to be_empty
59
+ end
60
+ end
61
+ context "when something goes wrong with updating token_id attributes during the callback" do
62
+ before(:each) { SurveyParticipation.any_instance.stub(:save!).and_raise }
63
+ specify { expect { SurveyParticipation.create!(:survey_id => test_survey.id, :participant_id => a_participant.id ) }.to raise_error}
64
+ end
65
+ end
66
+ describe "#destroy!" do
67
+ before(:each) { @ss = SurveyParticipation.create(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
68
+ context "when something goes wrong with limesurvey delete_participants call during the callback" do
69
+ before(:each) { LimesurveyRails.stub(:delete_participants).and_raise }
70
+ specify { expect { @ss.destroy }.to raise_error}
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "limesurvey attributes" do
76
+ before(:each) { test_survey.add_participant!(a_participant) }
77
+ let(:a_participant) { FactoryGirl.create(:test_model, :extra_id => "XX030459") }
78
+ let(:ss) { a_participant.survey_participations.for_survey(test_survey.id) }
79
+ ['email', 'firstname', 'lastname'].each do |a|
80
+ it "includes correct value for attribute #{a}" do
81
+ token_value = ss.send(a)
82
+ participant_value = a_participant[TestModel.send("limesurvey_participant_#{a}_attr")]
83
+ expect(token_value).to eq(participant_value)
84
+ end
85
+ end
86
+ it "includes correct value for attribute attribute_1" do
87
+ token_value = ss.attribute_1
88
+ participant_value = a_participant[TestModel.send("limesurvey_participant_attribute_1_attr")]
89
+ expect(token_value).to eq(participant_value)
90
+ end
91
+ it "includes OK as value for emailstatus" do
92
+ token_value = ss.emailstatus
93
+ expect(token_value).to eq('OK')
94
+ end
95
+ context "when email is not available" do
96
+ let(:a_participant) { FactoryGirl.create(:test_model, :email_address => nil) }
97
+ it "includes UNKNOWN as value for emailstatus" do
98
+ token_value = ss.emailstatus
99
+ expect(token_value).to eq('UNKNOWN')
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "#survey" do
105
+ let(:a_participation) { SurveyParticipation.create(:survey_id => test_survey.id, :participant_id => a_participant.id ) }
106
+ it "returns the survey" do
107
+ expect(a_participation.survey).to be_a(Survey)
108
+ end
109
+ end
110
+
111
+ end
112
+ end
113
+
@@ -0,0 +1,222 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ module LimesurveyRails
4
+
5
+ describe Survey, :survey => true do
6
+ before(:all) do
7
+ configure_and_connect
8
+ reset_models
9
+ TestModel.is_a_limesurvey_participant :attribute_1_attr => "extra_id", :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname'
10
+ end
11
+ after(:all) { remove_all_test_surveys }
12
+
13
+ describe ".add(title,lang)" do
14
+ it "returns true" do
15
+ expect(Survey.add('test title','en')).to be true
16
+ end
17
+ context "when wrong arguments provided" do
18
+ specify { expect { Survey.add('','en') }.to raise_error RemoteControlError }
19
+ specify { expect { Survey.add('test title','cds') }.to raise_error RemoteControlError }
20
+ end
21
+ end
22
+
23
+ describe ".all" do
24
+ before(:each) { remove_all_test_surveys }
25
+ it "returns an empty array" do
26
+ expect(Survey.all).to be_empty
27
+ end
28
+ context "when 3 surveys are present in the db" do
29
+ before(:each) { 3.times{|i| Survey.add("test survey number #{i}",'en') } }
30
+ it "returns an Array" do
31
+ expect(Survey.all).to be_an_instance_of(Array)
32
+ end
33
+ it "returns an Array with 3 elements" do
34
+ expect(Survey.all.size).to be 3
35
+ end
36
+ it "returns an Array made of Survey object" do
37
+ expect(Survey.all.inject(true){|memo,e| memo &= e.is_a? Survey}).to be true
38
+ end
39
+ end
40
+ end
41
+
42
+ context "when a test survey is present in the database" do
43
+
44
+ before(:each) { remove_all_test_surveys; @test_survey_id = get_brand_new_test_survey_id(:activate_tokens => true) }
45
+
46
+ let(:test_survey) { Survey.find(@test_survey_id) }
47
+
48
+ describe ".find(id)" do
49
+ context "when id is the test survey id" do
50
+ subject(:res) { Survey.find(@test_survey_id) }
51
+ it { should be_instance_of Survey }
52
+ it { should have_same_attributes_as test_survey }
53
+ end
54
+ context "when id is wrong" do
55
+ it "raise RemoteControlError with message containing 'Invalid survey ID'" do
56
+ expect { Survey.find(0) }.to raise_error(RemoteControlError, /Invalid survey ID/)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe ".url_for(id)" do
62
+ it "returns the specified limesurvey survey url" do
63
+ expect(Survey.url_for(@test_survey_id)).to eq(LIMESURVEY_BASE_URL + "/admin/survey/sa/view/surveyid/#{test_survey.id}")
64
+ end
65
+ end
66
+
67
+ describe ".new_survey_url" do
68
+ specify { expect(Survey.new_survey_url).to eq(LIMESURVEY_BASE_URL + '/admin/survey/sa/newsurvey') }
69
+ end
70
+
71
+ describe "#url" do
72
+ it "returns the limesurvey survey url" do
73
+ expect(test_survey.url).to eq(LIMESURVEY_BASE_URL + "/admin/survey/sa/view/surveyid/#{test_survey.id}")
74
+ end
75
+ end
76
+
77
+ describe "#description" do
78
+ it "returns a string with id title and status" do
79
+ expect(test_survey.description).to match /#{test_survey.id}.*#{test_survey.title}.*#{test_survey.status}/
80
+ end
81
+ end
82
+
83
+ describe "#destroy" do
84
+ it "returns true" do
85
+ expect(test_survey.destroy).to be true
86
+ end
87
+ context "when the survey has participants" do
88
+ # before(:all) { reset_models; TestModel.is_a_limesurvey_participant }
89
+ before(:each) do
90
+ FactoryGirl.create_list(:test_model,3)
91
+ TestModel.all.each{ |tm| FactoryGirl.create(:limesurvey_rails_survey_participation, :survey_id => test_survey.id, :participant_id => tm.id ) }
92
+ end
93
+ it "its survey participations are deleted" do
94
+ test_survey.destroy
95
+ expect(SurveyParticipation.for_survey(test_survey.id)).to be_empty
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "participants instance methods" do
101
+ # before(:all) { reset_models; TestModel.is_a_limesurvey_participant :attribute_1_attr => "extra_id", :email_attr => 'email_address', :firstname_attr => 'name', :lastname_attr => 'surname'}
102
+ # before(:each) do
103
+ # instances = FactoryGirl.create_list(:test_model,3)
104
+ # instances.each{ |tm| FactoryGirl.create(:limesurvey_rails_survey_participation, :survey_id => test_survey.id, :participant_id => tm.id ) }
105
+ # end
106
+
107
+ let(:a_participant) { FactoryGirl.create(:test_model) }
108
+
109
+ describe "#add_participant!(a_participant)" do
110
+ it_should_behave_like "adding a participant", 'add_participant!'
111
+ context "when a_participant is already registered" do
112
+ before(:each) do
113
+ test_survey.add_participant!(a_participant)
114
+ end
115
+ it "raise ActiveRecord::RecordInvalid" do
116
+ expect { test_survey.add_participant!(a_participant) }.to raise_error(ActiveRecord::RecordInvalid)
117
+ end
118
+ end
119
+ context "when a_participant is a regular ActiveRecord::Base object" do
120
+ it "raise WrongArgumentError" do
121
+ expect { test_survey.add_participant!(SurveyParticipation.new) }.to raise_error(WrongArgumentError)
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "#add_participant(a_participant)" do
127
+ it_should_behave_like "adding a participant", 'add_participant'
128
+ context "when a_participant is already registered" do
129
+ before(:each) do
130
+ test_survey.add_participant(a_participant)
131
+ end
132
+ it "returns false" do
133
+ expect(test_survey.add_participant(a_participant)).to be false
134
+ end
135
+ end
136
+ context "when a_participant is a regular ActiveRecord::Base object" do
137
+ it "returns false" do
138
+ expect(test_survey.add_participant(SurveyParticipation.new)).to be false
139
+ end
140
+ end
141
+ end
142
+
143
+ describe "#remove_participant!(a_participant)" do
144
+ it_should_behave_like "removing a participant", 'remove_participant!'
145
+ context "when a_participant is not registered" do
146
+ it "raise NoMethodError" do
147
+ expect { test_survey.remove_participant!(a_participant) }.to raise_error(NoMethodError)
148
+ end
149
+ end
150
+ context "when a_participant is a regular ActiveRecord::Base object" do
151
+ it "raise WrongArgumentError" do
152
+ expect { test_survey.remove_participant!(SurveyParticipation.new) }.to raise_error(WrongArgumentError)
153
+ end
154
+ end
155
+ end
156
+
157
+ describe "#remove_participant(a_participant)" do
158
+ it_should_behave_like "removing a participant", 'remove_participant'
159
+ context "when a_participant is not registered" do
160
+ it "returns false" do
161
+ expect(test_survey.remove_participant(a_participant)).to be false
162
+ end
163
+ end
164
+ context "when a_participant is a regular ActiveRecord::Base object" do
165
+ it "returns false" do
166
+ expect(test_survey.remove_participant(SurveyParticipation.new)).to be false
167
+ end
168
+ end
169
+ end
170
+
171
+ context "when 3 participants are registered" do
172
+ describe "#participants" do
173
+ before(:each) do
174
+ instances = FactoryGirl.create_list(:test_model,3)
175
+ instances.each{ |tm| FactoryGirl.create(:limesurvey_rails_survey_participation, :survey_id => test_survey.id, :participant_id => tm.id ) }
176
+ end
177
+ subject { test_survey.reload }
178
+ its(:participants) { is_expected.to match_ar_array TestModel.all}
179
+ context "when a participant is missinig on Limesurvey" do
180
+ before(:each) do
181
+ id_to_delete = LimesurveyRails.list_participants(test_survey.id).sample['tid']
182
+ LimesurveyRails.delete_participants(test_survey.id,[id_to_delete])
183
+ end
184
+ specify { expect { test_survey.participants }.to raise_error(InconsistentParticipantsError) }
185
+ end
186
+ end
187
+ end
188
+
189
+ describe "#invite_participants!" do
190
+ context "when 3 participants are registered with email defined" do
191
+ before(:each) do
192
+ instances = FactoryGirl.create_list(:test_model,3)
193
+ instances.each{ |tm| FactoryGirl.create(:limesurvey_rails_survey_participation, :survey_id => test_survey.id, :participant_id => tm.id ) }
194
+ end
195
+ it "returns array [3,0]" do
196
+ expect(test_survey.invite_participants!).to eq [3,0]
197
+ end
198
+ end
199
+ context "when email is missing for one of them" do
200
+ before(:each) do
201
+ instances = FactoryGirl.create_list(:test_model,2)
202
+ instances << FactoryGirl.create(:test_model,:email_address => nil)
203
+ instances.each{ |tm| FactoryGirl.create(:limesurvey_rails_survey_participation, :survey_id => test_survey.id, :participant_id => tm.id ) }
204
+ end
205
+ it "returns array [2,0]" do
206
+ expect(test_survey.invite_participants!).to eq [2,0]
207
+ end
208
+ describe "survey" do
209
+ before(:each) { test_survey.invite_participants! }
210
+ subject { test_survey.reload }
211
+ its(:token_count) { is_expected.to eq '3' }
212
+ its(:token_sent) { is_expected.to eq '2' }
213
+ end
214
+ end
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+ end
221
+ end
222
+