Skip to content

Commit 6843e7b

Browse files
committed
update tests
1 parent 22930ed commit 6843e7b

4 files changed

Lines changed: 67 additions & 18 deletions

File tree

spec/factories/project.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,10 @@
6767
instructions { Faker::Lorem.paragraph }
6868
school factory: %i[school]
6969
end
70+
71+
factory :scratch_project do
72+
project_type { Project::Types::CODE_EDITOR_SCRATCH }
73+
scratch_component
74+
end
7075
end
7176
end

spec/features/scratch/creating_a_scratch_project_spec.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,35 @@ def make_request(query: request_query, request_headers: headers, request_params:
5454
expect(response).to have_http_status(:unauthorized)
5555
end
5656

57-
it 'responds 404 Not Found when user is not part of a school' do
58-
user = create(:user)
59-
authenticated_in_hydra_as(user)
57+
context 'when authenticated but not part of a school' do
58+
let(:user) { create(:user) }
6059

61-
make_request
60+
before do
61+
authenticated_in_hydra_as(user)
62+
end
63+
64+
it 'responds 403 forbidden when the user cannot access the original project' do
65+
make_request
6266

63-
expect(response).to have_http_status(:not_found)
67+
expect(response).to have_http_status(:forbidden)
68+
end
69+
70+
context 'when the original project is anonymous scratch project' do
71+
let(:original_project) do
72+
create(
73+
:project,
74+
user_id: nil,
75+
project_type: Project::Types::CODE_EDITOR_SCRATCH,
76+
locale: 'en'
77+
)
78+
end
79+
80+
it 'responds 200 ok' do
81+
make_request
82+
83+
expect(response).to have_http_status(:ok)
84+
end
85+
end
6486
end
6587

6688
context 'when authenticated and part of a school' do

spec/features/scratch/creating_and_showing_a_scratch_asset_spec.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,29 @@
2020
expect(response).to have_http_status(:bad_request)
2121
end
2222

23-
context 'when the user can view the project' do
23+
context 'when the user is not logged in' do
24+
let(:project_headers) { { 'X-Project-ID' => project.identifier } }
25+
26+
it 'responds with unathorized' do
27+
create_uploaded_scratch_asset(filename: 'test_image_1.png', project:, body: 'global-body')
28+
get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/', headers: project_headers
29+
30+
expect(response).to have_http_status(:unauthorized)
31+
end
32+
33+
context 'when the project is anonymous' do
34+
let(:project) { create_scratch_project(locale: 'en', user_id: nil) }
35+
36+
it 'responds with 200 ok' do
37+
create_uploaded_scratch_asset(filename: 'test_image_1.png', project: nil, body: 'global-body')
38+
get '/api/scratch/assets/internalapi/asset/test_image_1.png/get/', headers: project_headers
39+
40+
expect(response).to have_http_status(:found)
41+
end
42+
end
43+
end
44+
45+
context 'when the project owner is logged in' do
2446
before do
2547
authenticated_in_hydra_as(teacher)
2648
end
@@ -385,15 +407,6 @@
385407

386408
expect(response).to have_http_status(:unauthorized)
387409
end
388-
389-
it 'responds 404 Not Found when user is not part of a school' do
390-
user = create(:user)
391-
authenticated_in_hydra_as(user)
392-
393-
post '/api/scratch/assets/example.svg', headers: project_headers
394-
395-
expect(response).to have_http_status(:not_found)
396-
end
397410
end
398411

399412
def create_scratch_project(**attributes)

spec/features/scratch/showing_a_scratch_project_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RSpec.describe 'Showing a Scratch project', type: :request do
66
let(:school) { create(:school) }
77
let(:teacher) { create(:teacher, school:) }
8+
let(:student) { create(:student, school:) }
89
let(:headers) { { 'Authorization' => UserProfileMock::TOKEN } }
910
let(:school_class) { create(:school_class, school:, teacher_ids: [teacher.id]) }
1011
let(:lesson) { create(:lesson, school:, school_class:, user_id: teacher.id) }
@@ -73,11 +74,19 @@
7374
expect(response).to have_http_status(:not_found)
7475
end
7576

76-
it 'returns a 401 unauthorized if not logged in' do
77-
project = create(:project, project_type: Project::Types::PYTHON, locale: 'en')
77+
it 'returns a 200 ok if not logged in for an anonymous scratch project' do
78+
project = create(:scratch_project, locale: 'en', user_id: nil)
7879
get "/api/scratch/projects/#{project.identifier}"
7980

80-
expect(response).to have_http_status(:unauthorized)
81+
expect(response).to have_http_status(:ok)
82+
end
83+
84+
it 'returns a 200 ok if logged in for an anonymous scratch project' do
85+
authenticated_in_hydra_as(student)
86+
project = create(:scratch_project, locale: 'en', user_id: nil)
87+
get "/api/scratch/projects/#{project.identifier}"
88+
89+
expect(response).to have_http_status(:ok)
8190
end
8291

8392
it 'returns a 403 forbidden if user does not have access to the project' do

0 commit comments

Comments
 (0)