Removed some shoulda context.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11863 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-05-18 08:57:27 +00:00
parent 0d4bb7558f
commit 8f7b69f77e
8 changed files with 683 additions and 870 deletions

View File

@ -27,9 +27,24 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
set_tmp_attachments_directory set_tmp_attachments_directory
end end
context "GET /projects" do # TODO: A private project is needed because should_allow_api_authentication
context ".xml" do # actually tests that authentication is *required*, not just allowed
should "return projects" do should_allow_api_authentication(:get, "/projects/2.xml")
should_allow_api_authentication(:get, "/projects/2.json")
should_allow_api_authentication(:post,
'/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test'}},
{:success_code => :created})
should_allow_api_authentication(:put,
'/projects/2.xml',
{:project => {:name => 'API update'}},
{:success_code => :ok})
should_allow_api_authentication(:delete,
'/projects/2.xml',
{},
{:success_code => :ok})
test "GET /projects.xml should return projects" do
get '/projects.xml' get '/projects.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -37,10 +52,8 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_tag :tag => 'projects', assert_tag :tag => 'projects',
:child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}} :child => {:tag => 'project', :child => {:tag => 'id', :content => '1'}}
end end
end
context ".json" do test "GET /projects.json should return projects" do
should "return projects" do
get '/projects.json' get '/projects.json'
assert_response :success assert_response :success
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
@ -51,16 +64,8 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_kind_of Hash, json['projects'].first assert_kind_of Hash, json['projects'].first
assert json['projects'].first.has_key?('id') assert json['projects'].first.has_key?('id')
end end
end
end
context "GET /projects/:id" do test "GET /projects/:id.xml should return the project" do
context ".xml" do
# TODO: A private project is needed because should_allow_api_authentication
# actually tests that authentication is *required*, not just allowed
should_allow_api_authentication(:get, "/projects/2.xml")
should "return requested project" do
get '/projects/1.xml' get '/projects/1.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -74,12 +79,18 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_no_tag 'issue_categories' assert_no_tag 'issue_categories'
end end
context "with hidden custom fields" do test "GET /projects/:id.json should return the project" do
setup do get '/projects/1.json'
ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
json = ActiveSupport::JSON.decode(response.body)
assert_kind_of Hash, json
assert_kind_of Hash, json['project']
assert_equal 1, json['project']['id']
end end
should "not display hidden custom fields" do test "GET /projects/:id.xml with hidden custom fields should not display hidden custom fields" do
ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
get '/projects/1.xml' get '/projects/1.xml'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -87,9 +98,8 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_no_tag 'custom_field', assert_no_tag 'custom_field',
:attributes => {:name => 'Development status'} :attributes => {:name => 'Development status'}
end end
end
should "return categories with include=issue_categories" do test "GET /projects/:id.xml with include=issue_categories should return categories" do
get '/projects/1.xml?include=issue_categories' get '/projects/1.xml?include=issue_categories'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -105,7 +115,7 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
} }
end end
should "return trackers with include=trackers" do test "GET /projects/:id.xml with include=trackers should return trackers" do
get '/projects/1.xml?include=trackers' get '/projects/1.xml?include=trackers'
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -120,39 +130,14 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
} }
} }
end end
end
context ".json" do test "POST /projects.xml with valid parameters should create the project" do
should_allow_api_authentication(:get, "/projects/2.json")
should "return requested project" do
get '/projects/1.json'
json = ActiveSupport::JSON.decode(response.body)
assert_kind_of Hash, json
assert_kind_of Hash, json['project']
assert_equal 1, json['project']['id']
end
end
end
context "POST /projects" do
context "with valid parameters" do
setup do
Setting.default_projects_modules = ['issue_tracking', 'repository'] Setting.default_projects_modules = ['issue_tracking', 'repository']
@parameters = {:project => {:name => 'API test', :identifier => 'api-test'}}
end
context ".xml" do
should_allow_api_authentication(:post,
'/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test'}},
{:success_code => :created})
should "create a project with the attributes" do
assert_difference('Project.count') do assert_difference('Project.count') do
post '/projects.xml', @parameters, credentials('admin') post '/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test'}},
credentials('admin')
end end
project = Project.first(:order => 'id DESC') project = Project.first(:order => 'id DESC')
@ -166,64 +151,41 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s} assert_tag 'project', :child => {:tag => 'id', :content => project.id.to_s}
end end
should "accept enabled_module_names attribute" do test "POST /projects.xml should accept enabled_module_names attribute" do
@parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
assert_difference('Project.count') do assert_difference('Project.count') do
post '/projects.xml', @parameters, credentials('admin') post '/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}},
credentials('admin')
end end
project = Project.first(:order => 'id DESC') project = Project.first(:order => 'id DESC')
assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
end end
should "accept tracker_ids attribute" do test "POST /projects.xml should accept tracker_ids attribute" do
@parameters[:project].merge!({:tracker_ids => [1, 3]})
assert_difference('Project.count') do assert_difference('Project.count') do
post '/projects.xml', @parameters, credentials('admin') post '/projects.xml',
{:project => {:name => 'API test', :identifier => 'api-test', :tracker_ids => [1, 3]}},
credentials('admin')
end end
project = Project.first(:order => 'id DESC') project = Project.first(:order => 'id DESC')
assert_equal [1, 3], project.trackers.map(&:id).sort assert_equal [1, 3], project.trackers.map(&:id).sort
end end
end
end
context "with invalid parameters" do test "POST /projects.xml with invalid parameters should return errors" do
setup do
@parameters = {:project => {:name => 'API test'}}
end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do assert_no_difference('Project.count') do
post '/projects.xml', @parameters, credentials('admin') post '/projects.xml', {:project => {:name => 'API test'}}, credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"} assert_tag 'errors', :child => {:tag => 'error', :content => "Identifier can't be blank"}
end end
end
end
end
context "PUT /projects/:id" do test "PUT /projects/:id.xml with valid parameters should update the project" do
context "with valid parameters" do
setup do
@parameters = {:project => {:name => 'API update'}}
end
context ".xml" do
should_allow_api_authentication(:put,
'/projects/2.xml',
{:project => {:name => 'API update'}},
{:success_code => :ok})
should "update the project" do
assert_no_difference 'Project.count' do assert_no_difference 'Project.count' do
put '/projects/2.xml', @parameters, credentials('jsmith') put '/projects/2.xml', {:project => {:name => 'API update'}}, credentials('jsmith')
end end
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
@ -232,11 +194,9 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_equal 'API update', project.name assert_equal 'API update', project.name
end end
should "accept enabled_module_names attribute" do test "PUT /projects/:id.xml should accept enabled_module_names attribute" do
@parameters[:project].merge!({:enabled_module_names => ['issue_tracking', 'news', 'time_tracking']})
assert_no_difference 'Project.count' do assert_no_difference 'Project.count' do
put '/projects/2.xml', @parameters, credentials('admin') put '/projects/2.xml', {:project => {:name => 'API update', :enabled_module_names => ['issue_tracking', 'news', 'time_tracking']}}, credentials('admin')
end end
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
@ -244,47 +204,27 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort assert_equal ['issue_tracking', 'news', 'time_tracking'], project.enabled_module_names.sort
end end
should "accept tracker_ids attribute" do test "PUT /projects/:id.xml should accept tracker_ids attribute" do
@parameters[:project].merge!({:tracker_ids => [1, 3]})
assert_no_difference 'Project.count' do assert_no_difference 'Project.count' do
put '/projects/2.xml', @parameters, credentials('admin') put '/projects/2.xml', {:project => {:name => 'API update', :tracker_ids => [1, 3]}}, credentials('admin')
end end
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
project = Project.find(2) project = Project.find(2)
assert_equal [1, 3], project.trackers.map(&:id).sort assert_equal [1, 3], project.trackers.map(&:id).sort
end end
end
end
context "with invalid parameters" do test "PUT /projects/:id.xml with invalid parameters should return errors" do
setup do
@parameters = {:project => {:name => ''}}
end
context ".xml" do
should "return errors" do
assert_no_difference('Project.count') do assert_no_difference('Project.count') do
put '/projects/2.xml', @parameters, credentials('admin') put '/projects/2.xml', {:project => {:name => ''}}, credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"} assert_tag 'errors', :child => {:tag => 'error', :content => "Name can't be blank"}
end end
end
end
end
context "DELETE /projects/:id" do test "DELETE /projects/:id.xml should delete the project" do
context ".xml" do
should_allow_api_authentication(:delete,
'/projects/2.xml',
{},
{:success_code => :ok})
should "delete the project" do
assert_difference('Project.count',-1) do assert_difference('Project.count',-1) do
delete '/projects/2.xml', {}, credentials('admin') delete '/projects/2.xml', {}, credentials('admin')
end end
@ -293,5 +233,3 @@ class Redmine::ApiTest::ProjectsTest < Redmine::ApiTest::Base
assert_nil Project.find_by_id(2) assert_nil Project.find_by_id(2)
end end
end end
end
end

