Skip to content

Commit 87c1692

Browse files
committed
Add test coverage for sending messages using stream and user IDs
1 parent 715ce93 commit 87c1692

File tree

1 file changed

+149
-10
lines changed

1 file changed

+149
-10
lines changed

spec/client_spec.rb

Lines changed: 149 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,61 @@
186186

187187
describe '#send_message' do
188188
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
190244

191245
context 'with a stream name' do
192246
subject { client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello') }
@@ -224,7 +278,8 @@
224278
}
225279

226280
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' }).
228283
to_return(body: JSON.generate(response_body))
229284
end
230285

@@ -246,14 +301,75 @@
246301
end
247302

248303
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
250362

251363
context 'with a list of email addresses' do
252364
subject do
253-
client.send_message(content: 'Hello, friend!',
365+
client.send_message(content: 'Hello, friends!',
254366
to: ['friend1@example.com', 'friend2@example.com'])
255367
end
256368

369+
before do
370+
@json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com'])
371+
end
372+
257373
context 'when the request succeeds' do
258374
before do
259375
response_body = {
@@ -262,26 +378,49 @@
262378
result: 'success'
263379
}
264380

265-
json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com'])
266-
267381
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,
269383
topic: nil, type: 'private' }).
270384
to_return(body: JSON.generate(response_body))
271385
end
272386

273387
it 'returns a private message' do
274388
expect(subject).to be_private
275-
expect(subject.content).to eq('Hello, friend!')
389+
expect(subject.content).to eq('Hello, friends!')
276390
expect(subject.id).to eq(2)
277391
expect(subject.to).to match_array(['friend1@example.com', 'friend2@example.com'])
278392
expect(subject.topic).to be_nil
279393
end
280394
end
281395

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+
}
283403

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
285424
end
286425
end
287426
end

0 commit comments

Comments
 (0)