|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.appservices.remotesettings |
| 6 | + |
| 7 | +import mozilla.appservices.httpconfig.RustHttpConfig |
| 8 | +import mozilla.appservices.remotesettings.RemoteSettings |
| 9 | +import mozilla.appservices.remotesettings.RemoteSettingsConfig |
| 10 | +import mozilla.components.concept.fetch.Client |
| 11 | +import mozilla.components.concept.fetch.Headers |
| 12 | +import mozilla.components.concept.fetch.MutableHeaders |
| 13 | +import mozilla.components.concept.fetch.Request |
| 14 | +import mozilla.components.concept.fetch.Response |
| 15 | +import org.junit.Assert.assertEquals |
| 16 | +import org.junit.Assert.assertTrue |
| 17 | +import org.junit.Before |
| 18 | +import org.junit.Rule |
| 19 | +import org.junit.Test |
| 20 | +import org.junit.rules.TemporaryFolder |
| 21 | +import org.junit.runner.RunWith |
| 22 | +import org.robolectric.RobolectricTestRunner |
| 23 | +import java.io.File |
| 24 | +import java.io.InputStream |
| 25 | + |
| 26 | +@RunWith(RobolectricTestRunner::class) |
| 27 | +class RemoteSettingsTest { |
| 28 | + |
| 29 | + @Rule @JvmField |
| 30 | + val tempDir = TemporaryFolder() |
| 31 | + |
| 32 | + private var fakeUrl = "" |
| 33 | + private var fakeStatus = 200 |
| 34 | + private var fakeHeaders: Headers = MutableHeaders("etag" to "\"1000\"") |
| 35 | + private var fakeBodyStream = "".byteInputStream() |
| 36 | + private var fakeContentType = "" |
| 37 | + private var fakeBody = Response.Body(fakeBodyStream, fakeContentType) |
| 38 | + private var doFetch: (Request) -> Response = { |
| 39 | + Response(fakeUrl, fakeStatus, fakeHeaders, fakeBody) |
| 40 | + } |
| 41 | + |
| 42 | + @Before |
| 43 | + fun setup() { |
| 44 | + RustHttpConfig.setClient( |
| 45 | + lazyOf(object : Client() { |
| 46 | + override fun fetch(request: Request): Response = doFetch(request) |
| 47 | + }), |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `test setting up, fetching records, and attachment downloading`() { |
| 53 | + val config = RemoteSettingsConfig( |
| 54 | + serverUrl = "http://localhost:8888", |
| 55 | + bucketName = "the-bucket", |
| 56 | + collectionName = "the-collection", |
| 57 | + ) |
| 58 | + |
| 59 | + // Setup the client |
| 60 | + val client = RemoteSettings(config) |
| 61 | + |
| 62 | + // Fetch the records |
| 63 | + setupRecordResponse(config, responseBodyStream = recordJson.byteInputStream()) |
| 64 | + val response = client.getRecords() |
| 65 | + val records = response.records |
| 66 | + assertEquals(records[0].fields.getString("title"), recordTitle) |
| 67 | + |
| 68 | + // Download an attachment |
| 69 | + val attachmentLocation = records[0].attachment!!.location |
| 70 | + val localAttachmentPath = "${tempDir.root}/path.jpg" |
| 71 | + setupAttachmentResponses(config, attachmentLocation) |
| 72 | + client.downloadAttachmentToPath(attachmentLocation, localAttachmentPath) |
| 73 | + val downloadedFile = File(localAttachmentPath) |
| 74 | + assertTrue(downloadedFile.exists()) |
| 75 | + assertEquals(csv, downloadedFile.readText()) |
| 76 | + } |
| 77 | + |
| 78 | + private fun setupAttachmentResponses( |
| 79 | + config: RemoteSettingsConfig, |
| 80 | + attachmentLocation: String, |
| 81 | + ) { |
| 82 | + val topUrl = config.serverUrl |
| 83 | + val attachmentUrl = "${config.serverUrl}/attachments/$attachmentLocation" |
| 84 | + doFetch = { req -> |
| 85 | + when (req.url) { |
| 86 | + "$topUrl/v1/" -> { |
| 87 | + Response( |
| 88 | + url = req.url, |
| 89 | + status = 200, |
| 90 | + headers = fakeHeaders, |
| 91 | + body = Response.Body( |
| 92 | + stream = attachmentsInfoJson(topUrl!!).byteInputStream(), |
| 93 | + null, |
| 94 | + ), |
| 95 | + ) |
| 96 | + } |
| 97 | + attachmentUrl -> { |
| 98 | + Response( |
| 99 | + url = attachmentUrl, |
| 100 | + status = 200, |
| 101 | + headers = fakeHeaders, |
| 102 | + body = Response.Body( |
| 103 | + stream = csv.byteInputStream(), |
| 104 | + null, |
| 105 | + ), |
| 106 | + ) |
| 107 | + } |
| 108 | + else -> error("unexpected url") |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + private fun setupRecordResponse( |
| 113 | + config: RemoteSettingsConfig, |
| 114 | + responseBodyStream: InputStream = recordJson.byteInputStream(), |
| 115 | + ) { |
| 116 | + fakeUrl = "${config.serverUrl}/v1/buckets/${config.bucketName}/collections/${config.collectionName}/records" |
| 117 | + fakeBody = Response.Body(responseBodyStream, null) |
| 118 | + } |
| 119 | + |
| 120 | + private fun attachmentsInfoJson(baseUrl: String) = """ |
| 121 | + { |
| 122 | + "capabilities": { |
| 123 | + "admin": { |
| 124 | + "description": "Serves the admin console.", |
| 125 | + "url": "https://github.com/Kinto/kinto-admin/", |
| 126 | + "version": "2.0.0" |
| 127 | + }, |
| 128 | + "attachments": { |
| 129 | + "description": "Add file attachments to records", |
| 130 | + "url": "https://github.com/Kinto/kinto-attachment/", |
| 131 | + "version": "6.3.1", |
| 132 | + "base_url": "$baseUrl/attachments/" |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + """.trimIndent() |
| 137 | + |
| 138 | + private val recordTitle = "with-txt-attachment" |
| 139 | + private val recordJson = """ |
| 140 | + { |
| 141 | + "data": [ |
| 142 | + { |
| 143 | + "title": "$recordTitle", |
| 144 | + "content": "content", |
| 145 | + "attachment": { |
| 146 | + "filename": "text-attachment.csv", |
| 147 | + "location": "the-bucket/the-collection/d3a5eccc-f0ca-42c3-b0bb-c0d4408c21c9.jpg", |
| 148 | + "hash": "2cbd593f3fd5f1585f92265433a6696a863bc98726f03e7222135ff0d8e83543", |
| 149 | + "mimetype": "text/csv", |
| 150 | + "size": 1374325 |
| 151 | + }, |
| 152 | + "schema": 1677694447771, |
| 153 | + "id": "7403c6f9-79be-4e0c-a37a-8f2b5bd7ad58", |
| 154 | + "last_modified": 1677694455368 |
| 155 | + } |
| 156 | + ] |
| 157 | + } |
| 158 | + """.trimIndent() |
| 159 | + |
| 160 | + private val csv = """ |
| 161 | + John,Doe,120 jefferson st.,Riverside, NJ, 08075 |
| 162 | + Jack,McGinnis,220 hobo Av.,Phila, PA,09119 |
| 163 | + "John ""Da Man""${'"'},Repici,120 Jefferson St.,Riverside, NJ,08075 |
| 164 | + Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234 |
| 165 | + ,Blankman,,SomeTown, SD, 00298 |
| 166 | + "Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 |
| 167 | + """.trimIndent() |
| 168 | +} |
0 commit comments