Skip to content

Commit b9b84fa

Browse files
authored
Add more tests for get followers API (#467)
This change re-wrote some test cases slightly and adds a case where there is access twice in one scenario. This is because, in the past, there were cases in other bot SDKs where making a request twice would cause issues. As we can see from the code, this doesn't happen in the Ruby SDK, but this test is just to be safe....
1 parent 780aaa4 commit b9b84fa

1 file changed

Lines changed: 94 additions & 38 deletions

File tree

spec/line/bot/v2/misc_spec.rb

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -295,54 +295,110 @@
295295
let(:client) { Line::Bot::V2::MessagingApi::ApiClient.new(channel_access_token: 'test-channel-access-token') }
296296
let(:response_code) { 200 }
297297

298-
it 'returns a list of followers successfully without optional parameters' do
299-
response_body = { "userIds" => ["U1234567890", "U0987654321"] }.to_json
300-
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
301-
.with(
302-
headers: {
303-
'Authorization' => "Bearer test-channel-access-token"
304-
}
305-
)
306-
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
298+
context 'when no optional parameters given' do
299+
it 'returns a list of followers successfully (strict check without any query params)' do
300+
response_body = { "userIds" => ["U1234567890", "U0987654321"] }.to_json
301+
302+
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
303+
.with(
304+
headers: {
305+
'Authorization' => "Bearer test-channel-access-token"
306+
},
307+
query: {}
308+
)
309+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
307310

308-
body, status_code, headers = client.get_followers_with_http_info
311+
body, status_code, headers = client.get_followers_with_http_info
309312

310-
expect(status_code).to eq(200)
311-
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
313+
expect(status_code).to eq(200)
314+
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
315+
end
312316
end
313317

314-
it 'query with only start' do
315-
response_body = { "userIds" => ["U1234567890", "U0987654321"], "next" => "nExT Token" }.to_json
316-
stub_request(:get, "https://api.line.me/v2/bot/followers/ids?start=from%20previous%20NEXT")
317-
.with(
318-
headers: {
319-
'Authorization' => "Bearer test-channel-access-token"
320-
}
321-
)
322-
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
318+
context 'when only start parameter is given' do
319+
it 'returns a list of followers with next token (strict check with only "start")' do
320+
response_body = { "userIds" => ["U1234567890", "U0987654321"], "next" => "nExT Token" }.to_json
321+
322+
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
323+
.with(
324+
headers: { 'Authorization' => "Bearer test-channel-access-token" },
325+
query: { "start" => "from previous NEXT" }
326+
)
327+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
323328

324-
body, status_code, headers = client.get_followers_with_http_info(start: "from previous NEXT")
329+
body, status_code, headers = client.get_followers_with_http_info(start: "from previous NEXT")
325330

326-
expect(status_code).to eq(200)
327-
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
328-
expect(body._next).to eq("nExT Token")
331+
expect(status_code).to eq(200)
332+
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
333+
expect(body._next).to eq("nExT Token")
334+
end
329335
end
330336

331-
it 'query with limit and start' do
332-
response_body = { "userIds" => ["U1234567890", "U0987654321"], "next" => "nExT Token" }.to_json
333-
stub_request(:get, "https://api.line.me/v2/bot/followers/ids?limit=10&start=from%20previous%20NEXT")
334-
.with(
335-
headers: {
336-
'Authorization' => "Bearer test-channel-access-token"
337-
}
338-
)
339-
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
337+
context 'when limit and start parameters are both given' do
338+
it 'returns a list of followers with next token (strict check with "limit" and "start")' do
339+
response_body = { "userIds" => ["U1234567890", "U0987654321"], "next" => "nExT Token" }.to_json
340+
341+
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
342+
.with(
343+
headers: { 'Authorization' => "Bearer test-channel-access-token" },
344+
query: { "limit" => "10", "start" => "from previous NEXT" }
345+
)
346+
.to_return(status: response_code, body: response_body, headers: { 'Content-Type' => 'application/json' })
340347

341-
body, status_code, headers = client.get_followers_with_http_info(start: "from previous NEXT", limit: 10)
348+
body, status_code, headers = client.get_followers_with_http_info(start: "from previous NEXT", limit: 10)
342349

343-
expect(status_code).to eq(200)
344-
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
345-
expect(body._next).to eq("nExT Token")
350+
expect(status_code).to eq(200)
351+
expect(body.user_ids).to eq(["U1234567890", "U0987654321"])
352+
expect(body._next).to eq("nExT Token")
353+
end
354+
end
355+
356+
context 'when making two consecutive requests in one test' do
357+
it 'handles two requests, first with the same params and second with different params' do
358+
first_response_body = {
359+
"userIds" => ["U1111111111", "U2222222222"],
360+
"next" => "firstNextToken"
361+
}.to_json
362+
second_response_body = {
363+
"userIds" => ["U3333333333", "U4444444444"],
364+
}.to_json
365+
366+
# First request: using start="from previous NEXT", limit=10
367+
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
368+
.with(
369+
headers: { 'Authorization' => "Bearer test-channel-access-token" },
370+
query: { "start" => "from previous NEXT", "limit" => "10" }
371+
)
372+
.to_return(
373+
status: 200,
374+
body: first_response_body,
375+
headers: { 'Content-Type' => 'application/json' }
376+
)
377+
378+
# Second request: using start="anotherParam", limit=5
379+
stub_request(:get, "https://api.line.me/v2/bot/followers/ids")
380+
.with(
381+
headers: { 'Authorization' => "Bearer test-channel-access-token" },
382+
query: { "start" => "anotherParam", "limit" => "5" }
383+
)
384+
.to_return(
385+
status: 200,
386+
body: second_response_body,
387+
headers: { 'Content-Type' => 'application/json' }
388+
)
389+
390+
# ---- First access ----
391+
body1, status_code1, headers1 = client.get_followers_with_http_info(start: "from previous NEXT", limit: 10)
392+
expect(status_code1).to eq(200)
393+
expect(body1.user_ids).to eq(["U1111111111", "U2222222222"])
394+
expect(body1._next).to eq("firstNextToken")
395+
396+
# ---- Second access ----
397+
body2, status_code2, headers2 = client.get_followers_with_http_info(start: "anotherParam", limit: 5)
398+
expect(status_code2).to eq(200)
399+
expect(body2.user_ids).to eq(["U3333333333", "U4444444444"])
400+
expect(body2._next).to be_nil
401+
end
346402
end
347403
end
348404

0 commit comments

Comments
 (0)