lectures_controller_test.rb 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. require 'test_helper'
  2. class LecturesControllerTest < ActionDispatch::IntegrationTest
  3. def setup
  4. @lecture = lectures(:two)
  5. @course = courses(:two)
  6. @student_user = users(:billy)
  7. @teacher_user = users(:ben)
  8. @school_admin = users(:daniel)
  9. @global_admin = users(:admin)
  10. end
  11. test "should show lecture" do
  12. log_in_as @student_user
  13. get lecture_path @lecture
  14. assert_response :success
  15. end
  16. test "student should not edit lecture" do
  17. log_in_as @student_user
  18. get edit_lecture_path @lecture
  19. assert_redirected_to root_url
  20. end
  21. test "teacher should edit and update lecture" do
  22. log_in_as @teacher_user
  23. get edit_lecture_path @lecture
  24. assert_response :success
  25. patch lecture_path(@lecture), params: { lecture: { description: "New Lorem Ipsum" } }
  26. assert_redirected_to @lecture
  27. assert_equal "New Lorem Ipsum", @lecture.reload.description
  28. end
  29. test "school admin should create new lecture" do
  30. log_in_as @school_admin
  31. get new_lecture_path
  32. assert_response :success
  33. assert_difference "Lecture.count", 1 do
  34. post lectures_path, params: { lecture: { description: "Lorem Ipsum",
  35. location: "1A",
  36. course_id: @course.id,
  37. starts_at: 2.days.ago,
  38. ends_at: 2.days.since } }
  39. end
  40. end
  41. test "global admin should create new lecture" do
  42. log_in_as @global_admin
  43. get new_lecture_path
  44. assert_response :success
  45. assert_difference "Lecture.count", 1 do
  46. post lectures_path, params: { lecture: { description: "New Lecture",
  47. location: "1A",
  48. course_id: @course.id,
  49. starts_at: 2.days.ago,
  50. ends_at: 2.days.since } }
  51. end
  52. end
  53. test "unauthorized should not create lecture" do
  54. log_in_as @student_user
  55. assert_no_difference "Lecture.count" do
  56. # With valid course id
  57. post lectures_path, params: { lecture: { description: "New Lecture",
  58. location: "1A",
  59. course_id: @course.id,
  60. starts_at: 2.days.ago,
  61. ends_at: 2.days.since } }
  62. # Without course id
  63. post lectures_path, params: { lecture: { description: "New Lecture",
  64. location: "1A",
  65. starts_at: 2.days.ago,
  66. ends_at: 2.days.since } }
  67. end
  68. end
  69. end