-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
164 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.jocmp.capy.accounts | ||
|
||
import com.jocmp.feedbinclient.Feedbin | ||
import com.jocmp.readerclient.GoogleReader | ||
|
||
interface Credentials { | ||
val username: String | ||
val secret: String | ||
|
||
companion object { | ||
suspend fun verify(credentials: Credentials): Result<Credentials> = | ||
when (credentials) { | ||
is FeedbinCredentials -> verifyFeedbin(credentials) | ||
is ReaderCredentials -> verifyReader(credentials) | ||
else -> throw UnsupportedOperationException() | ||
} | ||
|
||
private suspend fun verifyFeedbin(credentials: FeedbinCredentials): Result<FeedbinCredentials> { | ||
val response = Feedbin.verifyCredentials( | ||
username = credentials.username, | ||
password = credentials.secret | ||
) | ||
|
||
return if (response.isSuccessful) { | ||
Result.success(credentials) | ||
} else { | ||
Result.failure(Throwable("Failed with status ${response.message()}")) | ||
} | ||
} | ||
|
||
private suspend fun verifyReader(credentials: ReaderCredentials): Result<ReaderCredentials> { | ||
val response = GoogleReader.verifyCredentials( | ||
username = credentials.username, | ||
password = credentials.secret, | ||
baseURL = credentials.url | ||
) | ||
|
||
val resultCredentials = ReaderCredentials( | ||
username = "", | ||
secret = "", | ||
url = credentials.url, | ||
) | ||
|
||
|
||
return if (response.isSuccessful) { | ||
Result.success(resultCredentials) | ||
} else { | ||
Result.failure(Throwable("Failed with status ${response.message()}")) | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class FeedbinCredentials(override val username: String, override val secret: String) : | ||
Credentials | ||
|
||
data class ReaderCredentials( | ||
override val username: String, | ||
override val secret: String, | ||
val url: String | ||
) : Credentials |
6 changes: 6 additions & 0 deletions
6
capy/src/main/java/com/jocmp/capy/accounts/ReaderAccountDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.jocmp.capy.accounts | ||
|
||
/** | ||
* Save Auth Token for later use | ||
* self.credentials = Credentials(type: .readerAPIKey, username: credentials.username, secret: authString) | ||
*/ |
6 changes: 0 additions & 6 deletions
6
capy/src/main/java/com/jocmp/capy/accounts/VerifyCredentialsExt.kt
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
capy/src/test/java/com/jocmp/capy/accounts/CredentialsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.jocmp.capy.accounts | ||
|
||
import com.jocmp.feedbinclient.Feedbin | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkObject | ||
import kotlinx.coroutines.test.runTest | ||
import okhttp3.ResponseBody.Companion.toResponseBody | ||
import retrofit2.Response | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class FeedbinCredentialsTest { | ||
val username = "test@example.com" | ||
val password = "its-a-secret-to-everybody" | ||
val credentials = FeedbinCredentials(username, password) | ||
lateinit var feedbin: Feedbin | ||
|
||
@BeforeTest | ||
fun setup() { | ||
feedbin = mockk<Feedbin>() | ||
mockkObject(Feedbin.Companion) | ||
every { Feedbin.create(any(), any()) }.returns(feedbin) | ||
} | ||
|
||
@Test | ||
fun verify_onSuccess_shouldReturnCredentials() = runTest { | ||
coEvery { feedbin.authentication(authentication = any()) }.returns(Response.success(null)) | ||
|
||
val result = Credentials.verify(credentials).getOrNull()!! | ||
|
||
assertEquals(actual = result.username, expected = username) | ||
assertEquals(actual = result.secret, expected = password) | ||
} | ||
|
||
@Test | ||
fun verify_onError_shouldReturnFailure() = runTest { | ||
coEvery { feedbin.authentication(authentication = any()) }.returns( | ||
Response.error( | ||
401, | ||
"".toResponseBody() | ||
) | ||
) | ||
|
||
val result = Credentials.verify(credentials) | ||
|
||
assertTrue(result.isFailure) | ||
} | ||
} | ||
|
||
class ReaderCredentialsTest { | ||
@Test | ||
fun verify_onSuccess_shouldReturnCredentials() { | ||
|
||
} | ||
|
||
@Test | ||
fun verify_onError_shouldReturnFailure() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters