Commit ecfb99f8fa960b777007e00b7feeb0154e3d577e

Authored by Victor Costa
Committed by Rodrigo Souto
1 parent 3ddab2ed

Added tests to api

app/models/user.rb
@@ -124,7 +124,7 @@ class User < ActiveRecord::Base @@ -124,7 +124,7 @@ class User < ActiveRecord::Base
124 def generate_private_token! 124 def generate_private_token!
125 self.private_token = SecureRandom.hex 125 self.private_token = SecureRandom.hex
126 self.private_token_generated_at = DateTime.now 126 self.private_token_generated_at = DateTime.now
127 - save(false) 127 + save(:validate => false)
128 end 128 end
129 129
130 #FIXME make this test 130 #FIXME make this test
lib/api/helpers.rb
@@ -32,15 +32,12 @@ module API @@ -32,15 +32,12 @@ module API
32 end 32 end
33 33
34 def period(from_date, until_date) 34 def period(from_date, until_date)
35 - if from_date.nil?  
36 - begin_period = Time.at(0).to_datetime  
37 - end_period = until_date.nil? ? DateTime.now : until_date  
38 - else  
39 - begin_period = from_date  
40 - end_period = DateTime.now  
41 - end 35 + return nil if from_date.nil? && until_date.nil?
  36 +
  37 + begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date
  38 + end_period = until_date.nil? ? DateTime.now : until_date
42 39
43 - begin_period...end_period 40 + begin_period..end_period
44 end 41 end
45 42
46 def parse_content_type(content_type) 43 def parse_content_type(content_type)
@@ -50,7 +47,6 @@ module API @@ -50,7 +47,6 @@ module API
50 end 47 end
51 end 48 end
52 49
53 -  
54 def make_conditions_with_parameter(params = {}) 50 def make_conditions_with_parameter(params = {})
55 conditions = {} 51 conditions = {}
56 from_date = DateTime.parse(params[:from]) if params[:from] 52 from_date = DateTime.parse(params[:from]) if params[:from]
@@ -58,7 +54,7 @@ module API @@ -58,7 +54,7 @@ module API
58 54
59 conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil? 55 conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil?
60 56
61 - conditions[:created_at] = period(from_date, until_date) 57 + conditions[:created_at] = period(from_date, until_date) if from_date || until_date
62 58
63 conditions 59 conditions
64 end 60 end
test/unit/api_test.rb 0 → 100644
@@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class APITest < ActiveSupport::TestCase
  4 +
  5 + include Rack::Test::Methods
  6 +
  7 + def app
  8 + API::API
  9 + end
  10 +
  11 + def setup
  12 + @user = User.create!(:login => 'testapi', :password => 'testapi', :password_confirmation => 'testapi', :email => 'test@test.org', :environment => Environment.default)
  13 + @user.activate
  14 +
  15 + post "/api/v1/login?login=testapi&password=testapi"
  16 + json = JSON.parse(last_response.body)
  17 + @private_token = json["private_token"]
  18 + @params = {:private_token => @private_token}
  19 + end
  20 + attr_accessor :private_token, :user, :params
  21 +
  22 + should 'generate private token when login' do
  23 + params = {:login => "testapi", :password => "testapi"}
  24 + post "/api/v1/login?#{params.to_query}"
  25 + json = JSON.parse(last_response.body)
  26 + assert !json["private_token"].blank?
  27 + end
  28 +
  29 + should 'return 401 when login fails' do
  30 + user.destroy
  31 + params = {:login => "testapi", :password => "testapi"}
  32 + post "/api/v1/login?#{params.to_query}"
  33 + assert_equal 401, last_response.status
  34 + end
  35 +
  36 + should 'register a user' do
  37 + params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" }
  38 + post "/api/v1/register?#{params.to_query}"
  39 + assert_equal 201, last_response.status
  40 + end
  41 +
  42 + should 'do not register a user without email' do
  43 + params = {:login => "newuserapi", :password => "newuserapi", :email => nil }
  44 + post "/api/v1/register?#{params.to_query}"
  45 + assert_equal 400, last_response.status
  46 + end
  47 +
  48 + should 'do not register a duplicated user' do
  49 + params = {:login => "newuserapi", :password => "newuserapi", :email => "newuserapi@email.com" }
  50 + post "/api/v1/register?#{params.to_query}"
  51 + post "/api/v1/register?#{params.to_query}"
  52 + assert_equal 400, last_response.status
  53 + end
  54 +
  55 + should 'list articles' do
  56 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  57 + get "/api/v1/articles/?#{params.to_query}"
  58 + json = JSON.parse(last_response.body)
  59 + assert_includes json["articles"].map { |a| a["id"] }, article.id
  60 + end
  61 +
  62 + should 'return article by id' do
  63 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  64 + get "/api/v1/articles/#{article.id}?#{params.to_query}"
  65 + json = JSON.parse(last_response.body)
  66 + assert_equal article.id, json["article"]["id"]
  67 + end
  68 +
  69 + should 'return comments of an article' do
  70 + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
  71 + article.comments.create!(:body => "some comment", :author => user.person)
  72 + article.comments.create!(:body => "another comment", :author => user.person)
  73 +
  74 + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
  75 + json = JSON.parse(last_response.body)
  76 + assert_equal 2, json["comments"].length
  77 + end
  78 +
  79 + should 'list users' do
  80 + get "/api/v1/users/?#{params.to_query}"
  81 + json = JSON.parse(last_response.body)
  82 + assert_includes json["users"].map { |a| a["login"] }, user.login
  83 + end
  84 +
  85 +end