Skip to content

Commit a1cb5a2

Browse files
author
Pablo Guardiola
authored
[copilot] make sure created from attachments api is the same as startedAt (#6660)
1 parent 592818b commit a1cb5a2

File tree

6 files changed

+166
-81
lines changed

6 files changed

+166
-81
lines changed

libnavigation-copilot/src/main/java/com/mapbox/navigation/copilot/HistoryUploadWorker.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ internal class HistoryUploadWorker(
4444
val drive = buildNavigationSessionFrom(workerParams.inputData)
4545
val filePath = workerParams.inputData.getString(HISTORY_FILE_PATH)!!
4646
val file = File(filePath)
47+
val startedAt = workerParams.inputData.getString(STARTED_AT)!!
4748
val uploadSessionId = workerParams.inputData.getString(UPLOAD_SESSION_ID)!!
4849
val metadata = AttachmentMetadata(
4950
name = file.name,
51+
created = startedAt,
5052
fileId = "",
5153
format = GZ,
5254
type = ZIP,

libnavigation-copilot/src/main/java/com/mapbox/navigation/copilot/MapboxCopilotImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ internal class MapboxCopilotImpl(
312312
val sessionId = generateSessionId(copilotMetadata, owner)
313313
return AttachmentMetadata(
314314
name = filename,
315+
created = startedAt,
315316
fileId = "",
316317
format = GZ,
317318
type = ZIP,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.mapbox.navigation.copilot
2+
3+
import com.google.gson.Gson
4+
import com.google.gson.reflect.TypeToken
5+
6+
object CopilotTestUtils {
7+
8+
internal fun retrieveAttachments(metadata: String): List<AttachmentMetadata> {
9+
val gson = Gson()
10+
val itemType = object : TypeToken<List<AttachmentMetadata>>() {}.type
11+
return gson.fromJson(metadata, itemType)
12+
}
13+
}

libnavigation-copilot/src/test/java/com/mapbox/navigation/copilot/HistoryAttachmentsUtilsTest.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import org.junit.Assert.assertEquals
88
import org.junit.Test
99
import java.util.Base64.getDecoder
1010

11+
/**
12+
* HistoryAttachmentsUtilsTest
13+
*
14+
* NOTE FOR FUTURE SECURITY AUDITS:
15+
* The fakeAccessToken used below in the tests, although it seems legitimate it is a
16+
* fake one manually generated so that the owner associated to is is copilot-test-owner.
17+
*/
1118
class HistoryAttachmentsUtilsTest {
1219

1320
@Test
@@ -61,9 +68,8 @@ class HistoryAttachmentsUtilsTest {
6168
every { Base64.decode(any<String>(), any()) } answers {
6269
getDecoder().decode(firstArg<String>())
6370
}
64-
val accessToken = "pk.eyJ1IjoiY29waWxvdC10ZXN0LW93bmVyIiwiYSI6ImNpdDF3OHpoaTAwMDcye" +
65-
"XA5Y3Z0Nmk2dzEifQ.cit1w8zhi00072yp9cvt6i6w1"
66-
val owner = HistoryAttachmentsUtils.retrieveOwnerFrom(accessToken)
71+
val fakeAccessToken = "pk.eyJ1IjoiY29waWxvdC10ZXN0LW93bmVyIiwiYSI6ImZha2UifQ.8badf00d"
72+
val owner = HistoryAttachmentsUtils.retrieveOwnerFrom(fakeAccessToken)
6773
assertEquals("copilot-test-owner", owner)
6874
}
6975
}

libnavigation-copilot/src/test/java/com/mapbox/navigation/copilot/HistoryUploadWorkerTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.mapbox.common.UploadState
1212
import com.mapbox.common.UploadStatus
1313
import com.mapbox.common.UploadStatusCallback
1414
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
15+
import com.mapbox.navigation.copilot.CopilotTestUtils.retrieveAttachments
1516
import com.mapbox.navigation.copilot.HistoryAttachmentsUtils.copyToAndRemove
1617
import com.mapbox.navigation.copilot.internal.PushStatus
1718
import com.mapbox.navigation.copilot.internal.PushStatusObserver
@@ -75,6 +76,42 @@ class HistoryUploadWorkerTest {
7576
}
7677
}
7778

79+
@Test
80+
fun `AttachmentMetadata created is set to startedAt`() = runBlocking {
81+
mockkStatic("com.mapbox.navigation.utils.internal.LoggerProviderKt")
82+
every { logD(msg = any(), category = any()) } just Runs
83+
val mockedContext = mockk<Context>(relaxed = true)
84+
val mockedWorkerParams = mockk<WorkerParameters>(relaxed = true)
85+
val startedAt = "2022-05-12T17:47:42.353Z"
86+
every {
87+
mockedWorkerParams.inputData.getString("started_at")
88+
} returns startedAt
89+
val mockedUploadServiceInterface = prepareUploadMockks()
90+
val mockedUploadOptions = slot<UploadOptions>()
91+
val mockedUploadStatusCallback = slot<UploadStatusCallback>()
92+
val mockedUploadStatus = mockk<UploadStatus>(relaxed = true)
93+
every { mockedUploadStatus.state } returns UploadState.FINISHED
94+
every { mockedUploadStatus.httpResult?.value?.code } returns 204L
95+
val mockedPushStatusObserver = mockk<PushStatusObserver>(relaxUnitFun = true)
96+
MapboxCopilot.pushStatusObservers.add(mockedPushStatusObserver)
97+
every {
98+
mockedUploadServiceInterface.upload(
99+
capture(mockedUploadOptions),
100+
capture(mockedUploadStatusCallback),
101+
)
102+
} answers {
103+
mockedUploadStatusCallback.captured.run(mockedUploadStatus)
104+
1L
105+
}
106+
val historyUploadWorker = HistoryUploadWorker(mockedContext, mockedWorkerParams)
107+
108+
historyUploadWorker.doWork()
109+
110+
val attachmentsMetadata = mockedUploadOptions.captured.metadata
111+
val attachments = retrieveAttachments(attachmentsMetadata)
112+
assertEquals(startedAt, attachments[0].created)
113+
}
114+
78115
@Test
79116
fun `onPushStatusChanged Success when FINISHED - 204`() = runBlocking {
80117
mockkStatic("com.mapbox.navigation.utils.internal.LoggerProviderKt")

0 commit comments

Comments
 (0)