Init version of twitter unit tests.

This commit is contained in:
Martin Edenhofer 2013-05-06 01:27:20 +02:00
parent ee8202c3bf
commit 4349d6e92c
3 changed files with 204 additions and 11 deletions

View file

@ -69,6 +69,10 @@ class Channel::Twitter2
# use transaction
ActiveRecord::Base.transaction do
# reset current_user
UserInfo.current_user_id = 1
puts 'import tweet'
fetch_import( tweet, channel, group )
end
@ -146,6 +150,7 @@ class Channel::Twitter2
:note => sender.description,
:active => true,
:roles => roles,
:updated_by_id => 1,
:created_by_id => 1
)
puts 'autentication create...'
@ -187,6 +192,12 @@ class Channel::Twitter2
ticket = nil
if @article_type == 'twitter direct-message'
ticket = Ticket.where( :customer_id => user.id ).first
if ticket
ticket_state_type = Ticket::StateType.where( ticket.ticket_state.state_type_id )
if ticket_state_type.name == 'closed' || ticket_state_type.name == 'closed'
ticket = nil
end
end
end
if !ticket
group = Group.where( :name => group ).first
@ -243,7 +254,7 @@ class Channel::Twitter2
:from => sender.name,
:to => to,
:message_id => tweet.id,
:internal => false
:internal => false,
)
end
@ -258,22 +269,22 @@ class Channel::Twitter2
:oauth_token => channel[:options][:oauth_token],
:oauth_token_secret => channel[:options][:oauth_token_secret]
)
puts 'to:' + atts[:to].to_s
if atts[:type] == 'twitter direct-message'
if attr[:type] == 'twitter direct-message'
puts 'to:' + attr[:to].to_s
dm = client.direct_message_create(
atts[:to].to_s,
atts[:body].to_s,
options = {}
attr[:to].to_s,
attr[:body].to_s,
{}
)
# puts dm.inspect
return dm
end
if atts[:type] == 'twitter status'
if attr[:type] == 'twitter status'
message = client.update(
atts[:body].to_s,
options = {
:in_reply_to_status_id => atts[:in_reply_to]
attr[:body].to_s,
{
:in_reply_to_status_id => attr[:in_reply_to]
}
)
# puts message.inspect

View file

@ -23,7 +23,7 @@ class Observer::Ticket::Article::CommunicateTwitter < ActiveRecord::Observer
:body => record.body,
:in_reply_to => record.in_reply_to
},
Rails.application.config.channel_twitter
# Rails.application.config.channel_twitter
)
record.message_id = message.id
record.save

182
test/unit/twitter_test.rb Normal file
View file

@ -0,0 +1,182 @@
# encoding: utf-8
require 'test_helper'
class TwitterTest < ActiveSupport::TestCase
# app config
consumer_key = 'd2zoZBmMXmT7KLPgEHSzpw'
consumer_secret = 'QMUrlyDlqjITCkWdrOgsTxMVVLxr4A4IW3DIgtIg'
# user1: armin_theo (is system and is following marion_bauer)
user1_token = '1405469528-WQ6XHizgrbYasUwjp0I0TUONhftNkrfrpgFLrdc'
user1_token_secret = '0LHChGFlQx9jSxM8tkBsuDOMhbJMSXTL2zKJJO5Xk'
# user2: me_bauer (is following armin_theo)
user2_token = '1406098795-XQTjg1Zj5uVW0C11NNpNA4xopyclRJJoriWis0I'
user2_token_secret = 'T8ph5afeSDjGDA9X1ZBlzEvoSiXfN266ZZUMj5UaY'
# add channel
current = Channel.where( :adapter => 'Twitter2' )
current.each {|r|
r.destroy
}
Channel.create(
:adapter => 'Twitter2',
:area => 'Twitter::Inbound',
:options => {
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:oauth_token => user1_token,
:oauth_token_secret => user1_token_secret,
:search => [
{
:item => '#citheo42',
:group => 'Twitter',
},
{
:item => '#citheo24',
:group => 'Users',
},
],
:mentions => {
:group => 'Twitter',
},
:direct_messages => {
:group => 'Twitter',
}
},
:active => true,
:created_by_id => 1,
:updated_by_id => 1,
)
test 'new outbound and reply' do
user = User.find(2)
group = Group.where( :name => 'Twitter' ).first
state = Ticket::State.where( :name => 'new' ).first
priority = Ticket::Priority.where( :name => '2 normal' ).first
hash = '#citheo42' + rand(9999).to_s
text = 'Today the weather is really nice... ' + hash
ticket = Ticket.create(
:group_id => group.id,
:customer_id => user.id,
:title => text[0,40],
:ticket_state_id => state.id,
:ticket_priority_id => priority.id,
:updated_by_id => 1,
:created_by_id => 1,
)
assert( ticket, "outbound ticket created" )
article = Ticket::Article.create(
:ticket_id => ticket.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'twitter status' ).first.id,
:ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Agent' ).first.id,
:body => text,
# :from => sender.name,
# :to => to,
# :message_id => tweet.id,
:internal => false,
:updated_by_id => 1,
:created_by_id => 1,
)
assert( article, "outbound article created" )
assert_equal( article.ticket.articles.count, 1 )
sleep 10
# reply by me_bauer
client = Twitter::Client.new(
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:oauth_token => user2_token,
:oauth_token_secret => user2_token_secret
)
tweets = client.search( hash )
tweets.results.map do |tweet|
assert_equal( tweet.id, article.message_id )
end
reply_hash = '#weather' + rand(9999).to_s
reply_text = '@armin_theo on my side the weather is also nice! ' + reply_hash
tweet = client.update(
reply_text,
{
:in_reply_to_status_id => article.message_id
}
)
sleep 10
# fetch check system account
Channel.fetch
# check if follow up article has been created
assert_equal( article.ticket.articles.count, 2 )
reply_article = article.ticket.articles.last
assert_equal( reply_article.body, reply_text )
end
test 'new by direct message inbound' do
# cleanup direct messages of system
client = Twitter::Client.new(
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:oauth_token => user1_token,
:oauth_token_secret => user1_token_secret
)
dms = client.direct_messages( :count => 200 )
dms.each {|dm|
client.direct_message_destroy(dm.id)
}
# direct message to @armin_theo
client = Twitter::Client.new(
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:oauth_token => user2_token,
:oauth_token_secret => user2_token_secret
)
dms = client.direct_messages( :count => 200 )
dms.each {|dm|
client.direct_message_destroy(dm.id)
}
sleep 5
hash = '#citheo44' + rand(9999).to_s
text = 'How about the details? ' + hash
dm = client.direct_message_create(
'armin_theo',
text,
)
sleep 10
# fetch check system account
Channel.fetch
# check if ticket and article has been created
article = Ticket::Article.where(:body => text).last
assert( article, "inbound article created" )
ticket = article.ticket
article_count = ticket.articles.count
assert( article_count )
assert_equal( ticket.ticket_state.name, 'new' )
# reply via ticket
outbound_article = Ticket::Article.create(
:ticket_id => ticket.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'twitter direct-message' ).first.id,
:ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Agent' ).first.id,
:body => text,
# :from => sender.name,
:to => 'me_bauer',
:internal => false,
:updated_by_id => 1,
:created_by_id => 1,
)
assert( outbound_article, "outbound article created" )
assert_equal( outbound_article.ticket.articles.count, article_count + 1 )
sleep 10
end
end