View File

@ -31,10 +31,7 @@ class Redmine::ApiTest::QueriesTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/queries" do test "GET /queries.xml should return queries" do
context "GET" do
should "return queries" do
get '/queries.xml' get '/queries.xml'
assert_response :success assert_response :success
@ -54,5 +51,3 @@ class Redmine::ApiTest::QueriesTest < Redmine::ApiTest::Base
} }
end end
end end
end
end

View File

@ -24,10 +24,7 @@ class Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/roles" do test "GET /roles.xml should return the roles" do
context "GET" do
context "xml" do
should "return the roles" do
get '/roles.xml' get '/roles.xml'
assert_response :success assert_response :success
@ -48,10 +45,8 @@ class Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
} }
} }
end end
end
context "json" do test "GET /roles.json should return the roles" do
should "return the roles" do
get '/roles.json' get '/roles.json'
assert_response :success assert_response :success
@ -63,14 +58,8 @@ class Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
assert_kind_of Array, json['roles'] assert_kind_of Array, json['roles']
assert_include({'id' => 2, 'name' => 'Developer'}, json['roles']) assert_include({'id' => 2, 'name' => 'Developer'}, json['roles'])
end end
end
end
end
context "/roles/:id" do test "GET /roles/:id.xml should return the role" do
context "GET" do
context "xml" do
should "return the role" do
get '/roles/1.xml' get '/roles/1.xml'
assert_response :success assert_response :success
@ -85,6 +74,3 @@ class Redmine::ApiTest::RolesTest < Redmine::ApiTest::Base
end end
end end
end end
end
end
end

