diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-03-11 01:52:39 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-10 20:22:39 +0000 |
commit | 2f034bc2372d29b4ddebf74d532279073fb3d92b (patch) | |
tree | dc9197276622c56f32c9b395e9339a046f608103 /format-common/src/test | |
parent | 3e988b2a3428c3759535a2bd1f3e1ba0b5e411a3 (diff) |
Show remaining time in TOTP field (#1766)
* Pass down remaining time for TOTPs to UI layer
* format-common: switch TOTP flow to use co-operative cancelation
* format-common: add a regression test for OTP duration calculation
* Abstract out labels
* Switch to launchIn
Diffstat (limited to 'format-common/src/test')
-rw-r--r-- | format-common/src/test/kotlin/dev/msfjarvis/aps/data/passfile/PasswordEntryTest.kt | 31 | ||||
-rw-r--r-- | format-common/src/test/kotlin/dev/msfjarvis/aps/util/time/TestClocks.kt | 6 |
2 files changed, 33 insertions, 4 deletions
diff --git a/format-common/src/test/kotlin/dev/msfjarvis/aps/data/passfile/PasswordEntryTest.kt b/format-common/src/test/kotlin/dev/msfjarvis/aps/data/passfile/PasswordEntryTest.kt index d8a1fdc2..55c62533 100644 --- a/format-common/src/test/kotlin/dev/msfjarvis/aps/data/passfile/PasswordEntryTest.kt +++ b/format-common/src/test/kotlin/dev/msfjarvis/aps/data/passfile/PasswordEntryTest.kt @@ -8,6 +8,7 @@ package dev.msfjarvis.aps.data.passfile import dev.msfjarvis.aps.test.CoroutineTestRule import dev.msfjarvis.aps.test.test2 import dev.msfjarvis.aps.util.time.TestUserClock +import dev.msfjarvis.aps.util.time.UserClock import dev.msfjarvis.aps.util.totp.TotpFinder import java.util.Locale import kotlin.test.Test @@ -15,6 +16,7 @@ import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds import kotlin.time.ExperimentalTime import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest @@ -24,9 +26,9 @@ import org.junit.Rule class PasswordEntryTest { @get:Rule val coroutineTestRule: CoroutineTestRule = CoroutineTestRule() - private fun makeEntry(content: String) = + private fun makeEntry(content: String, clock: UserClock = fakeClock) = PasswordEntry( - fakeClock, + clock, testFinder, content.encodeToByteArray(), ) @@ -134,7 +136,26 @@ class PasswordEntryTest { val entry = makeEntry("secret\nextra\n$TOTP_URI") assertTrue(entry.hasTotp()) entry.totp.test2 { - assertEquals("818800", expectMostRecentItem()) + val otp = expectMostRecentItem() + assertEquals("818800", otp.value) + assertEquals(30.seconds, otp.remainingTime) + cancelAndIgnoreRemainingEvents() + } + } + + /** + * Same as [testGeneratesOtpFromTotpUri], but advances the clock by 5 seconds. This exercises the + * [Totp.remainingTime] calculation logic, and acts as a regression test to resolve the bug which + * blocked https://msfjarvis.dev/aps/issue/1550. + */ + @Test + fun testGeneratedOtpHasCorrectRemainingTime() = runTest { + val entry = makeEntry("secret\nextra\n$TOTP_URI", TestUserClock.withAddedSeconds(5)) + assertTrue(entry.hasTotp()) + entry.totp.test2 { + val otp = expectMostRecentItem() + assertEquals("818800", otp.value) + assertEquals(25.seconds, otp.remainingTime) cancelAndIgnoreRemainingEvents() } } @@ -144,7 +165,9 @@ class PasswordEntryTest { val entry = makeEntry(TOTP_URI) assertNull(entry.password) entry.totp.test2 { - assertEquals("818800", expectMostRecentItem()) + val otp = expectMostRecentItem() + assertEquals("818800", otp.value) + assertEquals(30.seconds, otp.remainingTime) cancelAndIgnoreRemainingEvents() } } diff --git a/format-common/src/test/kotlin/dev/msfjarvis/aps/util/time/TestClocks.kt b/format-common/src/test/kotlin/dev/msfjarvis/aps/util/time/TestClocks.kt index 5098bec9..ee74a40d 100644 --- a/format-common/src/test/kotlin/dev/msfjarvis/aps/util/time/TestClocks.kt +++ b/format-common/src/test/kotlin/dev/msfjarvis/aps/util/time/TestClocks.kt @@ -24,4 +24,10 @@ class TestUserClock(instant: Instant) : UserClock() { override fun getZone(): ZoneId = UTC override fun instant(): Instant = clock.instant() + + companion object { + fun withAddedSeconds(secondsToAdd: Long): TestUserClock { + return TestUserClock(Instant.EPOCH.plusSeconds(secondsToAdd)) + } + } } |