Skip to content

Commit 406c81a

Browse files
authored
Get student name with project (#413)
## Status - Closes _add issue numbers or delete_ - Related to _add issue numbers or delete_ ## Points for consideration: - Security - Performance ## What's changed? _Description of what's been done - bullets are often best_ ## Steps to perform after deploying to production _If the production environment requires any extra work after this PR has been deployed detail it here. This could be running a Rake task, a migration, or upgrading a Gem. That kind of thing._
1 parent 9e04668 commit 406c81a

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

app/controllers/api/projects_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def index
1717
end
1818

1919
def show
20+
if !@project.school_id.nil? && @project.lesson_id.nil?
21+
project_with_user = @project.with_user(@current_user)
22+
@user = project_with_user[1]
23+
end
24+
2025
render :show, formats: [:json]
2126
end
2227

app/models/project.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def self.with_users(current_user)
3232
end
3333

3434
def with_user(current_user)
35-
school = School.find_by(id: :school_id)
35+
school = School.find_by(id: school_id)
3636
students = SchoolStudent::List.call(school:, token: current_user.token,
37-
student_ids: [:user_id])[:school_students] || []
37+
student_ids: [user_id])[:school_students] || []
3838
[self, students.first]
3939
end
4040

app/views/api/projects/show.json.jbuilder

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ json.image_list(@project.images) do |image|
2929
json.filename(image.filename)
3030
json.url(rails_blob_url(image))
3131
end
32+
33+
json.user_name(@user&.name) if @user.present?

spec/requests/projects/show_spec.rb

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@
33
require 'rails_helper'
44

55
RSpec.describe 'Project show requests' do
6-
let!(:project) { create(:project, user_id: owner.id, locale: nil) }
7-
let(:project_json) do
8-
{
9-
identifier: project.identifier,
10-
project_type: 'python',
11-
locale: project.locale,
12-
name: project.name,
13-
user_id: project.user_id,
14-
components: [],
15-
image_list: []
16-
}.to_json
17-
end
186
let(:headers) { {} }
19-
let(:owner) { create(:owner, school:) }
7+
let(:teacher) { create(:teacher, school:) }
208
let(:school) { create(:school) }
219

2210
context 'when user is logged in' do
2311
let(:headers) { { Authorization: UserProfileMock::TOKEN } }
2412

2513
before do
26-
authenticated_in_hydra_as(owner)
14+
authenticated_in_hydra_as(teacher)
15+
stub_profile_api_list_school_students(school:, student_attributes: [{ name: 'Joe Bloggs' }])
2716
end
2817

2918
context 'when loading own project' do
19+
let!(:project) { create(:project, user_id: teacher.id, locale: nil) }
20+
let(:project_json) do
21+
{
22+
identifier: project.identifier,
23+
project_type: 'python',
24+
locale: project.locale,
25+
name: project.name,
26+
user_id: project.user_id,
27+
components: [],
28+
image_list: []
29+
}.to_json
30+
end
31+
3032
it 'returns success response' do
3133
get("/api/projects/#{project.identifier}", headers:)
3234

@@ -44,6 +46,39 @@
4446
end
4547
end
4648

49+
context 'when loading a student\'s project' do
50+
let(:student) { create(:student, school:) }
51+
let(:lesson) { build(:lesson, school:, user_id: teacher.id, visibility: 'students') }
52+
let(:teacher_project) { create(:project, school_id: school.id, lesson_id: lesson.id, user_id: teacher.id, locale: nil) }
53+
let!(:student_project) { create(:project, school_id: school.id, lesson_id: nil, user_id: student.id, remixed_from_id: teacher_project.id, locale: nil) }
54+
let(:student_project_json) do
55+
{
56+
identifier: student_project.identifier,
57+
project_type: 'python',
58+
locale: student_project.locale,
59+
name: student_project.name,
60+
user_id: student_project.user_id,
61+
parent: {
62+
name: teacher_project.name,
63+
identifier: teacher_project.identifier
64+
},
65+
components: [],
66+
image_list: [],
67+
user_name: 'Joe Bloggs'
68+
}.to_json
69+
end
70+
71+
it 'returns success response' do
72+
get("/api/projects/#{student_project.identifier}", headers:)
73+
expect(response).to have_http_status(:ok)
74+
end
75+
76+
it 'includes the student\'s name in the project json' do
77+
get("/api/projects/#{student_project.identifier}", headers:)
78+
expect(response.body).to eq(student_project_json)
79+
end
80+
end
81+
4782
context 'when loading another user\'s project' do
4883
let!(:another_project) { create(:project, user_id: SecureRandom.uuid, locale: nil) }
4984
let(:another_project_json) do
@@ -115,6 +150,19 @@
115150
end
116151

117152
context 'when loading an owned project' do
153+
let!(:project) { create(:project, user_id: teacher.id, locale: nil) }
154+
let(:project_json) do
155+
{
156+
identifier: project.identifier,
157+
project_type: 'python',
158+
locale: project.locale,
159+
name: project.name,
160+
user_id: project.user_id,
161+
components: [],
162+
image_list: []
163+
}.to_json
164+
end
165+
118166
it 'returns forbidden response' do
119167
get("/api/projects/#{project.identifier}", headers:)
120168

0 commit comments

Comments
 (0)