dbhero 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +21 -0
  4. data/app/assets/javascripts/dbhero/application.js +45 -0
  5. data/app/assets/javascripts/dbhero/dataclips.js +42 -0
  6. data/app/assets/stylesheets/dbhero/application.css.scss +88 -0
  7. data/app/controllers/dbhero/application_controller.rb +30 -0
  8. data/app/controllers/dbhero/dataclips_controller.rb +84 -0
  9. data/app/helpers/dbhero/application_helper.rb +4 -0
  10. data/app/helpers/dbhero/dataclips_helper.rb +4 -0
  11. data/app/models/dbhero/dataclip.rb +50 -0
  12. data/app/views/dbhero/dataclips/_clip_table.html.slim +38 -0
  13. data/app/views/dbhero/dataclips/_form.html.slim +32 -0
  14. data/app/views/dbhero/dataclips/edit.html.slim +6 -0
  15. data/app/views/dbhero/dataclips/index.html.slim +25 -0
  16. data/app/views/dbhero/dataclips/new.html.slim +2 -0
  17. data/app/views/dbhero/dataclips/show.html.slim +18 -0
  18. data/app/views/layouts/dbhero/application.html.slim +24 -0
  19. data/config/routes.rb +6 -0
  20. data/lib/dbhero.rb +7 -0
  21. data/lib/dbhero/configuration.rb +39 -0
  22. data/lib/dbhero/engine.rb +9 -0
  23. data/lib/dbhero/gdrive_exporter.rb +56 -0
  24. data/lib/dbhero/router_constraint.rb +43 -0
  25. data/lib/dbhero/version.rb +3 -0
  26. data/lib/generators/dbhero/install/install_generator.rb +29 -0
  27. data/lib/generators/dbhero/install/templates/dbhero.rb +26 -0
  28. data/lib/generators/dbhero/install/templates/migrations/create_dbhero_dataclips.rb +17 -0
  29. data/lib/tasks/dbhero_tasks.rake +4 -0
  30. data/spec/controllers/dbhero/dataclips_controller_spec.rb +293 -0
  31. data/spec/dummy/README.rdoc +28 -0
  32. data/spec/dummy/Rakefile +6 -0
  33. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  34. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  35. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  36. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  37. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  38. data/spec/dummy/bin/bundle +3 -0
  39. data/spec/dummy/bin/rails +4 -0
  40. data/spec/dummy/bin/rake +4 -0
  41. data/spec/dummy/config.ru +4 -0
  42. data/spec/dummy/config/application.rb +27 -0
  43. data/spec/dummy/config/boot.rb +5 -0
  44. data/spec/dummy/config/database.yml +85 -0
  45. data/spec/dummy/config/environment.rb +5 -0
  46. data/spec/dummy/config/environments/development.rb +37 -0
  47. data/spec/dummy/config/environments/production.rb +78 -0
  48. data/spec/dummy/config/environments/test.rb +39 -0
  49. data/spec/dummy/config/initializers/assets.rb +8 -0
  50. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  51. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  52. data/spec/dummy/config/initializers/dbhero.rb +26 -0
  53. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  54. data/spec/dummy/config/initializers/inflections.rb +16 -0
  55. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  56. data/spec/dummy/config/initializers/session_store.rb +3 -0
  57. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  58. data/spec/dummy/config/locales/en.yml +23 -0
  59. data/spec/dummy/config/routes.rb +4 -0
  60. data/spec/dummy/config/secrets.yml +22 -0
  61. data/spec/dummy/db/migrate/20150323172444_create_dbhero_dataclips.rb +17 -0
  62. data/spec/dummy/db/schema.rb +32 -0
  63. data/spec/dummy/log/development.log +27 -0
  64. data/spec/dummy/log/test.log +3758 -0
  65. data/spec/dummy/public/404.html +67 -0
  66. data/spec/dummy/public/422.html +67 -0
  67. data/spec/dummy/public/500.html +66 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/factories.rb +8 -0
  70. data/spec/models/dbhero/dataclip_spec.rb +119 -0
  71. data/spec/rails_helper.rb +57 -0
  72. data/spec/spec_helper.rb +90 -0
  73. metadata +312 -0
