Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Fix mend vulnerabilities #133

Merged
merged 8 commits into from
Nov 29, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ abstract class BaseComposeTest : KoinTest {

protected val context: Context get() = InstrumentationRegistry.getInstrumentation().context

fun <T> runBlockingWithTestTimeout(block: suspend () -> T): T {
/**
* Run a block of code with a timeout. The default test timeout is multiplied by the
* given [timeoutMultiplier].
* @param timeoutMultiplier The multiplier for the default test timeout. Can be used to increase
* the timeout for flaky tests
*/
fun <T> runBlockingWithTestTimeout(
timeoutMultiplier: Int = 1,
block: suspend () -> T
): T {
return runBlocking {
withTimeout(DefaultTimeoutMillis) {
withTimeout(DefaultTimeoutMillis * timeoutMultiplier) {
block()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package de.tum.informatics.www1.artemis.native_app.core.test.test_setup

val DefaultTimeoutMillis: Long get() = System.getenv("DEFAULT_TIMEOUT")?.toLong() ?: 15000L
val DefaultTimeoutMillis: Long get() = System.getenv("DEFAULT_TIMEOUT")?.toLong() ?: 10000L
Original file line number Diff line number Diff line change
@@ -1,50 +1,41 @@
package de.tum.informatics.www1.artemis.native_app.feature.courseregistration

import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.hasParent
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToKey
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.experimental.categories.Category
import org.junit.runner.RunWith
import org.koin.android.ext.koin.androidContext
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.koin.test.get
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals

@Category(EndToEndTest::class)
@RunWith(RobolectricTestRunner::class)
class CourseRegistrationE2eTest : KoinTest {

private val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@get:Rule
val composeTestRule = createComposeRule()
class CourseRegistrationE2eTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -58,11 +49,9 @@ class CourseRegistrationE2eTest : KoinTest {

@Before
fun setup() {
runBlocking {
withTimeout(DefaultTimeoutMillis) {
performTestLogin()
course = createCourse(getAdminAccessToken(), forceSelfRegistration = true)
}
runBlockingWithTestTimeout {
performTestLogin()
course = createCourse(getAdminAccessToken(), forceSelfRegistration = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package de.tum.informatics.www1.artemis.native_app.feature.courseview

import android.content.Context
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.CourseViewModel
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.course_overview.CourseUiScreen
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.course_overview.DEFAULT_CONVERSATION_ID
Expand All @@ -19,26 +17,17 @@ import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.koin.android.ext.koin.androidContext
import org.koin.compose.LocalKoinApplication
import org.koin.compose.LocalKoinScope
import org.koin.core.annotation.KoinInternalApi
import org.koin.mp.KoinPlatformTools
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.koin.test.get

abstract class BaseCourseTest : KoinTest {

protected val testDispatcher = UnconfinedTestDispatcher()

@get:Rule
val composeTestRule = createComposeRule()
abstract class BaseCourseTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -50,16 +39,11 @@ abstract class BaseCourseTest : KoinTest {

lateinit var course: Course

val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@Before
fun setup() {
runBlocking {
withTimeout(DefaultTimeoutMillis) {
performTestLogin()

course = createCourse(getAdminAccessToken())
}
runBlockingWithTestTimeout {
performTestLogin()
course = createCourse(getAdminAccessToken())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performScrollToKey
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.model.exercise.Exercise
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createModelingExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createProgramingExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createTextExercise
import de.tum.informatics.www1.artemis.native_app.feature.courseview.ui.exercise_list.TEST_TAG_EXERCISE_LIST_LAZY_COLUMN
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.Ignore
import org.junit.Test
import org.junit.experimental.categories.Category
Expand Down Expand Up @@ -80,10 +78,8 @@ class ExerciseListOverviewE2eTest : BaseCourseTest() {


private fun displayExerciseTypeTestImpl(createExercise: suspend () -> Exercise) {
val exercise = runBlocking {
withTimeout(DefaultTimeoutMillis) {
createExercise()
}
val exercise = runBlockingWithTestTimeout {
createExercise()
}

setupAndDisplayCourseUi()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
package de.tum.informatics.www1.artemis.native_app.feature.exercise_view.participate.text_exercise

import android.content.Context
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.model.Course
import de.tum.informatics.www1.artemis.native_app.core.model.exercise.TextExercise
import de.tum.informatics.www1.artemis.native_app.core.test.BaseComposeTest
import de.tum.informatics.www1.artemis.native_app.core.test.coreTestModules
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createCourse
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createExercise
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.course_creation.createTextExercise
import de.tum.informatics.www1.artemis.native_app.core.test.testWebsocketModule
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.exerciseModule
import de.tum.informatics.www1.artemis.native_app.feature.login.loginModule
import de.tum.informatics.www1.artemis.native_app.feature.login.test.getAdminAccessToken
import de.tum.informatics.www1.artemis.native_app.feature.login.test.performTestLogin
import de.tum.informatics.www1.artemis.native_app.feature.login.test.testLoginModule
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.koin.android.ext.koin.androidContext
import org.koin.test.KoinTest
import org.koin.test.KoinTestRule
import org.robolectric.shadows.ShadowLog

abstract class BaseExerciseTest : KoinTest {

protected val testDispatcher = UnconfinedTestDispatcher()

protected val context: Context get() = InstrumentationRegistry.getInstrumentation().context

@get:Rule
val composeTestRole = createComposeRule()
abstract class BaseExerciseTest : BaseComposeTest() {

@get:Rule
val koinTestRule = KoinTestRule.create {
Expand All @@ -52,18 +39,16 @@ abstract class BaseExerciseTest : KoinTest {
open fun setup() {
ShadowLog.stream = System.out

runBlocking {
withTimeout(DefaultTimeoutMillis) {
accessToken = performTestLogin()
runBlockingWithTestTimeout {
accessToken = performTestLogin()

course = createCourse(getAdminAccessToken())
exercise = createExercise(
getAdminAccessToken(),
course.id!!,
endpoint = "text-exercises",
creator = ::createTextExercise
) as TextExercise
}
course = createCourse(getAdminAccessToken())
exercise = createExercise(
getAdminAccessToken(),
course.id!!,
endpoint = "text-exercises",
creator = ::createTextExercise
) as TextExercise
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.navigation.compose.rememberNavController
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.common.test.DefaultTestTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.core.common.test.EndToEndTest
import de.tum.informatics.www1.artemis.native_app.core.test.test_setup.DefaultTimeoutMillis
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.ExerciseViewModel
import de.tum.informatics.www1.artemis.native_app.feature.exerciseview.home.ExerciseScreen
Expand All @@ -34,7 +34,7 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {
fun `displays correct exercise data`() {
setupUiAndViewModel()

composeTestRole.onAllNodesWithText(exercise.title!!).onFirst().assertExists()
composeTestRule.onAllNodesWithText(exercise.title!!).onFirst().assertExists()
}

@OptIn(ExperimentalTestApi::class)
Expand All @@ -44,14 +44,14 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {

setupUiAndViewModel { participationId = it }

composeTestRole.onNodeWithText(
composeTestRule.onNodeWithText(
context.getString(CoreUiR.string.exercise_actions_start_exercise_button)
)
.performClick()

composeTestRole.waitUntil(DefaultTimeoutMillis) { participationId != null }
composeTestRule.waitUntil(DefaultTimeoutMillis) { participationId != null }

composeTestRole
composeTestRule
.waitUntilExactlyOneExists(
hasText(context.getString(CoreUiR.string.exercise_actions_open_exercise_button)),
DefaultTimeoutMillis
Expand All @@ -73,7 +73,7 @@ class ExerciseOverviewE2eTest : BaseExerciseTest() {
coroutineContext = testDispatcher
)

composeTestRole.setContent {
composeTestRule.setContent {
CompositionLocalProvider(
LocalKoinScope provides KoinPlatformTools.defaultContext()
.get().scopeRegistry.rootScope,
Expand Down
Loading
Loading