Skip to content

Commit 4a8bc6d

Browse files
authored
Add rake task for removing teacher role (#785)
Based on the instructions here: https://digital-docs.rpf-internal.org/docs/technology/codebases-and-products/editor/processes/code-editor-for-education/removing-teachers Key differences: - The instructions say to connect to Profile's Postgres instance to lookup the user ID, but we instead use the `UserInfoApiClient` for that here. - This task doesn't ask for a school, since it should not be possible for the user to have roles in more than one school (see `role.rb`). ## Status - Related to RaspberryPiFoundation/digital-editor-issues#1318 ## What's changed? Adds Rake task to remove a user's current teacher role given their email address. ## Steps to perform after deploying to production Update https://digital-docs.rpf-internal.org/docs/technology/codebases-and-products/editor/processes/code-editor-for-education/removing-teachers.
1 parent 98b5aa6 commit 4a8bc6d

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

lib/tasks/remove_teacher.rake

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
namespace :remove_teacher do
4+
desc 'Remove teacher from their school'
5+
task :run, %i[teacher_email] => :environment do |_task, args|
6+
teacher = UserInfoApiClient.find_user_by_email(args[:teacher_email])
7+
8+
unless teacher
9+
Rails.logger.error("No user found for email '#{args[:teacher_email]}'. Did you spell it correctly?")
10+
next
11+
end
12+
13+
role = Role.find_by(roles: { user_id: teacher[:id], role: 'teacher' })
14+
15+
unless role
16+
Rails.logger.error("No teacher role found for user with email '#{args[:teacher_email]}'. Is this the right user?")
17+
next
18+
end
19+
20+
Rails.logger.info("Deleting '#{role[:role]}' role for user '#{args[:teacher_email]}' in school '#{role.school.name}'")
21+
role.destroy!
22+
23+
Rails.logger.info "Removed teacher role for #{args[:teacher_email]} successfully."
24+
Rails.logger.warn '⚠️ You must now manually remove the teacher safeguarding flag for the teacher.'
25+
Rails.logger.warn 'Open a bash console on the rpf-profile app: `heroku run bash -a rpf-profile`'
26+
Rails.logger.warn "Remove the teacher safeguarding flag: `node profile-cli remove-safeguarding-flag #{args[:teacher_email]} school:teacher`"
27+
end
28+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
require 'rake'
5+
6+
RSpec.describe 'remove_teacher', type: :task do
7+
describe ':run' do
8+
let(:task) { Rake::Task['remove_teacher:run'] }
9+
let(:owner_id) { SecureRandom.uuid }
10+
let(:student_id) { SecureRandom.uuid }
11+
let(:school) { create(:school, creator_id: owner_id) }
12+
let(:teacher_id) { SecureRandom.uuid }
13+
14+
before do
15+
stub_user_info_api_find_by_email(
16+
email: 'teacher@example.com',
17+
user: { id: teacher_id, email: 'teacher@example.com' }
18+
)
19+
20+
stub_user_info_api_find_by_email(
21+
email: 'owner@example.com',
22+
user: { id: owner_id, email: 'owner@example.com' }
23+
)
24+
25+
stub_user_info_api_find_by_email(
26+
email: 'student@example.com',
27+
user: { id: student_id, email: 'student@example.com' }
28+
)
29+
30+
create(:teacher_role, school:, user_id: teacher_id)
31+
create(:owner_role, school:, user_id: owner_id)
32+
create(:student_role, school:, user_id: student_id)
33+
end
34+
35+
it "exits early if the user doesn't exist" do
36+
# Arrange
37+
allow(UserInfoApiClient).to receive(:find_user_by_email)
38+
.with('not_real_teacher@example.com')
39+
.and_return(nil)
40+
41+
# Act
42+
task.invoke('not_real_teacher@example.com')
43+
44+
# assert
45+
expect(Role.find_by(user_id: teacher_id)).not_to be_nil
46+
end
47+
48+
it 'exits early if the user has no teacher role' do
49+
# Act
50+
task.invoke('student@example.com')
51+
52+
# Assert
53+
expect(Role.find_by(user_id: student_id)).not_to be_nil
54+
end
55+
56+
it 'removes the role if it exists' do
57+
# Act
58+
task.invoke('teacher@example.com')
59+
60+
# Assert
61+
expect(Role.find_by(user_id: teacher_id)).to be_nil
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)