Repository navigation
Show the digital files a reader cannot open yet, with the reason #50
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
Open
fabiodalez-dev
wants to merge
2
commits into
main
Choose a base branch
from
feat/digital-access-480
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+461
−19
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
app/src/androidTest/java/com/pinakes/app/staffe2e/DigitalLinkRefreshFlowTest.kt
This file contains hidden or 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,106 @@ | ||
| package com.pinakes.app.staffe2e | ||
|
|
||
| import androidx.lifecycle.SavedStateHandle | ||
| import androidx.lifecycle.ViewModelStore | ||
| import com.pinakes.app.data.model.BookDetail | ||
| import com.pinakes.app.data.model.DigitalAttachment | ||
| import com.pinakes.app.data.model.Envelope | ||
| import com.pinakes.app.data.network.ApiResult | ||
| import com.pinakes.app.data.network.ErrorCodes | ||
| import com.pinakes.app.ui.common.UiState | ||
| import com.pinakes.app.ui.navigation.Routes | ||
| import com.pinakes.app.ui.screens.detail.BookDetailViewModel | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.serialization.encodeToString | ||
| import kotlinx.serialization.json.Json | ||
| import org.junit.After | ||
| import org.junit.Assert.* | ||
| import org.junit.Test | ||
|
|
||
| /** Real repository/ViewModel, HTTP cache and access decisions on a disposable emulator. */ | ||
| class DigitalLinkRefreshFlowTest : AppFlowTest() { | ||
| private val old = DigitalAttachment("https://library.example/a.epub?pk_exp=1000&pk_uid=3&pk_sig=old", access = "granted") | ||
| private val fresh = old.copy(url = old.url.replace("1000", "4102444800").replace("old", "new")) | ||
| private val book = BookDetail(id = 1, title = "Digital", digitalAttachments = listOf(old)) | ||
| private val store = ViewModelStore() | ||
|
|
||
| private fun FakePinakes.answer(data: BookDetail = book) = body("GET catalog/books/1", 200, | ||
| Json.encodeToString(Envelope(data = data)), mapOf("ETag" to "\"digital-v1\"", "Cache-Control" to "private, max-age=3600")) | ||
|
|
||
| private fun viewModel(): BookDetailViewModel { | ||
| lateinit var vm: BookDetailViewModel | ||
| instrumentation.runOnMainSync { | ||
| vm = BookDetailViewModel(SavedStateHandle(mapOf(Routes.ARG_BOOK_ID to 1)), | ||
| deps.catalogRepository(), deps.libraryRepository(), deps.wishlistRepository(), deps.connectivity()) | ||
| store.put("digital", vm) | ||
| } | ||
| compose.waitUntil(10_000) { vm.state.value.content is UiState.Success } | ||
| return vm | ||
| } | ||
|
|
||
| @After fun clearViewModels() { instrumentation.runOnMainSync { store.clear() } } | ||
|
|
||
| @Test fun forcedRenewalBypassesTheHttpCacheAndGetsANewSignature() { | ||
| launch { answer() } | ||
| val repository = deps.catalogRepository() | ||
| assertTrue(runBlocking { repository.book(1) } is ApiResult.Success) | ||
| api.answer(book.copy(digitalAttachments = listOf(fresh))) | ||
| val response = runBlocking { repository.book(1, forceRefresh = true) } | ||
| assertEquals(fresh.url, (response as ApiResult.Success).data.digitalFiles.single().url) | ||
| val request = api.recorded("GET", "catalog/books/1").last() | ||
| assertNull(request.getHeader("If-None-Match")) | ||
| assertEquals("no-cache, no-store", request.getHeader("Cache-Control")) | ||
| assertEquals(2, api.recorded("GET", "catalog/books/1").size) | ||
| } | ||
|
|
||
| @Test fun aServerFailureDuringRenewalCannotReturnTheCachedSignedLink() { | ||
| launch { answer() } | ||
| val repository = deps.catalogRepository() | ||
| assertTrue(runBlocking { repository.book(1) } is ApiResult.Success) | ||
| api.body("GET catalog/books/1", 503, """{"error":{"code":"server_error","message":"Unavailable"}}""") | ||
| val response = runBlocking { repository.book(1, forceRefresh = true) } | ||
| assertTrue(response is ApiResult.Failure) | ||
| assertEquals(503, (response as ApiResult.Failure).httpStatus) | ||
| } | ||
|
|
||
| @Test fun anUnexpected304CannotPretendTheExpiredPayloadWasRenewed() { | ||
| launch { answer() } | ||
| val repository = deps.catalogRepository() | ||
| assertTrue(runBlocking { repository.book(1) } is ApiResult.Success) | ||
| api.body("GET catalog/books/1", 304, "") | ||
| assertTrue(runBlocking { repository.book(1, forceRefresh = true) } is ApiResult.Failure) | ||
| } | ||
|
|
||
| @Test fun renewalFailurePreservesTheLoadedPageAndASubsequentRetrySucceeds() { | ||
| launch { answer() } | ||
| val vm = viewModel() | ||
| val before = vm.state.value.content | ||
| api.body("GET catalog/books/1", 500, """{"error":{"code":"server_error","message":"Unavailable"}}""") | ||
| assertTrue(runBlocking { vm.refreshDigitalFile(old) } is ApiResult.Failure) | ||
| assertSame(before, vm.state.value.content) | ||
| api.answer(book.copy(digitalAttachments = listOf(fresh))) | ||
| val result = runBlocking { vm.refreshDigitalFile(old) } | ||
| assertEquals(fresh.url, (result as ApiResult.Success).data.url) | ||
| assertEquals(fresh.url, (vm.state.value.content as UiState.Success).data.digitalFiles.single().url) | ||
| } | ||
|
|
||
| @Test fun renewalSelectsTheSameFileEvenWhenTheServerReordersAndRenamesEntries() { | ||
| launch { answer() } | ||
| val vm = viewModel() | ||
| val other = fresh.copy(url = fresh.url.replace("a.epub", "b.epub")) | ||
| api.answer(book.copy(digitalAttachments = listOf(other, fresh.copy(label = "Renamed")))) | ||
| val result = runBlocking { vm.refreshDigitalFile(old) } | ||
| assertEquals(fresh.url, (result as ApiResult.Success).data.url) | ||
| } | ||
|
|
||
| @Test fun revokedFileAccessUpdatesTheCardAndReturnsNoOpenableAddress() { | ||
| launch { answer() } | ||
| val vm = viewModel() | ||
| api.answer(book.copy(digitalAttachments = listOf(old.copy(url = "", access = "loan_required")))) | ||
| val result = runBlocking { vm.refreshDigitalFile(old) } | ||
| assertEquals(ErrorCodes.FORBIDDEN, (result as ApiResult.Failure).code) | ||
| val file = (vm.state.value.content as UiState.Success).data.digitalFiles.single() | ||
| assertTrue(file.locked) | ||
| assertNull(file.safeUrl) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The player is matched to its card by the full signed URL. Every renewal replaces
contentwith freshly signed URLs for all files. So if an audiobook is playing and the reader taps Read/Open on another file of the same record, the audio card'sfile.urlchanges andselectedAudio == file.urlbecomes false. TheAudioPlayerthen leaves composition and playback stops.Technical details