/test/mongoshort_test.rb

http://github.com/dennmart/mongoshort · Ruby · 136 lines · 114 code · 22 blank · 0 comment · 0 complexity · 9ffcccb6c4a69469b5158550392a6015 MD5 · raw file

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