broadcast 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Email do
4
+
5
+ describe '#publish' do
6
+
7
+ before {
8
+ @medium = Broadcast::Medium::Email.new
9
+ @message = Broadcast::Message::SpecWithContent.new
10
+ Mail::TestMailer.deliveries = []
11
+ }
12
+
13
+ it "should deliver the message to one recipient if options.recipients is a string" do
14
+ @medium.publish(@message)
15
+ Mail::TestMailer.deliveries.size.should == 1
16
+ Mail::TestMailer.deliveries.first.to.should == ['foo@moo.com']
17
+ end
18
+
19
+ it "should deliver the message to many recipients if options.recipients is a array" do
20
+ @medium.options.recipients = ['mike@foo.com', 'tom@foo.com']
21
+ @medium.publish(@message)
22
+ Mail::TestMailer.deliveries.size.should == 2
23
+ Mail::TestMailer.deliveries.first.to.should == ['mike@foo.com']
24
+ Mail::TestMailer.deliveries.last.to.should == ['tom@foo.com']
25
+ end
26
+
27
+ it "should properly get delivery_options and set delivery method" do
28
+ @medium.options.delivery_method = :smtp
29
+ mail = Mail.new
30
+ # 'nueter' the mail instance
31
+ def mail.deliver; end
32
+ Mail.should_receive(:new).and_return(mail)
33
+ @medium.publish(@message)
34
+ mail.delivery_method.settings['user_name'].should == '<username>'
35
+ mail.delivery_method.settings['password'].should == '<password>'
36
+ mail.delivery_method.settings['address'].should == 'smtp.gmail.com'
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Irc do
4
+
5
+ describe '.new' do
6
+
7
+ it "should create a new instance with options provided in config" do
8
+ medium = Broadcast::Medium::Irc.new
9
+ medium.options.username.should == 'foo'
10
+ medium.options.server.should == 'irc.freenode.net'
11
+ medium.options.port.should == '6667'
12
+ medium.options.channel.should == 'broadcast'
13
+ end
14
+
15
+ it "should prioritize options argument over options provided in config" do
16
+ medium = Broadcast::Medium::Jabber.new(:username => 'someoneelse', :server => 'irc.otherfreenode.net', :port => '6666', :channel => 'newchannel')
17
+ medium.options.username.should == 'someoneelse'
18
+ medium.options.server.should == 'irc.otherfreenode.net'
19
+ medium.options.port.should == '6666'
20
+ medium.options.channel.should == 'newchannel'
21
+ end
22
+
23
+ end
24
+
25
+ describe '#publish' do
26
+
27
+ before do
28
+ @medium = Broadcast::Medium::Irc.new
29
+ @message = Broadcast::Message::SpecWithContent.new
30
+ end
31
+
32
+ it "should send the message to IRC channels with the message body" do
33
+ ShoutBot.should_receive(:shout).with("irc://foo@irc.freenode.net:6667/#broadcast")
34
+ @medium.publish(@message)
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Jabber do
4
+
5
+ # 'nueter' the Jabber::Client
6
+ class Jabber::Client
7
+ def connect(*args); end
8
+ def auth(*args); end
9
+ def send(*args); end
10
+ end
11
+
12
+ describe '::Client' do
13
+
14
+ before {
15
+ @client = Broadcast::Medium::Jabber::Client.new('foo@foo.com', '123')
16
+ }
17
+
18
+ describe '.new' do
19
+
20
+ it "should create a new Jabber::Client and connect to jabber" do
21
+ @client.client.should be_an_instance_of Jabber::Client
22
+ end
23
+
24
+ end
25
+
26
+ describe 'deliver' do
27
+
28
+ it "should use the client to send a message" do
29
+ @client.client.should_receive(:send)
30
+ @client.deliver('mike@foo.com', 'Hi')
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ describe '.new' do
38
+
39
+ it "should create a new instance with options provided in config" do
40
+ medium = Broadcast::Medium::Jabber.new
41
+ medium.options.username.should == 'foo@foo.com'
42
+ medium.options.password.should == 'mypass'
43
+ end
44
+
45
+ it "should prioritize options argument options provided in config" do
46
+ medium = Broadcast::Medium::Jabber.new(:username => 'someoneelse@foo.com', :password => 'elsepass')
47
+ medium.options.username.should == 'someoneelse@foo.com'
48
+ medium.options.password.should == 'elsepass'
49
+ end
50
+
51
+ end
52
+
53
+ describe '#jabber' do
54
+
55
+ it "should instantiate a new Broadcast::Medium::Jabber::Client instance" do
56
+ jabber = Broadcast::Medium::Jabber.new.jabber
57
+ jabber.should be_an_instance_of(Broadcast::Medium::Jabber::Client)
58
+ end
59
+
60
+ it "should memoize the Broadcast::Medium::Jabber::Client instance" do
61
+ medium = Broadcast::Medium::Jabber.new
62
+ jabber = medium.jabber
63
+ medium.jabber.object_id.should == jabber.object_id
64
+ end
65
+
66
+ end
67
+
68
+ describe '#publish' do
69
+
70
+ before {
71
+ @medium = Broadcast::Medium::Jabber.new(:username => 'foo@foo.com', :password => 'passkey')
72
+ @message = Broadcast::Message::SpecWithContent.new
73
+ @jabber = mock
74
+ @medium.stub!(:jabber).and_return(@jabber)
75
+ }
76
+
77
+ it "should deliver the message to one recipient if options.recipients is a string" do
78
+ @jabber.should_receive(:deliver).with('mike@foo.com', "message")
79
+ @medium.publish(@message)
80
+ end
81
+
82
+ it "should deliver the message to many recipients if options.recipients is a array" do
83
+ @medium.options.recipients = ['mike@foo.com', 'tom@foo.com']
84
+ @jabber.should_receive(:deliver).with('mike@foo.com', "message")
85
+ @jabber.should_receive(:deliver).with('tom@foo.com', "message")
86
+ @medium.publish(@message)
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Log do
4
+
5
+ before do
6
+ Broadcast.configuration.log.file = nil
7
+ end
8
+
9
+ describe '#publish' do
10
+
11
+ it "should send message to logfile if it was defined on class level" do
12
+ log = StringIO.new
13
+ Broadcast.configuration.log.file = log
14
+ message = Broadcast::Message::SpecWithContent.new
15
+ Broadcast::Medium::Log.new.publish(message)
16
+ log.rewind
17
+ log.read.should match(message.body)
18
+ end
19
+
20
+ it "should send message to logfile if it was defined in options" do
21
+ log = StringIO.new
22
+ message = Broadcast::Message::SpecWithContent.new
23
+ Broadcast::Medium::Log.new(:file => log).publish(message)
24
+ log.rewind
25
+ log.read.should match(message.body)
26
+ end
27
+
28
+ it "should look at options first before class when looking for file" do
29
+ log_default = StringIO.new
30
+ log_options = StringIO.new
31
+ Broadcast.configuration.log.file = log_default
32
+
33
+ message = Broadcast::Message::SpecWithContent.new
34
+ Broadcast::Medium::Log.new(:file => log_options).publish(message)
35
+ log_options.rewind
36
+ log_options.read.should match(message.body)
37
+ log_default.rewind
38
+ log_default.read.should == ''
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Oauth do
4
+
5
+ before do
6
+ Broadcast::Medium::Oauth.site = nil
7
+ end
8
+
9
+ describe '.consumer' do
10
+
11
+ before do
12
+ Broadcast::Medium::Oauth.site = 'http://site.com'
13
+ end
14
+
15
+ it "should return consumer object with proper keys and site" do
16
+ consumer = Broadcast::Medium::Oauth.new.consumer
17
+ consumer.should be_an_instance_of(OAuth::Consumer)
18
+ consumer.key.should == 'consumerkey'
19
+ consumer.secret.should == 'consumersecret'
20
+ end
21
+
22
+ it "should memoize the consumer object" do
23
+ medium = Broadcast::Medium::Oauth.new
24
+ consumer = medium.consumer
25
+ medium.consumer.object_id.should == consumer.object_id
26
+ end
27
+
28
+ end
29
+
30
+ describe '#token' do
31
+
32
+ before do
33
+ Broadcast::Medium::Oauth.site = 'http://site.com'
34
+ end
35
+
36
+ it "should return access token object with proper access token keys" do
37
+ token = Broadcast::Medium::Oauth.new.token
38
+ token.should be_an_instance_of(OAuth::AccessToken)
39
+ token.token.should == 'accesstoken'
40
+ token.secret.should == 'accesssecret'
41
+ end
42
+
43
+ it "should memoize the access token" do
44
+ medium = Broadcast::Medium::Oauth.new
45
+ token = medium.token
46
+ medium.token.object_id.should == token.object_id
47
+ end
48
+
49
+ it "should prioritize options argument over class level options" do
50
+ token = Broadcast::Medium::Oauth.new(:access_token => 'overridetoken', :access_secret => 'overridesecret').token
51
+ token.should be_an_instance_of(OAuth::AccessToken)
52
+ token.token.should == 'overridetoken'
53
+ token.secret.should == 'overridesecret'
54
+ end
55
+
56
+ end
57
+
58
+ describe '.authorize' do
59
+
60
+ before do
61
+ Broadcast.configuration.oauth.consumer_key = nil
62
+ Broadcast.configuration.oauth.consumer_secret = nil
63
+ Broadcast::Medium::Oauth.site = 'http://site.com'
64
+
65
+ medium = Broadcast::Medium::Oauth.new
66
+
67
+ @request_token = mock
68
+ @stdout = StringIO.new
69
+ @old_stdout, $stdout = $stdout, @stdout
70
+
71
+ $stdin.should_receive(:gets).and_return('consumerkey123')
72
+ $stdin.should_receive(:gets).and_return('consumersecret123')
73
+ $stdin.should_receive(:gets).and_return('12342')
74
+
75
+ @request_token.should_receive(:authorize_url).and_return('http://foo.com/1234')
76
+ medium.consumer.stub!(:get_request_token).and_return(@request_token)
77
+
78
+ @access_token = mock(:token => '6789', :secret => '3456')
79
+ @request_token.should_receive(:get_access_token).and_return(@access_token)
80
+
81
+ medium.authorize
82
+
83
+ medium.options.consumer_key.should == 'consumerkey123'
84
+ medium.options.consumer_secret.should == 'consumersecret123'
85
+ $stdout = @old_stdout
86
+ @stdout.rewind
87
+ end
88
+
89
+ it "should use the request token to get the url the user goes to" do
90
+ @stdout.read.should match('http://foo.com/1234')
91
+ end
92
+
93
+ it "should get the oauth_verifier from the user" do
94
+ @stdout.read.should match('Enter token')
95
+ end
96
+
97
+ it "should print out the lines the user should put in the config file" do
98
+ out = @stdout.read
99
+ out.should match('Put the following in your Broadcast configuration file:')
100
+ out.should match('config.oauth.consumer_key')
101
+ out.should match('config.oauth.consumer_secret')
102
+ out.should match('config.oauth.access_token')
103
+ out.should match('config.oauth.access_secret')
104
+ end
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Twitter do
4
+
5
+ describe '.site' do
6
+
7
+ it "should be set to TwitterO OAuth endpoint" do
8
+ Broadcast::Medium::Twitter.site.should == 'http://api.twitter.com'
9
+ end
10
+
11
+ end
12
+
13
+ describe '#publish' do
14
+
15
+ it "should send a post to twitter with the message body" do
16
+ message = Broadcast::Message::SpecWithContent.new
17
+ medium = Broadcast::Medium::Twitter.new
18
+ token = mock
19
+ token.should_receive(:post).with("/1/statuses/update.json", {:status=>"message"})
20
+ medium.should_receive(:token).and_return(token)
21
+ medium.publish(message)
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium::Yammer do
4
+
5
+ describe '.site' do
6
+
7
+ it "should be set to Yammer OAuth endpoint" do
8
+ Broadcast::Medium::Yammer.site.should == 'https://www.yammer.com'
9
+ end
10
+
11
+ end
12
+
13
+ describe '#publish' do
14
+
15
+ it "should send a post to yammer with the message body" do
16
+ message = Broadcast::Message::SpecWithContent.new
17
+ medium = Broadcast::Medium::Yammer.new
18
+ token = mock
19
+ token.should_receive(:post).with("/api/v1/messages.json", { :body=>"message" })
20
+ medium.should_receive(:token).and_return(token)
21
+ medium.publish(message)
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Medium do
4
+
5
+ describe '#publish' do
6
+
7
+ before do
8
+ @medium = Broadcast::Medium::Spec.new
9
+ @message = Broadcast::Message::Spec.new
10
+ end
11
+
12
+ it "should properly load options" do
13
+ @medium.options.option.should == 1
14
+ end
15
+
16
+ it "should accept 1 argument" do
17
+ lambda {
18
+ @medium.publish(@message)
19
+ }.should_not raise_error(ArgumentError)
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe Broadcast::Message do
4
+
5
+ before do
6
+ Broadcast::Message::Spec.media = []
7
+ @log = StringIO.new
8
+ Broadcast.logger = Logger.new(@log)
9
+ end
10
+
11
+ describe ".initialize" do
12
+
13
+ it "should accept options" do
14
+ msg = Broadcast::Message::Spec.new(:foo => 1)
15
+ msg.options.foo.should == 1
16
+ end
17
+
18
+ end
19
+
20
+ describe ".medium" do
21
+
22
+ it "should add a medium to media" do
23
+ Broadcast::Message::Spec.medium :spec
24
+ Broadcast::Message::Spec.media.size.should == 1
25
+ Broadcast::Message::Spec.media.first[:name].should == :spec
26
+ end
27
+
28
+ it "should allow adding of options" do
29
+ Broadcast::Message::Spec.medium :spec, :option => 1
30
+ Broadcast::Message::Spec.media.size.should == 1
31
+ Broadcast::Message::Spec.media.first[:name].should == :spec
32
+ Broadcast::Message::Spec.media.first[:options].should == { :option => 1 }
33
+ end
34
+
35
+ end
36
+
37
+ describe "#body" do
38
+
39
+ it "should be empty by default" do
40
+ Broadcast::Message::Spec.new.body.should == ''
41
+ end
42
+
43
+ end
44
+
45
+ describe '#publish' do
46
+
47
+ it "should properly load all media of the message" do
48
+ Broadcast::Message::Spec.medium :spec
49
+ Broadcast::Message::Spec.new.publish
50
+ end
51
+
52
+ it "should properly instantiate all media of the message" do
53
+ medium = mock
54
+ medium.should_receive(:publish)
55
+ Broadcast::Medium::Spec.should_receive(:new).once.and_return(medium)
56
+ Broadcast::Message::Spec.medium :spec
57
+ Broadcast::Message::Spec.new.publish
58
+ end
59
+
60
+ it "should send to logger if medium instantiation fails" do
61
+ medium = mock
62
+ medium.should_receive(:publish).and_raise 'Some Exception'
63
+ Broadcast::Medium::Spec.should_receive(:new).once.and_return(medium)
64
+ Broadcast::Message::Spec.medium :spec
65
+ lambda {
66
+ Broadcast::Message::Spec.new.publish
67
+ }.should_not raise_error
68
+ @log.rewind
69
+ @log.read.should match('Publishing of Broadcast::Message::Spec to spec failed:\nSome Exception')
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ ORIGINAL_CONFIG = Broadcast.configuration
4
+
5
+ describe Broadcast do
6
+
7
+ describe '.publish' do
8
+
9
+ it "should properly load the message class and instantiate the message" do
10
+ msg = mock(:publish => true)
11
+ Broadcast::Message::Spec.should_receive(:new).once.and_return(msg)
12
+ Broadcast.publish(:spec)
13
+ end
14
+
15
+ it "should properly load the message class if in camelcase and instantiate the message" do
16
+ msg = mock(:publish => true)
17
+ Broadcast::Message::SpecWithContent.should_receive(:new).once.and_return(msg)
18
+ Broadcast.publish(:spec_with_content)
19
+ end
20
+
21
+ it "should call publish on the message" do
22
+ msg = mock
23
+ msg.should_receive(:publish)
24
+ Broadcast::Message::Spec.should_receive(:new).once.and_return(msg)
25
+ Broadcast.publish(:spec)
26
+ end
27
+
28
+ end
29
+
30
+ describe '.setup' do
31
+
32
+ before {
33
+ Broadcast.configuration = ORIGINAL_CONFIG
34
+ }
35
+
36
+ it "should allow setting of single settings" do
37
+ Broadcast.setup { |config|
38
+ config.some_setting = 1
39
+ }
40
+ Broadcast.configuration.should be_an_instance_of(Broadcast::Config)
41
+ Broadcast.configuration.some_setting.should == 1
42
+ end
43
+
44
+ it "should allow setting of namespaced settings" do
45
+ Broadcast.setup { |config|
46
+ config.log.some_setting = 1
47
+ }
48
+ Broadcast.configuration.log.should be_an_instance_of(Hashie::Mash)
49
+ Broadcast.configuration.log.some_setting.should == 1
50
+ end
51
+
52
+ it "should allow setting of namespaced settings using a block" do
53
+ Broadcast.setup { |config|
54
+ config.log { |log|
55
+ log.file = "foo.log"
56
+ log.color = 'red'
57
+ }
58
+ }
59
+ Broadcast.configuration.log.should be_an_instance_of(Hashie::Mash)
60
+ Broadcast.configuration.log.file.should == 'foo.log'
61
+ Broadcast.configuration.log.color.should == 'red'
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,6 @@
1
+ $:.push File.expand_path("../../lib", __FILE__)
2
+ require 'broadcast'
3
+ require 'rspec'
4
+
5
+ $:.push File.expand_path("../..", __FILE__)
6
+ Dir.glob('spec/support/*').each { |f| require f }
@@ -0,0 +1,43 @@
1
+ Broadcast.setup { |config|
2
+ config.spec.option = 1
3
+
4
+ config.oauth { |oauth|
5
+ oauth.consumer_key = 'consumerkey'
6
+ oauth.consumer_secret = 'consumersecret'
7
+ oauth.access_token = 'accesstoken'
8
+ oauth.access_secret = 'accesssecret'
9
+ }
10
+
11
+ config.jabber { |jabber|
12
+ jabber.username = 'foo@foo.com'
13
+ jabber.password = 'mypass'
14
+ jabber.recipients = 'mike@foo.com'
15
+ }
16
+
17
+ config.email { |email|
18
+ email.recipients = ['foo@moo.com']
19
+ email.delivery_method = :test
20
+ email.delivery_options = {
21
+ :address => "smtp.gmail.com",
22
+ :port => 587,
23
+ :domain => 'your.host.name',
24
+ :user_name => '<username>',
25
+ :password => '<password>',
26
+ :authentication => 'plain',
27
+ :enable_starttls_auto => true
28
+ }
29
+ }
30
+
31
+ config.campfire { |campfire|
32
+ campfire.subdomain = 'myaccount'
33
+ campfire.token = 'token'
34
+ campfire.room = 'My Room'
35
+ }
36
+
37
+ config.irc { |irc|
38
+ irc.username = 'foo'
39
+ irc.server = 'irc.freenode.net'
40
+ irc.port = '6667'
41
+ irc.channel = 'broadcast'
42
+ }
43
+ }
@@ -0,0 +1,11 @@
1
+ class Broadcast::Message::Spec < Broadcast::Message
2
+ end
3
+
4
+ class Broadcast::Message::SpecWithContent < Broadcast::Message
5
+ def body
6
+ "message"
7
+ end
8
+ end
9
+
10
+ class Broadcast::Medium::Spec < Broadcast::Medium
11
+ end