Skip to content

Commit c472969

Browse files
committed
Specs added
1 parent e4dbba3 commit c472969

18 files changed

+2118
-81
lines changed

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ group :development, :test do
8080
gem 'database_cleaner'
8181
# Use to track how much code has been tested
8282
gem 'simplecov', '~> 0.7.1'
83+
end
84+
85+
group :test do
8386
# Webmock to stub http requests
8487
gem 'webmock'
8588
# VCR to record the responses from web and replay them when needed
+21-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class SpreadsheetsController < ApplicationController
2-
include GoogleSpreadsheets
2+
require 'google'
3+
include Google::Spreadsheets
34

45
def index
56
@spreadsheets = Spreadsheet.all.to_a
@@ -15,18 +16,23 @@ def new
1516

1617
if spreadsheet.save
1718
# Spreadsheets from google
18-
@spreadsheets = get_spreadsheets(spreadsheet)
19+
@spreadsheets = list(spreadsheet)
1920
@token = spreadsheet.access_token
2021
else
2122
# Handle if data does not get saved
23+
@msg = 'Getting same access token. Try deleting '
2224
end
2325
end
2426

2527
def edit
2628
token = spreadsheet_params['token']
2729
spreadsheet = Spreadsheet.where(access_token: token)[0]
28-
spreadsheet.add_spreadsheet_credentials(spreadsheet_params)
29-
spreadsheet.save
30+
31+
if spreadsheet.add_spreadsheet_credentials(spreadsheet_params)
32+
spreadsheet.save
33+
else
34+
@error = 'Already Present'
35+
end
3036

3137
@spreadsheets = Spreadsheet.all.to_a
3238

@@ -35,8 +41,9 @@ def edit
3541

3642
def update
3743
spreadsheet = Spreadsheet.find(params['id'])
38-
@worksheet = get_worksheets(spreadsheet)
44+
@worksheet = worksheets(spreadsheet)
3945
User.add_users_from_worksheet(@worksheet)
46+
4047
redirect_to users_path
4148
end
4249

@@ -45,15 +52,19 @@ def destroy
4552
redirect_to spreadsheets_path
4653
end
4754

48-
def spreadsheet_params
49-
params.permit(:title, :id, :token)
50-
end
51-
5255
def failure
5356
if params['message'].match('access_denied')
54-
@msg = "Account integration Failed. User Refused to grant permissions"
57+
@msg = 'Account integration Failed. User Refused to grant permissions'
5558
end
5659
@spreadsheets = Spreadsheet.all.to_a
5760
render action: 'index'
5861
end
62+
63+
#################################
64+
private
65+
#################################
66+
67+
def spreadsheet_params
68+
params.permit(:title, :id, :token)
69+
end
5970
end

app/helpers/callbacks_helper.rb

-2
This file was deleted.

app/models/spreadsheet.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Spreadsheet
77
field :expires_at, type: Time
88
field :access_token, type: String
99

10-
validates :access_token, uniqueness: true
10+
validates :access_token, presence: true, uniqueness: true
1111

1212
def add_tokens(tokens = {})
1313
self['access_token'] = tokens.fetch('token')
@@ -16,7 +16,6 @@ def add_tokens(tokens = {})
1616
end
1717

1818
def add_spreadsheet_credentials(credentials = {})
19-
#binding.pry
2019
self['spreadsheet_id'] = credentials['id']
2120
self['spreadsheet_title'] = credentials['title']
2221
end

app/views/spreadsheets/index.html.haml

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= @msg if @msg
22
%h4
3-
=link_to 'Add New Spreadsheet', 'auth/google/'
3+
=link_to 'Add New Spreadsheet', '/auth/google/'
44

55
%table.table.table-striped
66
%tr.info
@@ -19,10 +19,14 @@
1919
%td
2020
= i = i + 1
2121
%td
22-
- if ss['spreadsheet_title']
22+
-if ss['spreadsheet_title']
2323
=ss['spreadsheet_title']
2424
-else
25-
=link_to 'Spreadsheet not initialised', new_spreadsheet_path(access_token: ss['access_token'])
25+
=link_to 'Get Spreadsheet', new_spreadsheet_path(access_token: ss['access_token'])
26+
-if @error
27+
(
28+
=@error
29+
)
2630
%td
2731
=link_to 'Delete', spreadsheet_path(ss), method: :delete
2832
-if ss['spreadsheet_title']

