Skip to content

Commit

Permalink
fixing google doc deletion. refs #5796
Browse files Browse the repository at this point in the history
Change-Id: I9ebf2875367cafe1fd9f8d96174d1e62fd6bd542
Reviewed-on: https://gerrit.instructure.com/6485
Tested-by: Hudson <[email protected]>
Reviewed-by: Brian Palmer <[email protected]>
  • Loading branch information
jtolio committed Oct 31, 2011
1 parent b0f4c0c commit 98e18ba
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/google_doc_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def alternate_url
end

def edit_url
"http://docs.google.com/feeds/documents/private/full/#{@document_id}"
"https://docs.google.com/feeds/documents/private/full/#{@document_id}"
end

def content_type
Expand Down
145 changes: 145 additions & 0 deletions spec/lib/google_docs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas 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, version 3 of the License.
#
# Canvas 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 this program. If not, see <http://www.gnu.org/licenses/>.
#

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')

class GoogleDocsTest
include GoogleDocs

attr_accessor :user

def initialize(user)
@user = user
end
end

describe GoogleDocs do

before(:each) do
@user = User.create!
PluginSetting.create!(:name => 'google_docs', :settings => YAML.load_file(RAILS_ROOT + "/config/google_docs.yml")[RAILS_ENV])
UserService.register(
:service => "google_docs",
:token => GoogleDocs.config["test_user_token"],
:secret => GoogleDocs.config["test_user_secret"],
:user => @user,
:service_domain => "google.com",
:service_user_id => GoogleDocs.config["test_user_id"],
:service_user_name => GoogleDocs.config["test_user_name"]
)
@lib = GoogleDocsTest.new @user
end

it 'should add and remove documents' do
unless use_remote_services
consumer = mock()
OAuth::Consumer.expects(:new).with(GoogleDocs.config["api_key"],
GoogleDocs.config["secret_key"], {:signature_method => 'HMAC-SHA1',
:request_token_path => '/accounts/OAuthGetRequestToken',
:site => 'https://www.google.com',
:authorize_path => '/accounts/OAuthAuthorizeToken',
:access_token_path => '/accounts/OAuthGetAccessToken'}).returns(consumer)
access_token = mock()
OAuth::AccessToken.expects(:new).with(consumer,
GoogleDocs.config["test_user_token"],
GoogleDocs.config["test_user_secret"]).returns(access_token)
list_response = mock()
access_token.expects(:get).with('https://docs.google.com/feeds/documents/private/full').returns(list_response)
list_response.expects(:body).returns("<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'><id>https://docs.google.com/feeds/documents/private/full</id><updated>2011-10-31T22:23:05.864Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#item' label='item'/><title type='text'>Available Documents - [email protected]</title><link rel='alternate' type='text/html' href='http://docs.google.com'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/batch'/><link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><author><name>instructure.test.2011</name><email>[email protected]</email></author><openSearch:totalResults>0</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex></feed>")
end

document_id_list = @lib.google_doc_list.files.map(&:document_id)

unless use_remote_services
consumer = mock()
OAuth::Consumer.expects(:new).with(GoogleDocs.config["api_key"],
GoogleDocs.config["secret_key"], {:signature_method => 'HMAC-SHA1',
:request_token_path => '/accounts/OAuthGetRequestToken',
:site => 'https://www.google.com',
:authorize_path => '/accounts/OAuthAuthorizeToken',
:access_token_path => '/accounts/OAuthGetAccessToken'}).returns(consumer)
access_token = mock()
OAuth::AccessToken.expects(:new).with(consumer,
GoogleDocs.config["test_user_token"],
GoogleDocs.config["test_user_secret"]).returns(access_token)
create_response = mock()
access_token.expects(:post).with('https://docs.google.com/feeds/documents/private/full',
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<entry xmlns=\"http://www.w3.org/2005/Atom\">\n <title>test document</title>\n <category label=\"document\" scheme=\"http://schemas.google.com/g/2005#kind\" term=\"http://schemas.google.com/docs/2007#document\"/>\n</entry>\n", {'Content-Type' => 'application/atom+xml'}).returns(create_response)
create_response.expects(:body).returns("<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'><id>https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA</id><published>2011-10-31T22:23:06.375Z</published><updated>2011-10-31T22:23:06.993Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#document' label='document'/><category scheme='http://schemas.google.com/g/2005/labels' term='http://schemas.google.com/g/2005/labels#viewed' label='viewed'/><title type='text'>test document</title><content type='text/html' src='https://docs.google.com/feeds/download/documents/export/Export?id=1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/><link rel='alternate' type='text/html' href='https://docs.google.com/document/d/1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/edit?hl=en_US'/><link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/><link rel='edit' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/gug1bunq'/><link rel='edit-media' type='text/html' href='https://docs.google.com/feeds/media/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/gug1bunq'/><author><name>instructure.test.2011</name><email>[email protected]</email></author><gd:resourceId>document:1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA</gd:resourceId><gd:lastModifiedBy><name>instructure.test.2011</name><email>[email protected]</email></gd:lastModifiedBy><gd:lastViewed>2011-10-31T22:23:06.652Z</gd:lastViewed><gd:quotaBytesUsed>0</gd:quotaBytesUsed><docs:writersCanInvite value='true'/><gd:feedLink rel='http://schemas.google.com/acl/2007#accessControlList' href='https://docs.google.com/feeds/acl/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/></entry>")
end

