Last active
January 1, 2016 10:29
-
-
Save mickey24/8131836 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Feature: Basic communication | |
Background: | |
Given mimibot logs into Twitter as "mickey24_bot" | |
Scenario: No reaction if no mention | |
Given there's no mention to mimibot on Twitter | |
When mimibot runs | |
Then mimibot gets mentions from Twitter | |
And mimibot does nothing | |
Scenario: Reply "hi." to the mention "hi." | |
Given there's a mention "hi." to mimibot on Twitter | |
When mimibot runs | |
Then mimibot gets mentions from Twitter | |
And mimibot replies "hi." to the mention |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cgi' | |
module FakeTwitterService | |
def set_mentions_to_twitter(mentions) | |
stub_request(:get, "https://api.twitter.com/1.1/statuses/mentions_timeline.json"). | |
to_return(status: 200, body: mentions) | |
end | |
def mentions_request_should_have_been_made | |
get_request_should_have_been_made("https://api.twitter.com/1.1/statuses/mentions_timeline.json") | |
end | |
def update_request_should_have_been_made(status, in_reply_to_status_id) | |
status = CGI.escape(status) | |
post_request_should_have_been_made( | |
"https://api.twitter.com/1.1/statuses/update.json", | |
"in_reply_to_status_id=#{in_reply_to_status_id}&status=#{status}") | |
end | |
private | |
def stub_request(method, uri) | |
WebMock.stub_request(method, uri). | |
with {|request| validate_oauth_header(request.headers["Authorization"])} | |
end | |
def get_request_should_have_been_made(uri) | |
WebMock.a_request(:get, uri). | |
with {|request| validate_oauth_header(request.headers["Authorization"])}. | |
should have_been_made | |
end | |
def post_request_should_have_been_made(uri, body) | |
WebMock.a_request(:post, uri). | |
with {|request| validate_oauth_header(request.headers["Authorization"])}. | |
with {|request| request.body == body}. | |
should have_been_made | |
end | |
def validate_oauth_header(str) | |
# See: https://dev.twitter.com/docs/auth/authorizing-request | |
str =~ /\AOAuth\s/ && | |
str =~ /\boauth_consumer_key="CK"/ && | |
str =~ /\boauth_nonce="[^"]+"/ && | |
str =~ /\boauth_signature="[^"]+"/ && | |
str =~ /\boauth_signature_method="HMAC-SHA1"/ && | |
str =~ /\boauth_timestamp="\d+"/ && | |
str =~ /\boauth_token="OT"/ && | |
str =~ /\boauth_version="1.0"/ | |
end | |
end | |
module BasicSteps | |
include FakeTwitterService | |
step "mimibot logs into Twitter as :screen_name" do |screen_name| | |
@mimi = MimibotMain.new(screen_name) | |
end | |
step "there's no mention to mimibot on Twitter" do | |
set_mentions_to_twitter("[]") | |
end | |
step "there's a mention :mention to mimibot on Twitter" do |mention| | |
set_mentions_to_twitter(<<EOM) | |
[ | |
{ | |
"created_at": "Thu Dec 26 17:32:17 +0900 2013", | |
"id": 123, | |
"text": "@mickey24_bot #{mention}", | |
"user": { | |
"screen_name": "mickey24" | |
} | |
} | |
] | |
EOM | |
end | |
step "mimibot runs" do | |
# Forbid disallowed http requests. | |
WebMock.disable_net_connect! | |
@mimi.run | |
end | |
step "mimibot gets mentions from Twitter" do | |
mentions_request_should_have_been_made | |
end | |
step "mimibot does nothing" do | |
end | |
step "mimibot replies :reply to the mention" do |reply| | |
update_request_should_have_been_made("@mickey24 #{reply}", 123) | |
end | |
end | |
RSpec.configure {|c| c.include BasicSteps} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
require 'webmock/rspec' | |
require 'mimibot' | |
Dir.glob("spec/steps/**/*steps.rb") {|f| load f, true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.F | |
Failures: | |
1) Basic communication Reply "hi." to the mention "hi." there's a mention "hi." to mimibot on Twitter -> mimibot runs -> mimibot gets mentions from Twitter -> mimibot replies "hi." to the mention | |
Failure/Error: Unable to find matching line from backtrace | |
WebMock::NetConnectNotAllowedError: | |
Real HTTP connections are disabled. Unregistered request: POST https://api.twitter.com/1.1/statuses/update.json with body 'in_reply_to_status_id=123&status=%40mickey24+hi.' with headers {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth oauth_consumer_key="CK", oauth_nonce="86eb3bb1329e4ca821b2620797e3b399", oauth_signature="XMb43ClLUiLSns5ad6dJn%2Bh3%2Be8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1388052147", oauth_token="OT", oauth_version="1.0"', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Twitter Ruby Gem 5.4.1'} | |
You can stub this request with the following snippet: | |
stub_request(:post, "https://api.twitter.com/1.1/statuses/update.json"). | |
with(:body => {"in_reply_to_status_id"=>"123", "status"=>"@mickey24 hi."}, | |
:headers => {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'OAuth oauth_consumer_key="CK", oauth_nonce="86eb3bb1329e4ca821b2620797e3b399", oauth_signature="XMb43ClLUiLSns5ad6dJn%2Bh3%2Be8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1388052147", oauth_token="OT", oauth_version="1.0"', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Twitter Ruby Gem 5.4.1'}). | |
to_return(:status => 200, :body => "", :headers => {}) | |
registered request stubs: | |
stub_request(:get, "https://api.twitter.com/1.1/statuses/mentions_timeline.json") | |
============================================================ | |
# ./lib/mimibot/mimibot_main.rb:17:in `run' | |
# spec/steps/basic_steps.rb:100:in `block in <module:BasicSteps>' | |
Finished in 0.03332 seconds | |
2 examples, 1 failure |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment