Skip to content

Commit 9bfe103

Browse files
authored
Post new users to courses mooc fi (#592)
* Remove last of unused gdocs functionality * Post new users to courses and move password management * Mock courses.mooc.fi requests in users controller tests * Improve logging when posting new users to courses.mooc.fi
1 parent b479379 commit 9bfe103

File tree

7 files changed

+68
-10
lines changed

7 files changed

+68
-10
lines changed

app/controllers/api/v8/users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ def create
157157
if @user.errors.empty? && @user.save
158158
# TODO: Whitelist origins
159159
UserMailer.email_confirmation(@user, params[:origin], params[:language]).deliver_now
160+
161+
# Post the new user to courses.mooc.fi for password management
162+
if params[:user][:password].present?
163+
@user.post_new_user_to_courses_mooc_fi(params[:user][:password])
164+
end
165+
160166
render json: build_success_response(params[:include_id])
161167
else
162168
errors = @user.errors

app/controllers/points_controller.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,6 @@ def index
4747
end
4848
end
4949

50-
def refresh_gdocs
51-
authorize! :refresh, @course
52-
@sheetname = params[:id]
53-
@course = Course.find(params[:course_id])
54-
@notifications = @course.refresh_gdocs_worksheet @sheetname
55-
end
56-
5750
def show
5851
@sheetname = params[:id]
5952
@course = Course.find(params[:course_id])

app/controllers/users_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ def create
4545

4646
if @user.errors.empty? && @user.save
4747
UserMailer.email_confirmation(@user).deliver_now
48+
49+
# Post the new user to courses.mooc.fi for password management
50+
if params[:user][:password].present?
51+
@user.post_new_user_to_courses_mooc_fi(params[:user][:password])
52+
end
53+
4854
if @bare_layout
4955
render plain: '<div class="success" style="font-size: 14pt; margin: 10pt;">User account created.</div>', layout: true
5056
else

app/models/ability.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ def initialize(user)
2020
can :disable, Organization
2121

2222
can :rerun, Submission
23-
can :refresh_gdocs_spreadsheet, Course do |c|
24-
c.spreadsheet_key.present?
25-
end
2623
can :access, :pghero
2724
can :read_vm_log, Submission
2825
can :read, :instance_state

app/models/user.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,55 @@ def update_password_via_courses_mooc_fi(old_password, new_password)
237237
end
238238
end
239239

240+
def post_new_user_to_courses_mooc_fi(password)
241+
Rails.logger.info("Posting new user #{self.email} to courses.mooc.fi")
242+
create_url = SiteSetting.value('courses_mooc_fi_create_user_url')
243+
244+
conn = Faraday.new do |f|
245+
f.request :json
246+
f.response :json
247+
end
248+
249+
begin
250+
response = conn.post(create_url) do |req|
251+
req.headers['Content-Type'] = 'application/json'
252+
req.headers['Accept'] = 'application/json'
253+
req.headers['Authorization'] = Rails.application.secrets.tmc_server_secret_for_communicating_to_secret_project
254+
255+
req.body = {
256+
upstream_id: id,
257+
password: password,
258+
}
259+
end
260+
261+
data = response.body
262+
263+
unless data.is_a?(Hash) && data['user'].present?
264+
Rails.logger.error("Creating user in courses.mooc.fi returned unexpected response for user #{self.email}: #{data}")
265+
raise "Creating user in courses.mooc.fi failed for user #{self.email}"
266+
end
267+
268+
unless data['password_set']
269+
Rails.logger.warn("Password was not set for user #{self.email} in courses.mooc.fi")
270+
end
271+
272+
Rails.logger.info("User #{self.email} successfully created in courses.mooc.fi")
273+
true
274+
275+
rescue Faraday::ClientError => e
276+
Rails.logger.error(
277+
"Creating user in courses.mooc.fi failed for user #{self.email}: #{e.response}"
278+
)
279+
false
280+
281+
rescue => e
282+
Rails.logger.error(
283+
"Unexpected error creating user in courses.mooc.fi for user #{self.email}: #{e.message}"
284+
)
285+
false
286+
end
287+
end
288+
240289
def password_reset_key
241290
action_tokens.find { |t| t.action == 'reset_password' }
242291
end

config/site.defaults.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ course_instruction_page: http://mooc.fi/courses/general/ohjelmointi/
138138

139139
# Teacher manual link
140140
teacher_manual_url: http://testmycode.github.io/tmc-server/usermanual/
141+
142+
# URLs for password management via courses.mooc.fi
143+
courses_mooc_fi_auth_url:
144+
courses_mooc_fi_update_password_url:
145+
courses_mooc_fi_create_user_url:

spec/controllers/users_controller_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
password: 'xoox',
5353
password_repeat: 'xoox'
5454
}
55+
# Mock the courses.mooc.fi integration to avoid HTTP calls in tests
56+
allow_any_instance_of(User).to receive(:post_new_user_to_courses_mooc_fi).and_return(true)
5557
end
5658

5759
it 'should create a new user account' do

0 commit comments

Comments
 (0)