View File

@ -31,8 +31,7 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "GET /time_entries.xml" do test "GET /time_entries.xml should return time entries" do
should "return time entries" do
get '/time_entries.xml', {}, credentials('jsmith') get '/time_entries.xml', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
@ -40,30 +39,23 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
:child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}} :child => {:tag => 'time_entry', :child => {:tag => 'id', :content => '2'}}
end end
context "with limit" do test "GET /time_entries.xml with limit should return limited results" do
should "return limited results" do
get '/time_entries.xml?limit=2', {}, credentials('jsmith') get '/time_entries.xml?limit=2', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'time_entries', assert_tag :tag => 'time_entries',
:children => {:count => 2} :children => {:count => 2}
end end
end
end
context "GET /time_entries/2.xml" do test "GET /time_entries/:id.xml should return the time entry" do
should "return requested time entry" do
get '/time_entries/2.xml', {}, credentials('jsmith') get '/time_entries/2.xml', {}, credentials('jsmith')
assert_response :success assert_response :success
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag :tag => 'time_entry', assert_tag :tag => 'time_entry',
:child => {:tag => 'id', :content => '2'} :child => {:tag => 'id', :content => '2'}
end end
end
context "POST /time_entries.xml" do test "POST /time_entries.xml with issue_id should create time entry" do
context "with issue_id" do
should "return create time entry" do
assert_difference 'TimeEntry.count' do assert_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') post '/time_entries.xml', {:time_entry => {:issue_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
end end
@ -79,7 +71,7 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_equal TimeEntryActivity.find(11), entry.activity assert_equal TimeEntryActivity.find(11), entry.activity
end end
should "accept custom fields" do test "POST /time_entries.xml with issue_id should accept custom fields" do
field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string') field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'string')
assert_difference 'TimeEntry.count' do assert_difference 'TimeEntry.count' do
@ -93,10 +85,8 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
entry = TimeEntry.first(:order => 'id DESC') entry = TimeEntry.first(:order => 'id DESC')
assert_equal 'accepted', entry.custom_field_value(field) assert_equal 'accepted', entry.custom_field_value(field)
end end
end
context "with project_id" do test "POST /time_entries.xml with project_id should create time entry" do
should "return create time entry" do
assert_difference 'TimeEntry.count' do assert_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith') post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :hours => '3.5', :activity_id => '11'}}, credentials('jsmith')
end end
@ -111,10 +101,8 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_equal 3.5, entry.hours assert_equal 3.5, entry.hours
assert_equal TimeEntryActivity.find(11), entry.activity assert_equal TimeEntryActivity.find(11), entry.activity
end end
end
context "with invalid parameters" do test "POST /time_entries.xml with invalid parameters should return errors" do
should "return errors" do
assert_no_difference 'TimeEntry.count' do assert_no_difference 'TimeEntry.count' do
post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith') post '/time_entries.xml', {:time_entry => {:project_id => '1', :spent_on => '2010-12-02', :activity_id => '11'}}, credentials('jsmith')
end end
@ -123,12 +111,8 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
end end
end
end
context "PUT /time_entries/2.xml" do test "PUT /time_entries/:id.xml with valid parameters should update time entry" do
context "with valid parameters" do
should "update time entry" do
assert_no_difference 'TimeEntry.count' do assert_no_difference 'TimeEntry.count' do
put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith') put '/time_entries/2.xml', {:time_entry => {:comments => 'API Update'}}, credentials('jsmith')
end end
@ -136,10 +120,8 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_equal '', @response.body assert_equal '', @response.body
assert_equal 'API Update', TimeEntry.find(2).comments assert_equal 'API Update', TimeEntry.find(2).comments
end end
end
context "with invalid parameters" do test "PUT /time_entries/:id.xml with invalid parameters should return errors" do
should "return errors" do
assert_no_difference 'TimeEntry.count' do assert_no_difference 'TimeEntry.count' do
put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith') put '/time_entries/2.xml', {:time_entry => {:hours => '', :comments => 'API Update'}}, credentials('jsmith')
end end
@ -148,11 +130,8 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"} assert_tag 'errors', :child => {:tag => 'error', :content => "Hours can't be blank"}
end end
end
end
context "DELETE /time_entries/2.xml" do test "DELETE /time_entries/:id.xml should destroy time entry" do
should "destroy time entry" do
assert_difference 'TimeEntry.count', -1 do assert_difference 'TimeEntry.count', -1 do
delete '/time_entries/2.xml', {}, credentials('jsmith') delete '/time_entries/2.xml', {}, credentials('jsmith')
end end
@ -161,4 +140,3 @@ class Redmine::ApiTest::TimeEntriesTest < Redmine::ApiTest::Base
assert_nil TimeEntry.find_by_id(2) assert_nil TimeEntry.find_by_id(2)
end end
end end
end

