|
720 | 720 | expect(flash[:notice]).not_to match(/has been approved/) |
721 | 721 | end |
722 | 722 | end |
| 723 | + |
| 724 | + describe 'Mass-assignment and IDOR protections' do |
| 725 | + let(:other_user) { User.create!(email: 'other@example.com', canvas_uid: '777', name: 'Other Student') } |
| 726 | + let(:foreign_course) { create(:course, course_name: 'Foreign Course', canvas_id: '888', course_code: 'FOR101') } |
| 727 | + let(:foreign_assignment) { foreign_course.assignments.first } |
| 728 | + |
| 729 | + describe 'POST #create' do |
| 730 | + it 'ignores user_id in params and assigns the request to the current user' do |
| 731 | + post :create, params: { |
| 732 | + course_id: course.id, |
| 733 | + request: { |
| 734 | + assignment_id: assignment.id, |
| 735 | + reason: 'Sick', |
| 736 | + requested_due_date: Date.tomorrow.to_s, |
| 737 | + due_time: '10:00', |
| 738 | + user_id: other_user.id |
| 739 | + } |
| 740 | + } |
| 741 | + |
| 742 | + expect(Request.last.user).to eq(user) |
| 743 | + expect(Request.last.user).not_to eq(other_user) |
| 744 | + end |
| 745 | + |
| 746 | + it 'rejects an assignment that belongs to another course' do |
| 747 | + post :create, params: { |
| 748 | + course_id: course.id, |
| 749 | + request: { |
| 750 | + assignment_id: foreign_assignment.id, |
| 751 | + reason: 'Sick', |
| 752 | + requested_due_date: Date.tomorrow.to_s, |
| 753 | + due_time: '10:00' |
| 754 | + } |
| 755 | + } |
| 756 | + |
| 757 | + expect(response).to redirect_to(course_requests_path(course)) |
| 758 | + expect(flash[:alert]).to match(/Assignment not found for this course/) |
| 759 | + expect(Request.last).to be_nil |
| 760 | + end |
| 761 | + end |
| 762 | + |
| 763 | + describe 'PATCH #update' do |
| 764 | + it 'rejects reassigning to an assignment from another course' do |
| 765 | + original_assignment_id = request.assignment_id |
| 766 | + |
| 767 | + patch :update, params: { |
| 768 | + course_id: course.id, |
| 769 | + id: request.id, |
| 770 | + request: { |
| 771 | + assignment_id: foreign_assignment.id, |
| 772 | + reason: 'Updated reason', |
| 773 | + requested_due_date: Date.tomorrow.to_s, |
| 774 | + due_time: '12:00' |
| 775 | + } |
| 776 | + } |
| 777 | + |
| 778 | + expect(response).to render_template(:edit) |
| 779 | + expect(flash[:alert]).to match(/problem updating the request/) |
| 780 | + expect(request.reload.assignment_id).to eq(original_assignment_id) |
| 781 | + end |
| 782 | + end |
| 783 | + |
| 784 | + describe 'POST #create_for_student' do |
| 785 | + let(:enrolled_student) { User.create!(email: 'enrolled@example.com', canvas_uid: '901', name: 'Enrolled Student') } |
| 786 | + let(:unenrolled_student) { User.create!(email: 'unenrolled@example.com', canvas_uid: '902', name: 'Unenrolled Student') } |
| 787 | + |
| 788 | + before do |
| 789 | + session[:user_id] = instructor.canvas_uid |
| 790 | + UserToCourse.create!(user: instructor, course: course, role: 'teacher') |
| 791 | + end |
| 792 | + |
| 793 | + it 'rejects filing on behalf of a student who is not enrolled in the course' do |
| 794 | + post :create_for_student, params: { |
| 795 | + course_id: course.id, |
| 796 | + request: { |
| 797 | + user_id: unenrolled_student.id, |
| 798 | + assignment_id: assignment.id, |
| 799 | + reason: 'Sick', |
| 800 | + requested_due_date: Date.tomorrow.to_s, |
| 801 | + due_time: '10:00' |
| 802 | + } |
| 803 | + } |
| 804 | + |
| 805 | + expect(response).to redirect_to(new_course_request_path(course)) |
| 806 | + expect(flash[:alert]).to match(/not enrolled/) |
| 807 | + expect(Request.where(user: unenrolled_student)).to be_empty |
| 808 | + end |
| 809 | + |
| 810 | + it 'creates a request for an enrolled student' do |
| 811 | + UserToCourse.create!(user: enrolled_student, course: course, role: 'student') |
| 812 | + |
| 813 | + post :create_for_student, params: { |
| 814 | + course_id: course.id, |
| 815 | + request: { |
| 816 | + user_id: enrolled_student.id, |
| 817 | + assignment_id: assignment.id, |
| 818 | + reason: 'Sick', |
| 819 | + requested_due_date: Date.tomorrow.to_s, |
| 820 | + due_time: '10:00' |
| 821 | + } |
| 822 | + } |
| 823 | + |
| 824 | + expect(Request.last.user).to eq(enrolled_student) |
| 825 | + end |
| 826 | + |
| 827 | + it 'rejects an assignment that belongs to another course' do |
| 828 | + UserToCourse.create!(user: enrolled_student, course: course, role: 'student') |
| 829 | + |
| 830 | + post :create_for_student, params: { |
| 831 | + course_id: course.id, |
| 832 | + request: { |
| 833 | + user_id: enrolled_student.id, |
| 834 | + assignment_id: foreign_assignment.id, |
| 835 | + reason: 'Sick', |
| 836 | + requested_due_date: Date.tomorrow.to_s, |
| 837 | + due_time: '10:00' |
| 838 | + } |
| 839 | + } |
| 840 | + |
| 841 | + expect(response).to redirect_to(new_course_request_path(course)) |
| 842 | + expect(flash[:alert]).to match(/Assignment not found for this course/) |
| 843 | + expect(Request.where(user: enrolled_student)).to be_empty |
| 844 | + end |
| 845 | + end |
| 846 | + end |
723 | 847 | end |
0 commit comments