|
185 | 185 | end |
186 | 186 |
|
187 | 187 | describe '#send_message' do |
188 | | - subject do |
189 | | - client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream') |
190 | | - end |
| 188 | + context 'when sending a message to a stream' do |
| 189 | + context 'with a stream ID' |
191 | 190 |
|
192 | | - context 'when the request succeeds' do |
193 | | - before do |
194 | | - response_body = { |
195 | | - id: 1, |
196 | | - msg: '', |
197 | | - result: 'success' |
198 | | - } |
| 191 | + context 'with a stream name' do |
| 192 | + subject { client.send_message(content: 'Hello world!', to: 'greetings', topic: 'hello') } |
199 | 193 |
|
200 | | - stub_request(:post, 'https://test.example.com/api/v1/messages'). |
201 | | - with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }). |
202 | | - to_return(body: JSON.generate(response_body)) |
203 | | - end |
| 194 | + context 'when the request succeeds' do |
| 195 | + before do |
| 196 | + response_body = { |
| 197 | + id: 1, |
| 198 | + msg: '', |
| 199 | + result: 'success' |
| 200 | + } |
204 | 201 |
|
205 | | - it 'returns a message' do |
206 | | - expect(subject.content).to eq('Hello world!') |
207 | | - expect(subject.id).to eq(1) |
208 | | - expect(subject.to).to eq('greetings') |
209 | | - expect(subject.topic).to eq('hello') |
210 | | - expect(subject.type).to eq('stream') |
211 | | - end |
212 | | - end |
| 202 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 203 | + with(body: { content: 'Hello world!', to: 'greetings', |
| 204 | + topic: 'hello', type: 'stream' }). |
| 205 | + to_return(body: JSON.generate(response_body)) |
| 206 | + end |
| 207 | + |
| 208 | + it 'returns a stream message' do |
| 209 | + expect(subject).to be_stream |
| 210 | + expect(subject.content).to eq('Hello world!') |
| 211 | + expect(subject.id).to eq(1) |
| 212 | + expect(subject.to).to eq('greetings') |
| 213 | + expect(subject.topic).to eq('hello') |
| 214 | + end |
| 215 | + end |
213 | 216 |
|
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 \'greetings\' does not exist', |
219 | | - result: 'error', |
220 | | - stream: 'greetings' |
221 | | - } |
| 217 | + context 'when the Zulip server returns an error' do |
| 218 | + before do |
| 219 | + response_body = { |
| 220 | + code: 'STREAM_DOES_NOT_EXIST', |
| 221 | + msg: 'Stream \'greetings\' does not exist', |
| 222 | + result: 'error', |
| 223 | + stream: 'greetings' |
| 224 | + } |
222 | 225 |
|
223 | | - stub_request(:post, 'https://test.example.com/api/v1/messages'). |
224 | | - with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }). |
225 | | - to_return(body: JSON.generate(response_body)) |
226 | | - end |
| 226 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 227 | + with(body: { content: 'Hello world!', to: 'greetings', topic: 'hello', type: 'stream' }). |
| 228 | + to_return(body: JSON.generate(response_body)) |
| 229 | + end |
227 | 230 |
|
228 | | - it 'raises ZulipError' do |
229 | | - expect { subject }.to raise_error(WonderLlama::ZulipError, |
230 | | - 'Stream \'greetings\' does not exist') |
| 231 | + it 'raises ZulipError' do |
| 232 | + expect { subject }.to raise_error(WonderLlama::ZulipError, |
| 233 | + 'Stream \'greetings\' does not exist') |
| 234 | + end |
| 235 | + end |
| 236 | + |
| 237 | + context 'when the API credentials are invalid' do |
| 238 | + before do |
| 239 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 240 | + to_return(status: 401) |
| 241 | + end |
| 242 | + |
| 243 | + include_examples 'raises AuthorizationError' |
| 244 | + end |
231 | 245 | end |
232 | 246 | end |
233 | 247 |
|
234 | | - context 'when the API credentials are invalid' do |
235 | | - before do |
236 | | - stub_request(:post, 'https://test.example.com/api/v1/messages'). |
237 | | - to_return(status: 401) |
238 | | - end |
| 248 | + context 'when sending a private message' do |
| 249 | + context 'with a list of user IDs' |
239 | 250 |
|
240 | | - include_examples 'raises AuthorizationError' |
| 251 | + context 'with a list of email addresses' do |
| 252 | + subject do |
| 253 | + client.send_message(content: 'Hello, friend!', |
| 254 | + to: ['friend1@example.com', 'friend2@example.com']) |
| 255 | + end |
| 256 | + |
| 257 | + context 'when the request succeeds' do |
| 258 | + before do |
| 259 | + response_body = { |
| 260 | + id: 2, |
| 261 | + msg: '', |
| 262 | + result: 'success' |
| 263 | + } |
| 264 | + |
| 265 | + json_encoded_to = JSON.generate(['friend1@example.com', 'friend2@example.com']) |
| 266 | + |
| 267 | + stub_request(:post, 'https://test.example.com/api/v1/messages'). |
| 268 | + with(body: { content: 'Hello, friend!', to: json_encoded_to, |
| 269 | + topic: nil, type: 'private' }). |
| 270 | + to_return(body: JSON.generate(response_body)) |
| 271 | + end |
| 272 | + |
| 273 | + it 'returns a private message' do |
| 274 | + expect(subject).to be_private |
| 275 | + expect(subject.content).to eq('Hello, friend!') |
| 276 | + expect(subject.id).to eq(2) |
| 277 | + expect(subject.to).to match_array(['friend1@example.com', 'friend2@example.com']) |
| 278 | + expect(subject.topic).to be_nil |
| 279 | + end |
| 280 | + end |
| 281 | + |
| 282 | + context 'when the Zulip server returns an error' |
| 283 | + |
| 284 | + context 'when the API credentials are invalid' |
| 285 | + end |
241 | 286 | end |
242 | 287 | end |
243 | 288 |
|
|
0 commit comments