app/views/spreadsheets/new.html.haml

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
%br
2-
-count = @spreadsheets.count
3-
=count
4-
-count.times do |i|
5-
=link_to @spreadsheets[i]['title'], edit_spreadsheet_path(@spreadsheets[i]['id'], title: @spreadsheets[i]['title'], token: @token) if @spreadsheets[i]['mimeType'].include?('spreadsheet')
6-
%br
1+
-if @msg
2+
=@msg
3+
-else
4+
-count = @spreadsheets.count
5+
-count.times do |i|
6+
=link_to @spreadsheets[i]['title'], edit_spreadsheet_path(@spreadsheets[i]['id'], title: @spreadsheets[i]['title'], token: @token) if @spreadsheets[i]['mimeType'].include?('spreadsheet')
7+
%br

config/mongoid.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ development:
4444

4545
# Raise an error when performing a #find and the document is not found.
4646
# (default: true)
47-
# raise_not_found_error: true
47+
raise_not_found_error: false
4848

4949
# Raise an error when defining a scope with the same name as an
5050
# existing method. (default: false)

lib/google.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Google
2+
module Spreadsheets
3+
def list(spreadsheet)
4+
if Time.now > spreadsheet['expires_at']
5+
spreadsheet = refresh_token spreadsheet
6+
end
7+
8+
client = Google::APIClient.new
9+
client.authorization.access_token = spreadsheet.access_token
10+
drive = client.discovered_api('drive', 'v2')
11+
12+
wks = client.execute(
13+
api_method: drive.files.list,
14+
parameters: {},
15+
headers: {'Content-Type' => 'application/json'}
16+
)
17+
(JSON.parse(wks.data.to_json))['items']
18+
end
19+
20+
def worksheets(spreadsheet, index = 0)
21+
if DateTime.now.utc > spreadsheet['expires_at']
22+
spreadsheet = refresh_token spreadsheet
23+
end
24+
25+
session = GoogleDrive.login_with_oauth(spreadsheet['access_token'])
26+
session.spreadsheet_by_key(spreadsheet['spreadsheet_id']).worksheets[index]
27+
end
28+
29+
def refresh_token(spreadsheet)
30+
client = Google::APIClient.new
31+
client.authorization.client_id = ENV['GOOGLE_ID']
32+
client.authorization.client_secret = ENV['GOOGLE_KEY']
33+
client.authorization.grant_type = 'refresh_token'
34+
client.authorization.refresh_token = spreadsheet['refresh_token']
35+
re = client.authorization.fetch_access_token!
36+
37+
spreadsheet['access_token'] = re['access_token']
38+
spreadsheet['expires_at'] = (Time.now + re['expires_in'].second)
39+
spreadsheet.save
40+
41+
spreadsheet
42+
end
43+
end
44+
end

lib/google_spreadsheets.rb

-46
This file was deleted.
+112-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,131 @@
11
require 'rails_helper'
22
require 'webmock/rspec'
3+
require 'vcr'
34

45
RSpec.describe SpreadsheetsController, :type => :controller do
5-
google_auth_response = File.new('spec/controllers/google_auth_response.json')
6+
7+
context 'GET User Permission' do
8+
context 'User accepts' do
9+
it 'redirects to new ' do
10+
VCR.use_cassette 'controllers/api-permissions' do
11+
data = {
12+
name: 'google',
13+
scope: 'userinfo.profile,userinfo.email,drive,https://spreadsheets.google.com/feeds',
14+
prompt: 'consent',
15+
access_type: 'offline',
16+
redirect_uri: 'http://localhost:8080/auth/google/callback'
17+
}
18+
RestClient.post 'https://accounts.google.com/o/oauth2/auth', data
19+
end
20+
21+
end
22+
end
23+
end
24+
625
context 'GET Index' do
726
it 'it renders the index template' do
827
get :index
928
expect(response).to render_template(:index)
1029
end
1130

31+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
1232
it 'should return all the spreadhseets' do
13-
c = SpreadsheetsController.new
14-
r = c.instance_eval{index}
15-
expect(r.count).to eq(Spreadsheet.count)
33+
sheet
34+
get :index
35+
expect(assigns(:spreadsheets)).equal?(Spreadsheet.all.to_a.count)
1636
end
1737
end
1838