View File

@ -37,13 +37,6 @@ class Redmine::ApiTest::TokenAuthenticationTest < Redmine::ApiTest::Base
end end
# Using the NewsController because it's a simple API. # Using the NewsController because it's a simple API.
context "get /news" do
context "in :xml format" do
should_allow_key_based_auth(:get, "/news.xml") should_allow_key_based_auth(:get, "/news.xml")
end
context "in :json format" do
should_allow_key_based_auth(:get, "/news.json") should_allow_key_based_auth(:get, "/news.json")
end end
end
end

View File

@ -24,10 +24,7 @@ class Redmine::ApiTest::TrackersTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/trackers" do test "GET /trackers.xml should return trackers" do
context "GET" do
should "return trackers" do
get '/trackers.xml' get '/trackers.xml'
assert_response :success assert_response :success
@ -47,5 +44,3 @@ class Redmine::ApiTest::TrackersTest < Redmine::ApiTest::Base
} }
end end
end end
end
end

View File

@ -24,14 +24,46 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "GET /users" do
should_allow_api_authentication(:get, "/users.xml") should_allow_api_authentication(:get, "/users.xml")
should_allow_api_authentication(:get, "/users.json") should_allow_api_authentication(:get, "/users.json")
end should_allow_api_authentication(:post,
'/users.xml',
{:user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net', :password => 'secret123'
}},
{:success_code => :created})
should_allow_api_authentication(:post,
'/users.json',
{:user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net'
}},
{:success_code => :created})
should_allow_api_authentication(:put,
'/users/2.xml',
{:user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo'
}},
{:success_code => :ok})
should_allow_api_authentication(:put,
'/users/2.json',
{:user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo'
}},
{:success_code => :ok})
should_allow_api_authentication(:delete,
'/users/2.xml',
{},
{:success_code => :ok})
should_allow_api_authentication(:delete,
'/users/2.xml',
{},
{:success_code => :ok})
context "GET /users/2" do test "GET /users/:id.xml should return the user" do
context ".xml" do
should "return requested user" do
get '/users/2.xml' get '/users/2.xml'
assert_response :success assert_response :success
@ -39,20 +71,7 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
:child => {:tag => 'id', :content => '2'} :child => {:tag => 'id', :content => '2'}
end end
context "with include=memberships" do test "GET /users/:id.json should return the user" do
should "include memberships" do
get '/users/2.xml?include=memberships'
assert_response :success
assert_tag :tag => 'memberships',
:parent => {:tag => 'user'},
:children => {:count => 1}
end
end
end
context ".json" do
should "return requested user" do
get '/users/2.json' get '/users/2.json'
assert_response :success assert_response :success
@ -62,8 +81,16 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_equal 2, json['user']['id'] assert_equal 2, json['user']['id']
end end
context "with include=memberships" do test "GET /users/:id.xml with include=memberships should include memberships" do
should "include memberships" do get '/users/2.xml?include=memberships'
assert_response :success
assert_tag :tag => 'memberships',
:parent => {:tag => 'user'},
:children => {:count => 1}
end
test "GET /users/:id.json with include=memberships should include memberships" do
get '/users/2.json?include=memberships' get '/users/2.json?include=memberships'
assert_response :success assert_response :success
@ -75,26 +102,19 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
"roles"=>[{"name"=>"Manager", "id"=>1}] "roles"=>[{"name"=>"Manager", "id"=>1}]
}], json['user']['memberships'] }], json['user']['memberships']
end end
end
end
end
context "GET /users/current" do test "GET /users/current.xml should require authentication" do
context ".xml" do
should "require authentication" do
get '/users/current.xml' get '/users/current.xml'
assert_response 401 assert_response 401
end end
should "return current user" do test "GET /users/current.xml should return current user" do
get '/users/current.xml', {}, credentials('jsmith') get '/users/current.xml', {}, credentials('jsmith')
assert_tag :tag => 'user', assert_tag :tag => 'user',
:child => {:tag => 'id', :content => '2'} :child => {:tag => 'id', :content => '2'}
end end
end
end
test "GET /users/:id should not return login for other user" do test "GET /users/:id should not return login for other user" do
get '/users/3.xml', {}, credentials('jsmith') get '/users/3.xml', {}, credentials('jsmith')
@ -132,30 +152,15 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s} assert_tag 'user', :child => {:tag => 'status', :content => User.find(1).status.to_s}
end end
context "POST /users" do test "POST /users.xml with valid parameters should create the user" do
context "with valid parameters" do assert_difference('User.count') do
setup do post '/users.xml', {
@parameters = {
:user => { :user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname', :login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net', :password => 'secret123', :mail => 'foo@example.net', :password => 'secret123',
:mail_notification => 'only_assigned' :mail_notification => 'only_assigned'}
} },
} credentials('admin')
end
context ".xml" do
should_allow_api_authentication(:post,
'/users.xml',
{:user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net', :password => 'secret123'
}},
{:success_code => :created})
should "create a user with the attributes" do
assert_difference('User.count') do
post '/users.xml', @parameters, credentials('admin')
end end
user = User.first(:order => 'id DESC') user = User.first(:order => 'id DESC')
@ -171,20 +176,16 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_equal 'application/xml', @response.content_type assert_equal 'application/xml', @response.content_type
assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s} assert_tag 'user', :child => {:tag => 'id', :content => user.id.to_s}
end end
end
context ".json" do test "POST /users.json with valid parameters should create the user" do
should_allow_api_authentication(:post,
'/users.json',
{:user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net'
}},
{:success_code => :created})
should "create a user with the attributes" do
assert_difference('User.count') do assert_difference('User.count') do
post '/users.json', @parameters, credentials('admin') post '/users.json', {
:user => {
:login => 'foo', :firstname => 'Firstname', :lastname => 'Lastname',
:mail => 'foo@example.net', :password => 'secret123',
:mail_notification => 'only_assigned'}
},
credentials('admin')
end end
user = User.first(:order => 'id DESC') user = User.first(:order => 'id DESC')
@ -201,18 +202,10 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_kind_of Hash, json['user'] assert_kind_of Hash, json['user']
assert_equal user.id, json['user']['id'] assert_equal user.id, json['user']['id']
end end
end
end
context "with invalid parameters" do test "POST /users.xml with with invalid parameters should return errors" do
setup do
@parameters = {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}
end
context ".xml" do
should "return errors" do
assert_no_difference('User.count') do assert_no_difference('User.count') do
post '/users.xml', @parameters, credentials('admin') post '/users.xml', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
@ -222,12 +215,10 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
:content => "First name can't be blank" :content => "First name can't be blank"
} }
end end
end
context ".json" do test "POST /users.json with with invalid parameters should return errors" do
should "return errors" do
assert_no_difference('User.count') do assert_no_difference('User.count') do
post '/users.json', @parameters, credentials('admin') post '/users.json', {:user => {:login => 'foo', :lastname => 'Lastname', :mail => 'foo'}}, credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
@ -237,33 +228,15 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert json.has_key?('errors') assert json.has_key?('errors')
assert_kind_of Array, json['errors'] assert_kind_of Array, json['errors']
end end
end
end
end
context "PUT /users/2" do test "PUT /users/:id.xml with valid parameters should update the user" do
context "with valid parameters" do assert_no_difference('User.count') do
setup do put '/users/2.xml', {
@parameters = {
:user => { :user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed', :login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo' :mail => 'jsmith@somenet.foo'}
} },
} credentials('admin')
end
context ".xml" do
should_allow_api_authentication(:put,
'/users/2.xml',
{:user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo'
}},
{:success_code => :ok})
should "update user with the attributes" do
assert_no_difference('User.count') do
put '/users/2.xml', @parameters, credentials('admin')
end end
user = User.find(2) user = User.find(2)
@ -276,20 +249,15 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
end end
end
context ".json" do test "PUT /users/:id.json with valid parameters should update the user" do
should_allow_api_authentication(:put,
'/users/2.json',
{:user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo'
}},
{:success_code => :ok})
should "update user with the attributes" do
assert_no_difference('User.count') do assert_no_difference('User.count') do
put '/users/2.json', @parameters, credentials('admin') put '/users/2.json', {
:user => {
:login => 'jsmith', :firstname => 'John', :lastname => 'Renamed',
:mail => 'jsmith@somenet.foo'}
},
credentials('admin')
end end
user = User.find(2) user = User.find(2)
@ -302,23 +270,15 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
end end
end
end
context "with invalid parameters" do test "PUT /users/:id.xml with invalid parameters" do
setup do assert_no_difference('User.count') do
@parameters = { put '/users/2.xml', {
:user => { :user => {
:login => 'jsmith', :firstname => '', :lastname => 'Lastname', :login => 'jsmith', :firstname => '', :lastname => 'Lastname',
:mail => 'foo' :mail => 'foo'}
} },
} credentials('admin')
end
context ".xml" do
should "return errors" do
assert_no_difference('User.count') do
put '/users/2.xml', @parameters, credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
@ -328,12 +288,15 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
:content => "First name can't be blank" :content => "First name can't be blank"
} }
end end
end
context ".json" do test "PUT /users/:id.json with invalid parameters" do
should "return errors" do
assert_no_difference('User.count') do assert_no_difference('User.count') do
put '/users/2.json', @parameters, credentials('admin') put '/users/2.json', {
:user => {
:login => 'jsmith', :firstname => '', :lastname => 'Lastname',
:mail => 'foo'}
},
credentials('admin')
end end
assert_response :unprocessable_entity assert_response :unprocessable_entity
@ -343,18 +306,8 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert json.has_key?('errors') assert json.has_key?('errors')
assert_kind_of Array, json['errors'] assert_kind_of Array, json['errors']
end end
end
end
end
context "DELETE /users/2" do test "DELETE /users/:id.xml should delete the user" do
context ".xml" do
should_allow_api_authentication(:delete,
'/users/2.xml',
{},
{:success_code => :ok})
should "delete user" do
assert_difference('User.count', -1) do assert_difference('User.count', -1) do
delete '/users/2.xml', {}, credentials('admin') delete '/users/2.xml', {}, credentials('admin')
end end
@ -362,15 +315,8 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
end end
end
context ".json" do test "DELETE /users/:id.json should delete the user" do
should_allow_api_authentication(:delete,
'/users/2.xml',
{},
{:success_code => :ok})
should "delete user" do
assert_difference('User.count', -1) do assert_difference('User.count', -1) do
delete '/users/2.json', {}, credentials('admin') delete '/users/2.json', {}, credentials('admin')
end end
@ -379,5 +325,3 @@ class Redmine::ApiTest::UsersTest < Redmine::ApiTest::Base
assert_equal '', @response.body assert_equal '', @response.body
end end
end end
end
end

