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