|
1 | 1 | require 'rails_helper'
|
2 | 2 | require 'webmock/rspec'
|
| 3 | +require 'vcr' |
3 | 4 |
|
4 | 5 | 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 | + |
6 | 25 | context 'GET Index' do
|
7 | 26 | it 'it renders the index template' do
|
8 | 27 | get :index
|
9 | 28 | expect(response).to render_template(:index)
|
10 | 29 | end
|
11 | 30 |
|
| 31 | + let(:sheet) { FactoryGirl.create(:spreadsheet)} |
12 | 32 | 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) |
16 | 36 | end
|
17 | 37 | end
|
18 | 38 |
|
19 | 39 | 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' |
25 | 127 |
|
26 |
| - RestClient.get('http://localhost:8080/auth/google/') |
| 128 | + expect(assigns(:spreadsheets).count).equal?(Spreadsheet.all.count) |
27 | 129 | end
|
28 | 130 | end
|
29 | 131 | end
|
0 commit comments