versions_test.rb 5.1 KB
Newer Older
jplang's avatar
jplang committed
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2013  Jean-Philippe Lang
jplang's avatar
jplang committed
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
jplang's avatar
jplang committed
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
jplang's avatar
jplang committed
14 15 16 17 18 19
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../../test_helper', __FILE__)

20
class Redmine::ApiTest::VersionsTest < Redmine::ApiTest::Base
21 22 23 24 25 26 27 28
  fixtures :projects, :trackers, :issue_statuses, :issues,
           :enumerations, :users, :issue_categories,
           :projects_trackers,
           :roles,
           :member_roles,
           :members,
           :enabled_modules,
           :versions
jplang's avatar
jplang committed
29 30 31 32 33 34 35 36 37

  def setup
    Setting.rest_api_enabled = '1'
  end

  context "/projects/:project_id/versions" do
    context "GET" do
      should "return project versions" do
        get '/projects/1/versions.xml'
38

jplang's avatar
jplang committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        assert_response :success
        assert_equal 'application/xml', @response.content_type
        assert_tag :tag => 'versions',
          :attributes => {:type => 'array'},
          :child => {
            :tag => 'version',
            :child => {
              :tag => 'id',
              :content => '2',
              :sibling => {
                :tag => 'name',
                :content => '1.0'
              }
            }
          }
      end
    end
56

jplang's avatar
jplang committed
57 58 59
    context "POST" do
      should "create the version" do
        assert_difference 'Version.count' do
jplang's avatar
jplang committed
60
          post '/projects/1/versions.xml', {:version => {:name => 'API test'}}, credentials('jsmith')
jplang's avatar
jplang committed
61
        end
62

jplang's avatar
jplang committed
63 64
        version = Version.first(:order => 'id DESC')
        assert_equal 'API test', version.name
65

jplang's avatar
jplang committed
66 67 68 69
        assert_response :created
        assert_equal 'application/xml', @response.content_type
        assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
      end
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84
      should "create the version with due date" do
        assert_difference 'Version.count' do
          post '/projects/1/versions.xml', {:version => {:name => 'API test', :due_date => '2012-01-24'}}, credentials('jsmith')
        end

        version = Version.first(:order => 'id DESC')
        assert_equal 'API test', version.name
        assert_equal Date.parse('2012-01-24'), version.due_date

        assert_response :created
        assert_equal 'application/xml', @response.content_type
        assert_tag 'version', :child => {:tag => 'id', :content => version.id.to_s}
      end

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      should "create the version with custom fields" do
        field = VersionCustomField.generate!

        assert_difference 'Version.count' do
          post '/projects/1/versions.xml', {
              :version => {
                :name => 'API test',
                :custom_fields => [
                  {'id' => field.id.to_s, 'value' => 'Some value'}
                ]
              }
            }, credentials('jsmith')
        end

        version = Version.first(:order => 'id DESC')
        assert_equal 'API test', version.name
        assert_equal 'Some value', version.custom_field_value(field)

        assert_response :created
        assert_equal 'application/xml', @response.content_type
        assert_select 'version>custom_fields>custom_field[id=?]>value', field.id.to_s, 'Some value'
      end

jplang's avatar
jplang committed
108 109 110
      context "with failure" do
        should "return the errors" do
          assert_no_difference('Version.count') do
jplang's avatar
jplang committed
111
            post '/projects/1/versions.xml', {:version => {:name => ''}}, credentials('jsmith')
jplang's avatar
jplang committed
112 113 114 115 116 117 118 119
          end

          assert_response :unprocessable_entity
          assert_tag :errors, :child => {:tag => 'error', :content => "Name can't be blank"}
        end
      end
    end
  end
120

121
  context "/versions/:id" do
jplang's avatar
jplang committed
122 123
    context "GET" do
      should "return the version" do
124
        get '/versions/2.xml'
125

jplang's avatar
jplang committed
126 127
        assert_response :success
        assert_equal 'application/xml', @response.content_type
128 129 130 131 132
        assert_select 'version' do
          assert_select 'id', :text => '2'
          assert_select 'name', :text => '1.0'
          assert_select 'sharing', :text => 'none'
        end
jplang's avatar
jplang committed
133 134
      end
    end
135

jplang's avatar
jplang committed
136 137
    context "PUT" do
      should "update the version" do
jplang's avatar
jplang committed
138
        put '/versions/2.xml', {:version => {:name => 'API update'}}, credentials('jsmith')
139

jplang's avatar
jplang committed
140
        assert_response :ok
141
        assert_equal '', @response.body
jplang's avatar
jplang committed
142 143 144
        assert_equal 'API update', Version.find(2).name
      end
    end
145

jplang's avatar
jplang committed
146 147 148
    context "DELETE" do
      should "destroy the version" do
        assert_difference 'Version.count', -1 do
jplang's avatar
jplang committed
149
          delete '/versions/3.xml', {}, credentials('jsmith')
jplang's avatar
jplang committed
150
        end
151

jplang's avatar
jplang committed
152
        assert_response :ok
153
        assert_equal '', @response.body
jplang's avatar
jplang committed
154 155 156 157 158
        assert_nil Version.find_by_id(3)
      end
    end
  end
end