Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default assignments on classroom join #73

Merged
merged 5 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion app/operations/classrooms/join.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class Join < Operation
validates :join_token, presence: true

def execute
classroom = Classroom.active.find_by!(id: id, join_token: join_token)
response = join_panoptes_group(classroom)

if response
classroom.students << current_user
add_user_to_assignments unless classroom.program.custom?
else
errors.add(:id, "cannot join")
end
Expand All @@ -24,5 +24,19 @@ def join_panoptes_group(classroom)
rescue Panoptes::Client::ServerError
false
end

def add_user_to_assignments
classroom.assignments.each do |assignment|
assignment.student_users << student_user
end
end

def classroom
Classroom.active.find_by!(id: id, join_token: join_token)
end

def student_user
StudentUser.where(user_id: current_user.id, classroom_id: classroom.id).first
end
end
end
6 changes: 3 additions & 3 deletions spec/controllers/students/classrooms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
end

describe "POST join" do
let(:classroom) { create(:classroom) }
it "joins a classroom" do
expect(user_client).to receive(:join_user_group).with(nil, "9999", join_token: "asdf").and_return(true)
classroom = Classroom.create! name: '1', join_token: 'asdf'
post :join, params: {id: classroom.id, join_token: 'asdf'}, as: :json
expect(user_client).to receive(:join_user_group).with(classroom.zooniverse_group_id, current_user.zooniverse_id, join_token: classroom.join_token).and_return(true)
post :join, params: {id: classroom.id, join_token: classroom.join_token}, as: :json
expect(response.body).to eq(ActiveModelSerializers::SerializableResource.new(classroom, include: [:students]).to_json)
end
end
Expand Down
5 changes: 3 additions & 2 deletions spec/controllers/teachers/classrooms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
before { authenticate! }

describe "GET index" do
let(:program) {create(:program)}

it "returns an empty list if there are no classrooms" do
program = create(:program)
get :index, params: { program_id: program.id }, format: :json
expect(parsed_response).to eq("data" => [])
end

it 'returns the classrooms with students' do
classroom = create :classroom, name: 'Foo', zooniverse_group_id: 'asdf', join_token: 'abc', teachers: [current_user]
classroom = create :classroom, name: 'Foo', zooniverse_group_id: 'asdf', join_token: 'abc', teachers: [current_user], program: program
student = classroom.students.create! zooniverse_id: 'zoo1'

get :index, params: { program_id: classroom.program.id }, format: :json
Expand Down
3 changes: 1 addition & 2 deletions spec/factories/classroom_factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
name "Foo"
zooniverse_group_id "1"
join_token "asdf"

association :program, factory: :program
program

trait :deleted do
deleted_at { Time.now }
Expand Down
5 changes: 2 additions & 3 deletions spec/operations/assignments/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
let(:client) { instance_double(Panoptes::Client) }
let(:operation) { described_class.with(current_user: current_user, client: client) }

let(:program) { create :program }
let(:classroom) { create :classroom, teachers: [current_user], program: program }
let(:classroom) { create :classroom, teachers: [current_user] }

let(:custom_program) { create(:program, custom: true) }
let(:custom_classroom) { create :classroom, teachers: [current_user], program: custom_program }

let(:programless_classroom) { create :classroom, teachers: [current_user] }
let(:programless_classroom) { create :classroom, teachers: [current_user], program: nil}

let(:workflow_id) { 999 }

Expand Down
29 changes: 27 additions & 2 deletions spec/operations/classrooms/join_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'spec_helper'

RSpec.describe Classrooms::Join do
let(:current_user) { build :user }
let(:current_user) { create(:user) }
let(:client) { instance_double(Panoptes::Client) }
let(:classroom) { create :classroom }
let(:classroom) { create(:classroom) }

before { allow(client).to receive(:join_user_group).and_return(true) }

Expand All @@ -29,4 +29,29 @@
expect(classroom.students).to include(current_user)
expect(client).not_to have_received(:join_user_group)
end

describe "assignment assignment" do
describe "custom programs" do
let(:custom_program) { create(:program, custom: true) }
let(:custom_classroom) { create(:classroom, program: custom_program) }
let!(:custom_assignment) { create(:assignment, classroom: custom_classroom) }

it "doesn't affect assignments" do
expect{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parenthesize the param change{ custom_assignment.student_users } to make sure that the block will be associated with the change method call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like a known rubocop bug (rubocop/rubocop#4198), updating might fix.

outcome = described_class.run!(current_user: current_user, client: client,
id: classroom.id, join_token: classroom.join_token)
}.to_not change{ custom_assignment.student_users }
end
end

describe "standard programs" do
let!(:standard_assignment) { create(:assignment, classroom: classroom) }

it "adds the user to all assignments" do
outcome = described_class.run!(current_user: current_user, client: client,
id: classroom.id, join_token: classroom.join_token)
expect(standard_assignment.student_users.map(&:user_id)).to include(current_user.id)
end
end
end
end
3 changes: 1 addition & 2 deletions spec/operations/classrooms/student_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
RSpec.describe Classrooms::StudentIndex do
let(:current_user) { User.new zooniverse_id: 1 }
let(:client) { instance_double(Panoptes::Client, join_user_group: true) }
let(:program) { create(:program) }
let(:classroom) { Classroom.create! join_token: 'abc', zooniverse_group_id: "asdf", program: program }
let(:classroom) { create(:classroom, join_token: 'abc', zooniverse_group_id: "asdf") }

it 'returns classrooms the current user is a student of' do
Classrooms::Join.run! current_user: current_user, client: client, id: classroom.id, join_token: classroom.join_token
Expand Down
8 changes: 4 additions & 4 deletions spec/operations/classrooms/teacher_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
end

it 'filters by program id' do
program = create(:program)
new_program = create(:program)
classroom = create :classroom, teachers: [current_user]
program_classroom = create :classroom, teachers: [current_user], program: program
result = operation.run! program_id: program.id
expect(result).to include(program_classroom)
new_program_classroom = create :classroom, teachers: [current_user], program: new_program
result = operation.run! program_id: new_program.id
expect(result).to include(new_program_classroom)
expect(result).not_to include(classroom)
end
end