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.

http_basic_login_with_api_token_test.rb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. require "#{File.dirname(__FILE__)}/../../test_helper"
  2. class ApiTest::HttpBasicLoginWithApiTokenTest < 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 using the API token" do
  16. setup do
  17. @user = User.generate_with_protected!
  18. @token = Token.generate!(:user => @user, :action => 'api')
  19. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
  20. get "/news.xml", nil, :authorization => @authorization
  21. end
  22. should_respond_with :success
  23. should_respond_with_content_type :xml
  24. should "login as the user" do
  25. assert_equal @user, User.current
  26. end
  27. end
  28. context "with an invalid HTTP authentication" do
  29. setup do
  30. @user = User.generate_with_protected!
  31. @token = Token.generate!(:user => @user, :action => 'feeds')
  32. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
  33. get "/news.xml", nil, :authorization => @authorization
  34. end
  35. should_respond_with :unauthorized
  36. should_respond_with_content_type :xml
  37. should "not login as the user" do
  38. assert_equal User.anonymous, User.current
  39. end
  40. end
  41. end
  42. context "in :json format" do
  43. context "with a valid HTTP authentication" do
  44. setup do
  45. @user = User.generate_with_protected!
  46. @token = Token.generate!(:user => @user, :action => 'api')
  47. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'DoesNotMatter')
  48. get "/news.json", nil, :authorization => @authorization
  49. end
  50. should_respond_with :success
  51. should_respond_with_content_type :json
  52. should "login as the user" do
  53. assert_equal @user, User.current
  54. end
  55. end
  56. context "with an invalid HTTP authentication" do
  57. setup do
  58. @user = User.generate_with_protected!
  59. @token = Token.generate!(:user => @user, :action => 'feeds')
  60. @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'DoesNotMatter')
  61. get "/news.json", nil, :authorization => @authorization
  62. end
  63. should_respond_with :unauthorized
  64. should_respond_with_content_type :json
  65. should "not login as the user" do
  66. assert_equal User.anonymous, User.current
  67. end
  68. end
  69. end
  70. end
  71. end