From cae4f2997c6e16c06552bcb0c19897e501b98d9c Mon Sep 17 00:00:00 2001 From: "adeel.tajamul" Date: Tue, 21 Nov 2023 14:06:46 +0500 Subject: [PATCH] fix: 500 error if paginated_stats are empty --- api/users.rb | 18 ++++++++++++------ spec/api/user_spec.rb | 8 ++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/api/users.rb b/api/users.rb index bb85304cfc..8f5032044d 100644 --- a/api/users.rb +++ b/api/users.rb @@ -71,12 +71,18 @@ ] }} ]).to_a[0] - total_count = paginated_stats["pagination"][0]["total_count"] - num_pages = [1, (total_count / per_page.to_f).ceil].max - data = paginated_stats["data"].map do |user_stats| - { - :username => user_stats["username"] - }.merge(user_stats["course_stats"].except(*exclude_from_stats)) + data = [] + num_pages = 0 + page = 0 + total_count = 0 + if not paginated_stats["pagination"].empty? + total_count = paginated_stats["pagination"][0]["total_count"] + num_pages = [1, (total_count / per_page.to_f).ceil].max + data = paginated_stats["data"].map do |user_stats| + { + :username => user_stats["username"] + }.merge(user_stats["course_stats"].except(*exclude_from_stats)) + end end else # If a list of usernames is provided, then sort by the order in which those names appear diff --git a/spec/api/user_spec.rb b/spec/api/user_spec.rb index 9f7736a103..7bef755954 100644 --- a/spec/api/user_spec.rb +++ b/spec/api/user_spec.rb @@ -521,6 +521,14 @@ def build_structure_and_response(course_id, authors, build_initial_stats = true, expect(res["user_stats"]).to eq expected_result end + it "handle stats for user with no activity" do + invalid_course_id = "course-v1:edX+DNE+Not_EXISTS" + get "/api/v1/users/#{invalid_course_id}/stats" + expect(last_response.status).to eq(200) + res = parse(last_response.body) + expect(res["user_stats"]).to eq [] + end + it "returns user's stats filtered by user with default/activity sort" do usernames = %w[userauthor-1 userauthor-2 userauthor-3].sample(2) usernames = usernames.shuffle.join(',')