Skip to content

Commit 45d48e2

Browse files
authored
Merge pull request doubtfire-lms#517 from coskun-kilinc/fix/numbas-username-route
Fix/numbas username route
2 parents 8ed784d + 4474f71 commit 45d48e2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

app/api/scorm_api.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def stream_file_from_zip(zip_path, file_path)
5252
params do
5353
requires :task_def_id, type: Integer, desc: 'Task Definition ID to get SCORM test data for'
5454
end
55-
get '/scorm/:task_def_id/:username/:auth_token/*file_path' do
55+
56+
# NOTE: allow '.' in username; without this Rails/Grape treats '.' as format.
57+
get '/scorm/:task_def_id/:username/:auth_token/*file_path', requirements: { username: %r{[^/]+} } do
5658
task_def = TaskDefinition.find(params[:task_def_id])
5759

5860
unless authorise? current_user, task_def.unit, :get_unit

test/api/scorm_api_test.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def app
1010
end
1111

1212
def scorm_path(task_def, user, file, token_type = :scorm)
13-
"/api/scorm/#{task_def.id}/#{user.username.gsub('.', '%2e')}/#{auth_token(user, token_type)}/#{file}"
13+
# NOTE: Do not encode '.' to '%2e' here. That hid the bug because the request worked regardless
14+
# of the route constraint. Keeping the raw username ensures tests fail without the fix and pass
15+
# with it.
16+
"/api/scorm/#{task_def.id}/#{user.username}/#{auth_token(user, token_type)}/#{file}"
1417
end
1518

1619
def test_serve_scorm_content
@@ -86,4 +89,22 @@ def test_serve_scorm_content
8689
td.destroy!
8790
unit.destroy!
8891
end
92+
93+
def test_username_with_dot_fails_without_fix
94+
unit = FactoryBot.create(:unit)
95+
user = unit.projects.first.student
96+
user.update!(username: "user.name")
97+
98+
td = FactoryBot.create(:task_definition,
99+
unit: unit,
100+
tutorial_stream: unit.tutorial_streams.first,
101+
scorm_enabled: true
102+
)
103+
td.add_scorm_data(test_file_path("numbas.zip"), copy: true)
104+
td.save!
105+
106+
# This should fail without the route fix (Rails interprets '.' as format separator)
107+
get scorm_path(td, user, "index.html")
108+
assert_equal 200, last_response.status
109+
end
89110
end

0 commit comments

Comments
 (0)