Commit 1afbc9136835ac94721b9a6be44929d1cc24fba5

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent 4ec95e63

adding tests for helpers

lib/api/helpers.rb
... ... @@ -76,47 +76,10 @@ module API
76 76 objects
77 77 end
78 78  
79   -#FIXME see if its needed
80   -# def paginate(relation)
81   -# per_page = params[:per_page].to_i
82   -# paginated = relation.page(params[:page]).per(per_page)
83   -# add_pagination_headers(paginated, per_page)
84   -#
85   -# paginated
86   -# end
87   -
88 79 def authenticate!
89 80 unauthorized! unless current_user
90 81 end
91 82  
92   -#FIXME see if its needed
93   -# def authenticated_as_admin!
94   -# forbidden! unless current_user.is_admin?
95   -# end
96   -#
97   -#FIXME see if its needed
98   -# def authorize! action, subject
99   -# unless abilities.allowed?(current_user, action, subject)
100   -# forbidden!
101   -# end
102   -# end
103   -#
104   -#FIXME see if its needed
105   -# def can?(object, action, subject)
106   -# abilities.allowed?(object, action, subject)
107   -# end
108   -
109   - # Checks the occurrences of required attributes, each attribute must be present in the params hash
110   - # or a Bad Request error is invoked.
111   - #
112   - # Parameters:
113   - # keys (required) - A hash consisting of keys that must be present
114   - def required_attributes!(keys)
115   - keys.each do |key|
116   - bad_request!(key) unless params[key].present?
117   - end
118   - end
119   -
120 83 # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash
121 84 # or a Bad Request error is invoked.
122 85 #
... ... @@ -135,8 +98,11 @@ module API
135 98 end
136 99 attrs
137 100 end
  101 +
  102 + ##########################################
  103 + # error helpers #
  104 + ##########################################
138 105  
139   - # error helpers
140 106 def forbidden!
141 107 render_api_error!('403 Forbidden', 403)
142 108 end
... ... @@ -203,6 +169,19 @@ module API
203 169 20
204 170 end
205 171  
  172 + def parse_content_type(content_type)
  173 + return nil if content_type.blank?
  174 + content_type.split(',').map do |content_type|
  175 + content_type.camelcase
  176 + end
  177 + end
  178 +
  179 + def period(from_date, until_date)
  180 + begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date
  181 + end_period = until_date.nil? ? DateTime.now : until_date
  182 +
  183 + begin_period..end_period
  184 + end
206 185  
207 186 end
208 187 end
... ...
lib/api/session.rb
... ... @@ -26,8 +26,12 @@ module API
26 26 # login - login
27 27 # Example Request:
28 28 # POST /register?email=some@mail.com&password=pas&login=some
29   - post "/register" do
30   - required_attributes! [:email, :login, :password]
  29 + params do
  30 + requires :email, type: String, desc: _("Email")
  31 + requires :login, type: String, desc: _("Login")
  32 + requires :password, type: String, desc: _("Password")
  33 + end
  34 + get "/register" do
31 35 unique_attributes! User, [:email, :login]
32 36 attrs = attributes_for_keys [:email, :login, :password]
33 37 attrs[:password_confirmation] = attrs[:password]
... ...
test/unit/api/helpers_test.rb
... ... @@ -4,45 +4,153 @@ class APITest < ActiveSupport::TestCase
4 4  
5 5 include API::APIHelpers
6 6  
7   -# def setup
8   -# login_api
  7 + should 'get the current user with valid token' do
  8 + user = create_user('someuser')
  9 + user.generate_private_token!
  10 + self.params = {:private_token => user.private_token}
  11 + assert_equal user, current_user
  12 + end
  13 +
  14 + should 'not get the current user with expired token' do
  15 + user = create_user('someuser')
  16 + user.generate_private_token!
  17 + user.private_token_generated_at = DateTime.now.prev_year
  18 + user.save
  19 + self.params = {:private_token => user.private_token}
  20 + assert_nil current_user
  21 + end
  22 +
  23 + should 'get the person of current user' do
  24 + user = create_user('someuser')
  25 + user.generate_private_token!
  26 + self.params = {:private_token => user.private_token}
  27 + assert_equal user.person, current_person
  28 + end
  29 +
  30 +# #FIXME see how to make this test. Get the current_user variable
  31 +# should 'set current_user to nil after logout' do
  32 +# user = create_user('someuser')
  33 +# user.stubs(:private_token_expired?).returns(false)
  34 +# User.stubs(:find_by_private_token).returns(user)
  35 +# assert_not_nil current_user
  36 +# assert false
  37 +# logout
