From d2441cbf57e50895ac3b40534873c5d529cb3c4f Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Wed, 1 Jul 2015 20:53:25 -0500 Subject: [PATCH] VRFS-3276 : Test new calendar features. Use icalendar gem in test mode only to more deeply verify calendar in strict mode. --- db/Gemfile.lock | 3 - ruby/Gemfile | 1 + ruby/spec/jam_ruby/calendar_manager_spec.rb | 84 +++++++++++++++++---- 3 files changed, 71 insertions(+), 17 deletions(-) diff --git a/db/Gemfile.lock b/db/Gemfile.lock index 8d6d039c2..eb6aee107 100644 --- a/db/Gemfile.lock +++ b/db/Gemfile.lock @@ -16,6 +16,3 @@ PLATFORMS DEPENDENCIES pg_migrate (= 0.1.13) - -BUNDLED WITH - 1.10.3 diff --git a/ruby/Gemfile b/ruby/Gemfile index d46e44e82..0fa0fcb34 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -64,6 +64,7 @@ group :test do gem 'rspec-prof' gem 'time_difference' gem 'byebug' + gem 'icalendar' end # Specify your gem's dependencies in jam_ruby.gemspec diff --git a/ruby/spec/jam_ruby/calendar_manager_spec.rb b/ruby/spec/jam_ruby/calendar_manager_spec.rb index d3851d0d7..f68106264 100644 --- a/ruby/spec/jam_ruby/calendar_manager_spec.rb +++ b/ruby/spec/jam_ruby/calendar_manager_spec.rb @@ -1,29 +1,85 @@ require 'spec_helper' +require 'icalendar' + describe CalendarManager do + CALENDAR_NAME="Test Cal" before :all do - genre1 = FactoryGirl.create(:genre) - @creator = FactoryGirl.create(:user) - @music_session = FactoryGirl.create(:music_session, :creator => @creator, :description => "Hello Session", :genre => genre1) - @music_session.reload + @genre1 = FactoryGirl.create(:genre) @calendar_manager = JamRuby::CalendarManager.new + + # Time resolution is seconds: + @start = Time.at(Time.now.utc.to_i) + @stop =(@start+1.hours) end before(:each) do end - it "can create calendar feed for user" do - ics = @calendar_manager.create_ics_feed(@creator) - puts "ICS: #{ics}" - # Basic format checking...there are some online tools that - # check a lot more, but no ruby libs that I could find: - lines = ics.split("\r\n") - lines.should have(11).items - lines.first.should eq("BEGIN:VCALENDAR") - lines.last.should eq("END:VCALENDAR") - lines[-2].should eq("END:VEVENT") + describe "with music sessions" do + before :all do + @creator = FactoryGirl.create(:user) + @music_session = FactoryGirl.create(:music_session, :creator => @creator, :description => CALENDAR_NAME, :genre => @genre1, :scheduled_start=>@start, :scheduled_duration=>3600) + @music_session.reload + end + + it "validator detects bad calendar" do + lambda{verify_ical("Bad medicine calendar")} + .should raise_error(RuntimeError) + end + + it "can create calendar feed" do + ics = @calendar_manager.create_ics_feed(@creator) + # Basic format checking...there are some online tools that + # check a lot more, but no ruby libs that I could find: + lines = ics.split("\r\n") + lines.should have(12).items + lines.first.should eq("BEGIN:VCALENDAR") + lines.last.should eq("END:VCALENDAR") + lines[-2].should eq("END:VEVENT") + verify_ical(ics) + end end + describe "with manual calendars" do + before :all do + @creator = FactoryGirl.create(:user) + @creator.calendars<CALENDAR_NAME, :description=>"This is a test", :start_at=>(@start), :end_at=>@stop, :trigger_delete=>false}) + end + + it "can create calendar feed" do + #pending "foobar" + ics = @calendar_manager.create_ics_feed(@creator) + + # Basic format checking...there are some online tools that + # check a lot more, but no ruby libs that I could find: + lines = ics.split("\r\n") + lines.should have(12).items + lines.first.should eq("BEGIN:VCALENDAR") + lines.last.should eq("END:VCALENDAR") + lines[-2].should eq("END:VEVENT") + verify_ical(ics) + end + end + + def verify_ical(ics) + strict_parser = Icalendar::Parser.new(ics, true) + cals = strict_parser.parse + cals.should_not be_nil + cals.should have(1).items + + cal = cals.first + cal.should_not be_nil + cal.events.should have(1).items + event = cal.events.first + event.should_not be_nil + + event.summary.should eq("JamKazam Session #{CALENDAR_NAME}") + event.dtstart.to_i.should_not be_nil + event.dtend.to_i.should_not be_nil + (event.dtstart).to_time.utc.to_i.should eq(@start.to_i) + (event.dtend).to_time.utc.to_i.should eq(@stop.to_i) + end end