Chris@0: require "#{File.dirname(__FILE__)}/../test_helper" Chris@0: Chris@0: class DisabledRestApi < ActionController::IntegrationTest Chris@0: fixtures :all Chris@0: Chris@0: def setup Chris@0: Setting.rest_api_enabled = '0' Chris@0: Setting.login_required = '1' Chris@0: end Chris@0: Chris@0: def teardown Chris@0: Setting.rest_api_enabled = '1' Chris@0: Setting.login_required = '0' Chris@0: end Chris@0: Chris@0: # Using the NewsController because it's a simple API. Chris@0: context "get /news with the API disabled" do Chris@0: Chris@0: context "in :xml format" do Chris@0: context "with a valid api token" do Chris@0: setup do Chris@0: @user = User.generate_with_protected! Chris@0: @token = Token.generate!(:user => @user, :action => 'api') Chris@0: get "/news.xml?key=#{@token.value}" Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :xml Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: Chris@0: context "with a valid HTTP authentication" do Chris@0: setup do Chris@0: @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password') Chris@0: @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password') Chris@0: get "/news.xml", nil, :authorization => @authorization Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :xml Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: Chris@0: context "with a valid HTTP authentication using the API token" do Chris@0: setup do Chris@0: @user = User.generate_with_protected! Chris@0: @token = Token.generate!(:user => @user, :action => 'api') Chris@0: @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X') Chris@0: get "/news.xml", nil, :authorization => @authorization Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :xml Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: context "in :json format" do Chris@0: context "with a valid api token" do Chris@0: setup do Chris@0: @user = User.generate_with_protected! Chris@0: @token = Token.generate!(:user => @user, :action => 'api') Chris@0: get "/news.json?key=#{@token.value}" Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :json Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: Chris@0: context "with a valid HTTP authentication" do Chris@0: setup do Chris@0: @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password') Chris@0: @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password') Chris@0: get "/news.json", nil, :authorization => @authorization Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :json Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: Chris@0: context "with a valid HTTP authentication using the API token" do Chris@0: setup do Chris@0: @user = User.generate_with_protected! Chris@0: @token = Token.generate!(:user => @user, :action => 'api') Chris@0: @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'DoesNotMatter') Chris@0: get "/news.json", nil, :authorization => @authorization Chris@0: end Chris@0: Chris@0: should_respond_with :unauthorized Chris@0: should_respond_with_content_type :json Chris@0: should "not login as the user" do Chris@0: assert_equal User.anonymous, User.current Chris@0: end Chris@0: end Chris@0: Chris@0: end Chris@0: end Chris@0: end