1939
context 'GET New' do
20-
it 'opens authentication page on calling auth/google' do
21-
stub_request(:get, 'http://localhost:8080/auth/google/').to_return(google_auth_response)
22-
stub_request(:get, "https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367225507767-119uvbhdadqbft2kn4759rodoiivksn9.apps.googleusercontent.com&prompt=consent&redirect_uri=http://localhost:8080/auth/google/callback&response_type=code&scope=https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/drive%20https://spreadsheets.google.com/feeds&state=df37c7da59cf8f7a4899cd5a67a612e9d18946664bc98d4f").
23-
with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Cookie'=>'_lightair_session=Uy9SNDQ1LzZFandLd1g4S2VVTEptR3ZRa0RBcnNFWGtZbG1VZmNKMzc5Z1F6cDFuaHBCa2lScHcxL1FBQ044RTJZMU9iUWF2UitaVjQxcGVzeUZwS0Q3NkdXdzg2SHB6S0IrcU1sR2ExelhRRVZuWFF6MFpraktBMTNCcTM4UVZ5a01ReitYSzNYT2M0dXR1NWViUjhvVGVxUnYwRUtLREtoQUVjMkJPMW1PZG14MUVUQlN0Q2hkRUh5blVLMDZRLS1OZ3JGQXFWQmJKMGNCWGszQWoyMkNRPT0=--8345e9f7215c9ef812febd3588c505cfaef628c1', 'User-Agent'=>'Ruby'}).
24-
to_return(:status => 200, :body => "", :headers => {})
40+
it 'creates new spreadsheet when no access_token given' do
41+
VCR.use_cassette 'controllers/api-new_tokens' do
42+
request.env['omniauth.auth'] = {
43+
'credentials' => {
44+
'token' => 'ya29.QgC-5kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA',
45+
'refresh_token' => '1/DlAqfSUji69F3YuVAHSoWxWE0grR8aYSkb2OocVCNBw',
46+
'expires_at' => Time.now,
47+
'expires' => true
48+
}
49+
}
50+
51+
get :new
52+
expect(response).to render_template(:new)
53+
end
54+
end
55+
56+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
57+
it 'does not creates new spreadsheet when access_token is given' do
58+
VCR.use_cassette 'controllers/api-response' do
59+
get(:new, access_token: sheet.access_token)
60+
expect(response).to render_template(:new)
61+
end
62+
end
63+
end
64+
65+
context 'Get Edit' do
66+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
67+
it 'renders index page after executing' do
68+
get :edit, title: 'namecollection', id: sheet['spreadsheet_id'], token: sheet['access_token']
69+
70+
expect(response).to render_template(:index)
71+
end
72+
73+
it 'adds spreadsheet\'s credentials' do
74+
get :edit, title: 'namecollection', id: sheet['spreadsheet_id'], token: sheet['access_token']
75+
expect(assigns(:error)).to be(nil)
76+
end
77+
end
78+
79+
context 'Post Update' do
80+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
81+
it 'updates the spreadsheet' do
82+
VCR.use_cassette 'controllers/api-update-with-data', record: :new_episodes do
83+
post :update, id: sheet
84+
expect(response).to redirect_to users_path
85+
end
86+
end
87+
end
88+
89+
context 'Post Destroy' do
90+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
91+
it 'deletes spreadsheets from database' do
92+
sheet
93+
count1 = Spreadsheet.all.count
94+
post :destroy, id: sheet
95+
count2 = Spreadsheet.all.count
96+
97+
expect(count1).to be(count2 + 1)
98+
end
99+
100+
it 'redirects to spreadsheets index page' do
101+
post :destroy, id: sheet
102+
expect(response).to redirect_to spreadsheets_path
103+
end
104+
end
105+
106+
context 'Get Failure' do
107+
it 'renders index page when user does not accepts permissions' do
108+
get :failure, message: 'access_denied'
109+
110+
expect(response).to render_template(:index)
111+
end
112+
113+
it 'to give a failure message when it fails' do
114+
get :failure, message: 'access_denied'
115+
expect(assigns(:msg)).not_to be nil
116+
end
117+
118+
it 'not to give a failure message when it does not fails' do
119+
get :failure, message: 'access_granted'
120+
expect(assigns(:msg)).to be nil
121+
end
122+
123+
let(:sheet) { FactoryGirl.create(:spreadsheet)}
124+
it 'gets all the spreadsheets' do
125+
sheet
126+
get :failure, message: 'access_denied'
25127

26-
RestClient.get('http://localhost:8080/auth/google/')
128+
expect(assigns(:spreadsheets).count).equal?(Spreadsheet.all.count)
27129
end
28130
end
29131
end

spec/factories/spreadsheets.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Read about factories at https://github.com/thoughtbot/factory_girl
22

33
FactoryGirl.define do
4-
factory :spreadsheet do
4+
factory :spreadsheet do |s|
5+
s.spreadsheet_id {'1g2JX-w0XtcWD7yZo-K3cSHs7PhycCwhaYYmwajsLIwE'}
6+
s.spreadsheet_title {'namecollection'}
7+
s.access_token {'ya29.QgC-5kYKwAzcdh8AAABnuwXicpaXRvO_YSlv4V9J556542KazsYWEia63TlRyA'}
8+
s.refresh_token {'1/DlAqfSUji69F3YuVAHSoWxWE0grR8aYSkb2OocVCNBw'}
9+
s.expires_at {Time.now - 3600}
510
end
611
end

0 commit comments

Comments
 (0)