diff --git a/readingbat-core/src/main/kotlin/com/readingbat/dsl/ContentDsl.kt b/readingbat-core/src/main/kotlin/com/readingbat/dsl/ContentDsl.kt index e3114989e..36696f675 100644 --- a/readingbat-core/src/main/kotlin/com/readingbat/dsl/ContentDsl.kt +++ b/readingbat-core/src/main/kotlin/com/readingbat/dsl/ContentDsl.kt @@ -107,8 +107,14 @@ fun readingBatContent(block: ReadingBatContent.() -> Unit) = /** Returns true if the server is running in production mode. Accessible from Content.kt DSL files. */ fun isProduction() = IS_PRODUCTION.getProperty(false) -/** Returns true if the server is running in test mode. Accessible from Content.kt DSL files. */ -fun isTesting() = IS_TESTING.getProperty(false) +/** + * Returns true if the server is running in test mode. Accessible from Content.kt DSL files. + * + * IS_TESTING is not part of [initProperties] (it is set directly via setProperty in tests and + * defaults to false in production), so it is read with errorOnNonInit = false: the single global + * init guard can never meaningfully flag this specific property anyway. + */ +fun isTesting() = IS_TESTING.getProperty(false, errorOnNonInit = false) internal fun isDbmsEnabled() = DBMS_ENABLED.getProperty(false) diff --git a/readingbat-core/src/main/kotlin/com/readingbat/dsl/LanguageGroup.kt b/readingbat-core/src/main/kotlin/com/readingbat/dsl/LanguageGroup.kt index fb22133a7..f202c4726 100644 --- a/readingbat-core/src/main/kotlin/com/readingbat/dsl/LanguageGroup.kt +++ b/readingbat-core/src/main/kotlin/com/readingbat/dsl/LanguageGroup.kt @@ -18,7 +18,6 @@ package com.readingbat.dsl import com.pambrose.common.util.ContentRoot -import com.pambrose.common.util.ContentSource import com.pambrose.common.util.asRegex import com.pambrose.common.util.decode import com.pambrose.common.util.pathOf @@ -52,15 +51,10 @@ class LanguageGroup( val challengeGroups = mutableListOf>() /** - * The content source for this language's challenge files. Defaults to the parent - * [ReadingBatContent.repo] value. Must be set to a valid source before challenges are loaded. + * The content source for this language's challenge files. Inherits the parent + * [ReadingBatContent.repo] value unless overridden in the DSL. */ var repo: ContentRoot = content.repo // Defaults to outer-level value - get() = - if (field == defaultContentRoot) - error("$languageName section is missing a repo value") - else - field /** Git branch name for remote content. Defaults to the parent [ReadingBatContent.branchName]. */ var branchName = content.branchName // Defaults to outer-level value @@ -145,18 +139,5 @@ class LanguageGroup( companion object { private val logger = KotlinLogging.logger {} private val excludes = Regex("^__.*__.*$") - - internal val defaultContentRoot = - object : ContentRoot { - override val sourcePrefix = "" - override val remote = false - - override fun file(path: String) = - object : ContentSource { - override val content = "" - override val remote = false - override val source = "" - } - } } } diff --git a/readingbat-core/src/main/kotlin/com/readingbat/pages/ClassSummaryPage.kt b/readingbat-core/src/main/kotlin/com/readingbat/pages/ClassSummaryPage.kt index 49cd71dfd..ee8021bea 100644 --- a/readingbat-core/src/main/kotlin/com/readingbat/pages/ClassSummaryPage.kt +++ b/readingbat-core/src/main/kotlin/com/readingbat/pages/ClassSummaryPage.kt @@ -133,13 +133,11 @@ internal object ClassSummaryPage { throw InvalidRequestException("Invalid user") } - classCode.fetchClassTeacherId() != user.userId -> { - val teacherId = classCode.fetchClassTeacherId() - throw InvalidRequestException("User id ${user.userId} does not match class code's teacher id $teacherId") - } - else -> { - // Do nothing + // Look up the teacher id once and reuse it for both the check and the message. + val teacherId = classCode.fetchClassTeacherId() + if (teacherId != user.userId) + throw InvalidRequestException("User id ${user.userId} does not match class code's teacher id $teacherId") } } diff --git a/readingbat-core/src/main/kotlin/com/readingbat/pages/StudentSummaryPage.kt b/readingbat-core/src/main/kotlin/com/readingbat/pages/StudentSummaryPage.kt index cf24f7e69..7f0988c65 100644 --- a/readingbat-core/src/main/kotlin/com/readingbat/pages/StudentSummaryPage.kt +++ b/readingbat-core/src/main/kotlin/com/readingbat/pages/StudentSummaryPage.kt @@ -109,14 +109,12 @@ internal object StudentSummaryPage { throw InvalidRequestException("Invalid user") } - // classCode != activeClassCode -> throw InvalidRequestException("Class code mismatch") - classCode.fetchClassTeacherId() != user.userId -> { - val teacherId = classCode.fetchClassTeacherId() - throw InvalidRequestException("User id ${user.userId} does not match class code's teacher Id $teacherId") - } - else -> { - // Do nothing + // classCode != activeClassCode -> throw InvalidRequestException("Class code mismatch") + // Look up the teacher id once and reuse it for both the check and the message. + val teacherId = classCode.fetchClassTeacherId() + if (teacherId != user.userId) + throw InvalidRequestException("User id ${user.userId} does not match class code's teacher Id $teacherId") } } diff --git a/readingbat-core/src/main/kotlin/com/readingbat/server/ws/WsCommon.kt b/readingbat-core/src/main/kotlin/com/readingbat/server/ws/WsCommon.kt index 8450045df..4a25ad282 100644 --- a/readingbat-core/src/main/kotlin/com/readingbat/server/ws/WsCommon.kt +++ b/readingbat-core/src/main/kotlin/com/readingbat/server/ws/WsCommon.kt @@ -104,13 +104,13 @@ object WsCommon { false to "Invalid user id: ${user.userId}" } - classCode.fetchClassTeacherId() != user.userId -> { - val teacherId = classCode.fetchClassTeacherId() - false to "User id ${user.userId} does not match class code's teacher Id $teacherId" - } - else -> { - true to "" + // Look up the teacher id once and reuse it for both the check and the message. + val teacherId = classCode.fetchClassTeacherId() + if (teacherId != user.userId) + false to "User id ${user.userId} does not match class code's teacher Id $teacherId" + else + true to "" } }.also { (valid, msg) -> if (!valid) throw InvalidRequestException(msg) }