diff --git a/aai/tests/special_case_tests.py b/aai/tests/special_case_tests.py index b9a27f32bfc96a10cd05b1d420dd401565444afb..a09ca9236bc5b66aab1b2d475e041cc70638b690 100644 --- a/aai/tests/special_case_tests.py +++ b/aai/tests/special_case_tests.py @@ -62,6 +62,9 @@ class SpecialCaseTests(GraphQLApiTestCase): cls.trainee1.teams.add(cls.exercise.teams.first()) + cls.exercise.running = True + cls.exercise.save() + cls.thread = EmailClient.create_thread( ["team-1@mail.com", "doe@mail.ex"], "test", cls.exercise.id ) diff --git a/running_exercise/lib/email_client.py b/running_exercise/lib/email_client.py index e58a3fd5096af67bb88417eeae8aee49acd79893..72002bcc2695fc5b48eb29b58323028028f7f303 100644 --- a/running_exercise/lib/email_client.py +++ b/running_exercise/lib/email_client.py @@ -136,6 +136,11 @@ class EmailClient: participant_addresses: List[str], subject: str, exercise_id: str ) -> EmailThread: exercise = get_model(Exercise, id=exercise_id) + if not exercise.running: + raise RunningExerciseOperationException( + "Cannot create a thread when the exercise is not running" + ) + participants = _validate_participants(participant_addresses, exercise) new_thread = EmailThread.objects.create( diff --git a/running_exercise/schema/mutation.py b/running_exercise/schema/mutation.py index 45fce9fd03409b7fc61238d619319e628ac3badb..7a52493854eaa1c3d1a3328822c1c061ac6dc77a 100644 --- a/running_exercise/schema/mutation.py +++ b/running_exercise/schema/mutation.py @@ -5,6 +5,7 @@ from rest_framework.exceptions import PermissionDenied from aai.access import user_from_context, team_access, exercise_access from aai.decorators import protected +from common_lib.exceptions import RunningExerciseOperationException from common_lib.logger import logger from common_lib.schema.types import ExerciseType, EmailThreadType from common_lib.utils import get_model @@ -96,6 +97,10 @@ class SendEmailMutation(graphene.Mutation): send_email_input: SendEmailInput, ): thread = get_model(EmailThread, id=send_email_input.thread_id) + if not thread.exercise.running: + raise RunningExerciseOperationException( + "Cannot send an email when the exercise is not running" + ) participant = get_model( EmailParticipant, address=send_email_input.sender_address,