Commit 0385c756 authored by David F.'s avatar David F.
Browse files

Working on Google API update with VCR re-recording. #113

parent 1fbc4eb9
#
# OLIGAA - Stands for "Outil en Ligne d'Information et de Gestion des Activités
# des Associations" which is an online tool for associations to manage news,
# actors and activities.
# Copyright (C) 2010-2019 Comité Départemental du Sport Adapté de la Gironde (CDSA
# 33) <contact@cdsa33.org>
#
# This file is part of OLIGAA.
#
# OLIGAA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OLIGAA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OLIGAA. If not, see <http://www.gnu.org/licenses/>.
#
# Used for cookies store name : identifies your app
APP_ID=OLIGAA
# Used for pdf file names and other stuff
APP_NAME="OLIGAA Dev"
# Secret token to verify identity of the provided cookies
# at least 30 chars : try `rake secret`
APP_SECRET_TOKEN='cookies_secret'
# Secret used to prevent cross-site scripting attacks
# with URL store for breadcrumb
GRETEL_TRAILS_SECRET='gretel_secret'
# Google service account Oauth 2.0 client ID for Google
# Calendar synchronization
# GOOGLE_CLIENT_ID='See Keepass'
# Mainly used for ContactUs mails (usefull for SPF and DKIM)
# DEFAULT_MAIL_FROM=www@domain.tld
......@@ -34,6 +34,5 @@ GRETEL_TRAILS_SECRET='gretel_secret'
# Google service account Oauth 2.0 client ID for Google
# Calendar synchronization
# GOOGLE_CLIENT_ID=''
# GOOGLE_CLIENT_SECRET=''
# Mainly used for ContactUs mails (usefull for SPF and DKIM)
# DEFAULT_MAIL_FROM=www@domain.tld
......@@ -33,7 +33,6 @@ APP_SECRET_TOKEN='cookies_secret'
GRETEL_TRAILS_SECRET='gretel_secret'
# Google service account Oauth 2.0 client ID for Google
# Calendar synchronization
# GOOGLE_CLIENT_ID=''
# GOOGLE_CLIENT_SECRET=''
# GOOGLE_CLIENT_ID='See Keepass'
# Mainly used for ContactUs mails (usefull for SPF and DKIM)
DEFAULT_MAIL_FROM=www@test.tld
# DEFAULT_MAIL_FROM=www@test.tld
......@@ -69,8 +69,8 @@ end
group :test do
gem 'simplecov', '>= 0.4.0', require: false
gem 'webmock', '~> 3.13'
gem 'vcr'#, '~> 2.9.3'
gem 'webmock'
gem 'vcr'
gem 'timecop', '~> 0.8.0'
gem 'capybara'
gem 'launchy'
......
......@@ -150,7 +150,7 @@ GEM
railties (>= 5.0.0)
faker (2.18.0)
i18n (>= 1.6, < 2)
faraday (1.5.1)
faraday (1.7.0)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
faraday-excon (~> 1.1)
......@@ -158,6 +158,7 @@ GEM
faraday-net_http (~> 1.0)
faraday-net_http_persistent (~> 1.1)
faraday-patron (~> 1.0)
faraday-rack (~> 1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords (>= 0.0.4)
faraday-em_http (1.0.0)
......@@ -167,6 +168,7 @@ GEM
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
ffi (1.15.3)
fitvidsjs_rails (1.1.3)
railties (>= 3.1)
......@@ -181,8 +183,8 @@ GEM
ruby-progressbar (~> 1.4)
gems (1.2.0)
geocoder (1.6.7)
globalid (0.4.2)
activesupport (>= 4.2.0)
globalid (0.5.2)
activesupport (>= 5.0)
google-api-client (0.53.0)
google-apis-core (~> 0.1)
google-apis-generator (~> 0.1)
......@@ -203,7 +205,7 @@ GEM
google-apis-core (>= 0.4, < 2.a)
google-apis-discovery_v1 (~> 0.5)
thor (>= 0.20, < 2.a)
googleauth (0.16.2)
googleauth (0.17.0)
faraday (>= 0.17.3, < 2.0)
jwt (>= 1.4, < 3.0)
memoist (~> 0.16)
......@@ -232,10 +234,10 @@ GEM
addressable (~> 2.7)
leaflet-rails (1.7.0)
rails (>= 4.2.0)
listen (3.6.0)
listen (3.7.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.10.0)
loofah (2.12.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
......@@ -250,7 +252,7 @@ GEM
nokogiri (~> 1)
rake
mini_mime (1.1.0)
mini_portile2 (2.5.3)
mini_portile2 (2.6.1)
minitest (5.14.4)
momentjs-rails (2.20.1)
railties (>= 3.1)
......@@ -258,9 +260,9 @@ GEM
multipart-post (2.1.1)
mysql2 (0.5.3)
nested_form (0.3.2)
nio4r (2.5.7)
nokogiri (1.11.7)
mini_portile2 (~> 2.5.0)
nio4r (2.5.8)
nokogiri (1.12.3)
mini_portile2 (~> 2.6.1)
racc (~> 1.4)
os (1.1.1)
paperclip (6.1.0)
......@@ -280,7 +282,7 @@ GEM
sass-rails
uglifier
public_suffix (4.0.6)
puma (5.3.2)
puma (5.4.0)
nio4r (~> 2.0)
racc (1.5.2)
rack (2.2.3)
......@@ -302,7 +304,7 @@ GEM
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
rails-html-sanitizer (1.4.1)
loofah (~> 2.3)
railties (5.2.6)
actionpack (= 5.2.6)
......@@ -331,7 +333,7 @@ GEM
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-rails (5.0.1)
rspec-rails (5.0.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
railties (>= 5.2)
......@@ -340,16 +342,16 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
rubocop (1.18.3)
rubocop (1.19.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.7.0, < 2.0)
rubocop-ast (>= 1.9.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.8.0)
rubocop-ast (1.10.0)
parser (>= 3.0.1.1)
rubocop-faker (1.1.0)
faker (>= 2.12.0)
......@@ -415,19 +417,19 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webdrivers (4.6.0)
webdrivers (4.6.1)
nokogiri (~> 1.6)
rubyzip (>= 1.3.0)
selenium-webdriver (>= 3.0, < 4.0)
webmock (3.13.0)
addressable (>= 2.3.6)
webmock (3.14.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.7.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
will_paginate (3.3.0)
will_paginate (3.3.1)
xpath (3.2.0)
nokogiri (~> 1.8)
......@@ -489,7 +491,7 @@ DEPENDENCIES
vcr
web-console
webdrivers
webmock (~> 3.13)
webmock
will_paginate
BUNDLED WITH
......
......@@ -59,6 +59,22 @@ Avant de démarrer l'application pour la première fois, vous devez :
Vous pouvez alors vous connecter avec l'identifiant `admin` et le mot de passe noté plus tôt.
#### Authentification des API Google
La synchronisation des agendas Google nécessite une authentification par [compte de service](https://developers.google.com/identity/protocols/oauth2/service-account) G-suite. Pour ce faire, vous devez :
1. créer ce compte dans l'interface d'[administration des comptes de service](https://console.developers.google.com/iam-admin/serviceaccounts) ;
1. dans le menu "Gérer les clés" de ce compte :
1. créer un nouvelle clé ;
1. télécharger une clé de type `P12` ;
* Surtout, gardez-la précieusement, vous ne pourrez pas la télécharger ultérieurement ;
1. la renommer/déplacer vers `config/google-oauth2/keyfile.p12` ;
1. dans le menu "Gérer les détails" de ce compte :
1. activer la délégation au niveau du domaine pour que ce compte de service puisse accéder aux agendas de tous les comptes ;
1. dans la liste des comptes de service :
1. Afficher l'ID client et le copier ;
1. Coller cette valeur dans la variable d'environnement `GOOGLE_CLIENT_ID`.
#### Exécution des tests
Tout nouveau développement doit être accompagné de son jeu de tests, qui garantira qu'aucune régression ne pourra se glisser dans le futur. Pour leur écriture, nous utilisons la bibliothèque [RSpec](http://rspec.info/) pour tous les types de test. Pour vous assurer que tout fonctionne comme attendu, vous pouvez lancer la suite de tests complète et vérifier que tout se déroule sans erreur, grâce à la commande habituelle :
......
......@@ -35,18 +35,18 @@ module GoogleAgenda
end
def initialize(cal=nil)
@cal = cal
@cal = cal
@calendar_api = GoogleAgenda::Service.instance.calendar_api
end
def update(obj)
@cal['summary'] = obj.title
@cal['description'] = obj.description
calendar = Google::Apis::CalendarV3::Calendar.new(
summary: @cal['summary'],
description: @cal['description'],
).to_json
)
result = @calendar_api.patch_calendar(@cal['id'], calendar)
......@@ -58,14 +58,14 @@ module GoogleAgenda
def self.create_calendar calendar_api
calendar = Google::Apis::CalendarV3::Calendar.new(
summary: "TEMP #{DateTime.now}"
).to_json
)
result = calendar_api.insert_calendar(calendar)
JSON.parse(result.body)
end
def self.find_calendar_by_id id, calendar_api
result = calendar_api.get_calendar(id).to_json
result = calendar_api.get_calendar(id)
JSON.parse(result.body)
end
......
......@@ -52,13 +52,13 @@ module GoogleAgenda
if calendar_api = GoogleAgenda::Service.instance.calendar_api
event = Google::Apis::CalendarV3::Event.new(
event = Google::Apis::CalendarV3::Event.new(
summary: record.title,
description: record.description,
location: record.location,
start: Google::Apis::CalendarV3::EventDateTime.new(date_time: record.begin_at),
end: Google::Apis::CalendarV3::EventDateTime.new(date_time: record.end_at),
).to_json
)
result = calendar_api.insert_event(calendar_id, event)
data = JSON.parse(result.body)
......
......@@ -21,9 +21,7 @@
# along with OLIGAA. If not, see <http://www.gnu.org/licenses/>.
#
require 'google/apis/calendar_v3'
require 'googleauth'
require 'signet/oauth_2/client'
require 'fileutils'
module GoogleAgenda
class Service
......@@ -36,7 +34,7 @@ module GoogleAgenda
def calendar_api
return @calendar_api unless @calendar_api.nil?
key = OpenSSL::PKCS12.new(File.read(KEY_FILE), "notasecret")
key = OpenSSL::PKCS12.new(File.read(KEY_FILE), "notasecret").key
@calendar_api = Google::Apis::CalendarV3::CalendarService.new
@calendar_api.client_options.application_name = "#{ENV['APP_NAME']} - #{Rails.env}",
......
......@@ -29,18 +29,18 @@ describe GoogleAgenda::Calendar, :vcr do
end
before(:each) do
@settings.update_attribute :gcal_user, 'test@gcal.org'
@settings.update_attribute :gcal_user, 'test-gcal@cdsa33.org'
pkcs12_content = SecureRandom.uuid
allow(File).to receive(:read)
.with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
.and_return(pkcs12_content)
pkcs12_file = instance_double(OpenSSL::PKCS12)
allow(OpenSSL::PKCS12).to receive(:new)
.with(pkcs12_content, 'notasecret')
.and_return(pkcs12_file)
allow(pkcs12_file).to receive(:key)
.and_return(@key)
# pkcs12_content = SecureRandom.uuid
# allow(File).to receive(:read)
# .with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
# .and_return(pkcs12_content)
# pkcs12_file = instance_double(OpenSSL::PKCS12)
# allow(OpenSSL::PKCS12).to receive(:new)
# .with(pkcs12_content, 'notasecret')
# .and_return(pkcs12_file)
# allow(pkcs12_file).to receive(:key)
# .and_return(@key)
GoogleAgenda::Service.reset!
end
......
......@@ -36,18 +36,18 @@ describe GoogleAgenda::Event, :vcr do
end
before(:each) do
@settings.update_attribute :gcal_user, 'test@gcal.org'
pkcs12_content = SecureRandom.uuid
allow(File).to receive(:read)
.with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
.and_return(pkcs12_content)
pkcs12_file = instance_double(OpenSSL::PKCS12)
allow(OpenSSL::PKCS12).to receive(:new)
.with(pkcs12_content, 'notasecret')
.and_return(pkcs12_file)
allow(pkcs12_file).to receive(:key)
.and_return(@key)
@settings.update_attribute :gcal_user, 'test-gcal@cdsa33.org'
# pkcs12_content = SecureRandom.uuid
# allow(File).to receive(:read)
# .with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
# .and_return(pkcs12_content)
# pkcs12_file = instance_double(OpenSSL::PKCS12)
# allow(OpenSSL::PKCS12).to receive(:new)
# .with(pkcs12_content, 'notasecret')
# .and_return(pkcs12_file)
# allow(pkcs12_file).to receive(:key)
# .and_return(@key)
GoogleAgenda::Service.reset!
end
......
......@@ -31,18 +31,18 @@ describe GoogleAgenda::Service do
end
before(:each) do
@settings.update_attribute :gcal_user, 'test@gcal.org'
@settings.update_attribute :gcal_user, 'test-gcal@cdsa33.org'
pkcs12_content = SecureRandom.uuid
allow(File).to receive(:read)
.with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
.and_return(pkcs12_content)
pkcs12_file = instance_double(OpenSSL::PKCS12)
allow(OpenSSL::PKCS12).to receive(:new)
.with(pkcs12_content, 'notasecret')
.and_return(pkcs12_file)
allow(pkcs12_file).to receive(:key)
.and_return(@key)
# pkcs12_content = SecureRandom.uuid
# allow(File).to receive(:read)
# .with(File.join(Rails.root, "config", "google-oauth2", "keyfile.p12"))
# .and_return(pkcs12_content)
# pkcs12_file = instance_double(OpenSSL::PKCS12)
# allow(OpenSSL::PKCS12).to receive(:new)
# .with(pkcs12_content, 'notasecret')
# .and_return(pkcs12_file)
# allow(pkcs12_file).to receive(:key)
# .and_return(@key)
GoogleAgenda::Service.reset!
end
......@@ -53,7 +53,7 @@ describe GoogleAgenda::Service do
# When
result = subject.calendar_api
# Then
expect(result).to respond_to(:calendars)
expect(result).to respond_to(:get_calendar)
end
it 'returns the same calendar API client multiple times' do
......
......@@ -42,6 +42,7 @@ require 'faker'
require 'webdrivers/geckodriver'
require 'timecop'
require 'webmock'
require 'webmock/rspec'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
......
Webdrivers::Geckodriver.update
# Webdrivers::Geckodriver.update
VCR.configure do |c|
c.allow_http_connections_when_no_cassette = true
c.cassette_library_dir = 'spec/cassettes'
c.hook_into :webmock
c.configure_rspec_metadata!
# Uncomment the following lines to help debugging
c.debug_logger = $stdout
# c.default_cassette_options = { :match_requests_on => [:method, :uri, :body] }
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment