2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-28 11:39:42 +08:00
|
|
|
|
|
|
|
describe Admin::ReportsController do
|
|
|
|
it "is a subclass of AdminController" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(Admin::ReportsController < Admin::AdminController).to eq(true)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'while logged in as an admin' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2018-08-24 21:28:01 +08:00
|
|
|
describe '#bulk' do
|
|
|
|
context "valid params" do
|
|
|
|
it "renders the reports as JSON" do
|
|
|
|
Fabricate(:topic)
|
|
|
|
get "/admin/reports/bulk.json", params: {
|
|
|
|
reports: {
|
|
|
|
topics: { limit: 10 },
|
|
|
|
likes: { limit: 10 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body)["reports"].count).to eq(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "invalid params" do
|
|
|
|
context "inexisting report" do
|
2018-11-12 20:47:24 +08:00
|
|
|
it "returns not found reports" do
|
2018-08-24 21:28:01 +08:00
|
|
|
get "/admin/reports/bulk.json", params: {
|
|
|
|
reports: {
|
|
|
|
topics: { limit: 10 },
|
2018-11-12 20:47:24 +08:00
|
|
|
not_found: { limit: 10 }
|
2018-08-24 21:28:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2018-11-12 20:47:24 +08:00
|
|
|
expect(JSON.parse(response.body)["reports"].count).to eq(2)
|
2018-08-24 21:28:01 +08:00
|
|
|
expect(JSON.parse(response.body)["reports"][0]["type"]).to eq("topics")
|
2018-11-12 20:47:24 +08:00
|
|
|
expect(JSON.parse(response.body)["reports"][1]["type"]).to eq("not_found")
|
2018-08-24 21:28:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
describe '#show' do
|
2013-02-28 11:39:42 +08:00
|
|
|
context "invalid id form" do
|
|
|
|
let(:invalid_id) { "!!&asdfasdf" }
|
|
|
|
|
|
|
|
it "returns 404" do
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/#{invalid_id}.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(404)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "valid type form" do
|
|
|
|
context 'missing report' do
|
2018-06-11 12:49:28 +08:00
|
|
|
it "returns a 404 error" do
|
|
|
|
get "/admin/reports/nonexistent.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(404)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'a report is found' do
|
|
|
|
it "renders the report as JSON" do
|
2018-06-11 12:49:28 +08:00
|
|
|
Fabricate(:topic)
|
|
|
|
get "/admin/reports/topics.json"
|
2013-02-28 11:39:42 +08:00
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(JSON.parse(response.body)["report"]["total"]).to eq(1)
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-13 17:10:55 +08:00
|
|
|
describe 'when report is scoped to a category' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
fab!(:other_topic) { Fabricate(:topic) }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
it 'should render the report as JSON' do
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/topics.json", params: { category_id: category.id }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
report = JSON.parse(response.body)["report"]
|
|
|
|
|
|
|
|
expect(report["type"]).to eq('topics')
|
|
|
|
expect(report["data"].count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when report is scoped to a group' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:other_user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
it 'should render the report as JSON' do
|
|
|
|
group.add(user)
|
|
|
|
|
2018-06-11 12:49:28 +08:00
|
|
|
get "/admin/reports/signups.json", params: { group_id: group.id }
|
2017-04-13 17:10:55 +08:00
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-04-13 17:10:55 +08:00
|
|
|
|
|
|
|
report = JSON.parse(response.body)["report"]
|
|
|
|
|
|
|
|
expect(report["type"]).to eq('signups')
|
|
|
|
expect(report["data"].count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
2013-02-28 11:39:42 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|