test_helper.rb
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'test_helper'
class OutcomeCaptcha
class << self
attr_accessor :outcome_captcha_test
end
@outcome_captcha_test = true
end
module Noosfero
module API
module APIHelpers
def verify_captcha(*args)
return true if OutcomeCaptcha.outcome_captcha_test
render_api_error!("Error testing captcha", 403)
end
end
end
end
class ActiveSupport::TestCase
include Rack::Test::Methods
include Noosfero::API::APIHelpers
USER_PASSWORD = "testapi"
USER_LOGIN = "testapi"
def app
Api::App
end
def login_with_captcha
json = do_login_captcha_from_api
@private_token = json["private_token"]
@params = { "private_token" => @private_token}
json
end
def do_login_captcha_from_api
post "/api/v1/login-captcha"
json = JSON.parse(last_response.body)
json
end
def create_article(name)
@environment = Environment.default
person = fast_create(Person, :environment_id => @environment.id)
fast_create(Article, :profile_id => person.id, :name => name)
end
def create_and_activate_user
@environment = Environment.default
@user = User.create!(:login => USER_LOGIN, :password => USER_PASSWORD, :password_confirmation => USER_PASSWORD, :email => 'test@test.org', :environment => @environment)
@user.activate
@person = @user.person
@params = {}
end
def login_api
post "/api/v1/login?login=#{USER_LOGIN}&password=#{USER_PASSWORD}"
json = JSON.parse(last_response.body)
@private_token = json["private_token"]
unless @private_token
@user.generate_private_token!
@private_token = @user.private_token
end
@params[:private_token] = @private_token
end
def logout_api
@params.delete(:private_token)
end
attr_accessor :private_token, :user, :person, :params, :environment
def create_base64_image
image_path = File.absolute_path(Rails.root + 'public/images/noosfero-network.png')
image_name = File.basename(image_path)
image_type = "image/#{File.extname(image_name).delete "."}"
encoded_base64_img = Base64.encode64(File.open(image_path) {|io| io.read })
base64_image = {}
base64_image[:tempfile] = encoded_base64_img
base64_image[:filename] = image_name
base64_image[:type] = image_type
base64_image
end
private
def json_response_ids(kind)
json = JSON.parse(last_response.body)
json[kind.to_s].map {|c| c['id']}
end
end