|
| 1 | +require_relative '../test_helper' |
| 2 | + |
| 3 | +class UsersApiTest < Redmine::ApiTest::Base |
| 4 | + fixtures :users, |
| 5 | + :email_addresses, |
| 6 | + :members, |
| 7 | + :member_roles, |
| 8 | + :roles, |
| 9 | + :projects |
| 10 | + |
| 11 | + setup do |
| 12 | + @user = User.find_by_login 'jsmith' # id=2 |
| 13 | + end |
| 14 | + |
| 15 | + test 'should include geojson' do |
| 16 | + geo = { |
| 17 | + 'type' => 'Feature', |
| 18 | + 'geometry' => { |
| 19 | + 'type' => 'Point', |
| 20 | + 'coordinates' => [123.269691,9.305099] |
| 21 | + } |
| 22 | + } |
| 23 | + geojson = geo.to_json |
| 24 | + |
| 25 | + @user.update_attribute :geojson, geojson |
| 26 | + |
| 27 | + # xml format - index api |
| 28 | + get '/users.xml', :headers => credentials('admin') |
| 29 | + assert_response :success |
| 30 | + xml = xml_data |
| 31 | + assert json = xml.xpath('/users/user[id=2]/geojson').text |
| 32 | + assert json.present? |
| 33 | + assert_match(/123\.269691/, json) |
| 34 | + assert_equal geo['geometry'], JSON.parse(json)['geometry'], json |
| 35 | + # xml format - show api |
| 36 | + get '/users/2.xml', :headers => credentials('admin') |
| 37 | + assert_response :success |
| 38 | + xml = xml_data |
| 39 | + assert json = xml.xpath('/user/geojson').text |
| 40 | + assert json.present? |
| 41 | + assert_match(/123\.269691/, json) |
| 42 | + assert_equal geo['geometry'], JSON.parse(json)['geometry'], json |
| 43 | + |
| 44 | + # json format - index api |
| 45 | + get '/users.json', :headers => credentials('admin') |
| 46 | + assert_response :success |
| 47 | + assert json = JSON.parse(@response.body) |
| 48 | + hsh = json['users'].detect{|i|i['id'] == @user.id}['geojson'] |
| 49 | + assert_equal geo['geometry'], hsh['geometry'] |
| 50 | + # json format - show api |
| 51 | + get '/users/2.json', :headers => credentials('admin') |
| 52 | + assert_response :success |
| 53 | + assert json = JSON.parse(@response.body) |
| 54 | + hsh = json['user']['geojson'] |
| 55 | + assert_equal geo['geometry'], hsh['geometry'] |
| 56 | + end |
| 57 | + |
| 58 | + test 'should include empty geojson' do |
| 59 | + @user.update_attribute :geojson, nil |
| 60 | + |
| 61 | + # xml format - index api |
| 62 | + get '/users.xml', :headers => credentials('admin') |
| 63 | + assert_response :success |
| 64 | + xml = xml_data |
| 65 | + assert json = xml.xpath('/users/user[id=2]/geojson').text |
| 66 | + assert_equal "", json |
| 67 | + # xml format - show api |
| 68 | + get '/users/2.xml', :headers => credentials('admin') |
| 69 | + assert_response :success |
| 70 | + xml = xml_data |
| 71 | + assert json = xml.xpath('/user/geojson').text |
| 72 | + assert_equal "", json |
| 73 | + |
| 74 | + # json format - index api |
| 75 | + get '/users.json', :headers => credentials('admin') |
| 76 | + assert_response :success |
| 77 | + assert json = JSON.parse(@response.body) |
| 78 | + hsh = json['users'].detect{|p|p['id'] == @user.id}['geojson'] |
| 79 | + assert_nil hsh |
| 80 | + # json format - show api |
| 81 | + get '/users/2.json', :headers => credentials('admin') |
| 82 | + assert_response :success |
| 83 | + assert json = JSON.parse(@response.body) |
| 84 | + hsh = json['user']['geojson'] |
| 85 | + assert_nil hsh |
| 86 | + end |
| 87 | + |
| 88 | + def xml_data |
| 89 | + Nokogiri::XML(@response.body) |
| 90 | + end |
| 91 | +end |
0 commit comments