|
186 | 186 |
|
187 | 187 | describe '#send_message' do |
188 | 188 | context 'when sending a message to a stream' do |
189 | | - context 'with a stream ID' |
| 189 | + context 'with a stream ID' do |
| 190 | + subject { client.send_message(content: 'Hello world!', to: 789, topic: 'hello') } |
| 191 | + |
| 192 | + context 'when the request succeeds' do |
| 193 | + before do |
| 194 | + response_body = { |
| 195 | + id: 4, |
| 196 | + msg: '', |
| 197 | + result: 'success' |
| 198 | + } |
| 199 | + |
| 200 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 201 | + with(body: { content: 'Hello world!', to: 789, topic: 'hello', type: 'stream' }). |
| 202 | + to_return(body: JSON.generate(response_body)) |
| 203 | + end |
| 204 | + |
| 205 | + it 'returns a stream message' do |
| 206 | + expect(subject).to be_stream |
| 207 | + expect(subject.content).to eq('Hello world!') |
| 208 | + expect(subject.id).to eq(4) |
| 209 | + expect(subject.to).to eq(789) |
| 210 | + expect(subject.topic).to eq('hello') |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + context 'when the Zulip server returns an error' do |
| 215 | + before do |
| 216 | + response_body = { |
| 217 | + code: 'STREAM_DOES_NOT_EXIST', |
| 218 | + msg: 'Stream 789 does not exist', |
| 219 | + result: 'error', |
| 220 | + stream: 789 |
| 221 | + } |
| 222 | + |
| 223 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 224 | + with(body: { content: 'Hello world!', to: 789, |
| 225 | + topic: 'hello', type: 'stream' }). |
| 226 | + to_return(body: JSON.generate(response_body)) |
| 227 | + end |
| 228 | + |
| 229 | + it 'raises ZulipError' do |
| 230 | + expect { subject }.to raise_error(WonderLlama::ZulipError, |
| 231 | + 'Stream 789 does not exist') |
| 232 | + end |
| 233 | + end |
| 234 | + |
| 235 | + context 'when the API credentials are invalid' do |
| 236 | + before do |
| 237 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 238 | + to_return(status: 401) |
| 239 | + end |
| 240 | + |
| 241 | + include_examples 'raises AuthorizationError' |
| 242 | + end |
| 243 | + end |
190 | 244 |
|
191 | 245 | context 'with a stream name' do |
192 | 246 | subject { client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello') } |
|
224 | 278 | } |
225 | 279 |
|
226 | 280 | stub_request(:post, 'https://test.example.com/api/v1/messages'). |
227 | | - with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }). |
| 281 | + with(body: { content: 'Hello world!', to: 'greetings', |
| 282 | + topic: 'hello', type: 'stream' }). |
228 | 283 | to_return(body: JSON.generate(response_body)) |
229 | 284 | end |
230 | 285 |
|
|
246 | 301 | end |
247 | 302 |
|
248 | 303 | context 'when sending a private message' do |
249 | | - context 'with a list of user IDs' |
| 304 | + context 'with a list of user IDs' do |
| 305 | + subject { client.send_message(content: 'Hello, friends!', to: [123, 456]) } |
| 306 | + |
| 307 | + before do |
| 308 | + @json_encoded_to = JSON.generate([123, 456]) |
| 309 | + end |
| 310 | + |
| 311 | + context 'when the request succeeds' do |
| 312 | + before do |
| 313 | + response_body = { |
| 314 | + id: 3, |
| 315 | + msg: '', |
| 316 | + result: 'success' |
| 317 | + } |
| 318 | + |
| 319 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 320 | + with(body: { content: 'Hello, friends!', to: @json_encoded_to, |
| 321 | + topic: nil, type: 'private' }). |
| 322 | + to_return(body: JSON.generate(response_body)) |
| 323 | + end |
| 324 | + |
| 325 | + it 'returns a private message' do |
| 326 | + expect(subject).to be_private |
| 327 | + expect(subject.content).to eq('Hello, friends!') |
| 328 | + expect(subject.id).to eq(3) |
| 329 | + expect(subject.to).to match_array([123, 456]) |
| 330 | + expect(subject.topic).to be_nil |
| 331 | + end |
| 332 | + end |
| 333 | + |
| 334 | + context 'when the Zulip server returns an error' do |
| 335 | + before do |
| 336 | + response_body = { |
| 337 | + code: 'BAD_REQUEST', |
| 338 | + msg: 'Invalid user ID 123', |
| 339 | + result: 'error' |
| 340 | + } |
| 341 | + |
| 342 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 343 | + with(body: { content: 'Hello, friends!', to: @json_encoded_to, |
| 344 | + topic: nil, type: 'private' }). |
| 345 | + to_return(body: JSON.generate(response_body)) |
| 346 | + end |
| 347 | + |
| 348 | + it 'raises ZulipError' do |
| 349 | + expect { subject }.to raise_error(WonderLlama::ZulipError, 'Invalid user ID 123') |
| 350 | + end |
| 351 | + end |
| 352 | + |
| 353 | + context 'when the API credentials are invalid' do |
| 354 | + before do |
| 355 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 356 | + to_return(status: 401) |
| 357 | + end |
| 358 | + |
| 359 | + include_examples 'raises AuthorizationError' |
| 360 | + end |
| 361 | + end |
250 | 362 |
|
251 | 363 | context 'with a list of email addresses' do |
252 | 364 | subject do |
253 | | - client.send_message(content: 'Hello, friend!', |
| 365 | + client.send_message(content: 'Hello, friends!', |
254 | 366 | to: ['friend1@example.com', 'friend2@example.com']) |
255 | 367 | end |
256 | 368 |
|
| 369 | + before do |
| 370 | + @json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com']) |
| 371 | + end |
| 372 | + |
257 | 373 | context 'when the request succeeds' do |
258 | 374 | before do |
259 | 375 | response_body = { |
|
262 | 378 | result: 'success' |
263 | 379 | } |
264 | 380 |
|
265 | | - json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com']) |
266 | | - |
267 | 381 | stub_request(:post, 'https://test.example.com/api/v1/messages'). |
268 | | - with(body: { content: 'Hello, friend!', to: json_encoded_to, |
| 382 | + with(body: { content: 'Hello, friends!', to: @json_encoded_to, |
269 | 383 | topic: nil, type: 'private' }). |
270 | 384 | to_return(body: JSON.generate(response_body)) |
271 | 385 | end |
272 | 386 |
|
273 | 387 | it 'returns a private message' do |
274 | 388 | expect(subject).to be_private |
275 | | - expect(subject.content).to eq('Hello, friend!') |
| 389 | + expect(subject.content).to eq('Hello, friends!') |
276 | 390 | expect(subject.id).to eq(2) |
277 | 391 | expect(subject.to).to match_array(['friend1@example.com', 'friend2@example.com']) |
278 | 392 | expect(subject.topic).to be_nil |
279 | 393 | end |
280 | 394 | end |
281 | 395 |
|
282 | | - context 'when the Zulip server returns an error' |
| 396 | + context 'when the Zulip server returns an error' do |
| 397 | + before do |
| 398 | + response_body = { |
| 399 | + code: 'BAD_REQUEST', |
| 400 | + msg: 'Invalid email \'friend1@example.com\'', |
| 401 | + result: 'error' |
| 402 | + } |
283 | 403 |
|
284 | | - context 'when the API credentials are invalid' |
| 404 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 405 | + with(body: { content: 'Hello, friends!', to: @json_encoded_to, |
| 406 | + topic: nil, type: 'private' }). |
| 407 | + to_return(body: JSON.generate(response_body)) |
| 408 | + end |
| 409 | + |
| 410 | + it 'raises ZulipError' do |
| 411 | + expect { subject }.to raise_error(WonderLlama::ZulipError, |
| 412 | + 'Invalid email \'friend1@example.com\'') |
| 413 | + end |
| 414 | + end |
| 415 | + |
| 416 | + context 'when the API credentials are invalid' do |
| 417 | + before do |
| 418 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 419 | + to_return(status: 401) |
| 420 | + end |
| 421 | + |
| 422 | + include_examples 'raises AuthorizationError' |
| 423 | + end |
285 | 424 | end |
286 | 425 | end |
287 | 426 | end |
|
0 commit comments