Skip to content

Commit

Permalink
Merge pull request #1278 from concord-consortium/187420676-research-c…
Browse files Browse the repository at this point in the history
…lasses-no-archived-resources

Filter out archived resources on Research Classes page, add basic Research Classes API tests
  • Loading branch information
pjanik committed Apr 12, 2024
2 parents 9fe15a1 + 9702df8 commit 9548f9c
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rails/app/controllers/api/v1/research_classes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def query(options, user)
teachers = Pundit.policy_scope(user, Portal::Teacher)
cohorts = Pundit.policy_scope(user, Admin::Cohort)
runnables = Pundit.policy_scope(user, Portal::Offering)
.joins("INNER JOIN external_activities ON external_activities.id = portal_offerings.runnable_id")
.where("portal_offerings.runnable_type = 'ExternalActivity'")
.joins("INNER JOIN external_activities ON external_activities.id = portal_offerings.runnable_id AND portal_offerings.runnable_type = 'ExternalActivity'")
.where(external_activities: { is_archived: false })
classes = Pundit.policy_scope(user, Portal::Clazz)

cc_teacher_ids = []
Expand Down
152 changes: 152 additions & 0 deletions rails/spec/controllers/api/v1/research_classes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
require 'spec_helper'

describe API::V1::ResearchClassesController do

before(:each) {
# This silences warnings in the console when running
generate_default_settings_with_mocks
}

before (:each) do
@teacher1 = FactoryBot.create(:portal_teacher)
@teacher2 = FactoryBot.create(:portal_teacher)

@project1 = FactoryBot.create(:project, name: 'Project 1')
@project2 = FactoryBot.create(:project, name: 'Project 2')

@cohort1 = FactoryBot.create(:admin_cohort, project: @project1)
@cohort2 = FactoryBot.create(:admin_cohort, project: @project2)

@teacher1.cohorts << @cohort1
@teacher2.cohorts << @cohort2

# test different tools
@runnable1 = FactoryBot.create(:external_activity)
@runnable2 = FactoryBot.create(:external_activity)

@offering1 = FactoryBot.create(:portal_offering, {clazz: @teacher1.clazzes[0], runnable: @runnable1})
@offering2 = FactoryBot.create(:portal_offering, {clazz: @teacher2.clazzes[0], runnable: @runnable2})

sign_in researcher
end

let(:researcher) do
user = FactoryBot.create(:confirmed_user)
user.add_role_for_project('researcher', @project1)
user
end

describe "anonymous' access" do
before (:each) do
logout_user
end
describe "GET index" do
it "wont allow index, returns error 403" do
get :index
expect(response.status).to eql(403)
end
end
end

describe "simple user access" do
let(:simple_user) { FactoryBot.create(:confirmed_user, :login => "authorized_student") }
before (:each) do
sign_in simple_user
end
describe "GET index" do
it "wont allow index, returns error 403" do
get :index
expect(response.status).to eql(403)
end
end
end

describe "project admin access" do
before (:each) do
project = FactoryBot.create(:project)
user = FactoryBot.create(:confirmed_user)
user.add_role_for_project('admin', project)
sign_in user
end

describe "GET index" do
it "allows index" do
get :index
expect(response.status).to eql(200)
end
end
end

describe "project researcher access" do
before (:each) do
sign_in researcher
end

describe "GET index" do
it "allows index" do
get :index
expect(response.status).to eql(200)
end
end
end

describe "basic query" do
describe "GET index" do
let (:params) do
{
project_id: "#{@project1.id}",
cohorts: "#{@cohort1.id}",
teachers: "#{@teacher1.id}",
runnables: "#{@runnable1.id}"
}
end
it "allows index" do
get :index, params: params
expect(response.status).to eql(200)
end
it "gets class info" do
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
clazz = @teacher1.clazzes[0]
expect(json["hits"]).to eql({"classes"=>[{"class_url"=>materials_portal_clazz_url(clazz.id, researcher: true), "cohort_names"=>@cohort1.name, "id"=>clazz.id, "name"=>clazz.name, "school_name"=>@teacher1.school.name, "teacher_names"=>@teacher1.name}]})
end
it "gets totals" do
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
expect(json["totals"]).to eql({"classes"=>1, "cohorts"=>1, "runnables"=>1, "teachers"=>1})
end
it "gets all teachers" do
params[:load_only] = "teachers"
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
expect(json["hits"]["teachers"].length).to eql(1)
end
it "gets all cohorts" do
params[:load_only] = "cohorts"
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
expect(json["hits"]["cohorts"].length).to eql(1)
expect(json["hits"]["cohorts"][0]).to eql({"id"=>@cohort1.id, "label"=>"Project 1: test cohort"})
end
it "gets all runnables" do
params[:load_only] = "runnables"
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
expect(json["hits"]["runnables"].length).to eql(1)
end
it "filters out archived runnables" do
@runnable1.update_attribute(:is_archived, true)
params[:load_only] = "runnables"
get :index, params: params
json = JSON.parse(response.body)
expect(response.status).to eql(200)
expect(json["hits"]["runnables"].length).to eql(0)
end
end
end
end

0 comments on commit 9548f9c

Please sign in to comment.