Skip to content

Commit 46b372d

Browse files
committed
fix: guard pubky auth completion
1 parent b805610 commit 46b372d

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

app/src/main/java/to/bitkit/repositories/PubkyRepo.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class PubkyRepo @Inject constructor(
265265
}
266266

267267
suspend fun completeAuthentication(): Result<Unit> {
268-
val attemptId = _activeAuthAttemptId.value
268+
val attemptId = _activeAuthAttemptId.value ?: return Result.failure(PubkyAuthAttemptInactive())
269269
return runCatching {
270270
withContext(ioDispatcher) {
271271
val sessionSecret = pubkyService.completeAuth()

app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ class PubkyRepoTest : BaseUnitTest() {
102102
fun `completeAuthentication should save session and update state`() = test {
103103
val testSecret = "session_secret"
104104
val testPk = VALID_SELF_KEY.removePrefix("pubky")
105+
whenever(pubkyService.startAuth()).thenReturn("auth_uri")
105106
whenever(pubkyService.completeAuth()).thenReturn(testSecret)
106107
whenever(pubkyService.importSession(testSecret)).thenReturn(testPk)
107108

108109
val ffiProfile = mock<CorePubkyProfile>()
109110
whenever(ffiProfile.name).thenReturn("User")
110111
whenever(pubkyService.getProfile(VALID_SELF_KEY)).thenReturn(ffiProfile)
111112

113+
sut.startAuthentication()
112114
val result = sut.completeAuthentication()
113115

114116
assertTrue(result.isSuccess)
@@ -121,11 +123,13 @@ class PubkyRepoTest : BaseUnitTest() {
121123
fun `completeAuthentication should clear managed secret key`() = test {
122124
val testSecret = "session_secret"
123125
val testPk = VALID_SELF_KEY.removePrefix("pubky")
126+
whenever(pubkyService.startAuth()).thenReturn("auth_uri")
124127
whenever(pubkyService.completeAuth()).thenReturn(testSecret)
125128
whenever(pubkyService.importSession(testSecret)).thenReturn(testPk)
126129
val ffiProfile = createFfiProfile(name = "User")
127130
whenever(pubkyService.getProfile(VALID_SELF_KEY)).thenReturn(ffiProfile)
128131

132+
sut.startAuthentication()
129133
val result = sut.completeAuthentication()
130134

131135
assertTrue(result.isSuccess)
@@ -136,11 +140,13 @@ class PubkyRepoTest : BaseUnitTest() {
136140
fun `completeAuthentication should not load contacts automatically`() = test {
137141
val testSecret = "session_secret"
138142
val testPk = VALID_SELF_KEY.removePrefix("pubky")
143+
whenever(pubkyService.startAuth()).thenReturn("auth_uri")
139144
whenever(pubkyService.completeAuth()).thenReturn(testSecret)
140145
whenever(pubkyService.importSession(testSecret)).thenReturn(testPk)
141146
val ffiProfile = createFfiProfile(name = "User")
142147
whenever(pubkyService.getProfile(VALID_SELF_KEY)).thenReturn(ffiProfile)
143148

149+
sut.startAuthentication()
144150
val result = sut.completeAuthentication()
145151

146152
assertTrue(result.isSuccess)
@@ -149,15 +155,25 @@ class PubkyRepoTest : BaseUnitTest() {
149155

150156
@Test
151157
fun `completeAuthentication should reset state on failure`() = test {
158+
whenever(pubkyService.startAuth()).thenReturn("auth_uri")
152159
whenever(pubkyService.completeAuth()).thenAnswer { throw TestAppError("Failed") }
153160

161+
sut.startAuthentication()
154162
val result = sut.completeAuthentication()
155163

156164
assertTrue(result.isFailure)
157165
assertFalse(sut.isAuthenticated.value)
158166
assertNull(sut.publicKey.value)
159167
}
160168

169+
@Test
170+
fun `completeAuthentication should fail when auth attempt inactive`() = test {
171+
val result = sut.completeAuthentication()
172+
173+
assertTrue(result.isFailure)
174+
verifyBlocking(pubkyService, never()) { completeAuth() }
175+
}
176+
161177
@Test
162178
fun `cancelAuthentication should reset state to idle`() = test {
163179
whenever(pubkyService.startAuth()).thenReturn("auth_uri")
@@ -760,7 +776,10 @@ class PubkyRepoTest : BaseUnitTest() {
760776
whenever(pubkyService.sessionList(newSecret, Env.contactsBasePath)).thenReturn(emptyList())
761777
val staleProfile = createFfiProfile(name = "Stale Old")
762778
whenever(pubkyService.getProfile(oldPublicKey.ensurePubkyPrefixForTest())).thenAnswer {
763-
runBlocking { sut.completeAuthentication() }
779+
runBlocking {
780+
startAuthForTesting()
781+
sut.completeAuthentication()
782+
}
764783
staleProfile
765784
}
766785

@@ -803,7 +822,10 @@ class PubkyRepoTest : BaseUnitTest() {
803822
whenever(keychain.loadString(Keychain.Key.PAYKIT_SESSION.name)).thenReturn(oldSecret)
804823
whenever(pubkyService.sessionList(oldSecret, Env.contactsBasePath)).thenReturn(listOf(staleContactPath))
805824
whenever(pubkyService.fetchFileString(staleContactUri)).thenAnswer {
806-
runBlocking { sut.completeAuthentication() }
825+
runBlocking {
826+
startAuthForTesting()
827+
sut.completeAuthentication()
828+
}
807829
"""{"name":"Stale Contact","bio":""}"""
808830
}
809831

@@ -1063,9 +1085,15 @@ class PubkyRepoTest : BaseUnitTest() {
10631085
whenever(keychain.loadString(Keychain.Key.PAYKIT_SESSION.name)).thenReturn(secret)
10641086
whenever { pubkyService.sessionList(secret, Env.contactsBasePath) }.thenReturn(emptyList())
10651087

1088+
startAuthForTesting()
10661089
sut.completeAuthentication()
10671090
}
10681091

1092+
private suspend fun startAuthForTesting(authUri: String = "auth_uri") {
1093+
whenever { pubkyService.startAuth() }.thenReturn(authUri)
1094+
sut.startAuthentication()
1095+
}
1096+
10691097
private fun createFfiProfile(name: String): CorePubkyProfile {
10701098
val ffiProfile = mock<CorePubkyProfile>()
10711099
whenever(ffiProfile.name).thenReturn(name)

0 commit comments

Comments
 (0)