Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/controllers/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Handles CRUD for courses
class CoursesController < ApplicationController
before_action :set_course, only: %i[show add_students handle_add_students add_lecturers handle_add_lecturers settings handle_settings destroy export_csv profile]
before_action :set_course, only: %i[show add_students handle_add_students add_lecturers handle_add_lecturers settings handle_settings destroy export_csv profile update_coursecode]
def show
authorize @course

Expand Down Expand Up @@ -322,7 +322,8 @@ def import_details
end

def update_coursecode
@course = Course.find(params[:id])
authorize @course, :update?

@course.generate_coursecode!
flash.now[:notice] = 'Course join code successfully generated'
rescue StandardError => e
Expand Down
33 changes: 33 additions & 0 deletions test/integration/update_coursecode_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class UpdateCoursecodeTest < ActionDispatch::IntegrationTest
setup do
@course = create(:course)
@lecturer = create(:user, :staff)
create(:enrolment, :coordinator, user: @lecturer, course: @course)
end

test 'should generate and display a course code via update_coursecode API' do
# Sign in the lecturer (coordinator)
post session_path, params: { email_address: @lecturer.email_address, password: 'password' }
assert_redirected_to root_path

# Initial state
assert_nil @course.coursecode

# Call the API explicitly
post update_coursecode_course_path(@course), headers: { 'Accept' => 'text/vnd.turbo-stream.html' }

# The request should succeed and return turbo stream content
assert_response :success
assert_equal 'text/vnd.turbo-stream.html', response.media_type

# The coursecode must be generated and saved to the course via the API call
@course.reload
assert_not_nil @course.coursecode

# Check if there's a generated course code on the UI via the turbo stream response
# It replaces the 'course_code_form' which contains the new course code string.
assert_match @course.coursecode, response.body
end
end
Loading