View File

@ -31,9 +31,7 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
Setting.rest_api_enabled = '1' Setting.rest_api_enabled = '1'
end end
context "/projects/:project_id/versions" do test "GET /projects/:project_id/versions.xml should return project versions" do
context "GET" do
should "return project versions" do
get '/projects/1/versions.xml' get '/projects/1/versions.xml'
assert_response :success assert_response :success
@ -52,10 +50,8 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
} }
} }
end end
end
context "POST" do test "POST /projects/:project_id/versions.xml should create the version" do
should "create the version" do
assert_difference 'Version.count' do assert_difference 'Version.count' do
post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith') post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
end end
@ -68,7 +64,7 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s} assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
end end
should "create the version with due date" do test "POST /projects/:project_id/versions.xml should create the version with due date" do
assert_difference 'Version.count' do assert_difference 'Version.count' do
post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith') post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
end end
@ -82,7 +78,7 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s} assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
end end
should "create the version with custom fields" do test "POST /projects/:project_id/versions.xml should create the version with custom fields" do
field = VersionCustomField.generate! field = VersionCustomField.generate!
assert_difference 'Version.count' do assert_difference 'Version.count' do
@ -105,8 +101,7 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value' assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
end end
context "with failure" do test "POST /projects/:project_id/versions.xml with failure should return the errors" do
should "return the errors" do
assert_no_difference('Version.count') do assert_no_difference('Version.count') do
post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith') post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
end end
@ -114,13 +109,8 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_response :unprocessable_entity assert_response :unprocessable_entity
assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"} assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
end end
end
end
end
context "/versions/:id" do test "GET /versions/:id.xml should return the version" do
context "GET" do
should "return the version" do
get '/versions/2.xml' get '/versions/2.xml'
assert_response :success assert_response :success
@ -131,20 +121,16 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_select 'sharing', :text => 'none' assert_select 'sharing', :text => 'none'
end end
end end
end
context "PUT" do test "PUT /versions/:id.xml should update the version" do
should "update the version" do
put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith') put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
assert_response :ok assert_response :ok
assert_equal '', @response.body assert_equal '', @response.body
assert_equal 'API update', Version.find(2).name assert_equal 'API update', Version.find(2).name
end end
end
context "DELETE" do test "DELETE /versions/:id.xml should destroy the version" do
should "destroy the version" do
assert_difference 'Version.count', -1 do assert_difference 'Version.count', -1 do
delete '/versions/3.xml', {}, credentials('jsmith') delete '/versions/3.xml', {}, credentials('jsmith')
end end
@ -154,5 +140,3 @@ class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
assert_nil Version.find_by_id(3) assert_nil Version.find_by_id(3)
end end
end end
end
end