diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 548bcae..1eed9be 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -76,47 +76,10 @@ module API objects end -#FIXME see if its needed -# def paginate(relation) -# per_page = params[:per_page].to_i -# paginated = relation.page(params[:page]).per(per_page) -# add_pagination_headers(paginated, per_page) -# -# paginated -# end - def authenticate! unauthorized! unless current_user end -#FIXME see if its needed -# def authenticated_as_admin! -# forbidden! unless current_user.is_admin? -# end -# -#FIXME see if its needed -# def authorize! action, subject -# unless abilities.allowed?(current_user, action, subject) -# forbidden! -# end -# end -# -#FIXME see if its needed -# def can?(object, action, subject) -# abilities.allowed?(object, action, subject) -# end - - # Checks the occurrences of required attributes, each attribute must be present in the params hash - # or a Bad Request error is invoked. - # - # Parameters: - # keys (required) - A hash consisting of keys that must be present - def required_attributes!(keys) - keys.each do |key| - bad_request!(key) unless params[key].present? - end - end - # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash # or a Bad Request error is invoked. # @@ -135,8 +98,11 @@ module API end attrs end + + ########################################## + # error helpers # + ########################################## - # error helpers def forbidden! render_api_error!('403 Forbidden', 403) end @@ -203,6 +169,19 @@ module API 20 end + def parse_content_type(content_type) + return nil if content_type.blank? + content_type.split(',').map do |content_type| + content_type.camelcase + end + end + + def period(from_date, until_date) + begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date + end_period = until_date.nil? ? DateTime.now : until_date + + begin_period..end_period + end end end diff --git a/lib/api/session.rb b/lib/api/session.rb index 568e16f..d639b10 100644 --- a/lib/api/session.rb +++ b/lib/api/session.rb @@ -26,8 +26,12 @@ module API # login - login # Example Request: # POST /register?email=some@mail.com&password=pas&login=some - post "/register" do - required_attributes! [:email, :login, :password] + params do + requires :email, type: String, desc: _("Email") + requires :login, type: String, desc: _("Login") + requires :password, type: String, desc: _("Password") + end + get "/register" do unique_attributes! User, [:email, :login] attrs = attributes_for_keys [:email, :login, :password] attrs[:password_confirmation] = attrs[:password] diff --git a/test/unit/api/helpers_test.rb b/test/unit/api/helpers_test.rb index debb838..ff6d907 100644 --- a/test/unit/api/helpers_test.rb +++ b/test/unit/api/helpers_test.rb @@ -4,45 +4,153 @@ class APITest < ActiveSupport::TestCase include API::APIHelpers -# def setup -# login_api + should 'get the current user with valid token' do + user = create_user('someuser') + user.generate_private_token! + self.params = {:private_token => user.private_token} + assert_equal user, current_user + end + + should 'not get the current user with expired token' do + user = create_user('someuser') + user.generate_private_token! + user.private_token_generated_at = DateTime.now.prev_year + user.save + self.params = {:private_token => user.private_token} + assert_nil current_user + end + + should 'get the person of current user' do + user = create_user('someuser') + user.generate_private_token! + self.params = {:private_token => user.private_token} + assert_equal user.person, current_person + end + +# #FIXME see how to make this test. Get the current_user variable +# should 'set current_user to nil after logout' do +# user = create_user('someuser') +# user.stubs(:private_token_expired?).returns(false) +# User.stubs(:find_by_private_token).returns(user) +# assert_not_nil current_user +# assert false +# logout # end - should 'get the current user' do + should 'limit be defined as the params limit value' do + local_limit = 30 + self.params= {:limit => local_limit} + assert_equal local_limit, limit + end + + should 'return default limit if the limit parameter is minor than zero' do + self.params= {:limit => -1} + assert_equal 20, limit + end + + should 'the default limit be 20' do + assert_equal 20, limit + end + + should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do + assert_equal Time.at(0).to_datetime, period(nil, nil).to_a[0] + end + + should 'the beginning of the period be from date passsed as parameter' do + from = DateTime.now + assert_equal from, period(from, nil).min + end + + should 'the end of the period be now if no until date is passsed as parameter' do + assert_in_delta DateTime.now, period(nil, nil).max + end + + should 'the end of the period be until date passsed as parameter' do + until_date = DateTime.now + assert_equal until_date, period(nil, until_date).max + end + + should 'parse_content_type return nil if its blank' do + assert_nil parse_content_type("") + end + + should 'parse_content_type be an array' do + assert_kind_of Array, parse_content_type("text_article") + end + + should 'parse_content_type return all content types as an array' do + assert_equivalent ['TextArticle','TinyMceArticle'], parse_content_type("TextArticle,TinyMceArticle") + end + + should 'find_article return article by id in list passed for user with permission' do user = create_user('someuser') -# params = {:private_token => user.private_token} -# post "/api/v1/login?#{params.to_query}" -# json = JSON.parse(last_response.body) + a = fast_create(Article, :profile_id => user.person.id) + fast_create(Article, :profile_id => user.person.id) + fast_create(Article, :profile_id => user.person.id) + + user.generate_private_token! User.expects(:find_by_private_token).returns(user) - assert_equal user, current_user -# -# assert !json["private_token"].blank? + assert_equal a, find_article(user.person.articles, a.id) end -# should 'return 401 when login fails' do -# user.destroy -# params = {:login => "testapi", :password => "testapi"} -# post "/api/v1/login?#{params.to_query}" -# assert_equal 401, last_response.status -# end -# -# should 'register a user' do -# params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" } -# post "/api/v1/register?#{params.to_query}" -# assert_equal 201, last_response.status -# end -# -# should 'do not register a user without email' do -# params = {:login => "newuserapi", :password => "newuserapi", :email => nil } -# post "/api/v1/register?#{params.to_query}" -# assert_equal 400, last_response.status -# end -# -# should 'do not register a duplicated user' do -# params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" } -# post "/api/v1/register?#{params.to_query}" -# post "/api/v1/register?#{params.to_query}" -# assert_equal 400, last_response.status -# end -# + should 'find_article return forbidden when a user try to access an article without permission' do + user = create_user('someuser') + p = fast_create(Profile) + a = fast_create(Article, :published => false, :profile_id => p.id) + fast_create(Article, :profile_id => p.id) + + user.generate_private_token! + User.expects(:find_by_private_token).returns(user) + assert_equal 403, find_article(p.articles, a.id).last + end + + should 'make_conditions_with_parameter return no created at parameter if it was not defined from or until parameters' do + assert_nil make_conditions_with_parameter[:created_at] + end + + should 'make_conditions_with_parameter return created_at parameter if from period is defined' do + assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at] + end + + should 'make_conditions_with_parameter return created_at parameter if until period is defined' do + assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at] + end + +# should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do + should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do + assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min + end + + should 'make_conditions_with_parameter: the minimal created_at date be the from date passed as parameter' do + date = '2010-10-10' + assert_equal DateTime.parse(date), make_conditions_with_parameter(:from => date)[:created_at].min + end + + should 'make_conditions_with_parameter: the maximum created_at date be the until date passed as parameter' do + date = '2010-10-10' + assert_equal DateTime.parse(date), make_conditions_with_parameter(:until => date)[:created_at].max + end + + should 'make_conditions_with_parameter return the until date passed as parameter' do + date = '2010-10-10' + assert_equal DateTime.parse(date), make_conditions_with_parameter(:from => '2010-10-10')[:created_at].min + end + + should 'make_conditions_with_parameter return no type parameter if it was not defined any content type' do + assert_nil make_conditions_with_parameter[:type] + end + + protected + + def error!(info, status) + [info, status] + end + + def params + @params ||= {} + end + + def params= value + @params = value + end end -- libgit2 0.21.2