require File.dirname(__FILE__) + '/test_helper.rb' class ViddlerTest < Test::Unit::TestCase class KeyRequired < Exception def message 'In order to run this test, insert working Viddler API key inside API_KEY constant.' end end class CredentialsRequired < Exception def message 'In order to run this test, insert working Viddler username and password inside LOGIN and PASSWORD constants.' end end begin load 'api_settings.rb' raise LoadError unless defined?(API_KEY) raise LoadError unless defined?(LOGIN) raise LoadError unless defined?(PASSWORD) raise LoadError unless defined?(TEST_VIDEO_FILE_PATH) rescue LoadError puts "\n" puts "To run tests, create an test/api_settings.rb file with the following constants:\n\n" puts "API_KEY = 'your_viddler_api_key'" puts "LOGIN = 'your_viddler_login'" puts "PASSWORD = 'your_viddler_password'" puts "TEST_VIDEO_FILE_PATH = '/path/to/video/file'" puts "\n"*2 exit end def setup raise KeyRequired unless API_KEY @viddler = Viddler::Base.new(API_KEY, LOGIN, PASSWORD) end def test_should_authenticate credentials_required @viddler.authenticate assert @viddler.authenticated? end def test_should_get_record_token credentials_required token = @viddler.get_record_token assert_kind_of String, token end def test_should_upload_video credentials_required file = File.open(TEST_VIDEO_FILE_PATH) video = @viddler.upload_video(:file => file, :title => 'Testing', :description => 'Bla', :tags => 'one, two, three', :make_public => 1) assert_kind_of Viddler::Video, video end def test_should_upload_tempfile credentials_required file = File.open(TEST_VIDEO_FILE_PATH) tmpfile = Tempfile.new('viddlertest') tmpfile.write file.read tmpfile.seek 0 video = @viddler.upload_video(:file => tmpfile, :title => 'Testing Tempfile', :description => 'tempfile description', :tags => 'one, two, three', :make_public => 1) assert_kind_of Viddler::Video, video end # Works in v2 API, use that. # def test_should_delete_video # credentials_required # file = File.open(TEST_VIDEO_FILE_PATH) # video = @viddler.upload_video(:file => file, :title => 'Testing', :description => 'Bla', :tags => 'one, two, three', :make_public => 1) # assert @viddler.delete_video(video.id) # end # doesnt work # def test_should_update_profile # credentials_required # user = @viddler.update_profile(:first_name => 'Ilya', # :last_name => 'Sabanin', # :about_me => 'A guy', # :birthdate => '1987-05-22', # :gender => 'm', # :company => 'Wildbit', # :city => 'Krasnoyarsk') # assert_kind_of Viddler::User, user # end def test_should_find_profile user = @viddler.find_profile('ilya') assert_kind_of Viddler::User, user end def test_should_update_account credentials_required assert @viddler.update_account(:show_account => '0') end def test_should_get_video_status assert @viddler.get_video_status('f8605d95') end def test_should_find_video_by_id video = @viddler.find_video_by_id('6b0b9af1') assert_kind_of Viddler::Video, video end def test_should_get_dimensions video = @viddler.find_video_by_id('6b0b9af1') assert_equal 640, video.width assert_equal 360, video.height end def test_should_find_video_by_url video = @viddler.find_video_by_url('http://www.viddler.com/explore/ijustine/videos/293/') assert_kind_of Viddler::Video, video end def test_should_find_all_videos_by_user videos = @viddler.find_all_videos_by_user('ijustine') assert_kind_of Viddler::Video, videos.first end def test_should_update_video credentials_required videos = @viddler.find_all_videos_by_user(LOGIN) raise LoadError, "You must have at least one video in your account to test updates" unless videos.length > 0 video = videos.first update = @viddler.update_video(video.id, :title => video.title + ' [test]', :description => video.description + ' [tested]') assert_kind_of Viddler::Video, update end def test_should_find_all_videos_by_tag videos = @viddler.find_all_videos_by_tag('hot') assert_kind_of Viddler::Video, videos.first end def test_should_find_all_features_videos videos = @viddler.find_all_featured_videos assert_kind_of Viddler::Video, videos.first end private def credentials_required raise CredentialsRequired unless LOGIN and PASSWORD end end