Skip to content

Commit

Permalink
Added spec to test duplicacy of spreadsheets #5
Browse files Browse the repository at this point in the history
Test case to check duplicacy of spreadsheet was added and the
spreadsheet_controller was modified accordingly
  • Loading branch information
Pbasnal committed Jul 15, 2014
1 parent 2af43ae commit 734f3ff
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/controllers/spreadsheets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -65,6 +65,6 @@ def failure
#################################

def spreadsheet_params
params.permit(:title, :id, :token)
params.permit(:id, :title, :token)
end
end
10 changes: 8 additions & 2 deletions app/models/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions spec/controllers/spreadsheets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/spreadsheets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions spec/vcr/controllers/api-update-with-data.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 734f3ff

Please sign in to comment.