|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | + |
| 4 | +describe Buff::Client do |
| 5 | + let(:id) { "5160746d54f04a5e3a00000f" } |
| 6 | + |
| 7 | + subject do |
| 8 | + Buff::Client.new("some_token") |
| 9 | + end |
| 10 | + |
| 11 | + describe "#initialize" do |
| 12 | + it "allows a token to be set and retrieved" do |
| 13 | + subject.access_token.should eq("some_token") |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + describe "#user_info" do |
| 18 | + let(:rash) { subject.user_info } |
| 19 | + |
| 20 | + before(:each) do |
| 21 | + stub_request(:get, 'https://api.bufferapp.com/1/user.json'). |
| 22 | + with(query: { 'access_token' => 'some_token'}). |
| 23 | + to_return(fixture('user_authenticated.txt')) |
| 24 | + end |
| 25 | + |
| 26 | + it "returns a Rash object" do |
| 27 | + rash.class.should eq(Buff::UserInfo) |
| 28 | + end |
| 29 | + |
| 30 | + it "provides an accessor for plan" do |
| 31 | + rash.plan.should eq("free") |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + describe "#profiles" do |
| 36 | + let(:rash) { Buff::Client.new("some_token").profiles } |
| 37 | + |
| 38 | + before(:each) do |
| 39 | + stub_request(:get, 'https://api.bufferapp.com/1/profiles.json'). |
| 40 | + with(query: { 'access_token' => 'some_token'}). |
| 41 | + to_return(fixture('profile_authenticated.txt')) |
| 42 | + end |
| 43 | + |
| 44 | + it "makes the correct url request" do |
| 45 | + subject.profiles |
| 46 | + end |
| 47 | + |
| 48 | + it "returns a Rash collection object" do |
| 49 | + rash[0].class.should eq(Buff::Profile) |
| 50 | + end |
| 51 | + |
| 52 | + it "provides an accessor for plan" do |
| 53 | + rash[0].service.should eq("twitter") |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + describe "#profile_by_id" do |
| 58 | + let(:id) { "5160746d54f04a5e3a00000f" } |
| 59 | + before(:each) do |
| 60 | + stub_request(:get, "https://api.bufferapp.com/1/profiles/#{id}.json"). |
| 61 | + with(query: { 'access_token' => 'some_token'}). |
| 62 | + to_return(fixture('profiles_by_id.txt')) |
| 63 | + end |
| 64 | + |
| 65 | + let(:rash) { Buff::Client.new("some_token").profile_by_id(id) } |
| 66 | + |
| 67 | + it "returns a rash collection" do |
| 68 | + rash.class.should eq(Buff::Profile) |
| 69 | + end |
| 70 | + |
| 71 | + it "accesses formatted service" do |
| 72 | + rash.formatted_service.should eq("Twitter") |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe "#profile_schedules_by_id" do |
| 77 | + before(:each) do |
| 78 | + stub_request(:get, "https://api.bufferapp.com/1/profiles/#{id}/schedules.json"). |
| 79 | + with(query: { 'access_token' => 'some_token'}). |
| 80 | + to_return(fixture('profile_schedules_by_id.txt')) |
| 81 | + end |
| 82 | + |
| 83 | + let(:rash) { Buff::Client.new("some_token").profile_schedules_by_id(id) } |
| 84 | + |
| 85 | + it "returns a rash collection" do |
| 86 | + rash[0].class.should eq(Buff::Schedule) |
| 87 | + end |
| 88 | + |
| 89 | + it "accesses days" do |
| 90 | + expect(rash[0].days).to include("mon") |
| 91 | + end |
| 92 | + |
| 93 | + it "accesses times" do |
| 94 | + expect(rash[0].times).to include("06:13") |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe "#set_schedules" do |
| 99 | + #not yet implemented |
| 100 | + xit "throw error if schedules is empty" do |
| 101 | + lambda { |
| 102 | + Buff::Client.new("some_token").set_schedules(id) }. |
| 103 | + should raise_error(ArgumentError) |
| 104 | + |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "#info" do |
| 109 | + before do |
| 110 | + stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token"). |
| 111 | + to_return(fixture("info.txt")) |
| 112 | + end |
| 113 | + |
| 114 | + it "connects to the correct endpoint" do |
| 115 | + subject.info |
| 116 | + end |
| 117 | + |
| 118 | + it "retrieves the correct name" do |
| 119 | + subject.info.services.twitter.types.profile.name.should eq("Twitter") |
| 120 | + end |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +describe Buff::Schedules do |
| 125 | + before do |
| 126 | + @schedule = JSON.parse <<EOF |
| 127 | + { |
| 128 | + "days" : [ |
| 129 | + "mon", |
| 130 | + "tue", |
| 131 | + "wed", |
| 132 | + "thu", |
| 133 | + "fri" |
| 134 | + ], |
| 135 | + "times" : [ |
| 136 | + "12:00", |
| 137 | + "17:00", |
| 138 | + "18:00" |
| 139 | + ] |
| 140 | + } |
| 141 | +EOF |
| 142 | + |
| 143 | + @sample_schedules = JSON.parse <<EOF |
| 144 | + { "schedules" : [{ |
| 145 | + "days" : [ |
| 146 | + "mon", |
| 147 | + "tue", |
| 148 | + "wed", |
| 149 | + "thu", |
| 150 | + "fri" |
| 151 | + ], |
| 152 | + "times" : [ |
| 153 | + "12:00", |
| 154 | + "17:00", |
| 155 | + "18:00" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "days" : [ |
| 160 | + "mon", |
| 161 | + "tue", |
| 162 | + "wed", |
| 163 | + "thu", |
| 164 | + "fri" |
| 165 | + ], |
| 166 | + "times" : [ |
| 167 | + "12:00", |
| 168 | + "17:00", |
| 169 | + "18:00" |
| 170 | + ] |
| 171 | + }] |
| 172 | + } |
| 173 | +EOF |
| 174 | + end |
| 175 | + |
| 176 | + it "accepts an array of days" do |
| 177 | + lambda { |
| 178 | + schedules = Buff::Schedules.new |
| 179 | + schedules << Buff::Schedule.new |
| 180 | + }.should_not raise_error |
| 181 | + end |
| 182 | + |
| 183 | + it "dumping a double schedule yields correct json" do |
| 184 | + schedules = Buff::Schedules.new |
| 185 | + schedules << @schedule |
| 186 | + schedules << @schedule |
| 187 | + @sample_schedules = @sample_schedules.to_json |
| 188 | + |
| 189 | + schedules.dump.should eq(@sample_schedules) |
| 190 | + end |
| 191 | +end |
| 192 | + |
| 193 | +describe Buff::Client::Core do |
| 194 | + |
| 195 | + let(:client) { Buff::Client.new("some_token") } |
| 196 | + describe "#get" do |
| 197 | + it "delegates to #handle_response_code when code != 200" do |
| 198 | + stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token"). |
| 199 | + to_return(:status => 403, :body => "", :headers => {}) |
| 200 | + client.should_receive(:handle_response_code).once |
| 201 | + client.info |
| 202 | + end |
| 203 | + |
| 204 | + |
| 205 | + it "does not delegate to #handle_response_code when code = 200" do |
| 206 | + stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token"). |
| 207 | + to_return(fixture("link.txt")) |
| 208 | + client.should_not_receive(:handle_response_code) |
| 209 | + client.info |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 213 | + describe "#post" do |
| 214 | + |
| 215 | + it "connects to the correct endpoint" do |
| 216 | + |
| 217 | +#TODO improve test |
| 218 | + response = %Q[{"success": true, "message": "Schedule saved successfully"}] |
| 219 | + id = "4eb854340acb04e870000010" |
| 220 | + stub_request(:post, %r{https://api\.bufferapp\.com/1/profiles/4eb854340acb04e870000010/schedules/update\.json\?access_token=.*}). |
| 221 | + with(:body => {"schedules"=>{"days"=>["mon", "tue", "wed"], "times"=>["12:00", "17:00", "18:00"]}}, |
| 222 | + :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.8.7'}). |
| 223 | + to_return(:status => 200, :body => response, :headers => {}) |
| 224 | + client.set_schedules(id, :schedules => sample_schedules).success. |
| 225 | + should eq(true) |
| 226 | + end |
| 227 | + |
| 228 | + xit "does not delegate to #handle_response_code when code = 200" do |
| 229 | + stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token"). |
| 230 | + to_return(fixture("link.txt")) |
| 231 | + client.should_not_receive(:handle_response_code) |
| 232 | + client.info |
| 233 | + end |
| 234 | + |
| 235 | + xit "transforms sample_schedule into correct formatted url" do |
| 236 | + u = Addressable::URI.new |
| 237 | + u.query_values = {schedules: sample_schedules[0]} |
| 238 | + u.query.should eq(post_data) |
| 239 | + end |
| 240 | + end |
| 241 | + |
| 242 | + describe "#handle_response_code" do |
| 243 | + context "fails gracefully with undocumented responses" do |
| 244 | + it "responds to 401 unauthorized response" do |
| 245 | + id = "4eb8565e0acb04bb82000004" |
| 246 | + stub_request(:get, "https://api.bufferapp.com/1/updates/#{id}.json?access_token=some_token"). |
| 247 | + to_return(fixture("update_by_id_non_auth.txt")) |
| 248 | + |
| 249 | + lambda { client.update_by_id(id) }. |
| 250 | + should raise_error(Buff::APIError) |
| 251 | + end |
| 252 | + end |
| 253 | + end |
| 254 | + |
| 255 | + describe "error_table" do |
| 256 | + it "should report 1001 as appropriate message" do |
| 257 | + table = Buff::Client::Core.error_table |
| 258 | + table['1001'].should eq("Access token required.") |
| 259 | + end |
| 260 | + end |
| 261 | +end |
0 commit comments