2021-08-06 21:16:46 +02:00
|
|
|
package audio.funkwhale.ffa.activities
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import androidx.test.core.app.ActivityScenario
|
|
|
|
import androidx.test.core.app.ApplicationProvider
|
|
|
|
import audio.funkwhale.ffa.FFA
|
2021-08-09 06:50:46 +02:00
|
|
|
import audio.funkwhale.ffa.KoinTestApp
|
|
|
|
import audio.funkwhale.ffa.utils.OAuth
|
|
|
|
import com.preference.PowerPreference
|
|
|
|
import com.preference.Preference
|
2021-09-09 09:56:15 +02:00
|
|
|
import io.mockk.every
|
|
|
|
import io.mockk.mockk
|
|
|
|
import io.mockk.mockkStatic
|
|
|
|
import io.mockk.verify
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.junit.After
|
2021-08-06 21:16:46 +02:00
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.runner.RunWith
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.core.context.stopKoin
|
|
|
|
import org.koin.dsl.module
|
2021-08-06 21:16:46 +02:00
|
|
|
import org.robolectric.RobolectricTestRunner
|
|
|
|
import org.robolectric.Shadows
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.robolectric.annotation.Config
|
2021-08-06 21:16:46 +02:00
|
|
|
import strikt.api.expectThat
|
|
|
|
import strikt.assertions.isEqualTo
|
|
|
|
|
|
|
|
@RunWith(RobolectricTestRunner::class)
|
2021-08-09 06:50:46 +02:00
|
|
|
@Config(application = KoinTestApp::class, sdk = [30])
|
2021-08-06 21:16:46 +02:00
|
|
|
class SplashActivityTest {
|
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
private val app: KoinTestApp = ApplicationProvider.getApplicationContext()
|
|
|
|
|
|
|
|
@After
|
|
|
|
fun tearDown() {
|
|
|
|
stopKoin()
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:16:46 +02:00
|
|
|
@Test
|
|
|
|
fun `unauthorized and nonAnonymous request should redirect to LoginActivity`() {
|
2021-08-09 06:50:46 +02:00
|
|
|
|
|
|
|
mockkStatic(PowerPreference::class)
|
|
|
|
val preference = mockk<Preference> {
|
|
|
|
every { getBoolean("anonymous", false) } returns false
|
|
|
|
every { clear() } returns true
|
|
|
|
}
|
|
|
|
every { PowerPreference.getFileByName("credentials") } returns preference
|
|
|
|
|
|
|
|
val modules = module {
|
|
|
|
single<OAuth> {
|
|
|
|
mockk { every { isAuthorized(any()) } returns false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.loadModules(modules) {
|
|
|
|
val scenario = ActivityScenario.launch(SplashActivity::class.java)
|
|
|
|
scenario.onActivity { activity ->
|
|
|
|
val expectedIntent = Intent(activity, LoginActivity::class.java)
|
|
|
|
val appContext = Shadows.shadowOf(ApplicationProvider.getApplicationContext<FFA>())
|
|
|
|
expectThat(appContext.nextStartedActivity.component).isEqualTo(expectedIntent.component)
|
|
|
|
verify { preference.clear() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `authorized request should redirect to MainActivity`() {
|
|
|
|
val modules = module {
|
|
|
|
single<OAuth> {
|
|
|
|
mockk { every { isAuthorized(any()) } returns true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.loadModules(modules) {
|
|
|
|
val scenario = ActivityScenario.launch(SplashActivity::class.java)
|
|
|
|
scenario.onActivity { activity ->
|
|
|
|
val expectedIntent = Intent(activity, MainActivity::class.java)
|
|
|
|
val appContext = Shadows.shadowOf(ApplicationProvider.getApplicationContext<FFA>())
|
|
|
|
expectThat(appContext.nextStartedActivity.component).isEqualTo(expectedIntent.component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun `anonymous requests should redirect to MainActivity`() {
|
|
|
|
|
|
|
|
mockkStatic(PowerPreference::class)
|
|
|
|
val preference = mockk<Preference>() {
|
|
|
|
every { getBoolean("anonymous", false) } returns true
|
|
|
|
}
|
|
|
|
every { PowerPreference.getFileByName("credentials") } returns preference
|
|
|
|
|
|
|
|
val modules = module {
|
|
|
|
single<OAuth> {
|
|
|
|
mockk { every { isAuthorized(any()) } returns false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.loadModules(modules) {
|
|
|
|
val scenario = ActivityScenario.launch(SplashActivity::class.java)
|
|
|
|
scenario.onActivity { activity ->
|
|
|
|
val expectedIntent = Intent(activity, MainActivity::class.java)
|
|
|
|
val appContext = Shadows.shadowOf(ApplicationProvider.getApplicationContext<FFA>())
|
|
|
|
expectThat(appContext.nextStartedActivity.component).isEqualTo(expectedIntent.component)
|
|
|
|
}
|
2021-08-06 21:16:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|