PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/data_sources_controller_spec.rb

https://bitbucket.org/schatt/chorus
Ruby | 241 lines | 200 code | 41 blank | 0 comment | 14 complexity | 90ed1f107f5d25ed6f0f2ad6ffea630e MD5 | raw file
  1. require 'spec_helper'
  2. describe DataSourcesController do
  3. ignore_authorization!
  4. let(:user) { users(:owner) }
  5. before { log_in user }
  6. describe "#index" do
  7. let(:permitted_data_source) { data_sources(:owners) }
  8. let(:prohibited_data_source) { data_sources(:admins) }
  9. let(:online_data_source) { data_sources(:online) }
  10. let(:offline_data_source) { data_sources(:offline) }
  11. it_behaves_like "a paginated list"
  12. it_behaves_like :succinct_list
  13. it "returns only online data sources that the user can access" do
  14. get :index
  15. response.code.should == "200"
  16. decoded_response.map(&:id).should include(online_data_source.id)
  17. decoded_response.map(&:id).should_not include(offline_data_source.id)
  18. decoded_response.map(&:id).should include(permitted_data_source.id)
  19. decoded_response.map(&:id).should_not include(prohibited_data_source.id)
  20. end
  21. context 'when all => "true" is passed' do
  22. it "returns all data sources" do
  23. get :index, :all => true
  24. response.code.should == "200"
  25. decoded_response.size.should == DataSource.count
  26. decoded_response.map(&:id).should include(online_data_source.id)
  27. decoded_response.map(&:id).should include(offline_data_source.id)
  28. decoded_response.map(&:id).should include(permitted_data_source.id)
  29. decoded_response.map(&:id).should include(prohibited_data_source.id)
  30. end
  31. end
  32. describe "filtering by type" do
  33. it "filters by gpdb data sources" do
  34. get :index, :entity_type => "gpdb_data_source", :all => true
  35. decoded_response.map(&:id).should =~ GpdbDataSource.pluck(:id)
  36. end
  37. it "filters by oracle data sources" do
  38. get :index, :entity_type => "oracle_data_source", :all => true
  39. decoded_response.map(&:id).should =~ OracleDataSource.pluck(:id)
  40. end
  41. end
  42. end
  43. describe "#show" do
  44. let(:data_source) { DataSource.first }
  45. context "with a valid instance id" do
  46. it "does not require authorization" do
  47. dont_allow(subject).authorize!.with_any_args
  48. get :show, :id => data_source.to_param
  49. end
  50. it "succeeds" do
  51. get :show, :id => data_source.to_param
  52. response.should be_success
  53. end
  54. it "presents the gpdb instance" do
  55. mock.proxy(controller).present(data_source)
  56. get :show, :id => data_source.to_param
  57. end
  58. end
  59. generate_fixture "gpdbDataSource.json" do
  60. get :show, :id => data_sources(:owners).to_param
  61. end
  62. generate_fixture "oracleDataSource.json" do
  63. get :show, :id => data_sources(:oracle).to_param
  64. end
  65. context "with an invalid gpdb instance id" do
  66. it "returns not found" do
  67. get :show, :id => -1
  68. response.should be_not_found
  69. end
  70. end
  71. end
  72. describe "#update" do
  73. let(:changed_attributes) { {} }
  74. let(:gpdb_data_source) { data_sources(:shared) }
  75. let(:params) do
  76. {
  77. :id => gpdb_data_source.id,
  78. :name => "changed"
  79. }
  80. end
  81. before do
  82. any_instance_of(DataSource) { |ds| stub(ds).valid_db_credentials? { true } }
  83. end
  84. it "uses authorization" do
  85. mock(subject).authorize!(:edit, gpdb_data_source)
  86. put :update, params
  87. end
  88. it "presents the gpdb instance" do
  89. mock.proxy(controller).present(gpdb_data_source)
  90. put :update, params
  91. end
  92. it "returns 200" do
  93. put :update, params
  94. response.code.should == "200"
  95. end
  96. it "returns 422 when the update parameters are invalid" do
  97. params[:name] = ''
  98. put :update, params
  99. response.code.should == "422"
  100. end
  101. end
  102. describe "#create" do
  103. it_behaves_like "an action that requires authentication", :put, :update, :id => '-1'
  104. let(:valid_attributes) do
  105. {
  106. :name => 'create_spec_name',
  107. :port => 12345,
  108. :host => 'server.emc.com',
  109. :db_name => 'postgres',
  110. :description => 'old description',
  111. :db_username => 'bob',
  112. :db_password => 'secret',
  113. :entity_type => entity_type,
  114. :shared => false
  115. }
  116. end
  117. context 'for a GpdbDataSource' do
  118. let(:entity_type) { "gpdb_data_source" }
  119. before do
  120. any_instance_of(DataSource) { |ds| stub(ds).valid_db_credentials? { true } }
  121. end
  122. it 'creates the data source' do
  123. expect {
  124. post :create, valid_attributes
  125. }.to change(GpdbDataSource, :count).by(1)
  126. response.code.should == "201"
  127. end
  128. it 'presents the data source' do
  129. mock_present do |data_source|
  130. data_source.name == valid_attributes[:name]
  131. end
  132. post :create, valid_attributes
  133. end
  134. it 'schedules a job to refresh the data source' do
  135. stub(QC.default_queue).enqueue_if_not_queued(anything, anything)
  136. mock(QC.default_queue).enqueue_if_not_queued('DataSource.refresh', numeric, {'new' => true})
  137. post :create, :data_source => valid_attributes
  138. end
  139. context "with invalid attributes" do
  140. it "responds with validation errors" do
  141. valid_attributes.delete(:name)
  142. post :create, valid_attributes
  143. response.code.should == "422"
  144. end
  145. end
  146. end
  147. context "for an OracleDataSource" do
  148. let(:entity_type) { "oracle_data_source" }
  149. context "with valid db credentials" do
  150. before do
  151. any_instance_of(DataSource) { |ds| stub(ds).valid_db_credentials? { true } }
  152. stub(ChorusConfig.instance).oracle_configured? { true }
  153. end
  154. it "creates a new OracleDataSource" do
  155. expect {
  156. post :create, valid_attributes
  157. }.to change(OracleDataSource, :count).by(1)
  158. response.code.should == "201"
  159. end
  160. it "presents the OracleDataSource" do
  161. mock_present do |data_source|
  162. data_source.name == valid_attributes[:name]
  163. end
  164. post :create, :data_source => valid_attributes
  165. end
  166. it "creates a private OracleDataSource by default" do
  167. mock_present do |data_source|
  168. data_source.should_not be_shared
  169. end
  170. post :create, valid_attributes
  171. end
  172. it "can create a shared OracleDataSource" do
  173. mock_present do |data_source|
  174. data_source.should be_shared
  175. end
  176. post :create, valid_attributes.merge(:shared => true)
  177. end
  178. end
  179. context "when oracle is not configured" do
  180. before do
  181. stub(ChorusConfig.instance).oracle_configured? { false }
  182. end
  183. it "should return an error" do
  184. post :create, valid_attributes
  185. response.code.should == "422"
  186. decoded_errors.record.should == "DATA_SOURCE_DRIVER_NOT_CONFIGURED"
  187. decoded_errors.data_source.should == 'Oracle'
  188. end
  189. end
  190. end
  191. context "for an unknown entity type" do
  192. let(:entity_type) { "######" }
  193. it "returns an error" do
  194. post :create, valid_attributes
  195. response.code.should == "422"
  196. decoded_errors.fields.entity_type.should have_key :INVALID
  197. end
  198. end
  199. end
  200. end