diff --git a/app/controllers/spreadsheets_controller.rb b/app/controllers/spreadsheets_controller.rb index 51b6925..0933f9a 100644 --- a/app/controllers/spreadsheets_controller.rb +++ b/app/controllers/spreadsheets_controller.rb @@ -28,7 +28,7 @@ def edit token = spreadsheet_params['token'] spreadsheet = Spreadsheet.where(access_token: token)[0] - if spreadsheet.add_spreadsheet_credentials(spreadsheet_params) + if spreadsheet && spreadsheet.add_spreadsheet_credentials(spreadsheet_params) spreadsheet.save else @error = 'Already Present' @@ -65,6 +65,6 @@ def failure ################################# def spreadsheet_params - params.permit(:title, :id, :token) + params.permit(:id, :title, :token) end end diff --git a/app/models/spreadsheet.rb b/app/models/spreadsheet.rb index 640648b..60780c4 100644 --- a/app/models/spreadsheet.rb +++ b/app/models/spreadsheet.rb @@ -16,8 +16,14 @@ def add_tokens(tokens = {}) end def add_spreadsheet_credentials(credentials = {}) - self['spreadsheet_id'] = credentials['id'] - self['spreadsheet_title'] = credentials['title'] + if Spreadsheet.where('spreadsheet_id' => credentials['id']).to_a[0] + return false + else + self['spreadsheet_id'] = credentials['id'] + self['spreadsheet_title'] = credentials['title'] + end + + true end def access_token diff --git a/spec/controllers/spreadsheets_controller_spec.rb b/spec/controllers/spreadsheets_controller_spec.rb index c0e6689..77ececf 100644 --- a/spec/controllers/spreadsheets_controller_spec.rb +++ b/spec/controllers/spreadsheets_controller_spec.rb @@ -3,7 +3,6 @@ require 'vcr' RSpec.describe SpreadsheetsController, :type => :controller do - context 'GET User Permission' do context 'User accepts' do it 'redirects to new ' do @@ -65,15 +64,41 @@ context 'Get Edit' do let(:sheet) { FactoryGirl.create(:spreadsheet)} it 'renders index page after executing' do - get :edit, title: 'namecollection', id: sheet['spreadsheet_id'], token: sheet['access_token'] + + get :edit, id: sheet['spreadsheet_id'], title: sheet['spreadsheet_title'], token: sheet['access_token'] expect(response).to render_template(:index) end it 'adds spreadsheet\'s credentials' do - get :edit, title: 'namecollection', id: sheet['spreadsheet_id'], token: sheet['access_token'] + sheet + Spreadsheet.delete_all + spreadsheet = Spreadsheet.new + spreadsheet.add_tokens({ + 'token' => sheet[:access_token], + 'refresh_token' => sheet[:refresh_token], + 'expires_at' => sheet[:expires_at] + }) + spreadsheet.save + get :edit, id: sheet['spreadsheet_id'], title: sheet['spreadsheet_title'], token: sheet['access_token'] expect(assigns(:error)).to be(nil) end + + it 'does not add duplicate spreadsheet' do + sheet + spreadsheet = Spreadsheet.new + spreadsheet.add_tokens({ + 'token' => '0ya29.QgC-kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA', + 'refresh_token' => sheet[:refresh_token], + 'expires_at' => sheet[:expires_at] + }) + spreadsheet.save + + get :edit, title: sheet['spreadsheet_title'], id: sheet['spreadsheet_id'], token: '0ya29.QgC-kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA' + + expect(assigns(:error)).not_to be(nil) + end + end context 'Post Update' do diff --git a/spec/factories/spreadsheets.rb b/spec/factories/spreadsheets.rb index 63c310f..6790dd4 100644 --- a/spec/factories/spreadsheets.rb +++ b/spec/factories/spreadsheets.rb @@ -2,10 +2,10 @@ FactoryGirl.define do factory :spreadsheet do |s| - s.spreadsheet_id {'1g2JX-w0XtcWD7yZo-K3cSHs7PhycCwhaYYmwajsLIwE'} - s.spreadsheet_title {'namecollection'} - s.access_token {'ya29.QgC-5kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA'} - s.refresh_token {'1/DlAqfSUji69F3YuVAHSoWxWE0grR8aYSkb2OocVCNBw'} - s.expires_at {Time.now - 3600} + s.spreadsheet_id {'1g2JX-w0XtcWD7yZo-K3cSHs7PhycCwhaYYmwajsLIwE'} + s.spreadsheet_title {'namecollection'} + s.sequence(:access_token) {|n| "#{n}ya29.QgC-kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA"} + s.refresh_token {'1/DlAqfSUji69F3YuVAHSoWxWE0grR8aYSkb2OocVCNBw'} + s.expires_at {Time.now - 3600} end end diff --git a/spec/vcr/controllers/api-update-with-data.yml b/spec/vcr/controllers/api-update-with-data.yml index 9861930..5d91977 100644 --- a/spec/vcr/controllers/api-update-with-data.yml +++ b/spec/vcr/controllers/api-update-with-data.yml @@ -337,4 +337,57 @@ http_interactions: row='28' col='1' inputValue='airnow'>airnow" http_version: recorded_at: Mon, 14 Jul 2014 12:33:21 GMT +- request: + method: get + uri: https://spreadsheets.google.com/feeds/worksheets//private/full + body: + encoding: US-ASCII + string: '' + headers: + Authorization: + - Bearer ya29.QgAFK5QrFhLCIB8AAABfqjxL7sQjl5zVnL-YlD_fcUxIutL1k_KUFTr1NDwqCA + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 400 + message: Bad Request + headers: + Content-Type: + - text/html; charset=UTF-8 + Set-Cookie: + - NID=67=lnaiDCsR8YZhYNqU8f8qSS-gNY7HGAx6aNASsXwIMN20ES3nNmJ6dYAvXOcMgzc1TcT2GfM7bCJ-5mcmbT8QD1VOqSOsz8T_GCRf6KHMHrRJSbNw39de3Sl6uh5cOjQn;Domain=.google.com;Path=/;Expires=Wed, + 14-Jan-2015 05:47:35 GMT;HttpOnly + P3p: + - CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 + for more info." + X-Chromium-Appcache-Fallback-Override: + - disallow-fallback + Date: + - Tue, 15 Jul 2014 05:47:35 GMT + Expires: + - Tue, 15 Jul 2014 05:47:35 GMT + Cache-Control: + - private, max-age=0 + X-Content-Type-Options: + - nosniff + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1; mode=block + Server: + - GSE + Alternate-Protocol: + - 443:quic + Transfer-Encoding: + - chunked + body: + encoding: UTF-8 + string: Invalid request URI + http_version: + recorded_at: Tue, 15 Jul 2014 05:47:35 GMT recorded_with: VCR 2.9.2