You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

disabled_rest_api_test.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. require "#{File.dirname(__FILE__)}/../../test_helper"
  2. class ApiTest::DisabledRestApiTest < ActionController::IntegrationTest
  3. fixtures :all
  4. def setup
  5. Setting.rest_api_enabled = '0'
  6. Setting.login_required = '1'
  7. end
  8. def teardown
  9. Setting.rest_api_enabled = '1'
  10. Setting.login_required = '0'
  11. end
  12. # Using the NewsController because it's a simple API.
  13. context "get /news with the API disabled" do
  14. context "in :xml format" do
  15. context "with a valid api token" do
  16. setup do
  17. @user = User.generate_with_protected!
  18. @token = Token.generate!(:user => @user, :action => 'api')
  19. get "/news.xml?key=#{@token.value}"
  20. end
  21. should_respond_with :unauthorized
  22. should_respond_with_content_type :xml
  23. should "not login as the user" do
  24. assert_equal User.anonymous, User.current
  25. end
  26. end
  27. context "with a valid HTTP authentication" do
  28. setup do
  29. @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password')
  30. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_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 "with a valid HTTP authentication using the API token" do
  40. setup do
  41. @user = User.generate_with_protected!
  42. @token = Token.generate!(:user => @user, :action => 'api')
  43. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
  44. get "/news.xml", nil, :authorization => @authorization
  45. end
  46. should_respond_with :unauthorized
  47. should_respond_with_content_type :xml
  48. should "not login as the user" do
  49. assert_equal User.anonymous, User.current
  50. end
  51. end
  52. end
  53. context "in :json format" do
  54. context "with a valid api token" do
  55. setup do
  56. @user = User.generate_with_protected!
  57. @token = Token.generate!(:user => @user, :action => 'api')
  58. get "/news.json?key=#{@token.value}"
  59. end
  60. should_respond_with :unauthorized
  61. should_respond_with_content_type :json
  62. should "not login as the user" do
  63. assert_equal User.anonymous, User.current
  64. end
  65. end
  66. context "with a valid HTTP authentication" do
  67. setup do
  68. @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password')
  69. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
  70. get "/news.json", nil, :authorization => @authorization
  71. end
  72. should_respond_with :unauthorized
  73. should_respond_with_content_type :json
  74. should "not login as the user" do
  75. assert_equal User.anonymous, User.current
  76. end
  77. end
  78. context "with a valid HTTP authentication using the API token" do
  79. setup do
  80. @user = User.generate_with_protected!
  81. @token = Token.generate!(:user => @user, :action => 'api')
  82. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'DoesNotMatter')
  83. get "/news.json", nil, :authorization => @authorization
  84. end
  85. should_respond_with :unauthorized
  86. should_respond_with_content_type :json
  87. should "not login as the user" do
  88. assert_equal User.anonymous, User.current
  89. end
  90. end
  91. end
  92. end
  93. end