@@ -0,0 +1,32 @@
1
+ = javascript_include_tag "https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.8/ace.js"
2
+ = form_for @dataclip do |f|
3
+ - if @dataclip.errors.any?
4
+ .section
5
+ .card-panel.deep-orange.darken-4
6
+ .card-content.white-text
7
+ ul
8
+ - @dataclip.errors.full_messages.each do |message|
9
+ li
10
+ i class='mdi-alert-error left'
11
+ = message
12
+
13
+ .row.style-text-field
14
+ .input-field
15
+ = f.text_area :description, class: "materialize-textarea", placeholder: "describe this clip, first line should be title..."
16
+ .row
17
+ .input-field
18
+ = f.text_area :raw_query, class: 'editor-style z-depth-1'
19
+ .row
20
+ .col.s5
21
+ .input-field
22
+ .switch
23
+ label
24
+ i style="margin-top: -20px; float: left" class="small mdi-action-lock-open tooltipped" data-position="right" data-delay="50" data-tooltip="Everyone with the public link can see the results"
25
+ = f.check_box :private
26
+ span.lever style='float: left;'
27
+ i style="margin-top: -20px; float: left" class="small mdi-action-lock-outline tooltipped" data-position="right" data-delay="50" data-tooltip="Only users that same role as you can see the clip"
28
+ .col.s7.right-align
29
+ .input-field
30
+ button class="btn waves-effect waves-light blue darken-3" type="submit" name="action"
31
+ | Save
32
+ i class="mdi-action-done-all left"
@@ -0,0 +1,6 @@
1
+ .container
2
+ == render 'form'
3
+ - if @dataclip.valid? && @dataclip.q_result
4
+ .section
5
+ .row
6
+ == render 'clip_table', show_public_link: true
@@ -0,0 +1,25 @@
1
+ .container
2
+ .section
3
+ .row.style-text-field
4
+ .input-field.col.s12
5
+ = form_tag dataclips_path, method: :get do
6
+ input#icon_prefix.validate name='search' type="text" value=params[:search] /
7
+ label for="icon_prefix"
8
+ | search on all clips
9
+ i.mdi-action-search.prefix
10
+
11
+ .row
12
+ .col.s2.center-align.right
13
+ span style='font-size: 16px; color: #9e9e9e;' updated at
14
+ .col.s12
15
+ ul.collection.z-depth-1
16
+ - @dataclips.each do |dataclip|
17
+ li.collection-item
18
+ .row style='margin-bottom: 0;'
19
+ .col.s12
20
+ = link_to dataclip.title, edit_dataclip_path(dataclip)
21
+ - if dataclip.user
22
+ span style='font-size: 13px; font-weight: light; color: #607d8b;'
23
+ |   by #{dataclip.user}
24
+ span.right style='font-size: 12px; font-weight: bold; color: #607d8b;'= distance_of_time_in_words(dataclip.updated_at, DateTime.now)
25
+
@@ -0,0 +1,2 @@
1
+ .container
2
+ == render 'form'
@@ -0,0 +1,18 @@
1
+ .section.bottom-bord.grey.lighten-4
2
+ .container
3
+ .row
4
+ .col.s12.center-align
5
+ h5.clip-title=simple_format(@dataclip.title)
6
+ p=simple_format(@dataclip.description_without_title)
7
+
8
+
9
+ .container
10
+ - if params[:gdrive_file_url].present?
11
+ .card-panel.teal.up.green.lighten-1
12
+ .card-content.white-text.center-align style='text-transform: uppercase;'
13
+ | DATACLIP EXPORTED! 
14
+ strong
15
+ = link_to 'CLICK HERE TO VIEW IT', params[:gdrive_file_url], style: 'color: #33691e;'
16
+
17
+
18
+ == render 'clip_table'
@@ -0,0 +1,24 @@
1
+ doctype html
2
+ html.grey.lighten-3
3
+ head
4
+ title DBHero
5
+ = stylesheet_link_tag "dbhero/application", "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.95.3/css/materialize.min.css", media: "all"
6
+ = javascript_include_tag "dbhero/application", "https://cdnjs.cloudflare.com/ajax/libs/materialize/0.95.3/js/materialize.min.js"
7
+ = csrf_meta_tags
8
+ body
9
+ - unless action_name == 'show'
10
+ header.grey.lighten-5
11
+ .row
12
+ .col.s2
13
+ = link_to dataclips_path do
14
+ h1
15
+ | db
16
+ strong hero
17
+ .col.s10.right-align
18
+ .btn-group
19
+ = link_to 'clips', dataclips_path, class: 'btn blue darken-1 waves-effect waves-light'
20
+ | 
21
+ = link_to 'new clip', new_dataclip_path, class: 'btn blue darken-1 waves-effect waves-light'
22
+
23
+ = yield
24
+
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ Dbhero::Engine.routes.draw do
2
+ root to: "dataclips#index"
3
+ resources :dataclips do
4
+ get :drive, on: :collection
5
+ end
6
+ end
data/lib/dbhero.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "dbhero/engine"
2
+ require "dbhero/configuration"
3
+ require "dbhero/gdrive_exporter"
4
+
5
+ module Dbhero
6
+ extend Configuration
7
+ end
@@ -0,0 +1,39 @@
1
+ module Dbhero
2
+ module Configuration
3
+ VALID_CONFIG_KEYS = [:authenticate, :current_user_method, :custom_user_auth_condition,
4
+ :user_representation, :google_api_id, :google_api_secret].freeze
5
+
6
+ DEFAULT_AUTHENTICATE = true
7
+ DEFAULT_CURRENT_USER_METHOD = :current_user
8
+ DEFAULT_GOOGLE_API_ID = ''
9
+ DEFAULT_GOOGLE_API_SECRET = ''
10
+ DEFAULT_USER_PRESENTATION = :email
11
+ DEFAULT_CUSTOM_USER_AUTH_CONDITION = nil
12
+
13
+ attr_accessor *VALID_CONFIG_KEYS
14
+
15
+ def self.extended(base)
16
+ base.reset
17
+ end
18
+
19
+ def configure
20
+ yield self if block_given?
21
+ end
22
+
23
+ def options
24
+ Hash[ * VALID_CONFIG_KEYS.map { |key| [key, send(key)] }.flatten ]
25
+ end
26
+
27
+ def reset
28
+ self.authenticate = DEFAULT_AUTHENTICATE
29
+ self.current_user_method = DEFAULT_CURRENT_USER_METHOD
30
+ self.user_representation = DEFAULT_USER_PRESENTATION
31
+ self.google_api_id = DEFAULT_GOOGLE_API_ID
32
+ self.google_api_secret = DEFAULT_GOOGLE_API_SECRET
33
+ self.custom_user_auth_condition = DEFAULT_CUSTOM_USER_AUTH_CONDITION
34
+ end
35
+
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,9 @@
1
+ module Dbhero
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Dbhero
4
+
5
+ config.generators do |g|
6
+ g.template_engine :slim
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,56 @@
1
+ module Dbhero
2
+ class GdriveExporter
3
+ attr_accessor :options
4
+ attr_reader :client, :auth, :session,
5
+ :dataclip, :uploaded_file, :exported_file_url
6
+
7
+ def initialize options = {}
8
+ @client ||= ::Google::APIClient.new
9
+ @options = options
10
+
11
+ @auth = @client.authorization
12
+ @auth.client_id = Dbhero.google_api_id
13
+ @auth.client_secret = Dbhero.google_api_secret
14
+ @auth.scope =
15
+ "https://www.googleapis.com/auth/drive " +
16
+ "https://spreadsheets.google.com/feeds/"
17
+
18
+ @auth.redirect_uri = options[:redirect_uri]
19
+ end
20
+
21
+ def fetch_access_token! code
22
+ @auth.code = code
23
+ @auth.fetch_access_token!
24
+ @session ||= GoogleDrive.login_with_oauth(@auth.access_token)
25
+ end
26
+
27
+ def export_clip_by_token token
28
+ @dataclip ||= Dataclip.find_by token: token
29
+ raise 'unable to find dataclip' unless @dataclip
30
+
31
+ exported_file = find_or_create_spreadsheet!
32
+ @exported_file_url = exported_file.human_url
33
+ end
34
+
35
+ private
36
+
37
+ def find_or_create_spreadsheet!
38
+ file_title = "DBHero - #{@dataclip.title}"
39
+
40
+ spreadsheet = @session.spreadsheet_by_title(file_title)
41
+
42
+ worksheet = (spreadsheet || upload_from_string(file_title)).worksheets[0]
43
+ worksheet[1,1] = "=importData('#{@options[:import_data_url]}')"
44
+ worksheet.save
45
+
46
+ spreadsheet || @uploaded_file
47
+ end
48
+
49
+ def upload_from_string file_title
50
+ @uploaded_file ||= @session.upload_from_string(
51
+ @dataclip.csv_string,
52
+ file_title,
53
+ content_type: 'text/csv')
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,43 @@
1
+ module Dbhero
2
+ class RouterConstraint
3
+ @@routes = Dbhero::Engine.routes
4
+
5
+ def self.matches? request, options = {}
6
+ _ = new(request, options)
7
+ _.match?
8
+ end
9
+
10
+ def initialize(request, options)
11
+ @request = request
12
+ @options = options
13
+ @devise_mapping = options[:devise_mapping] || :user
14
+ @devise_auth = options[:devise_auth] || false
15
+ @enable_public_clip = options[:enable_public_clip] || false
16
+ end
17
+
18
+ def match?
19
+ return ((@enable_public_clip && check_if_is_public_dataclip) ||
20
+ (@devise_auth && authenticate_warden && check_custom_condition))
21
+ end
22
+
23
+ private
24
+
25
+ def check_if_is_public_dataclip
26
+ if @request.path.match(/\/dataclips\/([\w\-]{36}+)(\.[\w]{1,6})?$/)
27
+ return Dbhero::Dataclip.where(token: $1).exists?
28
+ end
29
+ end
30
+
31
+ def authenticate_warden
32
+ @request.env['warden'].send(:authenticate!, scope: @devise_mapping)
33
+ end
34
+
35
+ def check_custom_condition
36
+ auth_condition.is_a?(Proc) && auth_condition.call( @request.env['warden'].user(@devise_mapping) )
37
+ end
38
+
39
+ def auth_condition
40
+ @options[:custom_auth_condition]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Dbhero
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ module Dbhero
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ include Rails::Generators::Migration
5
+
6
+ desc "Installs DBHero and generate migrations"
7
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
8
+
9
+ def copy_initializer
10
+ template 'dbhero.rb', 'config/initializers/dbhero.rb'
11
+ end
12
+
13
+ def create_migrations
14
+ migration_template "migrations/create_dbhero_dataclips.rb", "db/migrate/create_dbhero_dataclips.rb"
15
+ end
16
+
17
+ # for migration generation
18
+ def self.next_migration_number(path)
19
+ unless @prev_migration_nr
20
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
21
+ else
22
+ @prev_migration_nr += 1
23
+ end
24
+ @prev_migration_nr.to_s
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,26 @@
1
+ Dbhero.configure do |config|
2
+ # if you are using devise you can keep the "authenticate_user!"
3
+ config.authenticate = true
4
+
5
+ # Method to get the current user authenticated on your app
6
+ # if you are using devise you can keep the "current_user"
7
+ config.current_user_method = :current_user
8
+
9
+ # uncomment to use custom user auth
10
+ # config.custom_user_auth_condition = lambda do |user|
11
+ # user.admin?
12
+ # end
13
+
14
+ # String representation for user
15
+ # when creating a dataclip just save on user field
16
+ config.user_representation = :email
17
+
18
+ # Google drive integration, uncomment to use ;)
19
+ # you can get you google api credentials here:
20
+ # https://developers.google.com/drive/web/auth/web-server
21
+ #
22
+ # config.google_api_id = 'GOOGLE_API_ID'
23
+ # config.google_api_secret = 'GOOGLE_API_SECRET'
24
+ end
25
+
26
+
@@ -0,0 +1,17 @@
1
+ class CreateDbheroDataclips < ActiveRecord::Migration
2
+ def change
3
+ create_table :dbhero_dataclips do |t|
4
+ t.text :description, null: false
5
+ t.text :raw_query, null: false
6
+ t.text :token, null: false
7
+ t.text :user
8
+ t.boolean :private, default: false, null: false
9
+
10
+ t.timestamps null: false
11
+ end
12
+
13
+ add_index :dbhero_dataclips, :token, unique: true
14
+ add_index :dbhero_dataclips, :user
15
+ end
16
+ end
17
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :dbhero do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,293 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.shared_examples "user not authenticated" do
4
+ let(:method_name) { :get }
5
+ let(:options) { nil }
6
+
7
+ before do
8
+ allow(Dbhero).to receive(:authenticate).and_return(true)
9
+ allow(controller).to receive(:_current_user).and_return(nil)
10
+ end
11
+
12
+ it "should raise forbidden error" do
13
+ expect {
14
+ send(method_name, *options)
15
+ }.to raise_error(ActionController::RoutingError, 'Forbidden')
16
+ end
17
+ end
18
+
19
+ RSpec.shared_examples "user authenticated" do
20
+ let(:method_name) { :get }
21
+ let(:options) { nil }
22
+ let(:expect_method) { :be_success }
23
+
24
+ before do
25
+ current_user = double()
26
+ allow(current_user).to receive(:email).and_return('foo@bar.com')
27
+ allow(controller).to receive(:_current_user).and_return(current_user)
28
+ allow(Dbhero).to receive(:authenticate).and_return(true)
29
+
30
+ send(method_name, *options)
31
+ end
32
+
33
+ it("response should be success") { expect(response).to send(expect_method) }
34
+ end
35
+
36
+ RSpec.shared_examples "user authenticated match for custom role" do
37
+ let(:method_name) { :get }
38
+ let(:options) { nil }
39
+ let(:expect_method) { :be_success }
40
+
41
+ before do
42
+ current_user = double()
43
+ allow(current_user).to receive(:email).and_return('foo@bar.com')
44
+ allow(current_user).to receive(:admin?).and_return(true)
45
+ allow(controller).to receive(:_current_user).and_return(current_user)
46
+ allow(Dbhero).to receive(:authenticate).and_return(true)
47
+ allow(Dbhero).to receive(:custom_user_auth_condition).and_return(->(u){u.admin?})
48
+
49
+ send(method_name, *options)
50
+ end
51
+
52
+ it("response should be success") { expect(response).to send(expect_method) }
53
+ end
54
+
55
+ RSpec.shared_examples "user authenticated don't match for custom role" do
56
+ let(:method_name) { :get }
57
+ let(:options) { nil }
58
+
59
+ before do
60
+ current_user = double()
61
+ allow(current_user).to receive(:email).and_return('foo@bar.com')
62
+ allow(current_user).to receive(:admin?).and_return(false)
63
+ allow(controller).to receive(:_current_user).and_return(current_user)
64
+ allow(Dbhero).to receive(:authenticate).and_return(true)
65
+ allow(Dbhero).to receive(:custom_user_auth_condition).and_return(->(u){u.admin?})
66
+ end
67
+
68
+ it "should raise forbidden error" do
69
+ expect {
70
+ send(method_name, *options)
71
+ }.to raise_error(ActionController::RoutingError, 'Forbidden')
72
+ end
73
+ end
74
+
75
+ RSpec.shared_examples "disabled auth" do
76
+ let(:method_name) { :get }
77
+ let(:options) { nil }
78
+ let(:expect_method) { :be_success }
79
+
80
+ before do
81
+ allow(Dbhero).to receive(:authenticate).and_return(false)
82
+ allow(controller).to receive(:_current_user).and_return(nil)
83
+ send(method_name, *options)
84
+ end
85
+
86
+ it("response should be success") { expect(response).to send(expect_method) }
87
+ end
88
+
89
+ RSpec.describe Dbhero::DataclipsController, type: :controller do
90
+ routes { Dbhero::Engine.routes }
91
+
92
+ describe "GET index" do
93
+ describe "with enabled auth" do
94
+ it_should_behave_like "user not authenticated" do
95
+ let(:options) { :index }
96
+ end
97
+
98
+ it_should_behave_like "user authenticated" do
99
+ let(:options) { :index }
100
+ end
101
+
102
+ it_should_behave_like "user authenticated match for custom role" do
103
+ let(:options) { :index }
104
+ end
105
+
106
+ it_should_behave_like "user authenticated don't match for custom role" do
107
+ let(:options) { :index }
108
+ end
109
+ end
110
+
111
+ describe "with disabled auth" do
112
+ it_should_behave_like "disabled auth" do
113
+ let(:options) { :index }
114
+ end
115
+ end
116
+ end
117
+
118
+ describe "GET new" do
119
+ describe "with enabled auth" do
120
+ it_should_behave_like "user not authenticated" do
121
+ let(:options) { :new }
122
+ end
123
+
124
+ it_should_behave_like "user authenticated" do
125
+ let(:options) { :new }
126
+ end
127
+
128
+ it_should_behave_like "user authenticated match for custom role" do
129
+ let(:options) { :new }
130
+ end
131
+
132
+ it_should_behave_like "user authenticated don't match for custom role" do
133
+ let(:options) { :new }
134
+ end
135
+ end
136
+
137
+ describe "with disabled auth" do
138
+ it_should_behave_like "disabled auth" do
139
+ let(:options) { :new }
140
+ end
141
+ end
142
+ end
143
+
144
+ describe "POST create" do
145
+ describe "with enabled auth" do
146
+ it_should_behave_like "user not authenticated" do
147
+ let(:method_name) { :post }
148
+ let(:options) { [:create, { dataclip: { description: "foo bar", raw_query: "select 'foo' as bar" } }] }
149
+ end
150
+
151
+ it_should_behave_like "user authenticated" do
152
+ let(:method_name) { :post }
153
+ let(:options) { [:create, { dataclip: { description: "foo bar", raw_query: "select 'foo' as bar" } }] }
154
+ let(:expect_method) { :be_redirect }
155
+
156
+ it { expect(Dbhero::Dataclip.find_by(description: 'foo bar')).not_to be_nil }
157
+ end
158
+
159
+ it_should_behave_like "user authenticated match for custom role" do
160
+ let(:method_name) { :post }
161
+ let(:options) { [:create, { dataclip: { description: "foo bar", raw_query: "select 'foo' as bar" } }] }
162
+ let(:expect_method) { :be_redirect }
163
+
164
+ it "find dataclip" do
165
+ clip = Dbhero::Dataclip.find_by(description: 'foo bar')
166
+
167
+ expect(clip.user).to eq(controller._current_user.email)
168
+ expect(clip.description).to eq("foo bar")
169
+ expect(clip.raw_query).to eq("select 'foo' as bar")
170
+ end
171
+ end
172
+
173
+ it_should_behave_like "user authenticated don't match for custom role" do
174
+ let(:method_name) { :post }
175
+ let(:options) { [:create, { dataclip: { description: "foo bar", raw_query: "select 'foo' as bar" } }] }
176
+ end
177
+ end
178
+
179
+ describe "with disabled auth" do
180
+ it_should_behave_like "disabled auth" do
181
+ let(:method_name) { :post }
182
+ let(:options) { [:create, { dataclip: { description: "foo bar disabled", raw_query: "select 'foo' as bar" } }] }
183
+ let(:expect_method) { :be_redirect }
184
+
185
+ it "find dataclip" do
186
+ clip = Dbhero::Dataclip.find_by(description: 'foo bar disabled')
187
+
188
+ expect(clip.user).to be_nil
189
+ expect(clip.description).to eq("foo bar disabled")
190
+ expect(clip.raw_query).to eq("select 'foo' as bar")
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+
197
+ describe "GET edit" do
198
+ describe "with enabled auth" do
199
+ it_should_behave_like "user not authenticated" do
200
+ let(:dataclip) { create(:dataclip) }
201
+ let(:options) { [:edit, { id: dataclip.token}] }
202
+ end
203
+
204
+ it_should_behave_like "user authenticated" do
205
+ let(:dataclip) { create(:dataclip) }
206
+ let(:options) { [:edit, { id: dataclip.token}] }
207
+ end
208
+
209
+ it_should_behave_like "user authenticated match for custom role" do
210
+ let(:dataclip) { create(:dataclip) }
211
+ let(:options) { [:edit, { id: dataclip.token}] }
212
+ end
213
+
214
+ it_should_behave_like "user authenticated don't match for custom role" do
215
+ let(:dataclip) { create(:dataclip) }
216
+ let(:options) { [:edit, { id: dataclip.token}] }
217
+ end
218
+ end
219
+
220
+ describe "with disabled auth" do
221
+ it_should_behave_like "disabled auth" do
222
+ let(:dataclip) { create(:dataclip) }
223
+ let(:options) { [:edit, { id: dataclip.token}] }
224
+ end
225
+ end
226
+ end
227
+
228
+ describe "PUT update" do
229
+ describe "with enabled auth" do
230
+ it_should_behave_like "user not authenticated" do
231
+ let(:method_name) { :post }
232
+ let(:options) { [:create, { dataclip: { description: "foo bar", raw_query: "select 'foo' as bar" } }] }
233
+ end
234
+
235
+ it_should_behave_like "user authenticated" do
236
+ let(:method_name) { :put }
237
+ let(:dataclip) { create(:dataclip, {description: "foo bar", raw_query: "select 'foo' as bar"}) }
238
+ let(:options) { [:update, id: dataclip.token, dataclip: { description: "updated" } ] }
239
+ let(:expect_method) { :be_redirect }
240
+
241
+ it "find dataclip" do
242
+ clip = Dbhero::Dataclip.find_by(description: 'updated')
243
+
244
+ expect(clip.description).to eq("updated")
245
+ expect(clip.raw_query).to eq("select 'foo' as bar")
246
+ end
247
+ end
248
+
249
+ it_should_behave_like "user authenticated match for custom role" do
250
+ let(:method_name) { :put }
251
+ let(:dataclip) { create(:dataclip, {description: "foo bar", raw_query: "select 'foo' as bar"}) }
252
+ let(:options) { [:update, id: dataclip.token, dataclip: { description: "updated" }] }
253
+ let(:expect_method) { :be_redirect }
254
+
255
+ it "find dataclip" do
256
+ clip = Dbhero::Dataclip.find_by(description: 'updated')
257
+
258
+ expect(clip.description).to eq("updated")
259
+ expect(clip.raw_query).to eq("select 'foo' as bar")
260
+ end
261
+ end
262
+
263
+ it_should_behave_like "user authenticated don't match for custom role" do
264
+ let(:method_name) { :put }
265
+ let(:dataclip) { create(:dataclip, {description: "foo bar", raw_query: "select 'foo' as bar"}) }
266
+ let(:options) { [:update, id: dataclip.token, dataclip: { description: "updated" }] }
267
+ let(:expect_method) { :be_redirect }
268
+
269
+ it "not update dataclip" do
270
+ clip = Dbhero::Dataclip.find dataclip.id
271
+
272
+ expect(clip.description).to eq("foo bar")
273
+ end
274
+ end
275
+ end
276
+
277
+ describe "with disabled auth" do
278
+ it_should_behave_like "disabled auth" do
279
+ let(:method_name) { :put }
280
+ let(:dataclip) { create(:dataclip, {description: "foo bar", raw_query: "select 'foo' as bar"}) }
281
+ let(:options) { [:update, id: dataclip.token, dataclip: { description: "updated" }] }
282
+ let(:expect_method) { :be_redirect }
283
+
284
+ it "find dataclip" do
285
+ clip = Dbhero::Dataclip.find_by(description: 'updated')
286
+
287
+ expect(clip.description).to eq("updated")
288
+ expect(clip.raw_query).to eq("select 'foo' as bar")
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end