/test/mongoshort_test.rb
Ruby | 136 lines | 114 code | 22 blank | 0 comment | 0 complexity | 9ffcccb6c4a69469b5158550392a6015 MD5 | raw file
1require 'rubygems' 2require 'bundler' 3Bundler.setup 4Bundler.require(:default, :test) 5require './mongoshort' 6require 'minitest/autorun' 7 8set :environment, :test 9 10class UrlTest < MiniTest::Test 11 include Rack::Test::Methods 12 13 def app 14 Sinatra::Application 15 end 16 17 def setup 18 URL.create(:url_key => '83802', :full_url => 'http://www.amazon.com') 19 URL.create(:url_key => '9b80a', :full_url => 'http://www.ebay.com') 20 URL.create(:url_key => '612c1', :full_url => 'http://news.ycombinator.com') 21 end 22 23 def teardown 24 URL.delete_all 25 end 26 27 def set_authorization! 28 authorize 'mongoshort', 'mongoshort' 29 end 30 31 def test_key_should_redirect_to_full_url_if_url_key_exists 32 get '/83802' 33 assert last_response.redirect? 34 follow_redirect! 35 assert_equal "http://www.amazon.com/", last_request.url 36 37 get '/9b80a' 38 assert last_response.redirect? 39 follow_redirect! 40 assert_equal "http://www.ebay.com/", last_request.url 41 42 get '/612c1' 43 assert last_response.redirect? 44 follow_redirect! 45 assert_equal "http://news.ycombinator.com/", last_request.url 46 end 47 48 def test_full_url_redirect_should_be_301 49 get '/612c1' 50 assert_equal 301, last_response.status 51 end 52 53 def test_key_should_update_the_last_access_date 54 Timecop.freeze do 55 get '/83802' 56 url = URL.find_by_url_key('83802') 57 assert_equal Time.now.utc.to_i, url.last_accessed.to_i 58 end 59 end 60 61 def test_key_should_increment_times_viewed 62 url = URL.find_by_url_key('83802') 63 assert_equal url.times_viewed, 0 64 get '/83802' 65 url.reload 66 assert_equal url.times_viewed, 1 67 end 68 69 def test_key_should_redirect_to_default_host_if_url_key_does_not_exist 70 get '/abcde' 71 assert last_response.redirect? 72 follow_redirect! 73 assert_equal "http://0.0.0.0/", last_request.url 74 end 75 76 def test_new_should_return_status_401_if_no_authentication_info_provided 77 post '/new' 78 assert_equal 401, last_response.status 79 end 80 81 def test_new_should_return_status_403_if_authentication_info_incorrect 82 authorize 'mongoshort', 'incorrect-password' 83 post '/new' 84 assert_equal 403, last_response.status 85 end 86 87 def test_new_content_type_should_be_json 88 set_authorization! 89 post '/new' 90 assert_equal "application/json", last_response.headers["Content-Type"] 91 end 92 93 def test_new_should_return_status_400_if_params_are_missing 94 set_authorization! 95 post '/new' 96 assert_equal 400, last_response.status 97 end 98 99 def test_new_should_return_error_key_if_params_are_missing 100 set_authorization! 101 post '/new' 102 response_hash = JSON.parse(last_response.body) 103 assert response_hash.has_key?('error') 104 assert "'url' parameter is missing", response_hash['error'] 105 end 106 107 def test_new_should_use_a_five_character_hash 108 set_authorization! 109 post '/new', { :url => 'http://www.google.com' } 110 new_url = URL.find_by_full_url('http://www.google.com') 111 assert_equal 5, new_url.url_key.length 112 end 113 114 def test_new_should_create_a_new_record_if_url_does_not_exist 115 set_authorization! 116 url_count = URL.count 117 post '/new', { :url => 'http://www.google.com' } 118 assert_equal url_count + 1, URL.count 119 end 120 121 def test_new_should_not_create_a_new_record_if_url_already_exists 122 set_authorization! 123 url_count = URL.count 124 post '/new', { :url => 'http://www.amazon.com' } 125 assert_equal url_count, URL.count 126 end 127 128 def test_new_should_return_short_url_and_full_url 129 set_authorization! 130 post '/new', { :url => 'http://www.amazon.com' } 131 new_url = URL.find_by_full_url('http://www.amazon.com') 132 response_hash = JSON.parse(last_response.body) 133 assert_equal new_url.short_url, response_hash["short_url"] 134 assert_equal 'http://www.amazon.com', response_hash["full_url"] 135 end 136end