9 38 # end
10 39  
11   - should 'get the current user' do
  40 + should 'limit be defined as the params limit value' do
  41 + local_limit = 30
  42 + self.params= {:limit => local_limit}
  43 + assert_equal local_limit, limit
  44 + end
  45 +
  46 + should 'return default limit if the limit parameter is minor than zero' do
  47 + self.params= {:limit => -1}
  48 + assert_equal 20, limit
  49 + end
  50 +
  51 + should 'the default limit be 20' do
  52 + assert_equal 20, limit
  53 + end
  54 +
  55 + should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do
  56 + assert_equal Time.at(0).to_datetime, period(nil, nil).to_a[0]
  57 + end
  58 +
  59 + should 'the beginning of the period be from date passsed as parameter' do
  60 + from = DateTime.now
  61 + assert_equal from, period(from, nil).min
  62 + end
  63 +
  64 + should 'the end of the period be now if no until date is passsed as parameter' do
  65 + assert_in_delta DateTime.now, period(nil, nil).max
  66 + end
  67 +
  68 + should 'the end of the period be until date passsed as parameter' do
  69 + until_date = DateTime.now
  70 + assert_equal until_date, period(nil, until_date).max
  71 + end
  72 +
  73 + should 'parse_content_type return nil if its blank' do
  74 + assert_nil parse_content_type("")
  75 + end
  76 +
  77 + should 'parse_content_type be an array' do
  78 + assert_kind_of Array, parse_content_type("text_article")
  79 + end
  80 +
  81 + should 'parse_content_type return all content types as an array' do
  82 + assert_equivalent ['TextArticle','TinyMceArticle'], parse_content_type("TextArticle,TinyMceArticle")
  83 + end
  84 +
  85 + should 'find_article return article by id in list passed for user with permission' do
12 86 user = create_user('someuser')
13   -# params = {:private_token => user.private_token}
14   -# post "/api/v1/login?#{params.to_query}"
15   -# json = JSON.parse(last_response.body)
  87 + a = fast_create(Article, :profile_id => user.person.id)
  88 + fast_create(Article, :profile_id => user.person.id)
  89 + fast_create(Article, :profile_id => user.person.id)
  90 +
  91 + user.generate_private_token!
16 92 User.expects(:find_by_private_token).returns(user)
17   - assert_equal user, current_user
18   -#
19   -# assert !json["private_token"].blank?
  93 + assert_equal a, find_article(user.person.articles, a.id)
20 94 end
21 95  
22   -# should 'return 401 when login fails' do
23   -# user.destroy
24   -# params = {:login => "testapi", :password => "testapi"}
25   -# post "/api/v1/login?#{params.to_query}"
26   -# assert_equal 401, last_response.status
27   -# end
28   -#
29   -# should 'register a user' do
30   -# params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" }
31   -# post "/api/v1/register?#{params.to_query}"
32   -# assert_equal 201, last_response.status
33   -# end
34   -#
35   -# should 'do not register a user without email' do
36   -# params = {:login => "newuserapi", :password => "newuserapi", :email => nil }
37   -# post "/api/v1/register?#{params.to_query}"
38   -# assert_equal 400, last_response.status
39   -# end
40   -#
41   -# should 'do not register a duplicated user' do
42   -# params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" }
43   -# post "/api/v1/register?#{params.to_query}"
44   -# post "/api/v1/register?#{params.to_query}"
45   -# assert_equal 400, last_response.status
46   -# end
47   -#
  96 + should 'find_article return forbidden when a user try to access an article without permission' do
  97 + user = create_user('someuser')
  98 + p = fast_create(Profile)
  99 + a = fast_create(Article, :published => false, :profile_id => p.id)
  100 + fast_create(Article, :profile_id => p.id)
  101 +
  102 + user.generate_private_token!
  103 + User.expects(:find_by_private_token).returns(user)
  104 + assert_equal 403, find_article(p.articles, a.id).last
  105 + end
  106 +
  107 + should 'make_conditions_with_parameter return no created at parameter if it was not defined from or until parameters' do
  108 + assert_nil make_conditions_with_parameter[:created_at]
  109 + end
  110 +
  111 + should 'make_conditions_with_parameter return created_at parameter if from period is defined' do
  112 + assert_not_nil make_conditions_with_parameter(:from => '2010-10-10')[:created_at]
  113 + end
  114 +
  115 + should 'make_conditions_with_parameter return created_at parameter if until period is defined' do
  116 + assert_not_nil make_conditions_with_parameter(:until => '2010-10-10')[:created_at]
  117 + end
  118 +
  119 +# should 'the beginning of the period be the first existent date if no from date is passsed as parameter' do
  120 + should 'make_conditions_with_parameter return created_at as the first existent date as parameter if only until is defined' do
  121 + assert_equal Time.at(0).to_datetime, make_conditions_with_parameter(:until => '2010-10-10')[:created_at].min
  122 + end
  123 +
  124 + should 'make_conditions_with_parameter: the minimal created_at date be the from date passed as parameter' do
  125 + date = '2010-10-10'
  126 + assert_equal DateTime.parse(date), make_conditions_with_parameter(:from => date)[:created_at].min
  127 + end
  128 +
  129 + should 'make_conditions_with_parameter: the maximum created_at date be the until date passed as parameter' do
  130 + date = '2010-10-10'
  131 + assert_equal DateTime.parse(date), make_conditions_with_parameter(:until => date)[:created_at].max
  132 + end
  133 +
  134 + should 'make_conditions_with_parameter return the until date passed as parameter' do
  135 + date = '2010-10-10'
  136 + assert_equal DateTime.parse(date), make_conditions_with_parameter(:from => '2010-10-10')[:created_at].min
  137 + end
  138 +
  139 + should 'make_conditions_with_parameter return no type parameter if it was not defined any content type' do
  140 + assert_nil make_conditions_with_parameter[:type]
  141 + end
  142 +
  143 + protected
  144 +
  145 + def error!(info, status)
  146 + [info, status]
  147 + end
  148 +
  149 + def params
  150 + @params ||= {}
  151 + end
  152 +
  153 + def params= value
  154 + @params = value
  155 + end
48 156 end
... ...