-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdating_a_lesson_spec.rb
131 lines (101 loc) · 4.35 KB
/
updating_a_lesson_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Updating a lesson', type: :request do
let(:headers) { { Authorization: UserProfileMock::TOKEN } }
let(:params) do
{
lesson: {
name: 'New Name'
}
}
end
let!(:lesson) { create(:lesson, name: 'Test Lesson', user_id: owner.id) }
let(:teacher) { create(:teacher, school:) }
let(:school) { create(:verified_school) }
let(:owner) { create(:owner, school:, name: 'School Owner') }
before do
authenticated_in_hydra_as(owner)
stub_user_info_api_for(teacher)
end
it 'responds 200 OK' do
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:ok)
end
it 'responds with the lesson JSON' do
put("/api/lessons/#{lesson.id}", headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:name]).to eq('New Name')
end
it 'responds with the user JSON' do
put("/api/lessons/#{lesson.id}", headers:, params:)
data = JSON.parse(response.body, symbolize_names: true)
expect(data[:user_name]).to eq('School Owner')
end
it 'responds 422 Unprocessable Entity when params are invalid' do
put("/api/lessons/#{lesson.id}", headers:, params: { lesson: { name: ' ' } })
expect(response).to have_http_status(:unprocessable_entity)
end
it 'responds 401 Unauthorized when no token is given' do
put("/api/lessons/#{lesson.id}", params:)
expect(response).to have_http_status(:unauthorized)
end
it "responds 403 Forbidden when the user is not the lesson's owner" do
lesson.update!(user_id: SecureRandom.uuid)
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:forbidden)
end
context 'when the lesson is associated with a school (library)' do
let!(:lesson) { create(:lesson, school:, name: 'Test Lesson', visibility: 'teachers', user_id: teacher.id) }
before do
lesson
end
it 'responds 200 OK when the user is a school-owner' do
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:ok)
end
it 'responds 200 OK when assigning the lesson to a school class' do
authenticated_in_hydra_as(teacher)
school_class = create(:school_class, school:, teacher_id: teacher.id)
new_params = { lesson: params[:lesson].merge(school_class_id: school_class.id) }
put("/api/lessons/#{lesson.id}", headers:, params: new_params)
expect(response).to have_http_status(:ok)
end
it "responds 403 Forbidden when the user a school-owner but visibility is 'private'" do
lesson.update!(visibility: 'private')
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:forbidden)
end
it 'responds 403 Forbidden when the user is another school-teacher in the school' do
teacher = create(:teacher, school:)
authenticated_in_hydra_as(teacher)
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:forbidden)
end
it 'responds 403 Forbidden when the user is a school-student' do
student = create(:student, school:)
authenticated_in_hydra_as(student)
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:forbidden)
end
end
context 'when the lesson is associated with a school class' do
let(:school) { create(:school) }
let(:school_class) { create(:school_class, teacher_id: teacher.id, school:) }
let!(:lesson) { create(:lesson, school_class:, name: 'Test Lesson', visibility: 'students', user_id: teacher.id) }
before do
authenticated_in_hydra_as(teacher)
end
it 'responds 200 OK when the user is a school-owner' do
put("/api/lessons/#{lesson.id}", headers:, params:)
expect(response).to have_http_status(:ok)
end
it 'responds 422 Unprocessable Entity when trying to re-assign the lesson to a different class' do
school = create(:school, id: SecureRandom.uuid)
teacher = create(:teacher, school:)
school_class = create(:school_class, school:, teacher_id: teacher.id)
new_params = { lesson: params[:lesson].merge(school_class_id: school_class.id) }
put("/api/lessons/#{lesson.id}", headers:, params: new_params)
expect(response).to have_http_status(:unprocessable_entity)
end
end
end