new_document = @lib.google_docs_create_doc "test document"
document_id_list.include?(new_document.document_id).should be_false

unless use_remote_services
consumer = mock()
OAuth::Consumer.expects(:new).with(GoogleDocs.config["api_key"],
GoogleDocs.config["secret_key"], {:signature_method => 'HMAC-SHA1',
:request_token_path => '/accounts/OAuthGetRequestToken',
:site => 'https://www.google.com',
:authorize_path => '/accounts/OAuthAuthorizeToken',
:access_token_path => '/accounts/OAuthGetAccessToken'}).returns(consumer)
access_token = mock()
OAuth::AccessToken.expects(:new).with(consumer,
GoogleDocs.config["test_user_token"],
GoogleDocs.config["test_user_secret"]).returns(access_token)
list_response = mock()
access_token.expects(:get).with('https://docs.google.com/feeds/documents/private/full').returns(list_response)
list_response.expects(:body).returns("<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'><id>https://docs.google.com/feeds/documents/private/full</id><updated>2011-10-31T22:23:07.920Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#item' label='item'/><title type='text'>Available Documents - [email protected]</title><link rel='alternate' type='text/html' href='http://docs.google.com'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/batch'/><link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><author><name>instructure.test.2011</name><email>[email protected]</email></author><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><entry><id>https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA</id><published>2011-10-31T22:23:06.375Z</published><updated>2011-10-31T22:23:06.993Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#document' label='document'/><category scheme='http://schemas.google.com/g/2005/labels' term='http://schemas.google.com/g/2005/labels#viewed' label='viewed'/><title type='text'>test document</title><content type='text/html' src='https://docs.google.com/feeds/download/documents/export/Export?id=1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/><link rel='alternate' type='text/html' href='https://docs.google.com/document/d/1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/edit?hl=en_US'/><link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/><link rel='edit' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/gug1buow'/><link rel='edit-media' type='text/html' href='https://docs.google.com/feeds/media/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA/gug1buow'/><author><name>instructure.test.2011</name><email>[email protected]</email></author><gd:resourceId>document:1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA</gd:resourceId><gd:lastModifiedBy><name>instructure.test.2011</name><email>[email protected]</email></gd:lastModifiedBy><gd:lastViewed>2011-10-31T22:23:07.015Z</gd:lastViewed><gd:quotaBytesUsed>0</gd:quotaBytesUsed><docs:writersCanInvite value='true'/><gd:feedLink rel='http://schemas.google.com/acl/2007#accessControlList' href='https://docs.google.com/feeds/acl/private/full/document%3A1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA'/></entry></feed>")
end

@lib.google_doc_list.files.map(&:document_id).include?(new_document.document_id).should be_true

unless use_remote_services
consumer = mock()
OAuth::Consumer.expects(:new).with(GoogleDocs.config["api_key"],
GoogleDocs.config["secret_key"], {:signature_method => 'HMAC-SHA1',
:request_token_path => '/accounts/OAuthGetRequestToken',
:site => 'https://www.google.com',
:authorize_path => '/accounts/OAuthAuthorizeToken',
:access_token_path => '/accounts/OAuthGetAccessToken'}).returns(consumer)
access_token = mock()
OAuth::AccessToken.expects(:new).with(consumer,
GoogleDocs.config["test_user_token"],
GoogleDocs.config["test_user_secret"]).returns(access_token)
access_token.expects(:delete).with('https://docs.google.com/feeds/documents/private/full/document:1HJoN38KHlnu32B5z_THgchnTMUbj7dgs8P-Twrm38cA', {'GData-Version' => '2', 'If-Match' => '*'}).returns(mock())
end

@lib.google_docs_delete_doc new_document

unless use_remote_services
consumer = mock()
OAuth::Consumer.expects(:new).with(GoogleDocs.config["api_key"],
GoogleDocs.config["secret_key"], {:signature_method => 'HMAC-SHA1',
:request_token_path => '/accounts/OAuthGetRequestToken',
:site => 'https://www.google.com',
:authorize_path => '/accounts/OAuthAuthorizeToken',
:access_token_path => '/accounts/OAuthGetAccessToken'}).returns(consumer)
access_token = mock()
OAuth::AccessToken.expects(:new).with(consumer,
GoogleDocs.config["test_user_token"],
GoogleDocs.config["test_user_secret"]).returns(access_token)
list_response = mock()
access_token.expects(:get).with('https://docs.google.com/feeds/documents/private/full').returns(list_response)
list_response.expects(:body).returns("<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'><id>https://docs.google.com/feeds/documents/private/full</id><updated>2011-10-31T22:23:09.244Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#item' label='item'/><title type='text'>Available Documents - [email protected]</title><link rel='alternate' type='text/html' href='http://docs.google.com'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full/batch'/><link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/documents/private/full'/><author><name>instructure.test.2011</name><email>[email protected]</email></author><openSearch:totalResults>0</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex></feed>")
end

@lib.google_doc_list.files.map(&:document_id).include?(new_document.document_id).should be_false
end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def outside_transaction?
Setting.reset_cache!
end

def use_remote_services; ENV['ACTUALLY_TALK_TO_REMOTE_SERVICES'].to_i > 0; end

def account_with_cas(opts={})
@account = opts[:account]
@account ||= Account.create!
Expand Down

0 comments on commit 98e18ba

Please sign in to comment.