api_token_login_test.rb 2.2 KB
Newer Older
1 2 3 4 5
require "#{File.dirname(__FILE__)}/../test_helper"

class ApiTokenLoginTest < ActionController::IntegrationTest
  fixtures :all

6
  def setup
7
    Setting.rest_api_enabled = '1'
8 9 10 11
    Setting.login_required = '1'
  end

  def teardown
12
    Setting.rest_api_enabled = '0'
13 14 15
    Setting.login_required = '0'
  end
  
16
  # Using the NewsController because it's a simple API.
17
  context "get /news" do
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

    context "in :xml format" do
      context "with a valid api token" do
        setup do
          @user = User.generate_with_protected!
          @token = Token.generate!(:user => @user, :action => 'api')
          get "/news.xml?key=#{@token.value}"
        end
        
        should_respond_with :success
        should_respond_with_content_type :xml
        should "login as the user" do
          assert_equal @user, User.current
        end
      end

34
      context "with an invalid api token" do
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        setup do
          @user = User.generate_with_protected!
          @token = Token.generate!(:user => @user, :action => 'feeds')
          get "/news.xml?key=#{@token.value}"
        end
        
        should_respond_with :unauthorized
        should_respond_with_content_type :xml
        should "not login as the user" do
          assert_equal User.anonymous, User.current
        end
      end
    end

    context "in :json format" do
      context "with a valid api token" do
        setup do
          @user = User.generate_with_protected!
          @token = Token.generate!(:user => @user, :action => 'api')
          get "/news.json?key=#{@token.value}"
        end
        
        should_respond_with :success
        should_respond_with_content_type :json
        should "login as the user" do
          assert_equal @user, User.current
        end
      end

64
      context "with an invalid api token" do
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        setup do
          @user = User.generate_with_protected!
          @token = Token.generate!(:user => @user, :action => 'feeds')
          get "/news.json?key=#{@token.value}"
        end
        
        should_respond_with :unauthorized
        should_respond_with_content_type :json
        should "not login as the user" do
          assert_equal User.anonymous, User.current
        end
      end
    end
    
  end
end