Added migration for existing PushbulletAgents and fixed the specs

Dominik Sander 9 years ago
parent
commit
9902c2313c

+ 6 - 5
app/models/agents/pushbullet_agent.rb

@@ -44,7 +44,7 @@ module Agents
44 44
     end
45 45
 
46 46
     def validate_options
47
-      errors.add(:base, "you need to specify a pushbullet api_key") unless options['api_key'].present?
47
+      errors.add(:base, "you need to specify a pushbullet api_key") if options['api_key'].blank?
48 48
       errors.add(:base, "you need to specify a device_id") if options['device_id'].blank?
49 49
       errors.add(:base, "you need to specify a valid message type") if options['type'].blank? or not ['note', 'link', 'address'].include?(options['type'])
50 50
     end
@@ -64,15 +64,16 @@ module Agents
64 64
 
65 65
     def query_options(event)
66 66
       mo = interpolated(event)
67
-      body = {:device_iden => mo[:device_id], :type => mo[:type]}
68
-      if mo[:type] == "note"
67
+      body = {device_iden: mo[:device_id], type: mo[:type]}
68
+      case mo[:type]
69
+      when "note"
69 70
         body[:title] = mo[:title]
70 71
         body[:body] = mo[:body]
71
-      elsif mo[:type] == "link"
72
+      when "link"
72 73
         body[:title] = mo[:title]
73 74
         body[:body] = mo[:body]
74 75
         body[:url] = mo[:url]
75
-      elsif mo[:type] == "address"
76
+      when "address"
76 77
         body[:name] = mo[:name]
77 78
         body[:address] = mo[:address]
78 79
       end

+ 19 - 0
db/migrate/20150219213604_add_type_option_attribute_to_pushbullet_agents.rb

@@ -0,0 +1,19 @@
1
+class AddTypeOptionAttributeToPushbulletAgents < ActiveRecord::Migration
2
+  def up
3
+    Agents::PushbulletAgent.all.each do |agent|
4
+      if agent.options['type'].nil?
5
+        agent.options['type'] = 'note'
6
+        agent.save!
7
+      end
8
+    end
9
+  end
10
+
11
+  def down
12
+    Agents::PushbulletAgent.all.each do |agent|
13
+      if agent.options['type'].present?
14
+        agent.options.delete 'type'
15
+        agent.save(validate: false)
16
+      end
17
+    end
18
+  end
19
+end

+ 39 - 11
spec/models/agents/pushbullet_agent_spec.rb

@@ -3,10 +3,14 @@ require 'spec_helper'
3 3
 describe Agents::PushbulletAgent do
4 4
   before(:each) do
5 5
     @valid_params = {
6
-                      'api_key' => 'token',
6
+                      'api_key'   => 'token',
7 7
                       'device_id' => '124',
8
-                      'body' => '{{body}}',
9
-                      'title' => 'hello from huginn'
8
+                      'body'      => '{{body}}',
9
+                      'url'       => 'url',
10
+                      'name'      => 'name',
11
+                      'address'   => 'address',
12
+                      'title'     => 'hello from huginn',
13
+                      'type'      => 'note'
10 14
                     }
11 15
 
12 16
     @checker = Agents::PushbulletAgent.new(:name => "somename", :options => @valid_params)
@@ -36,18 +40,42 @@ describe Agents::PushbulletAgent do
36 40
   end
37 41
 
38 42
   describe "helpers" do
39
-    it "should return the query_options" do
40
-      expect(@checker.send(:query_options, @event)).to eq({
41
-        :body => {:title => 'hello from huginn', :body => 'One two test', :device_iden => @checker.options[:device_id], :type => 'note'},
42
-        :basic_auth => {:username =>@checker.options[:api_key], :password=>''}
43
-      })
43
+    before(:each) do
44
+      @base_options = {
45
+        body: { device_iden: @checker.options[:device_id] },
46
+        basic_auth: { username: @checker.options[:api_key], :password=>'' }
47
+      }
48
+    end
49
+    context "#query_options" do
50
+      it "should work for a note" do
51
+        options = @base_options.deep_merge({
52
+          body: {title: 'hello from huginn', body: 'One two test', type: 'note'}
53
+        })
54
+        expect(@checker.send(:query_options, @event)).to eq(options)
55
+      end
56
+
57
+      it "should work for a link" do
58
+        @checker.options['type'] = 'link'
59
+        options = @base_options.deep_merge({
60
+          body: {title: 'hello from huginn', body: 'One two test', type: 'link', url: 'url'}
61
+        })
62
+        expect(@checker.send(:query_options, @event)).to eq(options)
63
+      end
64
+
65
+      it "should work for an address" do
66
+        @checker.options['type'] = 'address'
67
+        options = @base_options.deep_merge({
68
+          body: {name: 'name', address: 'address', type: 'address'}
69
+        })
70
+        expect(@checker.send(:query_options, @event)).to eq(options)
71
+      end
44 72
     end
45 73
   end
46 74
 
47 75
   describe "#receive" do
48
-    it "send a message to the hipchat" do
76
+    it "send a note" do
49 77
       stub_request(:post, "https://token:@api.pushbullet.com/v2/pushes").
50
-        with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
78
+        with(:body => "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
51 79
         to_return(:status => 200, :body => "ok", :headers => {})
52 80
       dont_allow(@checker).error
53 81
       @checker.receive([@event])
@@ -55,7 +83,7 @@ describe Agents::PushbulletAgent do
55 83
 
56 84
     it "should log resquests which return an error" do
57 85
       stub_request(:post, "https://token:@api.pushbullet.com/v2/pushes").
58
-        with(:body => "device_iden=124&title=hello%20from%20huginn&body=One%20two%20test&type=note").
86
+        with(:body => "device_iden=124&type=note&title=hello%20from%20huginn&body=One%20two%20test").
59 87
         to_return(:status => 200, :body => "error", :headers => {})
60 88
       mock(@checker).error("error")
61 89
       @checker.receive([@event])