/
app_test.rb
69 lines (51 loc) · 2.03 KB
/
app_test.rb
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
require File.expand_path('../test_helper', __FILE__)
require 'app'
set :environment, :test
class AppTest < Test::Unit::TestCase
include Rack::Test::Methods
def setup
setup_stubs
end
test '/ping is protected through http auth' do
Command::Poller::Twitter.stubs(:new).returns(Object.new.tap { |o| o.stubs(:run!) })
authorized_get '/ping'
assert_equal 200, last_response.status
unauthorized_get '/ping'
assert_equal 401, last_response.status
end
test '/ping is runs a twitter poller' do
Command::Message.stubs(:max_message_id).returns(12345)
poller = Command::Poller::Twitter.new(:reply, 'login', 'password')
Command::Poller::Twitter.stubs(:new).returns(poller)
poller.twitter.expects(:timeline_for).with(:replies, :since_id => 12345).returns([twitter_status('svenfuchs', '!update')])
log = capture_stdout { authorized_get '/ping' }
assert_match /imposing as @login/, log
assert_match /Received 1 reply/, log
end
test '/ responding to :html' do
setup_stubs
command('rugb', 'svenfuchs', '!create twitter:svenfuchs github:svenphoox').run
get '/'
assert_equal 'text/html', last_response['Content-Type']
assert_match /svenfuchs/, last_response.body # TODO use some tag matcher
end
test '/ responding to :json' do
Identity.new(:twitter => { :handle => 'svenfuchs' }).save
get '/', {}, { 'HTTP_ACCEPT' => 'application/json' }
assert_equal 'application/json', last_response['Content-Type']
assert_equal({ 'twitter' => { 'handle' => 'svenfuchs' } }, JSON.parse(last_response.body).first['profiles'])
end
protected
def app
Sinatra::Application
end
def authorized_get(path)
get path, {}, { 'HTTP_AUTHORIZATION' => encode_credentials(ENV['twitter_login'], ENV['twitter_password']) }
end
def unauthorized_get(path)
get path, {}, { 'HTTP_AUTHORIZATION' => encode_credentials('go', 'away') }
end
def encode_credentials(username, password)
"Basic " + Base64.encode64("#{username}:#{password}")
end
end