Commit d0f2adc52acae45efb5691bfd293b1b18f12a415
Exists in
staging
and in
1 other branch
Merge branch 'master' into staging
Conflicts: lib/noosfero/api/helpers.rb test/api/helpers_test.rb test/api/people_test.rb
Showing
7 changed files
with
75 additions
and
10 deletions
Show diff stats
lib/noosfero/api/helpers.rb
1 | 1 | require 'grape' |
2 | +require 'base64' | |
3 | +require 'tempfile' | |
2 | 4 | require_relative '../../find_by_contents' |
3 | 5 | |
4 | - module Noosfero; | |
5 | - module API | |
6 | - module APIHelpers | |
6 | +module Noosfero; | |
7 | + module API | |
8 | + module APIHelpers | |
7 | 9 | PRIVATE_TOKEN_PARAM = :private_token |
8 | 10 | DEFAULT_ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type, :author_id, :identifier, :archived] |
9 | 11 | |
... | ... | @@ -255,7 +257,7 @@ require_relative '../../find_by_contents' |
255 | 257 | else |
256 | 258 | created_at = scope.find(reference_id).created_at |
257 | 259 | scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) |
258 | - end | |
260 | + end | |
259 | 261 | end |
260 | 262 | |
261 | 263 | def by_categories(scope, params) |
... | ... | @@ -412,6 +414,30 @@ require_relative '../../find_by_contents' |
412 | 414 | not_found! if Noosfero::API::API.endpoint_unavailable?(self, @environment) |
413 | 415 | end |
414 | 416 | |
417 | + def asset_with_image params | |
418 | + if params.has_key? :image_builder | |
419 | + asset_api_params = params | |
420 | + asset_api_params[:image_builder] = base64_to_uploadedfile(asset_api_params[:image_builder]) | |
421 | + return asset_api_params | |
422 | + end | |
423 | + params | |
424 | + end | |
425 | + | |
426 | + def base64_to_uploadedfile(base64_image) | |
427 | + tempfile = base64_to_tempfile base64_image | |
428 | + converted_image = base64_image | |
429 | + converted_image[:tempfile] = tempfile | |
430 | + return {uploaded_data: ActionDispatch::Http::UploadedFile.new(converted_image)} | |
431 | + end | |
432 | + | |
433 | + def base64_to_tempfile base64_image | |
434 | + base64_img_str = base64_image[:tempfile] | |
435 | + decoded_base64_str = Base64.decode64(base64_img_str) | |
436 | + tempfile = Tempfile.new(base64_image[:filename]) | |
437 | + tempfile.write(decoded_base64_str.encode("ascii-8bit").force_encoding("utf-8")) | |
438 | + tempfile.rewind | |
439 | + tempfile | |
440 | + end | |
415 | 441 | private |
416 | 442 | |
417 | 443 | def parser_params(params) | ... | ... |
lib/noosfero/api/v1/articles.rb
... | ... | @@ -56,7 +56,7 @@ module Noosfero |
56 | 56 | post ':id' do |
57 | 57 | article = environment.articles.find(params[:id]) |
58 | 58 | return forbidden! unless article.allow_edit?(current_person) |
59 | - article.update_attributes!(params[:article]) | |
59 | + article.update_attributes!(asset_with_image(params[:article])) | |
60 | 60 | present_partial article, :with => Entities::Article |
61 | 61 | end |
62 | 62 | ... | ... |
lib/noosfero/api/v1/people.rb
... | ... | @@ -55,11 +55,10 @@ module Noosfero |
55 | 55 | post ':id' do |
56 | 56 | authenticate! |
57 | 57 | return forbidden! if current_person.id.to_s != params[:id] |
58 | - current_person.update_attributes!(params[:person]) | |
58 | + current_person.update_attributes!(asset_with_image(params[:person])) | |
59 | 59 | present current_person, :with => Entities::Person, :current_person => current_person |
60 | 60 | end |
61 | - | |
62 | - # Example Request: | |
61 | + | |
63 | 62 | # POST api/v1/people?person[login]=some_login&person[password]=some_password&person[name]=Jack |
64 | 63 | # for each custom field for person, add &person[field_name]=field_value to the request |
65 | 64 | desc "Create person" |
... | ... | @@ -76,7 +75,7 @@ module Noosfero |
76 | 75 | params[:person][:custom_values][key]=params[:person].delete(key) if Person.custom_fields(environment).any?{|cf| cf.name==key} |
77 | 76 | end |
78 | 77 | |
79 | - user = User.build(user_data, params[:person], environment) | |
78 | + user = User.build(user_data, asset_with_image(params[:person]), environment) | |
80 | 79 | |
81 | 80 | begin |
82 | 81 | user.signup! | ... | ... |
plugins/ldap/lib/ldap_plugin.rb
... | ... | @@ -53,7 +53,7 @@ class LdapPlugin < Noosfero::Plugin |
53 | 53 | return nil if attrs.nil? |
54 | 54 | |
55 | 55 | user_login = get_login(attrs, ldap.attr_login, login) |
56 | - user = User.find_or_initialize_by_login(user_login) | |
56 | + user = User.find_or_initialize_by(login: user_login) | |
57 | 57 | return nil if !user.new_record? && !user.activated? |
58 | 58 | |
59 | 59 | user.login = user_login | ... | ... |
test/api/helpers_test.rb
1 | 1 | require_relative 'test_helper' |
2 | +require "base64" | |
2 | 3 | require 'noosfero/api/helpers' |
3 | 4 | |
4 | 5 | class APIHelpersTest < ActiveSupport::TestCase |
... | ... | @@ -264,6 +265,23 @@ class APIHelpersTest < ActiveSupport::TestCase |
264 | 265 | end |
265 | 266 | |
266 | 267 | ###### END Captcha tests ###### |
268 | + should 'create a :uploaded_data hash, expected by image_builder ' do | |
269 | + base64_image = create_base64_image | |
270 | + uploadedfile = base64_to_uploadedfile base64_image | |
271 | + assert uploadedfile.has_key? :uploaded_data | |
272 | + assert_equal uploadedfile[:uploaded_data].original_filename, base64_image[:filename] | |
273 | + assert_equal uploadedfile[:uploaded_data].content_type, base64_image[:type] | |
274 | + assert uploadedfile[:uploaded_data].tempfile | |
275 | + end | |
276 | + | |
277 | + should 'return a params copy with a UploadedFile object' do | |
278 | + base64_image = create_base64_image | |
279 | + params = {} | |
280 | + params.merge!({image_builder: base64_image}) | |
281 | + asset_params = asset_with_image params | |
282 | + assert !asset_params[:image_builder][:uploaded_data].nil? | |
283 | + assert asset_params[:image_builder][:uploaded_data].is_a? ActionDispatch::Http::UploadedFile | |
284 | + end | |
267 | 285 | |
268 | 286 | protected |
269 | 287 | ... | ... |
test/api/people_test.rb
... | ... | @@ -388,4 +388,14 @@ class PeopleTest < ActiveSupport::TestCase |
388 | 388 | end |
389 | 389 | end |
390 | 390 | |
391 | + should 'update person image' do | |
392 | + login_api | |
393 | + base64_image = create_base64_image | |
394 | + params.merge!({person: {image_builder: base64_image}}) | |
395 | + assert_nil person.image | |
396 | + post "/api/v1/people/#{person.id}?#{params.to_query}" | |
397 | + person.reload | |
398 | + assert_not_nil person.image | |
399 | + assert_equal person.image.filename, base64_image[:filename] | |
400 | + end | |
391 | 401 | end | ... | ... |
test/api/test_helper.rb
... | ... | @@ -75,6 +75,18 @@ class ActiveSupport::TestCase |
75 | 75 | |
76 | 76 | attr_accessor :private_token, :user, :person, :params, :environment |
77 | 77 | |
78 | + def create_base64_image | |
79 | + image_path = File.absolute_path(Rails.root + 'public/images/noosfero-network.png') | |
80 | + image_name = File.basename(image_path) | |
81 | + image_type = "image/#{File.extname(image_name).delete "."}" | |
82 | + encoded_base64_img = Base64.encode64(File.open(image_path) {|io| io.read }) | |
83 | + base64_image = {} | |
84 | + base64_image[:tempfile] = encoded_base64_img | |
85 | + base64_image[:filename] = image_name | |
86 | + base64_image[:type] = image_type | |
87 | + base64_image | |
88 | + end | |
89 | + | |
78 | 90 | private |
79 | 91 | |
80 | 92 | def json_response_ids(kind) | ... | ... |