Permalink
Browse files

Google::Calendar#relaod renew connection to google calendar

removed trailing whitespace in test and fix test alignment
  • Loading branch information...
1 parent 4e2abb3 commit dbb90881fa0510ed669475032c05f170232dae45 @sbounmy sbounmy committed Nov 10, 2011
Showing with 100 additions and 71 deletions.
  1. +31 −7 lib/google/calendar.rb
  2. +69 −64 test/test_google_calendar.rb
View
@@ -27,14 +27,18 @@ class Calendar
# # Specify the app_name
# Calendar.new(:username => 'some.guy@gmail.com', :password => 'ilovepie!', :app_name => 'mycompany.com-googlecalendar-integration')
#
+
+ # Calendar attributes
+ attr_accessor :username, :password, :app_name, :auth_url, :connection, :calendar
+
def initialize(params)
- username = params[:username]
- password = params[:password]
- @calendar = params[:calendar]
- app_name = params[:app_name]
- auth_url = params[:auth_url]
+ self.username = params[:username]
+ self.password = params[:password]
+ self.calendar = params[:calendar]
+ self.app_name = params[:app_name]
+ self.auth_url = params[:auth_url]
- @connection = Connection.new(:username => username,
+ self.connection = Connection.new(:username => username,
:password => password,
:app_name => app_name,
:auth_url => auth_url)
@@ -134,6 +138,27 @@ def delete_event(event)
@connection.send(Addressable::URI.parse(events_url + "/#{event.id}"), :delete)
end
+ # Explicitly reload the connection to google calendar
+ #
+ # Examples
+ # class User
+ # def calendar
+ # @calendar ||= Google::Calendar.new :username => "foo@gmail.com", :password => "bar"
+ # end
+ # end
+ # user = User.new
+ # 2.times { user.calendar } #only one HTTP authentication request to google
+ # user.calendar.reload #new HTTP authentication request to google
+ #
+ # Returns Google::Calendar instance
+ def reload
+ self.connection = Connection.new(:username => username,
+ :password => password,
+ :app_name => app_name,
+ :auth_url => auth_url)
+ self
+ end
+
protected
def event_lookup(query_string = '') #:nodoc:
@@ -179,7 +204,6 @@ def setup_event(event) #:nodoc:
event.save
event
end
-
end
end
@@ -31,76 +31,76 @@ class TestGoogleCalendar < Test::Unit::TestCase
Calendar.new(:username => 'some.one@gmail.com', :password => 'wrong-password')
end
end
-
- should "login properly with an app_name" do
+
+ should "login properly with an app_name" do
assert_nothing_thrown do
- Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :app_name => 'northworld.com-googlecalendar-integration'
+ Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :app_name => 'northworld.com-googlecalendar-integration'
)
end
end
-
- should "catch login with invalid app_name" do
- @http_mock.stubs(:kind_of?).with(Net::HTTPForbidden).returns(true)
- @http_mock.stubs(:body).returns('Error=BadAuthentication')
- assert_raise(HTTPAuthorizationFailed) do
- Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :app_name => 'northworld.com-silly-cal'
- )
- end
- end
-
- should "login properly with an auth_url" do
- assert_nothing_thrown do
- Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :auth_url => "https://www.google.com/accounts/ClientLogin"
- )
- end
+
+ should "catch login with invalid app_name" do
+ @http_mock.stubs(:kind_of?).with(Net::HTTPForbidden).returns(true)
+ @http_mock.stubs(:body).returns('Error=BadAuthentication')
+ assert_raise(HTTPAuthorizationFailed) do
+ Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :app_name => 'northworld.com-silly-cal'
+ )
end
+ end
- should "catch login with invalid auth_url" do
- @http_mock.stubs(:kind_of?).with(Net::HTTPForbidden).returns(true)
- @http_mock.stubs(:body).returns('Error=BadAuthentication')
- assert_raise(HTTPAuthorizationFailed) do
- Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :auth_url => "https://www.google.com/accounts/ClientLogin/waffles"
- )
- end
- end
-
- should "login properly with a calendar" do
- assert_nothing_thrown do
- cal = Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :calendar => "Little Giants")
-
- #mock calendar list request
- calendar_uri = mock("get calendar uri")
- Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/default/allcalendars/full").once.returns(calendar_uri)
- Connection.any_instance.expects(:send).with(calendar_uri, :get).once.returns(mock("response", :body => get_mock_body('list_calendars.xml')))
-
- #mock events list request
- events_uri = mock("get events uri")
- Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com/private/full").once.returns(events_uri)
- Connection.any_instance.expects(:send).with(events_uri, :get).once.returns(mock("response", :body => get_mock_body('events.xml')))
-
- cal.events
- end
- end
-
- should "catch login with invalid calendar" do
-
- assert_raise(InvalidCalendar) do
- cal = Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
- :calendar => "invalid calendar")
-
- #mock calendar list request
- calendar_uri = mock("get calendar uri")
- Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/default/allcalendars/full").once.returns(calendar_uri)
- Connection.any_instance.expects(:send).with(calendar_uri, :get).once.returns(mock("response", :body => get_mock_body('list_calendars.xml')))
-
- cal.events
- end
- end
+ should "login properly with an auth_url" do
+ assert_nothing_thrown do
+ Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :auth_url => "https://www.google.com/accounts/ClientLogin"
+ )
+ end
+ end
+
+ should "catch login with invalid auth_url" do
+ @http_mock.stubs(:kind_of?).with(Net::HTTPForbidden).returns(true)
+ @http_mock.stubs(:body).returns('Error=BadAuthentication')
+ assert_raise(HTTPAuthorizationFailed) do
+ Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :auth_url => "https://www.google.com/accounts/ClientLogin/waffles"
+ )
+ end
+ end
+
+ should "login properly with a calendar" do
+ assert_nothing_thrown do
+ cal = Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :calendar => "Little Giants")
+
+ #mock calendar list request
+ calendar_uri = mock("get calendar uri")
+ Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/default/allcalendars/full").once.returns(calendar_uri)
+ Connection.any_instance.expects(:send).with(calendar_uri, :get).once.returns(mock("response", :body => get_mock_body('list_calendars.xml')))
+
+ #mock events list request
+ events_uri = mock("get events uri")
+ Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/rf1c66uld6dgk2t5lh43svev6g%40group.calendar.google.com/private/full").once.returns(events_uri)
+ Connection.any_instance.expects(:send).with(events_uri, :get).once.returns(mock("response", :body => get_mock_body('events.xml')))
+
+ cal.events
+ end
+ end
+
+ should "catch login with invalid calendar" do
+
+ assert_raise(InvalidCalendar) do
+ cal = Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret',
+ :calendar => "invalid calendar")
+
+ #mock calendar list request
+ calendar_uri = mock("get calendar uri")
+ Addressable::URI.expects(:parse).with("https://www.google.com/calendar/feeds/default/allcalendars/full").once.returns(calendar_uri)
+ Connection.any_instance.expects(:send).with(calendar_uri, :get).once.returns(mock("response", :body => get_mock_body('list_calendars.xml')))
+
+ cal.events
+ end
+ end
end # login context
@@ -109,6 +109,11 @@ class TestGoogleCalendar < Test::Unit::TestCase
@calendar = Calendar.new(:username => 'some.one@gmail.com', :password => 'super-secret')
end
+ should "reload connection" do
+ old_connection = @calendar.connection
+ assert_not_equal old_connection, @calendar.reload.connection
+ end
+
should "find all events" do
@http_mock.stubs(:body).returns( get_mock_body("events.xml") )
assert_equal @calendar.events.length, 3

0 comments on commit dbb9088

Please sign in to comment.