Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

http_basic_login_test.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. require "#{File.dirname(__FILE__)}/../../test_helper"
  2. class ApiTest::HttpBasicLoginTest < ActionController::IntegrationTest
  3. fixtures :all
  4. def setup
  5. Setting.rest_api_enabled = '1'
  6. Setting.login_required = '1'
  7. end
  8. def teardown
  9. Setting.rest_api_enabled = '0'
  10. Setting.login_required = '0'
  11. end
  12. # Using the NewsController because it's a simple API.
  13. context "get /news" do
  14. context "in :xml format" do
  15. context "with a valid HTTP authentication" do
  16. setup do
  17. @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password')
  18. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
  19. get "/news.xml", nil, :authorization => @authorization
  20. end
  21. should_respond_with :success
  22. should_respond_with_content_type :xml
  23. should "login as the user" do
  24. assert_equal @user, User.current
  25. end
  26. end
  27. context "with an invalid HTTP authentication" do
  28. setup do
  29. @user = User.generate_with_protected!
  30. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
  31. get "/news.xml", nil, :authorization => @authorization
  32. end
  33. should_respond_with :unauthorized
  34. should_respond_with_content_type :xml
  35. should "not login as the user" do
  36. assert_equal User.anonymous, User.current
  37. end
  38. end
  39. context "without credentials" do
  40. setup do
  41. get "/projects/onlinestore/news.xml"
  42. end
  43. should_respond_with :unauthorized
  44. should_respond_with_content_type :xml
  45. should "include_www_authenticate_header" do
  46. assert @controller.response.headers.has_key?('WWW-Authenticate')
  47. end
  48. end
  49. end
  50. context "in :json format" do
  51. context "with a valid HTTP authentication" do
  52. setup do
  53. @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password')
  54. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
  55. get "/news.json", nil, :authorization => @authorization
  56. end
  57. should_respond_with :success
  58. should_respond_with_content_type :json
  59. should "login as the user" do
  60. assert_equal @user, User.current
  61. end
  62. end
  63. context "with an invalid HTTP authentication" do
  64. setup do
  65. @user = User.generate_with_protected!
  66. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
  67. get "/news.json", nil, :authorization => @authorization
  68. end
  69. should_respond_with :unauthorized
  70. should_respond_with_content_type :json
  71. should "not login as the user" do
  72. assert_equal User.anonymous, User.current
  73. end
  74. end
  75. end
  76. context "without credentials" do
  77. setup do
  78. get "/projects/onlinestore/news.json"
  79. end
  80. should_respond_with :unauthorized
  81. should_respond_with_content_type :json
  82. should "include_www_authenticate_header" do
  83. assert @controller.response.headers.has_key?('WWW-Authenticate')
  84. end
  85. end
  86. end
  87. end